解决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]:解决缩略图缩放变形问题(宽度、高度为后台设置宽高)

001function ImageResize($srcFile, $toW, $toH, $toFile = "") {
002    global $cfg_photo_type;
003    if ($toFile == "") {
004        $toFile = $srcFile;
005    }
006    $info = "";
007    $srcInfo = GetImageSize($srcFile, $info);
008    switch ($srcInfo[2]) {
009    case 1:
010        if (!$cfg_photo_type['gif']) {
011            return false;
012        }
013        $im = imagecreatefromgif($srcFile);
014        break;
015    case 2:
016        if (!$cfg_photo_type['jpeg']) {
017            return false;
018        }
019        $im = imagecreatefromjpeg($srcFile);
020        break;
021    case 3:
022        if (!$cfg_photo_type['png']) {
023            return false;
024        }
025        $im = imagecreatefrompng($srcFile);
026        break;
027    case 6:
028        if (!$cfg_photo_type['bmp']) {
029            return false;
030        }
031        $im = imagecreatefromwbmp($srcFile);
032        break;
033    }
034    $srcW = ImageSX($im);
035    $srcH = ImageSY($im);
036    if ($srcW <= $toW && $srcH <= $toH) {
037        return true;
038    }
039    //缩略生成并裁剪
040    $newW = $toH * $srcW / $srcH;
041    $newH = $toW * $srcH / $srcW;
042    if ($newH >= $toH) {
043        $ftoW = $toW;
044        $ftoH = $newH;
045    else {
046        $ftoW = $newW;
047        $ftoH = $toH;
048    }
049    if ($srcW > $toW || $srcH > $toH) {
050        if (function_exists("imagecreatetruecolor")) {
051            @$ni = imagecreatetruecolor($ftoW, $ftoH);
052            if ($ni) {
053                imagecopyresampled($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH);
054            else {
055                $ni = imagecreate($ftoW, $ftoH);
056                imagecopyresized($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH);
057            }
058        else {
059            $ni = imagecreate($ftoW, $ftoH);
060            imagecopyresized($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH);
061        }
062        //裁剪图片成标准缩略图
063        $new_imgx = imagecreatetruecolor($toW, $toH);
064        if ($newH >= $toH) {
065            imagecopyresampled($new_imgx, $ni, 0, 0, 0, ($newH – $toH) / 2, $toW, $toH, $toW, $toH);
066        else {
067            imagecopyresampled($new_imgx, $ni, 0, 0, ($newW – $toW) / 2, 0, $toW, $toH, $toW, $toH);
068        }
069        switch ($srcInfo[2]) {
070        case 1:
071            imagegif($new_imgx, $toFile);
072            break;
073        case 2:
074            imagejpeg($new_imgx, $toFile, 85);
075            break;
076        case 3:
077            imagepng($new_imgx, $toFile);
078            break;
079        case 6:
080            imagebmp($new_imgx, $toFile);
081            break;
082        default:
083            return false;
084        }
085        imagedestroy($new_imgx);
086        imagedestroy($ni);
087    }
088    imagedestroy($im);
089    return true;
090}
091//[2020-11-04]:解决缩略图缩放变形问题(宽度、高度为后台设置宽高)
092function ImageResize($srcFile, $toW, $toH, $toFile = "") {
093    global $cfg_photo_type;
094    if ($toFile == "") {
095        $toFile = $srcFile;
096    }
097    $info = "";
098    $srcInfo = GetImageSize($srcFile, $info);
099    switch ($srcInfo[2]) {
100    case 1:
101        if (!$cfg_photo_type['gif']) {
102            return false;
103        }
104        $im = imagecreatefromgif($srcFile);
105        break;
106    case 2:
107        if (!$cfg_photo_type['jpeg']) {
108            return false;
109        }
110        $im = imagecreatefromjpeg($srcFile);
111        break;
112    case 3:
113        if (!$cfg_photo_type['png']) {
114            return false;
115        }
116        $im = imagecreatefrompng($srcFile);
117        break;
118    case 6:
119        if (!$cfg_photo_type['bmp']) {
120            return false;
121        }
122        $im = imagecreatefromwbmp($srcFile);
123        break;
124    }
125    $srcW = ImageSX($im);
126    $srcH = ImageSY($im);
127    if ($srcW <= $toW && $srcH <= $toH) {
128        return true;
129    }
130    //缩略生成并裁剪
131    $newW = $toH * $srcW / $srcH;
132    $newH = $toW * $srcH / $srcW;
133    if ($newH >= $toH) {
134        $ftoW = $toW;
135        $ftoH = $newH;
136    else {
137        $ftoW = $newW;
138        $ftoH = $toH;
139    }
140    if ($srcW > $toW || $srcH > $toH) {
141        if (function_exists("imagecreatetruecolor")) {
142@$ni = imagecreatetruecolor($ftoW, $ftoH);
143            if ($ni) {
144                imagecopyresampled($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH);
145            else {
146                $ni = imagecreate($ftoW, $ftoH);
147                imagecopyresized($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH);
148            }
149        else {
150            $ni = imagecreate($ftoW, $ftoH);
151            imagecopyresized($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH);
152        }
153        //裁剪图片成标准缩略图
154        $new_imgx = imagecreatetruecolor($toW, $toH);
155        if ($newH >= $toH) {
156            imagecopyresampled($new_imgx, $ni, 0, 0, 0, ($newH – $toH) / 2, $toW, $toH, $toW, $toH);
157        else {
158            imagecopyresampled($new_imgx, $ni, 0, 0, ($newW – $toW) / 2, 0, $toW, $toH, $toW, $toH);
159        }
160        switch ($srcInfo[2]) {
161        case 1:
162            imagegif($new_imgx, $toFile);
163            break;
164        case 2:
165            imagejpeg($new_imgx, $toFile, 85);
166            break;
167        case 3:
168            imagepng($new_imgx, $toFile);
169            break;
170        case 6:
171            imagebmp($new_imgx, $toFile);
172            break;
173        default:
174            return false;
175        }
176        imagedestroy($new_imgx);
177        imagedestroy($ni);
178    }
179    imagedestroy($im);
180    return true;
181}
182
183dedecms5.7版image.helper.php修改方法:
184
185if (!function_exists('ImageResize')) {
186    function ImageResize($srcFile, $toW, $toH, $toFile = "") {
187        global $cfg_photo_type;
188        if ($toFile == "") {
189            $toFile = $srcFile;
190        }
191        $info = "";
192        $srcInfo = GetImageSize($srcFile, $info);
193        switch ($srcInfo[2]) {
194        case 1:
195            if (!$cfg_photo_type['gif']) {
196                return false;
197            }
198            $im = imagecreatefromgif($srcFile);
199            break;
200        case 2:
201            if (!$cfg_photo_type['jpeg']) {
202                return false;
203            }
204            $im = imagecreatefromjpeg($srcFile);
205            break;
206        case 3:
207            if (!$cfg_photo_type['png']) {
208                return false;
209            }
210            $im = imagecreatefrompng($srcFile);
211            break;
212        case 6:
213            if (!$cfg_photo_type['bmp']) {
214                return false;
215            }
216            $im = imagecreatefromwbmp($srcFile);
217            break;
218        }
219        $srcW = ImageSX($im);
220        $srcH = ImageSY($im);
221        if ($srcW <= $toW && $srcH <= $toH) {
222            return true;
223        }
224        //缩略生成并裁剪
225        $newW = $toH * $srcW / $srcH;
226        $newH = $toW * $srcH / $srcW;
227        if ($newH >= $toH) {
228            $ftoW = $toW;
229            $ftoH = $newH;
230        else {
231            $ftoW = $newW;
232            $ftoH = $toH;
233        }
234        if ($srcW > $toW || $srcH > $toH) {
235            if (function_exists("imagecreatetruecolor")) {
236        @$ni = imagecreatetruecolor($ftoW, $ftoH);
237                if ($ni) {
238                    imagecopyresampled($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH);
239                else {
240                    $ni = imagecreate($ftoW, $ftoH);
241                    imagecopyresized($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH);
242                }
243            else {
244                $ni = imagecreate($ftoW, $ftoH);
245                imagecopyresized($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH);
246            }
247            //裁剪图片成标准缩略图
248            $new_imgx = imagecreatetruecolor($toW, $toH);
249            if ($newH >= $toH) {
250                imagecopyresampled($new_imgx, $ni, 0, 0, 0, ($newH – $toH) / 2, $toW, $toH, $toW, $toH);
251            else {
252                imagecopyresampled($new_imgx, $ni, 0, 0, ($newW – $toW) / 2, 0, $toW, $toH, $toW, $toH);
253            }
254            switch ($srcInfo[2]) {
255            case 1:
256                imagegif($new_imgx, $toFile);
257                break;
258            case 2:
259                imagejpeg($new_imgx, $toFile, 85);
260                break;
261            case 3:
262                imagepng($new_imgx, $toFile);
263                break;
264            case 6:
265                imagebmp($new_imgx, $toFile);
266                break;
267            default:
268                return false;
269            }
270            imagedestroy($new_imgx);
271            imagedestroy($ni);
272        }
273        imagedestroy($im);
274        return true;
275    }
276}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值