php 多图片上传及图片压缩
多图片上传
- 使用多个input 框上传,这个种方法比较普通,就略过了
- 使用一个input框上传多图
先看代码:
<form id="uploadForm" method="post" enctype="multipart/form-data" action="">
<textarea id="content" class="form-control" rows="4" name="content">
</textarea>
<input id="myfile_input" type="file" name="myfile[]" multiple="multiple" accept="image/*" />
</form>
form 表单的重点在 enctype=”multipart/form-data”
input 的重点在于 multiple=”multiple” accept=”image/*”
参数具体介绍自己百度吧.
ps: 这样设置后,input框弹出文件选择时,可以多选文件
php 后台接收处理
普通参数: from 表单的其他参数正常获取
文件对象:使用$_FILES 对象获取
if (isset($_FILES['myfile'])){
$total = count($_FILES['myfile']['name']);
// Loop through each file
for ($i = 0; $i < $total; $i++) {
//Get the temp file path
$tmpFileName = $_FILES['myfile']['name'][$i];
$tmp = explode(".", $tmpFileName);
$code = $customer_id . rand(1000, 9999);
$tempFileName = $code . '.' . $tmp[1];
$small_image = $code . '_small' . '.' . $tmp[1];
$url = $_SERVER['DOCUMENT_ROOT'] . "/assert/application/upload/" . $tempFileName;
$small_url = $_SERVER['DOCUMENT_ROOT'] . "/assert/application/upload/" . $small_image;
//Make sure we have a file path
if ($tmpFileName != "") {
// 上传到本地方式
if (file_exists($url))//当文件存在
{
$tempFileName = $code . '-1' . '.' . $tmp[1];
$small_image = $code . '-1_small' . '.' . $tmp[1];
$url = $_SERVER['DOCUMENT_ROOT'] . "/assert/application/upload/" . $tempFileName;
$small_url = $_SERVER['DOCUMENT_ROOT'] . "/assert/application/upload/" . $small_image;
}
move_uploaded_file($_FILES["myfile"]["tmp_name"][$i], $url);
// 压缩图片
$image = new ImgCompress($url, 1);
$image->compressImg($small_url);
}
}
}
total 获取到文件数量后循环把文件转存,存本地,存图片服务器都可以;
压缩的话,使用ImgCompress压缩,ImgCompress是网上分享的一个压缩类,亲测好用,而且压缩效果不错。
ImgCompress 源码:
class ImgCompress
{
private $src;
private $image;
private $imageinfo;
private $percent=0.5;
/*
param $src源图
param $percent压缩比例
*/
public function __construct($src,$percent=1)
{
$this->src = $src;
$this->percent = $percent;
}
/*
param string $saveName 图片名(可不带扩展名用原图名)用于保存。或不提供文件名直接显示
*/
public function compressImg($saveName='')
{
$this->_openImage();
if(!empty($saveName))
{
$this->_saveImage($saveName);//保存
}
else
{
$this->_showImage();
}
}
/*
内部:打开图片
*/
private function _openImage()
{
list($width, $height, $type, $attr) = getimagesize($this->src);
$this->imageinfo = array(
'width'=>$width,
'height'=>$height,
'type'=>image_type_to_extension($type,false),
'attr'=>$attr
);
$fun = "imagecreatefrom".$this->imageinfo['type'];
$this->image = $fun($this->src);
$this->_thumpImage();
}
/**
* 内部:操作图片
*/
private function _thumpImage()
{
$new_width = $this->imageinfo['width'] * $this->percent;
$new_height = $this->imageinfo['height'] * $this->percent;
$image_thump = imagecreatetruecolor($new_width,$new_height);
//将原图复制带图片载体上面,并且按照一定比例压缩,极大的保持了清晰度
imagecopyresampled($image_thump,$this->image,0,0,0,0,$new_width,$new_height,$this->imageinfo['width'],$this->imageinfo['height']);
imagedestroy($this->image);
$this->image = $image_thump;
}
/**
* 输出图片:保存图片则用saveImage()
*/
private function _showImage()
{
header('Content-Type: image/'.$this->imageinfo['type']);
$funcs = "image".$this->imageinfo['type'];
$funcs($this->image);
}
/**
* 保存图片到硬盘:
* @param string $dstImgName 1、可指定字符串不带后缀的名称,使用源图扩展名 。2、直接指定目标图片名带扩展名。
*/
private function _saveImage($dstImgName)
{
if(empty($dstImgName)) return false;
$allowImgs = ['.jpg', '.jpeg', '.png', '.bmp', '.wbmp','.gif']; //如果目标图片名有后缀就用目标图片扩展名 后缀,如果没有,则用源图的扩展名
$dstExt = strrchr($dstImgName ,".");
$sourseExt = strrchr($this->src ,".");
if(!empty($dstExt)) $dstExt =strtolower($dstExt);
if(!empty($sourseExt)) $sourseExt =strtolower($sourseExt);
//有指定目标名扩展名
if(!empty($dstExt) && in_array($dstExt,$allowImgs))
{
$dstName = $dstImgName;
}
elseif(!empty($sourseExt) && in_array($sourseExt,$allowImgs))
{
$dstName = $dstImgName.$sourseExt;
}
else
{
$dstName = $dstImgName.$this->imageinfo['type'];
}
$funcs = "image".$this->imageinfo['type'];
$funcs($this->image,$dstName);
}
/**
* 销毁图片
*/
public function __destruct()
{
imagedestroy($this->image);
}
}