如何解决dedecms(5.6/5.7)缩略图缩放变形问题方法

属于自己的风景,从来不曾错过;不是自己的风景,永远只是路过。天地太大,人太渺小,不是每一道亮丽的风景都能拥有。一辈子,只求有一道令自己流连忘返,不离不弃的风景就已足够。人生的风景,是物也是人。陪自己到最后的,才是自己的风景。
解决dedecms(5.6/5.7)缩略图缩放变形问题方法,我们知道,dedecms缩略图是自动提取,相当于原图的等比例缩放了,比如后台设置缩略图的尺码为:120*90即为3:2的图片,但是假如内容里的大图尺码为300*300即1:1,这样生成出来的图片就会变形,直接后台设置的缩略图大小不起作用啊,这样严重影响网站美观,本文介绍通过修改dedecms生成缩略源码方法解决定问题。
打开include/image.func.php文件,该文件在dedecms5.6/5.7中所在的目录不一样,5.6中文件在/include/下,5.7中文件在/include/helpers/
如果你使用的是dedecms5.7,打开目录/include/helpers/找到image.helper.php文件。
如果你使用的是dedecms5.6,打开目录/include/找到image.func.php文件。
dedecms5.6版image.func.php修改方法(直接替换原来方法)
//[2020-11-04]:解决缩略图缩放变形问题(宽度、高度为后台设置宽高)
function ImageResize($srcFile, $toW, $toH, $toFile = "") {
    global $cfg_photo_type;
    if ($toFile == "") {
        $toFile = $srcFile;
    }
    $info = "";
    $srcInfo = GetImageSize($srcFile, $info);
    switch ($srcInfo[2]) {
    case 1:
        if (!$cfg_photo_type['gif']) {
            return false;
        }
        $im = imagecreatefromgif($srcFile);
        break;
    case 2:
        if (!$cfg_photo_type['jpeg']) {
            return false;
        }
        $im = imagecreatefromjpeg($srcFile);
        break;
    case 3:
        if (!$cfg_photo_type['png']) {
            return false;
        }
        $im = imagecreatefrompng($srcFile);
        break;
    case 6:
        if (!$cfg_photo_type['bmp']) {
            return false;
        }
        $im = imagecreatefromwbmp($srcFile);
        break;
    }
    $srcW = ImageSX($im);
    $srcH = ImageSY($im);
    if ($srcW <= $toW && $srcH <= $toH) {
        return true;
    }
    //缩略生成并裁剪
    $newW = $toH * $srcW / $srcH;
    $newH = $toW * $srcH / $srcW;
    if ($newH >= $toH) {
        $ftoW = $toW;
        $ftoH = $newH;
    } else {
        $ftoW = $newW;
        $ftoH = $toH;
    }
    if ($srcW > $toW || $srcH > $toH) {
        if (function_exists("imagecreatetruecolor")) {
            @$ni = imagecreatetruecolor($ftoW, $ftoH);
            if ($ni) {
                imagecopyresampled($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH);
            } else {
                $ni = imagecreate($ftoW, $ftoH);
                imagecopyresized($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH);
            }
        } else {
            $ni = imagecreate($ftoW, $ftoH);
            imagecopyresized($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH);
        }
        //裁剪图片成标准缩略图
        $new_imgx = imagecreatetruecolor($toW, $toH);
        if ($newH >= $toH) {
            imagecopyresampled($new_imgx, $ni, 0, 0, 0, ($newH – $toH) / 2, $toW, $toH, $toW, $toH);
        } else {
            imagecopyresampled($new_imgx, $ni, 0, 0, ($newW – $toW) / 2, 0, $toW, $toH, $toW, $toH);
        }
        switch ($srcInfo[2]) {
        case 1:
            imagegif($new_imgx, $toFile);
            break;
        case 2:
            imagejpeg($new_imgx, $toFile, 85);
            break;
        case 3:
            imagepng($new_imgx, $toFile);
            break;
        case 6:
            imagebmp($new_imgx, $toFile);
            break;
        default:
            return false;
        }
        imagedestroy($new_imgx);
        imagedestroy($ni);
    }
    imagedestroy($im);
    return true;
}
//[2020-11-04]:解决缩略图缩放变形问题(宽度、高度为后台设置宽高)
function ImageResize($srcFile, $toW, $toH, $toFile = "") {
    global $cfg_photo_type;
    if ($toFile == "") {
        $toFile = $srcFile;
    }
    $info = "";
    $srcInfo = GetImageSize($srcFile, $info);
    switch ($srcInfo[2]) {
    case 1:
        if (!$cfg_photo_type['gif']) {
            return false;
        }
        $im = imagecreatefromgif($srcFile);
        break;
    case 2:
        if (!$cfg_photo_type['jpeg']) {
            return false;
        }
        $im = imagecreatefromjpeg($srcFile);
        break;
    case 3:
        if (!$cfg_photo_type['png']) {
            return false;
        }
        $im = imagecreatefrompng($srcFile);
        break;
    case 6:
        if (!$cfg_photo_type['bmp']) {
            return false;
        }
        $im = imagecreatefromwbmp($srcFile);
        break;
    }
    $srcW = ImageSX($im);
    $srcH = ImageSY($im);
    if ($srcW <= $toW && $srcH <= $toH) {
        return true;
    }
    //缩略生成并裁剪
    $newW = $toH * $srcW / $srcH;
    $newH = $toW * $srcH / $srcW;
    if ($newH >= $toH) {
        $ftoW = $toW;
        $ftoH = $newH;
    } else {
        $ftoW = $newW;
        $ftoH = $toH;
    }
    if ($srcW > $toW || $srcH > $toH) {
        if (function_exists("imagecreatetruecolor")) {
@$ni = imagecreatetruecolor($ftoW, $ftoH);
            if ($ni) {
                imagecopyresampled($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH);
            } else {
                $ni = imagecreate($ftoW, $ftoH);
                imagecopyresized($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH);
            }
        } else {
            $ni = imagecreate($ftoW, $ftoH);
            imagecopyresized($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH);
        }
        //裁剪图片成标准缩略图
        $new_imgx = imagecreatetruecolor($toW, $toH);
        if ($newH >= $toH) {
            imagecopyresampled($new_imgx, $ni, 0, 0, 0, ($newH – $toH) / 2, $toW, $toH, $toW, $toH);
        } else {
            imagecopyresampled($new_imgx, $ni, 0, 0, ($newW – $toW) / 2, 0, $toW, $toH, $toW, $toH);
        }
        switch ($srcInfo[2]) {
        case 1:
            imagegif($new_imgx, $toFile);
            break;
        case 2:
            imagejpeg($new_imgx, $toFile, 85);
            break;
        case 3:
            imagepng($new_imgx, $toFile);
            break;
        case 6:
            imagebmp($new_imgx, $toFile);
            break;
        default:
            return false;
        }
        imagedestroy($new_imgx);
        imagedestroy($ni);
    }
    imagedestroy($im);
    return true;
}

