适用dedecms版本5.7:
其他版本不适用。
找到如下目录文件:include/extend.func.php
最后面加上如下代码:
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)) {
if ($bg == true) {
image_resize(DEDEROOT . $thumb, DEDEROOT . $newthumb, $width, $height);
} else {
image_resize(DEDEROOT . $thumb, DEDEROOT . $newthumb, $width, $height);
}
}
return $cfg_multi_site == 'Y' ? $cfg_mainsite . $newthumb : $newthumb;
}
function image_resize($f, $t, $tw, $th) {
// 按指定大小生成缩略图,而且不变形,缩略图函数
$temp = array(1 => 'gif', 2 => 'jpeg', 3 => 'png');
list($fw, $fh, $tmp) = getimagesize($f);
if (!$temp[$tmp]) {
return false;
}
$tmp = $temp[$tmp];
$infunc = "imagecreatefrom$tmp";
$outfunc = "image$tmp";
$fimg = $infunc($f);
if ($fw / $tw > $fh / $th) {
$fw = $tw * ($fh / $th);
} else {
$fh = $th * ($fw / $tw);
}
$timg = imagecreatetruecolor($tw, $th);
imagecopyresampled($timg, $fimg, 0, 0, 0, 0, $tw, $th, $fw, $fh);
if ($outfunc($timg, $t)) {
return true;
} else {
return false;
}
}调用方法
[field:picname function='thumb(@me,232,141)'/] 232是截取图片宽 141是截取图片高度 压缩缩略图片会自动生成在原文件同一个目录


关键字词:

