php处理多图上传压缩代码功能

本文原创自news.mkq.online
本文地址:news.wpj.online
版权声明:本文为原创文章,版权牛站www.niuzhan.com所有

网上看了一些资料,关于处理图片压缩的,找到的大部分是单图压缩的,要么是单前端或者后端的,所以就自己整了下前后端压缩,并支持多图的压缩图片实例。代码有点多,直接复制到编辑器看会比较清楚

1、先创建的一个简单的上传页面upload.php。先通过前端代码压缩图片,直接上代码

001
实名验证
002

003 营业执照:门店照: 004
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
r e s u l t = i m a g e w b m p ( result = imagewbmp( result=imagewbmp(im,$name);
137
break;
138
}
139
}
140
return $result;
141
}
总结

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值