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


$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出错");
}
}
}

posted @ 2017-03-10 17:44  明月照亮天涯  阅读(908)  评论(0编辑  收藏  举报