失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > php图片留白 PHP:图片不变形处理(留白处理与截取处理)-奇乐网

php图片留白 PHP:图片不变形处理(留白处理与截取处理)-奇乐网

时间:2020-02-04 05:00:44

相关推荐

php图片留白 PHP:图片不变形处理(留白处理与截取处理)-奇乐网

//将$filestring的图像文件变成等比不变形的缩略图($dstwidth*$dstheight)

//$type 用1表示留白方式处理,2表示截取方式处理

function getThumb($filestring,$dstwidth,$dstheight,$type)

{

//0.局部变量声明

$srcimg=0;

$dstx=$dsty=$srcx=$srcy=0;

$dstimg=imagecreatetruecolor($dstwidth,$dstheight);

//1.得到$filestring里的文件类型(png,gif,jpeg,jpg)

$yuan=getimagesize($filestring);

$filetype=$yuan['mime'];

switch($filetype)

{

case "image/png":$srcimg=imagecreatefrompng($filestring);break;

case "image/jpg":

case "image/jpeg":$srcimg=imagecreatefromjpeg($filestring);break;

case "image/gif":$srcimg=imagecreatefromgif($filestring);break;

}

//2.得到原来文件的宽和高$src_w,$src_h

$src_w=imagesx($srcimg);

$src_h=imagesy($srcimg);

//3.计算变形

if($dstwidth/$dstheight < $src_w/$src_h)//传来的图像是横图

{

if($type==1)//用户需要留白处理

{

$dsty=(int)($dstheight-$src_h*$dstwidth/$src_w)/2;

$dstheight=$dstheight-2*$dsty;

}

else//用户需要截取处理

{

$srcx=(int)($src_w-$dstwidth*$src_h/$dstheight)/2;

$src_w=$src_w-2*$srcx;

}

}

else //传来的图像是竖图或等比的图

{

if($type==1)//用户需要留白处理

{

$dstx=(int)($dstwidth-$src_w*$dstheight/$src_h)/2;

$dstwidth=$dstwidth-2*$dstx;

}

else//用户需要截取处理

{

$srcy=(int)($src_h-$dstheight*$src_w/$dstwidth)/2;

$src_h=$src_h-2*$srcy;

}

}

//4.绘制缩略图

imagecopyresampled($dstimg,$srcimg,$dstx,$dsty,$srcx,$srcy,$dstwidth,$dstheight,$src_w,$src_h);

switch($filetype)

{

case "image/png":imagepng($dstimg,"img_thumb.jpg",9);break;

case "image/jpg":

case "image/jpeg":imagejpeg($dstimg,"img_thumb.jpg",100);break;

case "image/gif":imagegif($dstimg,"img_thumb.jpg",100);break;

}

return $dstimg;

}

?>

如果觉得《php图片留白 PHP:图片不变形处理(留白处理与截取处理)-奇乐网》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。