php上传图片后 实现压缩图片功能

时间:2021-08-16 20:45:53
  所用的版本是php7 框架是yii2   <span style="font-family: Arial, Helvetica, sans-serif;"> 程序代码仅供参考,如有不当,大家一起改正 </span><pre code_snippet_id="1815636" snippet_file_name="blog_20160808_1_7833210" name="code" class="php">  <a target=_blank href="http://php.net/manual/zh/ref.image.php" target="_blank">php GD库 和图像处理</a> 资料参考
 
    public function actionUpload()
{
$path = '/uploads/'; //图片存放根目录 根据自己项目路径而定
$file = $_FILES['fileList']['name'];
$root = Yii::getAlias('@img').'/img';
$folder = $path.date('Ymd')."/";
$pre = rand(999,9999).time();
$ext = strrchr($file,'.');
$newName = $pre.$ext;


$out = ['msg'=>'','status'=>'error','img_url'=>''];


if(!is_dir($root.$folder))
{
if(!mkdir($root.$folder, 0777, true)){
$out['msg'] = '图片目录创建失败!';
echo json_encode($out);
exit;
}
}
$im = $_FILES['fileList']['tmp_name']; //上传图片资源
$maxwidth="1056"; //设置图片的最大宽度
$maxheight="500"; //设置图片的最大高度
$imgname = $root.$folder.$newName; //图片存放路径 根据自己图片路径而定
$filetype=$_FILES['fileList']['type'];//图片类型
$result = $this->thumbImage($im,$maxwidth,$maxheight,$imgname,$filetype);
if($result){
$out['msg'] = '图片上传成功';
$out['status'] = 'success';
$out['img_url'] = $folder.$newName;
}else{
$out['msg'] = '图片上传失败';
}

echo json_encode($out);
exit;
}
php上传图片后 实现压缩图片功能

//压缩图片
public function thumbImage($im,$maxwidth,$maxheight,$name,$filetype)
{
switch ($filetype) {
case 'image/pjpeg':
case 'image/jpeg':
$im = imagecreatefromjpeg($im); //PHP图片处理系统函数
break;
case 'image/gif':
$im = imagecreatefromgif($im);
break;
case 'image/png':
$im = imagecreatefrompng($im);
break;
case 'image/wbmp':
$im = imagecreatefromwbmp($im);
break;
}


$resizewidth_tag = $resizeheight_tag = false;
$pic_width = imagesx($im);
$pic_height = imagesy($im);


if(($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight))
{
$resizewidth_tag = $resizeheight_tag = false;

if($maxwidth && $pic_width>$maxwidth)
{
$widthratio = $maxwidth / $pic_width;
$resizewidth_tag = true;
}


if($maxheight && $pic_height>$maxheight)
{
$heightratio = $maxheight / $pic_height;
$resizeheight_tag = true;
}


if($resizewidth_tag && $resizeheight_tag)
{
if($widthratio < $heightratio)
$ratio = $widthratio;
else
$ratio = $heightratio;
}


if($resizewidth_tag && !$resizeheight_tag)
$ratio = $widthratio;


if($resizeheight_tag && !$resizewidth_tag)
$ratio = $heightratio;


$newwidth = $pic_width * $ratio;
$newheight = $pic_height * $ratio;



if(function_exists("imagecopyresampled"))
{
$newim = imagecreatetruecolor($newwidth,$newheight);//PHP图片处理系统函数
imagecopyresampled($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);//PHP图片处理系统函数
}
else
{
$newim = imagecreate($newwidth,$newheight);
imagecopyresized($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);
}


switch ($filetype) {
case 'image/pjpeg' :
case 'image/jpeg' :
$result = imagejpeg($newim,$name);
break;
case 'image/gif' :
$result = imagegif($newim,$name);
break;
case 'image/png' :
$result = imagepng($newim,$name);
break;
case 'image/wbmp' :
$result = imagewbmp($newim,$name);
break;
}
imagedestroy($newim);
}
else
{
switch ($filetype) {
case 'image/pjpeg' :
case 'image/jpeg' :
$result = imagejpeg($im,$name);
break;
case 'image/gif' :
$result = imagegif($im,$name);
break;
case 'image/png' :
$result = imagepng($im,$name);
break;
case 'image/wbmp' :
$result = imagewbmp($im,$name);
break;
}
}
return $result; 返回结果
}