PHP Create thumbnail image

Source: Internet
Author: User
<无详细内容>
  1. /**
  2. * Upload images to generate thumbnails
  3. *
  4. * Requires support of GD2 Library
  5. *
  6. * Initialization requires parameter new thumbnails (' original address of thumbnail image ', ' width of thumbnail ', ' height of thumbnail ', ' (optional parameter) thumbnail save path ');
  7. * If the last parameter is not specified, then the thumbnail is saved in the small folder in the same directory as the original image.
  8. * If the small folder does not exist, the small folder will be created automatically
  9. *
  10. * Call method produce to create thumbnails after initialization
  11. * $thumbnails = new thumbnails ("...);
  12. * $thumbnails->produce ();
  13. *
  14. * You can get information about the original picture, width, height, and picture mime
  15. *
  16. * $thumbnails->getimagewidth (); int picture width
  17. * $thumbnails->getimageheight (); int picture Height
  18. * $thumbnails->getimagemime (); MIME of a string picture
  19. *
  20. * $thumbnails->truesize (); Array this is a value that contains the width and height values after the thumbnail of the picture, etc.
  21. * $size = Array (' width ' = ', ' height ' = ');
  22. * Get the width and height of the image after the thumbnail
  23. * $size the width of [' width ']//, etc. than the thumbnail image
  24. * $size the height of the [' Height ']//, etc. than the thumbnail image
  25. *
  26. */
  27. Class thumbnails{
  28. Private $IMGSRC; The path to the picture
  29. Private $SAVESRC; Save path of picture, default is empty
  30. Private $canvasWidth; Width of canvas
  31. Private $canvasHeight; The height of the canvas
  32. Private $im; Canvas Resources
  33. Private $DM; Copy the resources returned by the picture
  34. /**
  35. * Initialize class, load related settings
  36. *
  37. * @param $imgSrc The path of the picture that needs to be abbreviated
  38. * @param $canvasWidth the width of the thumbnail image
  39. * @param $canvasHeight The height of the thumbnail image
  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 thumbnail image
  50. */
  51. Public function Produce ()
  52. {
  53. $this->createcanvas ();
  54. $this->judgeimage ();
  55. $this->copyimage ();
  56. $this->headerimage ();
  57. }
  58. /**
  59. * Get information on loading images
  60. *
  61. * Includes length, width, picture type
  62. *
  63. * @return array containing the image length, width, mime
  64. */
  65. Private Function Getimageinfo ()
  66. {
  67. Return getimagesize ($this->imgsrc);
  68. }
  69. /**
  70. * Get the length of the picture
  71. *
  72. * @return the width of the int picture
  73. */
  74. Public Function Getimagewidth ()
  75. {
  76. $imageInfo = $this->getimageinfo ();
  77. return $imageInfo [' 0 '];
  78. }
  79. /**
  80. * Get Picture height
  81. *
  82. * @return the height of the int image
  83. */
  84. Public Function Getimageheight ()
  85. {
  86. $imageInfo = $this->getimageinfo ();
  87. return $imageInfo [' 1 '];
  88. }
  89. /**
  90. * Get the type of picture
  91. *
  92. * MIME value of @return picture
  93. */
  94. Public Function Getimagemime ()
  95. {
  96. $imageInfo = $this->getimageinfo ();
  97. return $imageInfo [' MIME '];
  98. }
  99. /**
  100. * Create Canvas
  101. *
  102. * Also put the created canvas resource into the property $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 decide which function to use
  111. *
  112. * Also put the created picture resources into the $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 the thumbnail
  132. *
  133. * This width and height also as the size of the canvas
  134. *
  135. * @return the size of an array image after a proportional thumbnail
  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 picture to the new canvas
  157. *
  158. * Images will be scaled in equal proportions and will not deform
  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. * Output the picture
  167. *
  168. * The name of the picture is the same as the original picture name
  169. *
  170. * Path is large picture in small directory under current directory
  171. *
  172. * If the small directory does not exist, it will be created automatically
  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. }
Copy Code
  • 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.