- 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);
- }
- }
Copy CodeParameter description: $im Picture object, before applying the function, you need to read the picture object with Imagecreatefromjpeg (), if the PHP environment supports Png,gif, you can also use Imagecreatefromgif (), Imagecreatefrompng (); $maxwidth defines the maximum width (in pixels) of the resulting picture $maxheight the maximum height (in pixels) of the resulting picture $name the resulting picture name $filetype the resulting picture type (. jpg/. Png/.gif) Code Comment: Line 3rd to 4th: Read the picture that needs to be scaled the actual width and height of the 8th to 26th line: By calculating the actual picture width and the width of the image needs to be generated by the compression ratio of the resulting picture scaling is based on the width or height of the zoom, the current program is based on the width of the picture scaling. If you want to scale the picture according to the height, you can change the statement of Line 22nd to $widthratio> $heightratio 28th to 31st line: If the length or width of the actual picture is less than the length or width of the specified image, then the picture is scaled according to the length, Or zoom in on the width of the picture. Line 33rd to 34th: calculates the length and width of the image that is generated by the final zoom. Line 36th to 45th: Based on the calculated length of the resulting image to change the image size, there are two ways to change the image size: the imagecopyresized () function in all GD versions, but its scaled image algorithm is rough. Imagecopyresamples (), the pixel interpolation algorithm gets a smoother image edge, but the function is slower than imagecopyresized (). Line 47th to 49th: Eventually generate the processed picture, if you need to generate GIF or PNG, you need to change the imagejpeg () function to Imagegif () or imagepng () 51st to 56th Line: If the length of the actual picture is less than the length of the specified image, Keep the picture as it is, and if you need to generate GIF or PNG, you need to change the imagejpeg () function to Imagegif () or imagepng (). The 1.6.2 version of the GD library was previously supported in GIF format, but the GIF format was not supported after the GD1.6.2 version because of the proprietary rights involved with the LZW algorithm. If you are the Windows environment, you just go into 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, and FreeType fonts and install them. |