今天在做网站后台维护的时候,公司提出要把后台上传的图片按宽600px,高auto进行压缩,查了很多资料,终于找到了解决办法。
问题还是要解决。
1、修改/editor/php/config.json
修改8、9行
"imageCompressEnable": true, /* 是否压缩图片,默认是true */ "imageCompressBorder": 600, /* 图片压缩最长边限制 */
2、修改/editor/php/action_upload.php 在17行后面加上部分代码,增加resize、maxwidth2个参数
case 'uploadimage': $config = array( "pathFormat" => $CONFIG['imagePathFormat'], "maxSize" => $CONFIG['imageMaxSize'], "allowFiles" => $CONFIG['imageAllowFiles'], "resize"=> $CONFIG['imageCompressEnable'], "maxwidth"=> CONFIG['imageCompressBorder'] );
3、修改/editor/php/Uploader.class.php 添加压缩方法 在upFile()方法,移动文件后面,大约在125行添加一个压缩图片的判断
//移动文件 if (!(move_uploaded_file($file["tmp_name"], $this->filePath) && file_exists($this->filePath))) { //移动失败 $this->stateInfo = $this->getStateInfo("ERROR_FILE_MOVE"); } else { //移动成功 $this->stateInfo = $this->stateMap[0]; } //压缩图片 if($this->config['resize']){ $this->resize($this->filePath,$this->filePath); }
在文档的最后面增加压缩文件的resize方法
/** * 压缩图片 */ public function resize($oldfilepath,$newfilepath){ list($owidth,$oheight)=getimagesize($oldfilepath); if($owidth > $this->config['maxwidth'] ){ switch ($this->fileType) { case '.jpg': $nsrc = imagecreatefromjpeg($oldfilepath); break; case '.jpeg': $nsrc = imagecreatefromjpeg($oldfilepath); break; case '.gif': $nsrc = imagecreatefromgif($oldfilepath); break; case '.png': $nsrc = imagecreatefrompng($oldfilepath); break; default: //alert("上传图片格式不正确。"); } $newwidth= $this->config['maxwidth']; $newheight=($oheight/$owidth)*$this->config['maxwidth']; $tmp=imagecreatetruecolor($newwidth,$newheight); imagecopyresampled($tmp,$nsrc,0,0,0,0,$newwidth,$newheight,$owidth,$oheight); //输出不同类型图片 switch ($this->fileType) { case '.jpg': imagejpeg($tmp,$newfilepath,100); break; case '.jpeg': imagejpeg($tmp,$newfilepath,100); break; case '.gif': imagegif($tmp,$newfilepath); break; case '.png': imagepng($tmp,$newfilepath); break; default: //alert("上传图片格式不正确。"); } imagedestroy($nsrc); imagedestroy($tmp); } }
单张多张都可以进行压缩了,很好的。
ps:转载自创业是喝可乐 https://my.oschina.net/lambert519/blog/892475#comments