在进行缩略图生成的时候,图片的大小总是会同时保存好几个尺寸的,那该怎么处理呢?
$filename = 'meinv.jpg';
list($src_w,$src_h,$imageTypeNum) = getimagesize($filename);
//获取图片mime类型 image_type_to_mime_type(int imagetype); image/jpeg
$imageTypeMime = image_type_to_mime_type($imageTypeNum);
//拼凑出 imagecreatefromjpeg()
$createFun = str_replace('/','createfrom',$imageTypeMime);
//拼凑出imagejpeg()
$outFun = str_replace('/',null,$imageTypeMime);
//创建图片源
$src_img = $createFun($filename);
//创建缩略图片
$dst_50_image = imagecreatetruecolor(50,50);
$dst_220_image = imagecreatetruecolor(220,220);
$dst_350_image = imagecreatetruecolor(350,350);
$dst_800_image = imagecreatetruecolor(800,800);
//生成缩略图
imagecopyresampled($dst_50_image,$src_img,0, 0, 0, 0, 50,50,$src_w,$src_h);
imagecopyresampled($dst_220_image,$src_img,0, 0, 0, 0, 220,220,$src_w,$src_h);
imagecopyresampled($dst_350_image,$src_img,0, 0, 0, 0, 350,350,$src_w,$src_h);
imagecopyresampled($dst_800_image,$src_img,0, 0, 0, 0, 800,800,$src_w,$src_h);
//输出缩略图
$outFun($dst_50_image,'./images_50/'.'50_'.$filename);
$outFun($dst_220_image,'./images_220/'.'220_'.$filename);
$outFun($dst_350_image,'./images_350/'.'350_'.$filename);
$outFun($dst_800_image,'./images_800/'.'800_'.$filename);
//销毁图片
imagedestroy($src_img);
imagedestroy($dst_50_image);
imagedestroy($dst_220_image);
imagedestroy($dst_350_image);
imagedestroy($dst_800_image);
效果如下:
上述的code的核心,就是通过图片mime类型拼凑出 imagecreatefromjpeg() 、imagecreatefrompng()、imagejpeg() 、imagepng() 等函数调用… ~~!