关于缩略图

封装缩略图函数

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

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

$scale=0.5;

if($dst_w==null||$dst_h==null){

$dst_w=ceil($src_w*$scale);

$dst_h=ceil($src_h*$scale);

}

$mine=image_type_to_mime_type($imagetype);

$createFun=str_replace("/","createfrom",$mine);

$outFun=str_replace("/",null,$mine);

$dst_image=$createFun($filename);

$src_image=imagecreatetruecolor($dst_w,$dst_h);

imagecopyresqmple($dst_image,$src_image,0,0,0,0,$dst_w,$dst_h,$src_w,$src_h);

if($destination&&!file_exists($filename){
mkdir(dirname($destination),0777,true);

}else{

echo “<script>alert('文件没有');</script>";

}

$dstFilename=$destination==null?md5(uniqid(microtime(true),true)).".".pathinfo($filename,PATHINFO_EXTENSION):$destination;

$outFun($dst_image);

imagedestroy($dst_image);

imagedestroy($src_image);

if(!isResevrsource){

unlink($filename);

}

//首先getimagesize会返回图片的类型,长度,宽度,等资源标示符,然后用image_type_to_mime_type返回该图片的类型,比如image/jpeg,然后用str_replace("/"."createfrom"$mine)使其转变称函数imagecreatefromjpeg,或者其他imagefrompng根据上传文件的类型决定,生成原始图片,然后再创建一个新的画布资源,imagecreatetruecolor,然后重新采样,将原始图片复制过去,然后根据有没有目录,再创建目录,生成文件唯一标示名,最后返回出去.

 

posted @ 2016-08-28 10:31  小白-前端  阅读(1061)  评论(0编辑  收藏  举报