1. 程式人生 > >DEDECMS5.7生成縮圖大小自適應呼叫尺寸及縮圖自動裁剪

DEDECMS5.7生成縮圖大小自適應呼叫尺寸及縮圖自動裁剪

 溫馨提示:修改前請做好備份事情。

-------
A.首先在網站後臺系統中設定網站縮圖尺寸大小和模板中呼叫圖片最大尺寸同樣。
B.再開啟檔案"include/helpers/extend.helpes.php"在最下面加上以下程式碼

  1. if ( ! function_exists('thumb')) 

  2. function thumb($imgurl, $width, $height, $bg = true) 

  3. global $cfg_mainsite,$cfg_multi_site; 

  4. $thumb = eregi("http://",$imgurl)?str_replace($cfg_mainsite,'',$imgurl):$imgurl; 

  5. list($thumbname,$extname) = explode('.',$thumb); 

  6. $newthumb = $thumbname.'_'.$width.'_'.$height.'.'.$extname; 

  7. if(!$thumbname || !$extname || !file_exists(DEDEROOT.$thumb)) return $imgurl; 

  8. if(!file_exists(DEDEROOT.$newthumb)) 

  9. include_once DEDEINC.'/image.func.php'; 

  10. if($bg==true) 

  11. ImageResizeNew(DEDEROOT.$thumb, $width, $height, DEDEROOT.$newthumb); 

  12. else 

  13. ImageResize(DEDEROOT.$thumb, $width, $height, DEDEROOT.$newthumb); 

  14. return $cfg_multi_site=='Y'?$cfg_mainsite.$newthumb:$newthumb; 

  15. }


呼叫方法:
標籤 : [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 中寫入以下程式碼

/** 

  1. * 縮圖片自動生成函式,來源支援bmp、gif、jpg、png 

  2. * 但生成的小圖只用jpg或png格式 

  3. * @access public 

  4. * @param string $srcFile

  5. * @param string $toW 轉換到的寬度 

  6. * @param string $toH 轉換到的高度

  7. * @param string $toFile 輸出檔案到 

  8. * @return string 

  9. */ 

  10. if ( ! function_exists('ImageResize')) 

  11. function ImageResize($srcFile, $toW, $toH, $toFile="") 

  12. global $cfg_photo_type; 

  13. if($toFile=="") 

  14. $toFile = $srcFile; 

  15. $info = ""; 

  16. $srcInfo = GetImageSize($srcFile,$info); 

  17. switch ($srcInfo[2]) 

  18. case 1: 

  19. if(!$cfg_photo_type['gif']) 

  20. return false; 

  21. $im = imagecreatefromgif($srcFile); 

  22. break; 

  23. case 2: 

  24. if(!$cfg_photo_type['jpeg']) 

  25. return false; 

  26. $im = imagecreatefromjpeg($srcFile); 

  27. break; 

  28. case 3: 

  29. if(!$cfg_photo_type['png']) 

  30. return false; 

  31. $im = imagecreatefrompng($srcFile); 

  32. break; 

  33. case 6: 

  34. if(!$cfg_photo_type['bmp']) 

  35. return false; 

  36. $im = imagecreatefromwbmp($srcFile); 

  37. break; 

  38. $srcW=ImageSX($im); 

  39. $srcH=ImageSY($im); 

  40. if($srcW<=$toW && $srcH<=$toH ) 

  41. return true; 

  42. //縮略生成並裁剪 

  43. $newW = $toH * $srcW / $srcH; 

  44. $newH = $toW * $srcH / $srcW; 

  45. if($newH >= $toH) 

  46. $ftoW = $toW; 

  47. $ftoH = $newH; 

  48. else 

  49. $ftoW = $newW; 

  50. $ftoH = $toH; 

  51. if($srcW>$toW||$srcH>$toH) 

  52. if(function_exists("imagecreatetruecolor")) 

  53. @$ni = imagecreatetruecolor($ftoW,$ftoH); 

  54. if($ni) 

  55. imagecopyresampled($ni,$im,0,0,0,0,$ftoW,$ftoH,$srcW,$srcH); 

  56. else 

  57. $ni=imagecreate($ftoW,$ftoH); 

  58. imagecopyresized($ni,$im,0,0,0,0,$ftoW,$ftoH,$srcW,$srcH); 

  59. else 

  60. $ni=imagecreate($ftoW,$ftoH); 

  61. imagecopyresized($ni,$im,0,0,0,0,$ftoW,$ftoH,$srcW,$srcH); 

  62. //裁剪圖片成標準縮圖 

  63. $new_imgx = imagecreatetruecolor($toW,$toH); 

  64. if($newH >= $toH) 

  65. imagecopyresampled($new_imgx,$ni,0,0,0,($newH - $toH)/2,$toW,$toH,$toW,$toH); 

  66. else 

  67. imagecopyresampled($new_imgx,$ni,0,0,($newW - $toW)/2,0,$toW,$toH,$toW,$toH); 

  68. switch ($srcInfo[2]) 

  69. case 1: 

  70. imagegif($new_imgx,$toFile); 

  71. break; 

  72. case 2: 

  73. imagejpeg($new_imgx,$toFile,85); 

  74. break; 

  75. case 3: 

  76. imagepng($new_imgx,$toFile); 

  77. break; 

  78. case 6: 

  79. imagebmp($new_imgx,$toFile); 

  80. break; 

  81. default: 

  82. return false; 

  83. imagedestroy($new_imgx); 

  84. imagedestroy($ni); 

  85. imagedestroy($im); 

  86. return true; 

  87. }