本文原創自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
}
總結