解决dedecms(5.6/5.7)缩略图缩放变形问题方法,我们知道,dedecms缩略图是自动提取,相当于原图的等比例缩放了,比如后台设置缩略图的尺码为:120*90即为3:2的图片,但是假如内容里的大图尺码为300*300即1:1,这样生成出来的图片就会变形,直接后台设置Q c e ? ,的缩略图大小不起作用啊,这样严重影响网站N Q 7 B P美观,本文介绍通过修改dedecms生成缩略源码方法解决定问题。
打开inH m &clude/image.func.php文件,该文件在dedq v @ S \ . ? kecms5.6/5.7中所在的目录不一样,5.6中文件在/include/下,5.7中文件在/include/helpers/c d 7 J n d V U
如果你使用的是dedecms5.7,打开目录/include/hel] E M . 5 I t J jpers/找到image.helper.php文件。
如果你使用的是dedecms5.6,打开目录/include/找到image.func.php文件。
dedecms_ b L v e n5.6版image.func.phpu # H $ M * ;修改方Y m _ [ t ? x E法(直接替换原来方法)
/* g ] E l ~ c/[2020-11-04]:解决缩略图缩放变形问题(宽度、高度为后台设置宽高)
fun% Q J 3 q d @ction ImageResizei 1 M K #($srcFile, $toW, $toH, $toFile = "") { global $cfg_photo_: . * \ g M \ wtype; if ($toFile == "") { $toq f - z w 5 0File = $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_ph& x :oto_type['jpeg']) { return false; } $im = imagecreatefromjpeg($srcFile); br_ \ 1 9eak; caseO % V 3: if (!$cfg_photo_type['png']) { return false; } $im = imagecreatefrompng($srcFile); break; case 6: if (!$cfg_photo_type['bmp']) { return false;u c V C M g R c ? } $im = imagecreatefromwX L ` f V ; nbmp($srcFile); break; } $srcW = ImageSX($im); $srcH = ImageSY($im); iS ( v A 8 5 1 If ($srcW <= $toW && $srcH <= $toH) { retv P 6 2urn true; } //缩略生成并裁剪 $newW = $toH * $srcW / $srcH; $newH = $toW * $srcH / $srcW; if ($/ j b O XnewH >= $toH) { $ftoW = $toW; $ftoH = $` ^ $ A 9 ] 9newH; } elsey 3 F { X { $ftoW = $newW; $ftoH = $toH; } iH { t @f ($srcW > $toW || $srcH >C a 7 { 1 z ! $toH) { if (function_exists("imagecreatetruecolor")) { @$ni = imagecreatetru. & S $ y % 7 4 -ecolor($ftoW, $ftoH); if ($ni) { imagecopyresampled($ni, $im? } y a % f \ 9, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH); }b ~ 5 else { $ni = imagecreate(M 0 ~ G T ? n$ftoW, $ftoH); iman 8 T AgecoL b B ; n ~pyresized($ni, $im, 0, 0, 0, 0, $ftoW, $fto$ k H . J t p m =H, $srcW, $srcH); } } else { $ni = imagecreate($ftoW, $ftoH); imagecopyresized($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $sH % 8 ) 6 4 ircW, $srcH); } //裁剪? f 1 ) * w : -图片成标准缩略图 $new_imgx = imagecreateS L _ ( Z \ [ +truecolor($to\ , d WW, $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, ($newW9 _ ; [ l N – $toW) / 2, 0, $D d z } \ ,toW, $toH, $toW, $toH); } switch ($srcInfo[2]) { case 1: imagegif($new_ima t K ` h [ ! a lgx, $toFile); break; case 2: imagejpeg($new_imgx, $toFile, 85); break; casv K p ke 3: imagepng($new_imgx, $toFile); break; case 6: imagebmG v 2 \p($new_imgx, $toFile); break; default: return false; } imagedestroy($new_imgx); imagedestroy($no Y ; u 5 f !i); } imagedestroy($im); return tr\ G x h Due; } //[2020-11-04]:解决缩略图缩放变形问题! ~ 8 z(宽度、高度为后台设置宽高) function Imager ? } O RResize($srcFile, $toW, $toH, $toFile = "") { gl& , t 8 *obal $cfg_photo_type; if ($toFile == "") { $toFile = $srcFile; } $info =d B } ""; $srcInfY / - h 6 P 5o = GetImageSize($srcFile, $info); switch ($srcInfo[2]) { case 1: if (!$cfg_photo_type['gif']) { return false; } $3 f a n g V Qim = imagecreatefromgif($srcFile); break; case 2: if (!$cfg_photo_type['jpeg']) { return false; } $im = imagecreatefromjpegm | . E Y &($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 <k d ,= $toW && $V 1 o | *srcH <= $toH) { return tr+ # ) xue; } //c } ) U ~缩略生成并裁剪 $newW = $toH * $srcW / $srcH; $newH = $toW * $srcH / $srcW; if ($newH >= $toH) { $ftoW = $tp 6 ] k L ( q VoW; $ftoH = $newH; } else2 m F B E { $ftoW = $newWc K C { u ^; $ftoH = $toH; } if ($s; q U yrcW > $toW |# C `| $srcH > $toH); d 3 { if (funct/ L ~ U Sion_exists("] { }imagecreatetruc } $ ) k ] Eecolor")) { @$ni = imagecreatetruecolor(8 b j g C$ftoW, $ftoH); if ($ni) { imagecopyresampled($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH); } else { $ni = imagecreate($ftoW, $fh 5 A ( N s ltoH); imagecopyresized($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH); } } else { $ni = imagecreate# ! g ( Y v # i /($ftoW, $ftoH); imagecopx ) v R B ~ ! R nyresized($ni, $im,7 $ ? a 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH)I k _ D P 4 v h; } //裁剪图片成标* ` O O o % @ %准缩略图 $ne4 | 7 \w_imgx = imagecreatetruecolor($toW, $toH); if ($newH >= $toH) {2 M x / q P o G 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)U S | w @ 9 K T; } switch ($srcInfo[2]) { case 1G } b R ; \ / .: imagegif($new_imgx, $toFile); break; case 2: imagejpeg($new_imgx, $toFile, 85); breq L F H [ _ak; case 3:` t e . v q F # imagepnM L v r 1 1g($new_imgx, $toFile); break; case 6: imagebmp($new_imgx, $toFile); break; default: return false; } imagedestroy($new_im0 v 6 = N # ( Xgx);^ 5 X imagedestroy($ni); } i! a / p + = \ ^ ]magedestroy($im); return true; } dedecms5.\ r 2 B l ] N7版image- , 9 } B - # W 7.helper.php修改方法: if (!fuH * Y 7 inction_exists(j x N 7 S s O w'ImageResize'? % b)) { funcI | ) 9 s 6tion ImageResize($srcu b ; [ ~ 3File, $5 m h 6 j ) C !toW, $toH, $toFile = "") { global $cfg_photo_type; if ($toFile == "") { $toFile = $srcFile; } $info = ""; $srcInfo = GetImageSize($srcFile, $info); switch ($srcInfo[2]) { ca: H P ( m N K zse 1: if (!$cfg_photo_type['gif']) { return false; } $im = imagec% C o y U [ &reatefromgif($srcFile); break; case 2: if (!$c? [ L 4 :fg_photo_type['jpeg']) { return false; } $im = imagecreatefromjpeg($srcFile); break; case 3: if (!$cfg_photo_1 ] \ r 0 / 7type['png']) { returnt 7 a E w T $ 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 ($srch 7 kW <= $toW && $srcH <= $toH) { return true; } //缩略生成并裁剪 $newW = $toH * $srcW / $srcH; $newH = $toW * $srcH / $src2 f b s | 1 A j HW; if ($newH >= $tZ 1 X FoH) { $ftoW = $toW; $ftoH = $newH; } else { $fm J : [ ! \ 1 Y *toi j $ r c 8W = $newW;\ c = H E ` t ] } $ftoH = $toH; } if ($srcW > $toW || $srcH > $toH) { if (function_exists("imagecreatetruecolor")) { @$ni = imagea W F E k :createtruecolor($ftoW,8 C ] $ftoH); if ($n\ a , g E j \i) { imageco. u o - @pyresampled($ni, $im, 0, 0, 0, 0, $ftoW, $a A g t i + ! } uftoH, $sJ q ] N ; NrcW,_ / T ` 0 4 S O $ $srcH); } else { $ni = imagecreate($ftoW` x I, $ftoH); imagecopyresized($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH); } } else { $ni = imagecreate($ftoW, $ftoH); imag\ ] ! 2 k / | ye] = - I Rcopyresized($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH\ 5 Z ? O u); } //裁剪图片成标准缩略图 $new_imgx = imagecreatetruecolor($toW, $toH); if ($newH >= $toH) { imagecopyresampled($new\ J Z : ` ^ U_imgx, $ni, 0, 0, 0, ($newH – $toH) / 2, $toW, $toH, $toW, $toH); } else { imagecopyresampk } % * A Z 3led($new_imgx, $) x { z S 4 Gni, 0, 0, ($newW – $toW) / 2, 0, $toW, $toH, $toW, $t, ` } FoH); } switch ($srcInfo[2]) { case 1: imagegi6 O ; Of($new_imgx, $toFile); break; casR E _ Q le 2: imagejpeg($new_imgx, $toFile, 85); br9 } ( teak# # 9 # N j 9; case 3:T O # o s H o z 5 imagepng($new_imgx, $tow E f j J ] 3 :File); break; case 6: imagebmp($new_imgx, $toFile); break; default: return false; } imagedestroy($new_imgx); imagedestro3 ) ? cy($ni)7 + A; } imagedeO E j \ k g _ # sstrc k aoy($im); return trup ^ _ q K = de; } }