dedecms5.7版image.helper.php修改方法:

if (!function_exists('ImageResize')) {
    function ImageResize($srcFile, $toW, $toH, $toFile = "") {
        global $cfg_photo_type;
        if ($toFile == "") {
            $toFile = $srcFile;
        }
        $info = "";
        $srcInfo = GetImageSize($srcFile, $info);
        switch ($srcInfo[2]) {
        case 1:
            if (!$cfg_photo_type['gif']) {
                return false;
            }
            $im = imagecreatefromgif($srcFile);
            break;
        case 2:
            if (!$cfg_photo_type['jpeg']) {
                return false;
            }
            $im = imagecreatefromjpeg($srcFile);
            break;
        case 3:
            if (!$cfg_photo_type['png']) {
                return false;
            }
            $im = imagecreatefrompng($srcFile);
            break;
        case 6:
            if (!$cfg_photo_type['bmp']) {
                return false;
            }
            $im = imagecreatefromwbmp($srcFile);
            break;
        }
        $srcW = ImageSX($im);
        $srcH = ImageSY($im);
        if ($srcW <= $toW && $srcH <= $toH) {
            return true;
        }
        //缩略生成并裁剪
        $newW = $toH * $srcW / $srcH;
        $newH = $toW * $srcH / $srcW;
        if ($newH >= $toH) {
            $ftoW = $toW;
            $ftoH = $newH;
        } else {
            $ftoW = $newW;
            $ftoH = $toH;
        }
        if ($srcW > $toW || $srcH > $toH) {
            if (function_exists("imagecreatetruecolor")) {
        @$ni = imagecreatetruecolor($ftoW, $ftoH);
                if ($ni) {
                    imagecopyresampled($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH);
                } else {
                    $ni = imagecreate($ftoW, $ftoH);
                    imagecopyresized($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH);
                }
            } else {
                $ni = imagecreate($ftoW, $ftoH);
                imagecopyresized($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH);
            }
            //裁剪图片成标准缩略图
            $new_imgx = imagecreatetruecolor($toW, $toH);
            if ($newH >= $toH) {
                imagecopyresampled($new_imgx, $ni, 0, 0, 0, ($newH – $toH) / 2, $toW, $toH, $toW, $toH);
            } else {
                imagecopyresampled($new_imgx, $ni, 0, 0, ($newW – $toW) / 2, 0, $toW, $toH, $toW, $toH);
            }
            switch ($srcInfo[2]) {
            case 1:
                imagegif($new_imgx, $toFile);
                break;
            case 2:
                imagejpeg($new_imgx, $toFile, 85);
                break;
            case 3:
                imagepng($new_imgx, $toFile);
                break;
            case 6:
                imagebmp($new_imgx, $toFile);
                break;
            default:
                return false;
            }
            imagedestroy($new_imgx);
            imagedestroy($ni);
        }
        imagedestroy($im);
        return true;
    }
}

到此这篇关于如何解决dedecms(5.6/5.7)缩略图缩放变形问题方法就介绍到这了。喜迎阴晴圆缺,笑傲风霜雨雪更多相关如何解决dedecms(5.6/5.7)缩略图缩放变形问题方法内容请查看相关栏目,小编编辑不易,再次感谢大家的支持!

您可能有感兴趣的文章
织梦后台文档列表添加复制文档功能支持所有模型

织梦无法上传ico图标格式文件的如何解决方法

dedecms如何实现任意页面调用当前会员信息的方

织梦dede:tag调用指定多个栏目的TAG标签

织梦栏目列表按附加表自定义字段排序