百度编辑器Ueditor的多图上传用的是百度自家的webuploader插件,本身这个插件上传是有图片压缩的,可能由于某种bug压缩时好时坏,经常不压缩。
这部分的代码在/editor/dialogs/image/image.js 在380行左右有这部分的代码。
其中imageCompressBorder数值是在/editor/php/congfig.json里面设置的
奇怪的时候部分图片就是不压缩,可能的确图片某些问题,例如png图片改后缀jpg这样的图片就不压缩。
问题还是要解决。
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); } }
好啦,可以愉快的上传压缩图片了,管你是html5上传还是flash上传,单张还是多张,都可以压缩了