Home  >  Article  >  Backend Development  >  Create image thumbnails with PHP

Create image thumbnails with PHP

WBOY
WBOYOriginal
2016-07-25 08:48:241019browse
  1. /**
  2. * Upload images to generate thumbnails
  3. *
  4. * Requires GD2 library support
  5. *
  6. * Parameters new thumbnails('original address of the image to be thumbnailed', 'width of thumbnail', 'height of thumbnail' are required during initialization ','(optional parameter) Thumbnail saving path');
  7. * If the last parameter is not specified, the thumbnail will be saved in the small folder in the directory of the original image by default,
  8. * If small does not exist folder, the small folder will be automatically created
  9. *
  10. * After initialization, you need to call the method produce to create thumbnails
  11. * $thumbnails = new thumbnails(''....);
  12. * $thumbnails->produce();
  13. *
  14. * You can get the relevant information of the original image, width, height, and image mime
  15. *
  16. * $thumbnails->getImageWidth(); //int image width
  17. * $thumbnails->getImageHeight(); / / int Image height
  18. * $thumbnails->getImageMime(); // string mime of the image
  19. *
  20. * $thumbnails->trueSize(); //array This is an array containing the width and sum of the image after scaling it down. Array of height values ​​
  21. * $size = array('width'=>'','height'=>'');
  22. * Get the width and height of the image after scaling it
  23. * $size['width ']//The width of the proportional thumbnail
  24. * $size['height']//The height of the proportional thumbnail
  25. *
  26. */
  27. class thumbnails{
  28. private $imgSrc; //Picture path
  29. private $saveSrc; //Picture saving path, default is empty
  30. private $ canvasWidth; //The width of the canvas
  31. private $canvasHeight; //The height of the canvas
  32. private $im; //Canvas resources
  33. private $dm; //Resources returned by copying the image
  34. /**
  35. * Initialize the class and load related settings
  36. *
  37. * @param $imgSrc The path of the image that needs to be thumbnailed
  38. * @param $canvasWidth The width of the thumbnail
  39. * @param $canvasHeight The height of the thumbnail
  40. */
  41. public function __construct($imgSrc,$canvasWidth,$canvasHeight,$saveSrc=null)
  42. {
  43. $this->imgSrc = $imgSrc;
  44. $this->canvasWidth = $canvasWidth;
  45. $this->canvasHeight = $ canvasHeight;
  46. $this->saveSrc = $saveSrc;
  47. }
  48. /**
  49. * Generate thumbnails
  50. */
  51. public function produce()
  52. {
  53. $this->createCanvas();
  54. $this->judgeImage ();
  55. $this->copyImage();
  56. $this->headerImage();
  57. }
  58. /**
  59. * Get the information of the loaded image
  60. *
  61. * Contains the length, width, and image type
  62. *
  63. * @return array An array containing the image length, width, and mime
  64. */
  65. private function getImageInfo()
  66. {
  67. return getimagesize($this- >imgSrc);
  68. }
  69. /**
  70. * Get the length of the image
  71. *
  72. * @return int The width of the image
  73. */
  74. public function getImageWidth()
  75. {
  76. $imageInfo = $this->getImageInfo();
  77. return $imageInfo['0'];
  78. }
  79. /**
  80. * Get the height of the image
  81. *
  82. * @return int The height of the image
  83. */
  84. public function getImageHeight()
  85. {
  86. $imageInfo = $this->getImageInfo();
  87. return $imageInfo['1'];
  88. }
  89. /**
  90. * Get the type of image
  91. *
  92. * @return the mime value of the image
  93. */
  94. public function getImageMime()
  95. {
  96. $imageInfo = $this->getImageInfo();
  97. return $imageInfo['mime'];
  98. }
  99. /**
  100. * Create canvas
  101. *
  102. * At the same time, put the created canvas resource into the attribute $this->im
  103. */
  104. private function createCanvas ()
  105. {
  106. $size = $this->trueSize();
  107. $this->im = imagecreatetruecolor($size['width'],$size['height']);
  108. }
  109. /* *
  110. * Determine the mime value of the image and determine the function to use
  111. *
  112. * At the same time, put the created image resource into $this->dm
  113. */
  114. private function judgeImage()
  115. {
  116. $mime = $this->getImageMime();
  117. switch ($mime)
  118. {
  119. case 'image/png':$dm = imagecreatefrompng($this ->imgSrc);
  120. break;
  121. case 'image/gif':$dm = imagecreatefromgif($this->imgSrc);
  122. break;
  123. case 'image/jpg':$dm = imagecreatefromjpeg($this ->imgSrc);
  124. break;
  125. case 'image/jpeg':$dm = imagecreatefromgjpeg($this->imgSrc);
  126. break;
  127. }
  128. $this->dm = $dm;
  129. }
  130. /**
  131. * Determine the width and height of the image after being reduced
  132. *
  133. * This width and height are also used as the size of the canvas
  134. *
  135. * @return array The size of the image after being reduced in equal proportions
  136. */
  137. public function trueSize()
  138. {
  139. $proportionW = $this->getImageWidth() / $this->canvasWidth;
  140. $proportionH = $this->getImageHeight() / $this->canvasHeight;
  141. if( ($this->getImageWidth() < $this->canvasWidth) && ($this->getImageHeight() < $this->canvasHeight) )
  142. {
  143. $trueSize = array('width'=>$this->getImageWidth(),'height'=>$this->getImageHeight());
  144. }
  145. elseif($proportionW >= $proportionH)
  146. {
  147. $trueSize = array('width'=>$this->canvasWidth,'height'=>$this->getImageHeight() / $proportionW);
  148. }
  149. else
  150. {
  151. $trueSize = array('width'=>$this->getImageWidth() / $proportionH,'height'=>$this->canvasHeight);
  152. }
  153. return $trueSize;
  154. }
  155. /**
  156. * Copy the image to a new canvas
  157. *
  158. * The image will be scaled proportionally and will not be deformed
  159. */
  160. private function copyImage()
  161. {
  162. $size = $this->trueSize();
  163. imagecopyresized($this->im, $this->dm , 0 , 0 , 0 , 0 , $size['width'] , $size['height'] , $this->getImageWidth() , $this->getImageheight());
  164. }
  165. /**
  166. * Export the image
  167. *
  168. * The name of the image is the same as the original image name by default
  169. *
  170. * The path is the small directory under the current directory of the large image
  171. *
  172. * If the small directory does not exist, it will be automatically created
  173. */
  174. public function headerImage()
  175. {
  176. $position = strrpos($this->imgSrc,'/');
  177. $imageName = substr($this->imgSrc,($position + 1));
  178. if($this->saveSrc)
  179. {
  180. $imageFlode = $this->saveSrc.'/';
  181. }
  182. else
  183. {
  184. $imageFlode = substr($this->imgSrc,0,$position).'/small/';
  185. }
  186. if(!file_exists($imageFlode))
  187. {
  188. mkdir($imageFlode);
  189. }
  190. $saveSrc = $imageFlode.$imageName;
  191. imagejpeg($this->im,$saveSrc);
  192. }
  193. }
复制代码


Statement:
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