按规定宽度等比压缩图片(压缩后图片的大小也会…

2018-06-22 05:11:45来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折


$newWidth=600;

$file_path='D:/myWorkspace/get_img/deal_img/jiaShuiYin/1.jpg';

$newName='D:/myWorkspace/get_img/deal_img/jiaShuiYin/2.jpg';

resizeImage($newWidth,$file_path,$newName);

 

/*
功能:按规定宽度等比压缩图片(压缩后图片的大小也会被压缩)
*/
function resizeImage($maxwidth,$file_path,$name)
{

$im=imagecreatefromjpeg($file_path);
$pic_width = imagesx($im);
$pic_height = imagesy($im);

if($maxwidth && $pic_width > $maxwidth)
{
$ratio = $maxwidth/$pic_width;

$newwidth = $maxwidth;
$newheight = $pic_height * $ratio;
if(function_exists("imagecopyresampled"))
{
$newim = imagecreatetruecolor($newwidth,$newheight);
$state=imagecopyresampled($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);
}else
{
$newim = imagecreate($newwidth,$newheight);
$state=imagecopyresized($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);
}
if($state)
{
if(!imagejpeg($newim,$name)){
write_log($name,"图片写入出错");
}
imagedestroy($newim);
}else
{
write_log($name,"图片写入出错");
}
}else
{
if (!copy($file_path,$name)) {
write_log($name,"图片copy出错");
}
}
}

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:yii2的变量是如何注入到视图中去的?

下一篇:ajax三级联动下拉菜单