PHP图片缩放函数:实现等比例不失真缩放

function resizeImage($im,$maxwidth,$maxheight,$name,$filetype)

{

$pic_width = imagesx($im);

$pic_height = imagesy($im);

if(($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight))

{

if($maxwidth && $pic_width>$maxwidth)

{

$widthratio = $maxwidth/$pic_width;

$resizewidth_tag = true;

}

if($maxheight && $pic_height>$maxheight)

{

$heightratio = $maxheight/$pic_height;

$resizeheight_tag = true;

}

if($resizewidth_tag && $resizeheight_tag)

{

if($widthratio<$heightratio)

$ratio = $widthratio;

else

$ratio = $heightratio;

}

if($resizewidth_tag && !$resizeheight_tag)

$ratio = $widthratio;

if($resizeheight_tag && !$resizewidth_tag)

$ratio = $heightratio;

$newwidth = $pic_width * $ratio;

$newheight = $pic_height * $ratio;

if(function_exists("imagecopyresampled"))

{

$newim = imagecreatetruecolor($newwidth,$newheight);

imagecopyresampled($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);

}

else

{

$newim = imagecreate($newwidth,$newheight);

imagecopyresized($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);

}

$name = $name.$filetype;

imagejpeg($newim,$name);

imagedestroy($newim);

}

else

{

$name = $name.$filetype;

imagejpeg($im,$name);

}

}

参数说明

$im 图片对象,应用函数之前,你需要用imagecreatefromjpeg()读取图片对象,如果PHP环境支持PNG,GIF,也可使用imagecreatefromgif(),imagecreatefrompng()

$maxwidth 定义生成图片的最大宽度(单位:像素)

$maxheight 生成图片的最大高度(单位:像素)

$name 生成的图片名

$filetype 最终生成的图片类型(.jpg/.png/.gif)

代码注释

第3~4行:读取需要缩放的图片实际宽高

第8~26行:通过计算实际图片宽高与需要生成图片的宽高的压缩比例最终得出进行图片缩放是根据宽度还是高度进行缩放,当前程序是根据宽度进行图片缩放。如果你想根据高度进行图片缩放,你可以将第22行的语句改成$widthratio>$heightratio

第28~31行:如果实际图片的长度或者宽度小于规定生成图片的长度或者宽度,则要么根据长度进行图片缩放,要么根据宽度进行图片缩放。

第33~34行:计算最终缩放生成的图片长宽。

第36~45行:根据计算出的最终生成图片的长宽改变图片大小,有两种改变图片大小的方法:ImageCopyResized()函数在所有GD版本中有效,但其缩放图像的算法比较粗糙。ImageCopyResamples(),其像素插值算法得到的图像边缘比较平滑,但该函数的速度比ImageCopyResized()慢。

第47~49行:最终生成经过处理后的图片,如果你需要生成GIF或PNG,你需要将imagejpeg()函数改成imagegif()或imagepng()

第51~56行:如果实际图片的长宽小于规定生成的图片长宽,则保持图片原样,同理,如果你需要生成GIF或PNG,你需要将imagejpeg()函数改成imagegif()或imagepng()。

特别说明

  GD库1.6.2版以前支持GIF格式,但因GIF格式使用LZW演算法牵涉专利权,因此在GD1.6.2版之后不支持GIF的格式。如果你是WINDOWS的环境,你只要进入PHP.INI文件找到extension=php_gd2.dll,将#去除,重启APACHE即可,如果你是Linux环境,又想支持GIF,PNG,JPEG,你需要去下载libpngzlib,以及freetype字体并安装。