解决dedecms缩略图变形问题 dede缩略图按比例裁剪.docx
温馨提示:修改前请做好备份事情。
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 中写入以下代码
/**
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.{
12.function ImageResize($srcFile, $toW, $toH, $toFile="")
13.{
14.global $cfg_photo_type;
15.if($toFile=="")
16.{
17.$toFile = $srcFile;
18.}
19.$info = "";
20.$srcInfo = GetImageSize($srcFile,$info);
21.switch ($srcInfo[2])
22.{
23.case 1:
24.if(!$cfg_photo_type['gif'])
25.{
26.return false;
27.}
28.$im = imagecreatefromgif($srcFile);
29.break;
30.case 2:
31.if(!$cfg_photo_type['jpeg'])
32.{
33.return false;
34.}
35.$im = imagecreatefromjpeg($srcFile);
36.break;
37.case 3:
38.if(!$cfg_photo_type['png'])
39.{
40.return false;
41.}
42.$im = imagecreatefrompng($srcFile);
43.break;
44.case 6:
45.if(!$cfg_photo_type['bmp'])
46.{
47.return false;
48.}
49.$im = imagecreatefromwbmp($srcFile);
50.break;
51.}
52.$srcW=ImageSX($im);
53.$srcH=ImageSY($im);
54.if($srcW<=$toW && $srcH<=$toH )
55.{
56.return true;
57.}
58.//缩略生成并裁剪
59.$newW = $toH * $srcW / $srcH;
60.$newH = $toW * $srcH / $srcW;
61.if($newH >= $toH)
62.{
63.$ftoW = $toW;
64.$ftoH = $newH;
65.}
66.else
67.{
68.$ftoW = $newW;
69.$ftoH = $toH;
70.}
71.if($srcW>$toW||$srcH>$toH)
72.{
73.if(function_exists("imagecreatetruecolor"))
74.{
75.@$ni = imagecreatetruecolor($ftoW,$ftoH);
76.if($ni)
77.{
78.imagecopyresampled($ni,$im,0,0,0,0,$ftoW,$ftoH,$srcW,$srcH);
79.}
80.else
81.{
82.$ni=imagecreate($ftoW,$ftoH);
83.imagecopyresized($ni,$im,0,0,0,0,$ftoW,$ftoH,$srcW,$srcH);
84.}
85.}
86.else
87.{
88.$ni=imagecreate($ftoW,$ftoH);
89.imagecopyresized($ni,$im,0,0,0,0,$ftoW,$ftoH,$srcW,$srcH);
90.}
91.//裁剪图片成标准缩略图
92.$new_imgx = imagecreatetruecolor($toW,$toH);
93.if($newH >= $toH)
94.{
95.imagecopyresampled($new_imgx,$ni,0,0,0,($newH - $toH)/2,$toW,$toH,$toW,$toH);
96.}
97.else
98.{
99.imagecopyresampled($new_imgx,$ni,0,0,($newW - $toW)/2,0,$toW,$toH,$toW,$toH);
100.}
101.switch ($srcInfo[2])
102.{
103.case 1:
104.imagegif($new_imgx,$toFile);
105.break;
106.case 2:
107.imagejpeg($new_imgx,$toFile,85);
108.break;
109.case 3:
110.imagepng($new_imgx,$toFile);
111.break;
112.case 6:
113.imagebmp($new_imgx,$toFile);
114.break;
115.default:
116.return false;
117.}
118.imagedestroy($new_imgx);
119.imagedestroy($ni);
120.}
121.imagedestroy($im);
122.return true;
123.}
124.}
发表评论 取消回复