DEDECMS5.7生成縮圖大小自適應呼叫尺寸及縮圖自動裁剪
溫馨提示:修改前請做好備份事情。
-------
A.首先在網站後臺系統中設定網站縮圖尺寸大小和模板中呼叫圖片最大尺寸同樣。
B.再開啟檔案"include/helpers/extend.helpes.php"在最下面加上以下程式碼
-
if ( ! function_exists('thumb'))
-
{
-
function thumb($imgurl, $width, $height, $bg = true)
-
{
-
global $cfg_mainsite,$cfg_multi_site;
-
$thumb = eregi("http://",$imgurl)?str_replace($cfg_mainsite,'',$imgurl):$imgurl;
-
list($thumbname,$extname) = explode('.',$thumb);
-
$newthumb = $thumbname.'_'.$width.'_'.$height.'.'.$extname;
-
if(!$thumbname || !$extname || !file_exists(DEDEROOT.$thumb)) return $imgurl;
-
if(!file_exists(DEDEROOT.$newthumb))
-
{
-
include_once DEDEINC.'/image.func.php';
-
if($bg==true)
-
{
-
ImageResizeNew(DEDEROOT.$thumb, $width, $height, DEDEROOT.$newthumb);
-
}
-
else
-
{
-
ImageResize(DEDEROOT.$thumb, $width, $height, DEDEROOT.$newthumb);
-
}
-
}
-
return $cfg_multi_site=='Y'?$cfg_mainsite.$newthumb:$newthumb;
-
}
-
}
呼叫方法:
標籤 : [field:picname function='thumb(@me,$width,$height,$bg)'/]
引數說明:
$width:縮圖寬度(整數)
$height:縮圖高度(整數)
$bg:是否用空白填補,預設自動填補,背景填充顏色在系統-附件設定裡(true/false)
舉例:
呼叫長寬為100畫素的縮圖:[field:picname function='thumb(@me,100,100)'/]
保留原有比例,不自動填充(不建議):[field:picname function='thumb(@me,100,100,false)'/]
----
C.再到 include/helpers/image.helpes.php 中寫入以下程式碼
/**
-
* 縮圖片自動生成函式,來源支援bmp、gif、jpg、png
-
* 但生成的小圖只用jpg或png格式
-
* @access public
-
* @param string $srcFile
-
* @param string $toW 轉換到的寬度
-
* @param string $toH 轉換到的高度
-
* @param string $toFile 輸出檔案到
-
* @return string
-
*/
-
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;
-
}
-
}