php生成缩略图方法封装

-------------------------------------------------

参数:

$filename : 要裁剪的图片路径

$destination : 要生成的图片文件夹和路径

$dst_w : 要把图片裁剪到多宽

$dst_h : 要把图片裁剪到多高

$scale : 缩小倍数(默认0.5)

返回:

新生成的缩略图的路径

示例调用:

$filename = 'meinv.jpg';

thumb($filename,'lgc/'.$filename,50,50);

-------------------------------------------------

function thumb($filename,$destination=null,$dst_w=null,$dst_h=null,$scale=0.5)

{

list($src_w,$src_h,$imageTypeNum) = getimagesize($filename);

$scale = 0.5;



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($imageTypeNum);

$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);

}



//要生成的新的图片的路径

$imageName = dirname($destination).DIRECTORY_SEPARATOR.getUniName().'.'.getExt($filename);

$outFun($dst_image,$imageName);

//销毁图片资源

imagedestroy($dst_image);

imagedestroy($src_image);



return $imageName;

}

猜你喜欢

转载自blog.csdn.net/WU5229485/article/details/82952369