php生成缩略图,支持gif,jpg,png (需要gd库的支持)

发布时间:2009年01月06日      浏览次数:992 次
<?
/*把老外的resizeToFile改了一下,现在支持三种图片的缩略图:gif,jpg,png
* $sourcefile 源图像的文件名
* $dest_x 要生成的图像宽
* $dest_y 要生成的图像的高
* $targetfile 要生成的图像
* $jpegqual 图像的质量
*/
function resizeToFile2 ($sourcefile, $dest_x, $dest_y, $targetfile, $jpegqual)
{
$picsize=getimagesize("$sourcefile");
$source_x = $picsize[0];
$source_y = $picsize[1];
//[-------------------------------修改过的地方
$arr=explode(".",$sourcefile);
$ext="";
if(isset($arr[count($arr)-1]))
{
$ext=$arr[count($arr)-1];
$ext=strtolower($ext);
}
if($ext=="jpg" or $ext=="jpeg"){
$source_id = imageCreateFromJPEG("$sourcefile");
}elseif($ext=="gif"){
$source_id =imagecreatefromgif("$sourcefile");
}elseif($ext=="png"){
$source_id=imagecreatefrompng("$sourcefile");
}

$width=imagesx($source_id);
$height=imagesy($source_id);
if($width>$height){
$dest_y=($dest_x/$width)*$height;
}else{
$dest_x=($dest_y/$height)*$width;
}
//---------------------------------]
$target_id=imagecreatetruecolor($dest_x, $dest_y);
$target_pic=imagecopyresampled($target_id,$source_id,
0,0,0,0,
$dest_x,$dest_y,
$source_x,$source_y);
imagejpeg ($target_id,"$targetfile",$jpegqual);
return true;
} //end of function
//下面是一个使用的例子:
//resizeToFile2("baboon.png",100,100,"b.png",100);
?>
免责声明:本站相关技术文章信息部分来自网络,目的主要是传播更多信息,如果您认为本站的某些信息侵犯了您的版权,请与我们联系,我们会即时妥善的处理,谢谢合作!