PHP image thumbnail generation code (supports png transparency)

WBOY
Release: 2016-07-25 08:52:19
Original
1382 people have browsed it
  1. /*

  2. * desc: Resize Image(png, jpg, gif)
  3. */
  4. class ResizeImage {
  5. //Image type
  6. private $type;
  7. // Actual width
  8. private $width;
  9. //Actual height
  10. private $height;
  11. //Changed width
  12. private $resize_width;
  13. //Changed height
  14. private $resize_height;
  15. //Whether to crop the image
  16. private $ cut;
  17. //Source image
  18. private $srcimg;
  19. //Destination image address
  20. private $dstimg;
  21. //Temporarily created image
  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. //Initialize image

  31. $this->initi_img();
  32. //Target image address
  33. $this -> dst_img($savePath);
  34. //--
  35. $this->width = imagesx($this->im);
  36. $this->height = imagesy($this->im);
  37. //Generate image
  38. $this ->newimg();
  39. ImageDestroy ($this->im);
  40. }
  41. private function newimg() {

  42. //The ratio of the changed image
  43. $resize_ratio = ($this->resize_width)/($this->resize_height);
  44. //The ratio of the actual image
  45. $ratio = ($this->width)/($this->height);
  46. if ($this->cut) {
  47. //Cut image
  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 crop the image
  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. //Initialize 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 target 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

When using it, just call the constructor of the class directly. The constructor: $resizeimage = new resizeimage($imgPath, $width, $height, $isCut, $savePath);

Parameters $imgPath: original image address $width: thumbnail width $height: Thumbnail height $isCut: Whether to crop, 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" , 320, 240, false, "img/test_320_240.jpg");
  4. //png

  5. $pngResize = new ResizeImage("img/test_1024_746.png", 320, 240, false, "img/test_320_240.png");
  6. ?>
Copy code

Effect: PHP image thumbnail generation code (supports png transparency) PHP image thumbnail generation code (supports png transparency)



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template