百度编辑器Ueditor 多图上传出现部分照片尺寸不压缩的问题解决

今天在做网站后台维护的时候,公司提出要把后台上传的图片按宽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

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值