本文原创自news.mkq.online
本文地址:news.wpj.online
版权声明:本文为原创文章,版权牛站www.niuzhan.com所有
网上看了一些资料,关于处理图片压缩的,找到的大部分是单图压缩的,要么是单前端或者后端的,所以就自己整了下前后端压缩,并支持多图的压缩图片实例。代码有点多,直接复制到编辑器看会比较清楚
1、先创建的一个简单的上传页面upload.php。先通过前端代码压缩图片,直接上代码
001
实名验证
002
<div>
003
营业执照:门店照:
004
</div>
005
2、前端图片压缩后,请求到自定义的接口upload_deal.php.代码如下
006
'',
021
'status'=>'error',
022
'img_url'=>''
023
);
024
if(!is_dir($folder))
025
{
026
if(!mkdir($folder, 0777, true)){
027
$out['msg'] = '图片目录创建失败!';
028
echo json_encode($out);
029
exit;
030
}
031
}
032
$im = $_FILES[$filename]['tmp_name']; //上传图片资源
033
$maxwidth="1056"; //设置图片的最大宽度
034
$maxheight="500"; //设置图片的最大高度
035
$imgname = $folder.$newName; //图片存放路径 根据自己图片路径而定
036
$filetype=$_FILES[$filename]['type'];//图片类型
037
$result = thumbImage($im,$maxwidth,$maxheight,$imgname,$filetype);
038
if($result){
039
$out['msg'] = '图片上传成功';
040
$out['status'] = 'success';
041
$out['img_url'] = $folder.$newName;
042
}else{
043
$out['msg'] = '图片上传失败';
044
}
045
return json_encode($out);
046
exit;
047
}
048
//压缩图片
049
function thumbImage($im,$maxwidth,$maxheight,$name,$filetype)
050
{
051
switch ($filetype) {
052
case 'image/pjpeg':
053
case 'image/jpeg':
054
$im = imagecreatefromjpeg($im); //PHP图片处理系统函数
055
break;
056
case 'image/gif':
057
$im = imagecreatefromgif($im);
058
break;
059
case 'image/png':
060
$im = imagecreatefrompng($im);
061
break;
062
case 'image/wbmp':
063
$im = imagecreatefromwbmp($im);
064
break;
065
}
066
$resizewidth_tag = $resizeheight_tag = false;
067
$pic_width = imagesx($im);
068
$pic_height = imagesy($im);
069
if(($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight))
070
{
071
$resizewidth_tag = $resizeheight_tag = false;
072
if($maxwidth && $pic_width>$maxwidth)
073
{
074
$widthratio = $maxwidth / $pic_width;
075
$resizewidth_tag = true;
076
}
077
if($maxheight && $pic_height>$maxheight)
078
{
079
$heightratio = $maxheight / $pic_height;
080
$resizeheight_tag = true;
081
}
082
if($resizewidth_tag && $resizeheight_tag)
083
{
084
if($widthratio < $heightratio)
085
$ratio = $widthratio;
086
else
087
$ratio = $heightratio;
088
}
089
if($resizewidth_tag && !$resizeheight_tag)
090
$ratio = $widthratio;
091
if($resizeheight_tag && !$resizewidth_tag)
092
$ratio = $heightratio;
093
$newwidth = $pic_width * $ratio;
094
$newheight = $pic_height * $ratio;
095
if(function_exists("imagecopyresampled"))
096
{
097
$newim = imagecreatetruecolor($newwidth,$newheight);//PHP图片处理系统函数
098
imagecopyresampled($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);//PHP图片处理系统函数
099
}
100
else
101
{
102
$newim = imagecreate($newwidth,$newheight);
103
imagecopyresized($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);
104
}
105
switch ($filetype) {
106
case 'image/pjpeg' :
107
case 'image/jpeg' :
108
$result = imagejpeg($newim,$name);
109
break;
110
case 'image/gif' :
111
$result = imagegif($newim,$name);
112
break;
113
case 'image/png' :
114
$result = imagepng($newim,$name);
115
break;
116
case 'image/wbmp' :
117
$result = imagewbmp($newim,$name);
118
break;
119
}
120
imagedestroy($newim);
121
}
122
else
123
{
124
switch ($filetype) {
125
case 'image/pjpeg' :
126
case 'image/jpeg' :
127
$result = imagejpeg($im,$name);
128
break;
129
case 'image/gif' :
130
$result = imagegif($im,$name);
131
break;
132
case 'image/png' :
133
$result = imagepng($im,$name);
134
break;
135
case 'image/wbmp' :
136
$result = imagewbmp($im,$name);
137
break;
138
}
139
}
140
return $result;
141
}
总结