/*
* 前台调用创建缩略图
*/
function thumb_img($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;
}
}
关键字词:

