The implementation code of the non-distortion of the scale image of PHP

Source: Internet
Author: User
  1. /**
  2. * PHP Picture Shear Scaling Function Parameter description:
  3. * $im Picture object, need to use IMAGECREATEFROMJPEG () to read the picture object, if PHP support PNG, GIF, can use Imagecreatefromgif (), imagecreatefrompng ();
  4. * $maxwidth define the maximum width (in pixels) of the resulting picture
  5. * $maxheight the maximum height (in pixels) of the generated picture
  6. * $name the generated picture name
  7. * $filetype The resulting picture type (. jpg/.png/.gif)
  8. */
  9. function Resizeimage ($im, $maxwidth, $maxheight, $name, $filetype) {
  10. Read the actual width of the picture that needs to be scaled
  11. $pic _width = Imagesx ($im);
  12. $pic _height = Imagesy ($im);
  13. 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.
  14. if (($maxwidth && $pic _width > $maxwidth) | | ($maxheight && $pic _height > $maxheight)) {
  15. 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.
  16. if ($maxwidth && $pic _width > $maxwidth) {
  17. $widthratio = $maxwidth/$pic _width;
  18. $resizewidth _tag = true;
  19. }
  20. if ($maxheight && $pic _height > $maxheight) {
  21. $heightratio = $maxheight/$pic _height;
  22. $resizeheight _tag = true;
  23. }
  24. if ($resizewidth _tag && $resizeheight _tag) {
  25. if ($widthratio < $heightratio)
  26. $ratio = $widthratio;
  27. Else
  28. $ratio = $heightratio;
  29. }
  30. if ($resizewidth _tag &&! $resizeheight _tag)
  31. $ratio = $widthratio;
  32. if ($resizeheight _tag &&! $resizewidth _tag)
  33. $ratio = $heightratio;
  34. Calculates the length of the image that is generated by the final zoom.
  35. $newwidth = $pic _width * $ratio;
  36. $newheight = $pic _height * $ratio;
  37. There are two ways to change the size of a picture based on the length and width of the resulting image:
  38. The imagecopyresized () function works in all GD versions, but its algorithm for scaling images is coarser.
  39. Imagecopyresamples (), the pixel interpolation algorithm gets a smoother image edge, but the function is slower than imagecopyresized ().
  40. if (function_exists ("imagecopyresampled")) {
  41. $newim = Imagecreatetruecolor ($newwidth, $newheight);
  42. Imagecopyresampled ($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic _width, $pic _height);
  43. } else {
  44. $newim = Imagecreate ($newwidth, $newheight);
  45. Imagecopyresized ($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $pic _width, $pic _height);
  46. }
  47. 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 ()
  48. $name = $name. $filetype;
  49. Imagejpeg ($newim, $name);
  50. Imagedestroy ($newim);
  51. } else {
  52. $name = $name. $filetype;
  53. Imagejpeg ($im, $name);
  54. }
  55. }
Copy Code

Description: 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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.