This article describes the method of PHP custom picture scaling function to achieve equal-scale undistorted scaling. Share it for your reference, as follows:
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); } }
Parameter description:
$im image object. Before applying the function, you need to use imagecreatefromjpeg() to read the image object. If the PHP environment supports PNG and GIF, you can also use imagecreatefromgif() and imagecreatefrommpng();
$maxwidth defines the maximum width of the generated image (units: pixels)
$maxheight Maximum height of the generated image (unit: pixels)
$name The generated image name
$filetype The final generated image type (.jpg/.png/.gif)
Code comments:
Lines 3~4: Read the actual width and height of the picture that needs to be scaled
Lines 8 to 26: By calculating the compression ratio between the actual image width and height of the image to be generated, it is finally concluded whether the image scaling is scaled based on the width or height. The current program is scaling based on the width. If you want to zoom in on image by height, you can change the statement on line 22 to $widthratio>$heightratio
Lines 28-31: If the length or width of the actual picture is smaller than the length or width of the specified generated picture, either the picture is scaled according to the length or the picture is scaled according to the width.
Lines 33~34: Calculate the length and width of the image generated by the final scale.
Lines 36~45: Change the image size based on the calculated length and width of the final generated image. There are two ways to change the image size: The ImageCopyResized() function is valid in all GD versions, but its algorithm for scaling the image is relatively rough. ImageCopyResamples(), the pixel interpolation algorithm obtains smoother image edges, but the speed of this function is slower than ImageCopyResized().
Lines 47~49: Finally, the processed image is generated. If you need to generate GIF or PNG, you need to change the imagejpeg() function to imagegif() or imagepng()
Lines 51~56: If the length and width of the actual image are smaller than the length and width of the specified generated image, keep the image as it is. Similarly, if you need to generate GIF or PNG, you need to change the imagejpeg() function to imagegif() or imagepng().
Special Note:
GD library version 1.6.2 previously supported the GIF format, but because the use of LZW algorithm for GIF format involves patent rights, the GIF format does not support the GIF format after GD1.6.2. If you are in the WINDOWS environment, you just need to enter the file and find extension=php_gd2.dll, remove #, and restart APACHE. If you are in the Linux environment and want to support GIF, PNG, and JPEG, you need to download libpng, zlib, and freetype fonts and install them.
For more information about PHP related content, please check out the topic of this site:Summary of PHP graphics and picture operation skills》、《Complete collection of PHP array (Array) operation techniques》、《Summary of PHP mathematical operation skills》、《Summary of the usage of php date and time》、《PHP object-oriented programming tutorial》、《Summary of usage of php strings》、《PHP+mysql database operation tutorial"and"Summary of common database operation techniques for php》
I hope this article will be helpful to everyone's PHP programming.