Function resizeImage($im, $maxwidth, $maxheight, $name, $filetype)
{
$ pic _ width = imagesx($ im);
$ pic _ height = imagesy($ im);
if(($ maxwidth & amp; & amp$ pic _ width & gt$ maxwidth)($ max height & amp; & amp$ pic _ height & gt$maxheight))
{
if($ maxwidth & amp; & amp$ pic _ width & gt$maxwidth)
{
$ width ratio = $ maxwidth/$ pic _ width;
$ resizewidth _ tag = true
}
if($max height & amp; & amp$ pic _ height & gt$maxheight)
{
$ height ratio = $ max height/$ pic _ height;
$ resizeheight _ tag = true
}
if($ resize width _ tag & amp; & amp$resizeheight_tag)
{
if($ width ratio & lt; $heightratio)
$ ratio = $ widthratio
other
$ ratio = $ heightratio
}
if($ resize width _ tag & amp; & amp! $resizeheight_tag)
$ ratio = $ widthratio
if($ resize height _ tag & amp; & amp! $resizewidth_tag)
$ ratio = $ heightratio
$ newwidth = $ pic _ width * $ ratio
$ newheight = $ pic _ height * $ ratio
if(function _ exists(" imagecopyresampled "))
{
$ newim = imagecreatetruecolor($ new width,$ new height);
imagecopyresampled($newim,$im,0,0,0,$newwidth,$newheight,$pic_width,$ pic _ height);
}
other
{
$newim = imagecreate($newwidth,$ new height);
imagecopyrestized($ newim,$im,0,0,0,$newwidth,$newheight,$pic_width,$ pic _ height);
}
$name = $name。 $ filetype
imagejpeg($newim,$ name);
image destroy($ newim);
}
other
{
$name = $name。 $ filetype
imagejpeg($im,$ name);
}
}
Parameter description:
$im picture object, before applying this function, you need to use imagecreatefromjpeg () to read the picture object. If the PHP environment supports PNG and GIF, you can also use imagecreatefromgif () and ImageCreateFrompng ();
$maxwidth defines the maximum width (in pixels) of the generated picture.
$maxheight Maximum height of generated picture (unit: pixels)
Name of the picture generated by $name
$filetype(.jpg/。 png/。 gif)
Code comments:
Lines 3~4: Read the actual width and height of the picture to be scaled.
Line 8-26: By calculating the compression ratio between the width and height of the actual picture and the width and height of the picture to be generated, it is finally concluded whether to scale the picture according to the width or height. The current scheme is to scale the picture according to its width. If you want to scale the picture according to its height, you can change the statement in line 22 to $ widthratio & gt$heightratio.
Line 28~3 1: If the length or width of the actual picture is less than the specified length or width of the generated picture, the picture will be scaled according to the length or width.
Line 33-34: Calculate the length and width of the image generated by the final scaling.
Line 36~45: There are two ways to change the image size according to the calculated length and width of the final generated image: the ImageCopyResized () function is effective in all GD versions, but its algorithm for scaling the image is relatively rough. ImageCopyResamples (), whose pixel interpolation algorithm gets smoother image edges, is slower than ImageCopyResized ().
Lines 47-49: The processed image is finally generated. If you need to generate GIF or PNG, you need to change the imagejpeg () function to imagegif () or imagepng ().
Line 5 1~56: If the length and width of the actual picture are less than the specified length and width of the generated picture, the picture will remain the same. Similarly, if you need to generate GIF or PNG, you need to change the imagejpeg () function to imagegif () or imagepng ().
Special instructions:
GD library supported GIF format before 1.6.2, but it did not support GIF format after 1.6.2, because the use of LZW algorithm in GIF format involves patent rights. If it is a WINDOWS environment, just enter PHP. The INI file finds extension=php_gd2.dll, removes # and restarts APACHE. If it is a Linux environment and wants to support GIF, PNG and JPEG, you need to download and install libpng, zlib and freetype fonts.
Ok, PHP image compression function is completed, and finally, summarize the whole processing idea:
By calculating the scaling ratio between the length and width of the actual picture and the specified length and width of the generated picture, the size of the final generated picture (whether the picture is scaled by width or by height) is calculated according to the actual needs, and then the picture is processed by using the PHP picture processing function, and finally the picture is output.
The above is a functional description of how to compress pictures and keep them undistorted in PHP image processing.