解決dedecms(5.6/5.7)縮略圖縮放變形問題方法,我們知道,dedecms縮略圖是自動提取,相當(dāng)于原圖的等比例縮放了,比如后臺設(shè)置縮略圖的尺碼為:120*90即為3:2的圖片,但是假如內(nèi)容里的大圖尺碼為300*300即1:1,這樣生成出來的圖片就會變形,直接后臺設(shè)置的縮略圖大小不起作用啊,這樣嚴(yán)重影響網(wǎng)站美觀,本文介紹通過修改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文件。
5.6版image.func.php修改方法(直接替換原來方法)
//[2020-11-04]:解決縮略圖縮放變形問題(寬度、高度為后臺設(shè)置寬高) 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); } //裁剪圖片成標(biāo)準(zhǔn)縮略圖 $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; }
5.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); } //裁剪圖片成標(biāo)準(zhǔn)縮略圖 $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; } }
到此這篇關(guān)于解決dedecms(5.6/5.7)縮略圖縮放變形問題方法的文章就介紹到這了,更多相關(guān)dedecms縮略圖變形問題內(nèi)容請搜索素材無憂以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持素材無憂!
版權(quán)聲明: 本站資源均來自互聯(lián)網(wǎng)或會員發(fā)布,如果侵犯了您的權(quán)益請與我們聯(lián)系,我們將在24小時內(nèi)刪除!謝謝!
轉(zhuǎn)載請注明: 解決dedecms(5.6/5.7)縮略圖縮放變形問題方法