//生成不同大小的图
function thumb($filename,$destination=null,$scale=0.5,$dst_w=null,$dst_h=null,$isReservedSource=true){
//获取原图片宽、高、类型
list($src_w,$src_h,$imagetype)=getimagesize($filename);
/**
* 返回结果说明
索引 0 给出的是图像宽度的像素值
索引 1 给出的是图像高度的像素值
索引 2 给出的是图像的类型,返回的是数字,其中1 = GIF,2 = JPG,3 = PNG,4 = SWF,5 = PSD,6 = BMP,7 = TIFF(intel byte order),8 = TIFF(motorola byte order),9 = JPC,10 = JP2,11 = JPX,12 = JB2,13 = SWC,14 = IFF,15 = WBMP,16 = XBM
索引 3 给出的是一个宽度和高度的字符串,可以直接用于 HTML 的 <image> 标签
索引 bits 给出的是图像的每种颜色的位数,二进制格式
索引 channels 给出的是图像的通道值,RGB 图像默认是 3
索引 mime 给出的是图像的 MIME 信息,此信息可以用来在 HTTP C
*
*/
//若参数没有指定宽高,则按默认比例缩放
if(is_null($dst_w)||is_null($dst_h)){
$dst_w=ceil($src_w*$scale);
$dst_h=ceil($src_h*$scale);
}
$mime=image_type_to_mime_type($imagetype);//返回值 image/jpg
$createFun=str_replace("/", "createfrom", $mime);
$outFun=str_replace("/", null, $mime);
$src_image=$createFun($filename);
$dst_image=imagecreatetruecolor($dst_w, $dst_h);
imagecopyresampled($dst_image, $src_image, 0, 0, 0, 0, $dst_w, $dst_h, $src_w, $src_h);
//若目录文件夹不存在则创建文件夹
if($destination&&!file_exists(dirname($destination))){
mkdir(dirname($destination),0777,true);
}
$dstFilename=$destination==null?getUniName().".".getExt($filename):$destination;
$outFun($dst_image,$dstFilename);//将生成的缩略图储存到$dstFilename下
imagedestroy($src_image);
imagedestroy($dst_image);
if(!$isReservedSource){
unlink($filename);
}
return $dstFilename;
}
?>