- /**
- * PHP Picture Shear Scaling Function Parameter description:
- * $im Picture object, need to use IMAGECREATEFROMJPEG () to read the picture object, if PHP support PNG, GIF, can use Imagecreatefromgif (), imagecreatefrompng ();
- * $maxwidth define the maximum width (in pixels) of the resulting picture
- * $maxheight the maximum height (in pixels) of the generated picture
- * $name the generated picture name
- * $filetype The resulting picture type (. jpg/.png/.gif)
- */
- function Resizeimage ($im, $maxwidth, $maxheight, $name, $filetype) {
- Read the actual width of the picture that needs to be scaled
- $pic _width = Imagesx ($im);
- $pic _height = Imagesy ($im);
- By calculating the actual picture width and the compression ratio of the image that needs to be generated, the scale of the image is scaled according to the width or height, and the current program scales the picture according to the width.
- if (($maxwidth && $pic _width > $maxwidth) | | ($maxheight && $pic _height > $maxheight)) {
- If the length or width of the actual picture is less than the length or width of the specified image, the picture is scaled either by length or by the width of the picture.
- 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;
- Calculates the length of the image that is generated by the final zoom.
- $newwidth = $pic _width * $ratio;
- $newheight = $pic _height * $ratio;
- There are two ways to change the size of a picture based on the length and width of the resulting image:
- The imagecopyresized () function works in all GD versions, but its algorithm for scaling images is coarser.
- Imagecopyresamples (), the pixel interpolation algorithm gets a smoother image edge, but the function is slower than imagecopyresized ().
- 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);
- }
- Eventually the processed picture is generated, and if you need to generate GIF or PNG, you need to change the imagejpeg () function to Imagegif () or imagepng ()
- $name = $name. $filetype;
- Imagejpeg ($newim, $name);
- Imagedestroy ($newim);
- } else {
- $name = $name. $filetype;
- Imagejpeg ($im, $name);
- }
- }
Copy CodeDescription: PHP GD library 1.6.2 version previously supported GIF format, but because the GIF format using the LZW algorithm involves patent rights, so after the gd1.6.2 version of the GIF format is not supported. If it is a Windows environment, just enter the php.ini file to find Extension=php_gd2.dll, will # removal, restart Apache, if you are a Linux environment, and want to support gif,png,jpeg, you need to download libpng, Zlib, as well as FreeType fonts and install them. |