码迷,mamicode.com
首页 > 其他好文 > 详细

将一张图片生成多张缩略图并保存

时间:2015-01-24 23:57:24      阅读:301      评论:0      收藏:0      [点我收藏+]

标签:

代码:

$filename="des_big.jpg";
list($src_w,$src_h,$imagetype)=getimagesize($filename);
$mime=image_type_to_mime_type($imagetype);
//echo $mime;//输出为imae/jpeg
$createFun=str_replace("/","createfrom",$mime);
//imagejpeg()
$outFun=str_replace("/",null,$mime);
$src_image=$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_image,0,0,0,0,50,50,$src_w,$src_h);
imagecopyresampled($dst_220_image,$src_image,0,0,0,0,220,220,$src_w,$src_h);
imagecopyresampled($dst_350_image,$src_image,0,0,0,0,350,350,$src_w,$src_h);
imagecopyresampled($dst_800_image,$src_image,0,0,0,0,800,800,$src_w,$src_h);

$outFun($dst_50_image,"uploads/image_50/".$filename);
$outFun($dst_220_image,"uploads/image_220/".$filename);
$outFun($dst_350_image,"uploads/image_350/".$filename);
$outFun($dst_800_image,"uploads/image_800/".$filename);
imagedestroy($src_image);
imagedestroy($dst_50_image);
imagedestroy($dst_220_image);
imagedestroy($dst_350_image);
imagedestroy($dst_800_image);

 最后会生成一张50*50,220*220,350*350,800*800的图片并分别存在uploads/image_50、uploads/image_220、uploads/image_350、uploads/image_800的文件夹内。

代码中的函数说明:

image_type_to_mime_type — 取得 getimagesize,exif_read_data,exif_thumbnail,exif_imagetype 所返回的图像类型的 MIME 类型

 

imagecreatefromjpeg — 由文件或 URL 创建一个新图象。
说明
resource imagecreatefromjpeg ( string $filename )

imagecreatefromjpeg() 返回一图像标识符,代表了从给定的文件名取得的图像。 

 

将一张图片生成多张缩略图并保存

标签:

原文地址:http://www.cnblogs.com/jacson/p/4246763.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!