PHP Image thumbnail Generation code (PNG transparent support)

Source: Internet
Author: User
  1. /*

  2. * Desc:resize Image (PNG, JPG, GIF)
  3. */
  4. Class Resizeimage {
  5. Type of picture
  6. Private $type;
  7. Actual width
  8. Private $width;
  9. Actual height
  10. Private $height;
  11. The width after the change
  12. Private $resize _width;
  13. The height after the change
  14. Private $resize _height;
  15. Whether or not to cut the map
  16. Private $cut;
  17. Source image
  18. Private $srcimg;
  19. Destination image Address
  20. Private $dstimg;
  21. Temporarily created images
  22. Private $im;

  23. function __construct ($imgPath, $width, $height, $isCut, $savePath) {

  24. $this->srcimg = $imgPath;
  25. $this->resize_width = $width;
  26. $this->resize_height = $height;
  27. $this->cut = $isCut;
  28. Type of picture

  29. $this->type = Strtolower (substr (STRRCHR ($this->srcimg, "."), 1);

  30. Initializing an image

  31. $this->initi_img ();
  32. Destination image Address
  33. $this-Dst_img ($savePath);
  34. //--
  35. $this->width = imagesx ($this->im);
  36. $this->height = Imagesy ($this->im);
  37. Creating images
  38. $this->newimg ();
  39. Imagedestroy ($this->im);
  40. }

  41. Private Function newimg () {

  42. The proportions of the altered image
  43. $resize _ratio = ($this->resize_width)/($this->resize_height);
  44. Ratio of actual images
  45. $ratio = ($this->width)/($this->height);
  46. if ($this->cut) {
  47. Crop chart
  48. $newimg = Imagecreatetruecolor ($this->resize_width, $this->resize_height);
  49. if ($this->type== "png") {
  50. Imagefill ($newimg, 0, 0, Imagecolorallocatealpha ($newimg, 0, 0, 0, 127));
  51. }
  52. if ($ratio >= $resize _ratio) {
  53. High priority
  54. Imagecopyresampled ($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, (($this Height) * $resize _ratio), $this->height);
  55. } else {
  56. Width first
  57. Imagecopyresampled ($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width , (($this->width)/$resize _ratio));
  58. }
  59. } else {
  60. Do not cut the map
  61. if ($ratio >= $resize _ratio) {
  62. $newimg = Imagecreatetruecolor ($this->resize_width, ($this->resize_width)/$ratio);
  63. if ($this->type== "png") {
  64. Imagefill ($newimg, 0, 0, Imagecolorallocatealpha ($newimg, 0, 0, 0, 127));
  65. }
  66. Imagecopyresampled ($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, ($this->resize_width)/$ratio, $this- >width, $this->height);
  67. } else {
  68. $newimg = Imagecreatetruecolor (($this->resize_height) * $ratio, $this->resize_height);
  69. if ($this->type== "png") {
  70. Imagefill ($newimg, 0, 0, Imagecolorallocatealpha ($newimg, 0, 0, 0, 127));
  71. }
  72. Imagecopyresampled ($newimg, $this->im, 0, 0, 0, 0, ($this->resize_height) * $ratio, $this->resize_height, $ This->width, $this->height);
  73. }
  74. }
  75. if ($this->type== "png") {
  76. Imagesavealpha ($newimg, true);
  77. Imagepng ($newimg, $this->dstimg);
  78. } else {
  79. Imagejpeg ($newimg, $this->dstimg);
  80. }
  81. }

  82. Initializing an image

  83. Private Function initi_img () {
  84. if ($this->type== "jpg") {
  85. $this->im = imagecreatefromjpeg ($this->srcimg);
  86. }
  87. if ($this->type== "gif") {
  88. $this->im = imagecreatefromgif ($this->srcimg);
  89. }
  90. if ($this->type== "png") {
  91. $this->im = imagecreatefrompng ($this->srcimg);
  92. }
  93. }

  94. Image Destination Address

  95. Private Function Dst_img ($dstpath) {
  96. $full _length = strlen ($this->srcimg);

  97. $type _length = strlen ($this->type);

  98. $name _length = $full _length-$type _length;

  99. $name = substr ($this->srcimg,0, $name _length-1);
  100. $this->dstimg = $dstpath;
  101. }
  102. }
  103. ?>

Copy Code

The constructor of the class is called directly using the constructor: $resizeimage = new Resizeimage ($imgPath, $width, $height, $isCut, $savePath);

Parameter $imgpath: Original image address $width: Thumbnail width $height: Thumbnail high $iscut: Whether clipping, bool value $savepath: Thumbnail address (can be the same as the original image address)

Example:

    1. Include "resizeimage.php";

    2. Jpg

    3. $jpgResize = new Resizeimage ("Img/test_1920_1200.jpg", "img/test_320_240.jpg");

    4. Png

    5. $pngResize = new Resizeimage ("Img/test_1024_746.png", "img/test_320_240.png");
    6. ?>

Copy Code

Effect:

  • 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.