迅睿CMS压缩图片变大的解决方法

      发布在:后端技术      评论:0 条评论

结果分析迅睿CMS代码发现压缩图片的方法保留了最高质量造成图片文件大小变大的原因,我们只需要找到文件dayrui/Fcms/Library/Image.php中的reduce方法替换压缩质量的值即可,最终实现代码参考如下:

public function reduce($imgsrc, $cw) {

        list($width, $height, $type) = getimagesize($imgsrc);
        list($width, $height) = $this->_fix_orientation($imgsrc, $width, $height);
        if ($type != 18 && strpos($imgsrc, '.webp')) {
            $type = 18;
        }

        if ($width > $cw) {
            $per = $cw / $width;//计算比例
            $new_width = floor($width * $per); //压缩后的图片宽
            $new_height = floor($height * $per); //压缩后的图片高
            switch ($type) {
                case 1:
                    // gif
                    break;
                case 2:
                    //header('Content-Type:image/jpeg');
                    $image_wp = imagecreatetruecolor($new_width, $new_height);
                    $image = imagecreatefromjpeg($imgsrc);
                    imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
                    //75代表的是质量、压缩图片容量大小
                    imagejpeg($image_wp, $imgsrc, 75);
                    imagedestroy($image_wp);
                    imagedestroy($image);
                    break;
                case 3:
                    header('Content-Type:image/png');
                    $image_wp = imagecreatetruecolor($new_width, $new_height);
                    $image = imagecreatefrompng($imgsrc);

                    //2.上色
                    $color=imagecolorallocate($image_wp,255,255,255);
                    //3.设置透明
                    imagecolortransparent($image_wp,$color);
                    imagefill($image_wp,0,0,$color);

                    imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
                    //90代表的是质量、压缩图片容量大小
//                    imagejpeg($image_wp, $imgsrc, 75);
                    imagepng($image_wp, $imgsrc, 6);
                    imagedestroy($image_wp);
                    imagedestroy($image);
                    break;
                case 18:
                    header('Content-Type:image/webp');
                    $image_wp = imagecreatetruecolor($new_width, $new_height);
                    if (!function_exists('imagecreatefromwebp')) {
                        $image = imagecreatefromjpeg($imgsrc);
                    } else {
                        $image = imagecreatefromwebp($imgsrc);
                    }
                    imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
                    //80代表的是质量、压缩图片容量大小
                    imagewebp($image_wp, $imgsrc, 80);
                    imagedestroy($image_wp);
                    imagedestroy($image);
                    break;
            }
        } else {
            CI_DEBUG && log_message('debug', '系统要求宽度>'.$cw.'px才进行压缩处理,当前图片宽度='.$width.',不满足压缩条件:'.$imgsrc);
        }

        return;
    }

52d72f05-6bd7-4b24-a8d1-ebbdd3bab964

39fbd343-2b9b-4695-8a2d-9ad731427f25

相关文章
热门推荐