Php code for generating image thumbnails

Source: Internet
Author: User
Php code for generating image thumbnails

Multiple types of thumbnails are generated.

  1. /***************************************
  2. * Author: beluckly)
  3. * Completion Time: 2006-12-18
  4. * Class name: CreatMiniature
  5. * Function: generate various types of thumbnails.
  6. * Basic parameters: $ srcFile, $ echoType
  7. * Parameters used by the method:
  8. $ ToFile: the generated file.
  9. $ ToW: the generated width.
  10. $ ToH, the generated height
  11. $ Bk1. The highest background color parameter is 255.
  12. $ Bk2, background color parameter
  13. $ Bk3, background color parameter
  14. * Example:
  15. Include ("thumb. php ");
  16. $ Cm = new CreatMiniature ();
  17. $ Cm-> SetVar ("1.jpg"," file ");
  18. $ Cm-> Distortion ("dis_bei.jpg", 150,200 );
  19. $ Cm-> Prorate ("pro_bei.jpg", 150,200 );
  20. $ Cm-& gt; Cut ("cut_bei.jpg", 150,200 );
  21. $ Cm-> BackFill ("fill_bei.jpg", 150,200 );
  22. ***************************************/
  23. Class CreatMiniature
  24. {
  25. // Public variables
  26. Var $ srcFile = ""; // source image
  27. Var $ echoType; // The type of the output image, link -- not saved as a file; file -- saved as a file
  28. Var $ im = ""; // temporary variable
  29. Var $ srcW = ""; // source image width
  30. Var $ srcH = ""; // original height
  31. // Set variables and initialization
  32. Function SetVar ($ srcFile, $ echoType)
  33. {
  34. $ This-> srcFile = $ srcFile;
  35. $ This-> echoType = $ echoType;
  36. $ Info = "";
  37. $ Data = GetImageSize ($ this-> srcFile, $ info );
  38. Switch ($ data [2])
  39. {
  40. Case 1:
  41. If (! Function_exists ("imagecreatefromgif ")){
  42. Echo "your GD library cannot use images in GIF format. please use Jpeg or PNG format! Return ";
  43. Exit ();
  44. }
  45. $ This-> im = ImageCreateFromGIF ($ this-> srcFile );
  46. Break;
  47. Case 2:
  48. If (! Function_exists ("imagecreatefromjpeg ")){
  49. Echo "your GD library cannot use images in jpeg format. please use images in other formats! Return ";
  50. Exit ();
  51. }
  52. $ This-> im = ImageCreateFromJpeg ($ this-> srcFile );
  53. Break;
  54. Case 3:
  55. $ This-> im = ImageCreateFromPNG ($ this-> srcFile );
  56. Break;
  57. }
  58. $ This-> srcW = ImageSX ($ this-> im );
  59. $ This-> srcH = ImageSY ($ this-> im );
  60. }
  61. // Generate a distorted thumbnail
  62. Function Distortion ($ toFile, $ toW, $ toH)
  63. {
  64. $ CImg = $ this-> CreatImage ($ this-> im, $ toW, $ toH, 0, 0, 0, $ this-> srcW, $ this-> srcH );
  65. Return $ this-> EchoImage ($ cImg, $ toFile );
  66. ImageDestroy ($ cImg );
  67. }
  68. // Generate a scaled chart
  69. Function Prorate ($ toFile, $ toW, $ toH)
  70. {
  71. $ ToWH = $ toW/$ toH;
  72. $ SrcWH = $ this-> srcW/$ this-> srcH;
  73. If ($ toWH <= $ srcWH)
  74. {
  75. $ FtoW = $ toW;
  76. $ FtoH = $ ftoW * ($ this-> srcH/$ this-> srcW );
  77. }
  78. Else
  79. {
  80. $ FtoH = $ toH;
  81. $ FtoW = $ ftoH * ($ this-> srcW/$ this-> srcH );
  82. }
  83. If ($ this-> srcW> $ toW | $ this-> srcH> $ toH)
  84. {
  85. $ CImg = $ this-> CreatImage ($ this-> im, $ ftoW, $ ftoH, 0, 0, 0, $ this-> srcW, $ this-> srcH );
  86. Return $ this-> EchoImage ($ cImg, $ toFile );
  87. ImageDestroy ($ cImg );
  88. }
  89. Else
  90. {
  91. $ CImg = $ this-> CreatImage ($ this-> im, $ this-> srcW, $ this-> srcH, 0, 0, 0, $ this-> srcW, $ this-> srcH );
  92. Return $ this-> EchoImage ($ cImg, $ toFile );
  93. ImageDestroy ($ cImg );
  94. }
  95. }
  96. // Generate a thumbnail after minimum cropping
  97. Function Cut ($ toFile, $ toW, $ toH)
  98. {
  99. $ ToWH = $ toW/$ toH;
  100. $ SrcWH = $ this-> srcW/$ this-> srcH;
  101. If ($ toWH <= $ srcWH)
  102. {
  103. $ CtoH = $ toH;
  104. $ CtoW = $ ctoH * ($ this-> srcW/$ this-> srcH );
  105. }
  106. Else
  107. {
  108. $ CtoW = $ toW;
  109. $ CtoH = $ ctoW * ($ this-> srcH/$ this-> srcW );
  110. }
  111. $ AllImg = $ this-> CreatImage ($ this-> im, $ ctoW, $ ctoH, 0, 0, 0, $ this-> srcW, $ this-> srcH );
  112. $ CImg = $ this-> CreatImage ($ allImg, $ toW, $ toH, 0, ($ ctoW-$ toW)/2, ($ ctoH-$ toH)/2, $ toW, $ toH );
  113. Return $ this-> EchoImage ($ cImg, $ toFile );
  114. ImageDestroy ($ cImg );
  115. ImageDestroy ($ allImg );
  116. }
  117. // Generate a thumbnail for background filling
  118. Function BackFill ($ toFile, $ toW, $ toH, $ bk1 = 255, $ bk2 = 255, $ bk3 = 255)
  119. {
  120. $ ToWH = $ toW/$ toH;
  121. $ SrcWH = $ this-> srcW/$ this-> srcH;
  122. If ($ toWH <= $ srcWH)
  123. {
  124. $ FtoW = $ toW;
  125. $ FtoH = $ ftoW * ($ this-> srcH/$ this-> srcW );
  126. }
  127. Else
  128. {
  129. $ FtoH = $ toH;
  130. $ FtoW = $ ftoH * ($ this-> srcW/$ this-> srcH );
  131. }
  132. If (function_exists ("imagecreatetruecolor "))
  133. {
  134. @ $ CImg = ImageCreateTrueColor ($ toW, $ toH );
  135. If (! $ CImg)
  136. {
  137. $ CImg = ImageCreate ($ toW, $ toH );
  138. }
  139. }
  140. Else
  141. {
  142. $ CImg = ImageCreate ($ toW, $ toH );
  143. }
  144. $ Backcolor = imagecolorallocate ($ cImg, $ bk1, $ bk2, $ bk3); // fill in the background color
  145. ImageFilledRectangle ($ cImg, 0, 0, $ toW, $ toH, $ backcolor );
  146. If ($ this-> srcW> $ toW | $ this-> srcH> $ toH)
  147. {
  148. $ ProImg = $ this-> CreatImage ($ this-> im, $ ftoW, $ ftoH, 0, 0, 0, $ this-> srcW, $ this-> srcH );
  149. /*
  150. If ($ ftoW <$ toW)
  151. {
  152. ImageCopyMerge ($ cImg, $ proImg, ($ toW-$ ftoW)/100, $ ftoW, $ ftoH );
  153. }
  154. Else if ($ ftoH <$ toH)
  155. {
  156. ImageCopyMerge ($ cImg, $ proImg, 0, ($ toH-$ ftoH)/100, 0, $ ftoW, $ ftoH );
  157. }
  158. */
  159. If ($ ftoW <$ toW)
  160. {
  161. ImageCopy ($ cImg, $ proImg, ($ toW-$ ftoW)/2, 0, 0, $ ftoW, $ ftoH );
  162. }
  163. Else if ($ ftoH <$ toH)
  164. {
  165. ImageCopy ($ cImg, $ proImg, 0, ($ toH-$ ftoH)/2,0, 0, $ ftoW, $ ftoH );
  166. }
  167. Else
  168. {
  169. ImageCopy ($ cImg, $ proImg, 0, 0, 0, $ ftoW, $ ftoH );
  170. }
  171. }
  172. Else
  173. {
  174. ImageCopyMerge ($ cImg, $ this-> im, ($ toW-$ ftoW)/2, ($ toH-$ ftoH)/2,0, 0, $ ftoW, $ ftoH, 100 );
  175. }
  176. Return $ this-> EchoImage ($ cImg, $ toFile );
  177. ImageDestroy ($ cImg );
  178. }
  179. Function CreatImage ($ img, $ creatW, $ creatH, $ dstX, $ dstY, $ srcX, $ srcY, $ srcImgW, $ srcImgH)
  180. {
  181. If (function_exists ("imagecreatetruecolor "))
  182. {
  183. @ $ CreatImg = ImageCreateTrueColor ($ creatW, $ creatH );
  184. If ($ creatImg)
  185. ImageCopyResampled ($ creatImg, $ img, $ dstX, $ dstY, $ srcX, $ srcY, $ creatW, $ creatH, $ srcImgW, $ srcImgH );
  186. Else
  187. {
  188. $ CreatImg = ImageCreate ($ creatW, $ creatH );
  189. ImageCopyResized ($ creatImg, $ img, $ dstX, $ dstY, $ srcX, $ srcY, $ creatW, $ creatH, $ srcImgW, $ srcImgH );
  190. }
  191. }
  192. Else
  193. {
  194. $ CreatImg = ImageCreate ($ creatW, $ creatH );
  195. ImageCopyResized ($ creatImg, $ img, $ dstX, $ dstY, $ srcX, $ srcY, $ creatW, $ creatH, $ srcImgW, $ srcImgH );
  196. }
  197. Return $ creatImg;
  198. }
  199. // Output image, link --- only output, not save file. File -- save as a file
  200. Function EchoImage ($ img, $ to_File)
  201. {
  202. Switch ($ this-> echoType)
  203. {
  204. Case "link ":
  205. If (function_exists ('imagejpeg ') return imagejpeg ($ img );
  206. Else return ImagePNG ($ img );
  207. Break;
  208. Case "file ":
  209. If (function_exists ('imagejpeg ') return imagejpeg ($ img, $ to_File );
  210. Else return ImagePNG ($ img, $ to_File );
  211. Break;
  212. }
  213. }
  214. }
  215. ?>


Php

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.