直接上源码:
该函数支持3中格式的图片等比例缩放
<?php
/*
thumb() 图片缩放
说明:
string thumb(string $file, int $dw ,int $dh ,string $pre)
$file 图片名称+路径
$dw 目标图宽
$dh 目标图高
$pre 目标图前缀
*/
function thumb($file,$dw,$dh,$pre){
$brr=pathinfo($file);
$dir=$brr['dirname']; //图片保存路径
$basename=$brr['basename'];//图片名字
$dstname=$pre.$basename; //目标图片名字
$path=$dir.'/'.$dstname;//目标图片保存轮径
$arr=getimagesize($file); //获得图片信息
$sw=$arr[0]; //原图宽
$sh=$arr[1]; //原图的高
$type=$arr[2]; //图片格式 1 = GIF,2 = JPG,3 = PNG
$mime=$arr['mime']; //MIME 类型
switch ($type) {
case 1:
$imgcreate='imagecreatefromgif';
$imgout='imagegif';
break;
case 2:
$imgcreate='imagecreatefromjpeg';
$imgout='imagejpeg';
break;
case 3:
$imgcreate='imagecreatefrompng';
$imgout='imagepng';
break;
}
$src=$imgcreate($file);
$b=$sw/$dw>$sh/$dh?$sw/$dw:$sh/$dh;
$dw=floor($sw/$b);
$dh=floor($sh/$b);
$dst=imagecreatetruecolor($dw,$dh);
$bool=imagecopyresampled($dst, $src, 0, 0 , 0 , 0, $dw , $dh , $sw, $sh);
$imgout($dst,$path);
return $dstname;
}
?>