PHP implements a method for zooming a PNG image (supports transparent backgrounds)

Source: Internet
Author: User
Tags transparent color

The example in this paper describes how PHP implements scaling of PNG images. Share to everyone for your reference. The implementation method is as follows:

  1. function Smart_resize_image ($file, $width = 0, $height = 0, $proportional = false, $output = ' file ', $delete _original = t Rue, $use _linux_commands = False)
  2. {
  3. if ($height <= 0 && $width <= 0) {
  4. return false;
  5. }
  6. $info = getimagesize ($file);
  7. $image = ";
  8. $final _width = 0;
  9. $final _height = 0;
  10. List ($width _old, $height _old) = $info;
  11. if ($proportional) {
  12. if ($width = = 0) $factor = $height/$height _old;
  13. ElseIf ($height = = 0) $factor = $width/$width _old;
  14. else $factor = min ($width/$width _old, $height/$height _old);
  15. $final _width = Round ($width _old * $factor);
  16. $final _height = Round ($height _old * $factor);
  17. }
  18. else {
  19. $final _width = ($width <= 0)? $width _old: $width;
  20. $final _height = ($height <= 0)? $height _old: $height;
  21. }
  22. Switch ($info [2]) {
  23. Case Imagetype_gif:
  24. $image = Imagecreatefromgif ($file);
  25. Break
  26. Case IMAGETYPE_JPEG:
  27. $image = Imagecreatefromjpeg ($file);
  28. Break
  29. Case Imagetype_png:
  30. $image = Imagecreatefrompng ($file);
  31. Break
  32. Default
  33. return false;
  34. }
  35. $image _resized = Imagecreatetruecolor ($final _width, $final _height);
  36. if (($info [2] = = Imagetype_gif) | | ($info [2] = = Imagetype_png)) {
  37. $trnprt _indx = imagecolortransparent ($image);
  38. If we have a specific transparent color
  39. if ($trnprt _indx >= 0) {
  40. Get the original image ' s transparent color ' s RGB values
  41. $trnprt _color = Imagecolorsforindex ($image, $trnprt _indx);
  42. Allocate the same color in the new image resource
  43. $trnprt _indx = imagecolorallocate ($image _resized, $trnprt _color[' Red '], $trnprt _color[' green ', $trnprt _color[' Blue ']);
  44. Completely fill the background of the new image with allocated color.
  45. Imagefill ($image _resized, 0, 0, $trnprt _indx);
  46. Set the background color for new image to Transparent
  47. Imagecolortransparent ($image _resized, $trnprt _indx);
  48. }
  49. Always make a transparent background color for PNGs that don ' t has one allocated already
  50. ElseIf ($info [2] = = Imagetype_png) {
  51. Turn off transparency Blending (temporarily)
  52. Imagealphablending ($image _resized, false);
  53. Create a new transparent color for image
  54. $color = Imagecolorallocatealpha ($image _resized, 0, 0, 0, 127);
  55. Completely fill the background of the new image with allocated color.
  56. Imagefill ($image _resized, 0, 0, $color);
  57. Restore Transparency Blending
  58. Imagesavealpha ($image _resized, true);
  59. }
  60. }
  61. Imagecopyresampled ($image _resized, $image, 0, 0, 0, 0, $final _width, $final _height, $width _old, $height _old);
  62. if ($delete _original) {
  63. if ($use _linux_commands)
  64. EXEC (' rm '. $file);
  65. Else
  66. @unlink ($file);
  67. }
  68. Switch (Strtolower ($output)) {
  69. Case ' Browser ':
  70. $mime = Image_type_to_mime_type ($info [2]);
  71. Header ("Content-type: $mime");
  72. $output = NULL;
  73. Break
  74. Case ' file ':
  75. $output = $file;
  76. Break
  77. Case ' return ':
  78. return $image _resized;
  79. Break
  80. Default
  81. Break
  82. }
  83. Switch ($info [2]) {
  84. Case Imagetype_gif:
  85. Imagegif ($image _resized, $output);
  86. Break
  87. Case IMAGETYPE_JPEG:
  88. Imagejpeg ($image _resized, $output);
  89. Break
  90. Case Imagetype_png:
  91. Imagepng ($image _resized, $output);
  92. Break
  93. Default
  94. return false;
  95. }
  96. return true;
  97. }
Copy Code

I hope this article is helpful to everyone's PHP programming.

PHP, PNG
  • Related Article

    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.