PHP generated image thumbnail code class

Source: Internet
Author: User

Generate multiple types of thumbnails

  1. /***************************************
  2. * Author: Fall Dream Scorpio (beluckly)
  3. * Completion time: 2006-12-18
  4. * Class Name: Creatminiature
  5. * Function: Generate multiple types of thumbnails
  6. * Basic parameters: $srcFile, $echoType
  7. * The parameters used in the method:
  8. $toFile, the resulting file
  9. $toW, the resulting width
  10. $toH, the resulting high
  11. $BK 1, the background color parameter is the highest of 255
  12. $BK 2, background color parameters
  13. $BK 3, background color parameters
  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->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 = ""; Original
  27. var $echoType; Output picture type, link--not saved as file; file--saved as File
  28. var $im = ""; Temp variable
  29. var $srcW = ""; Original width
  30. var $srcH = ""; Original height
  31. Setting 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 GIF format pictures, 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 can not use JPEG format pictures, please use other format pictures!" 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. To create a distorted shape thumbnail
  62. function Distortion ($toFile, $toW, $toH)
  63. {
  64. $CIMG = $this->creatimage ($this->im, $toW, $toH, 0,0,0,0, $this->SRCW, $this->srch);
  65. return $this->echoimage ($CIMG, $toFile);
  66. Imagedestroy ($CIMG);
  67. }
  68. To generate a proportionally scaled thumbnail
  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,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,0, $this->SRCW, $this->srch);
  92. return $this->echoimage ($CIMG, $toFile);
  93. Imagedestroy ($CIMG);
  94. }
  95. }
  96. Generate the smallest cropped thumbnail
  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,0, $this->SRCW, $this->srch);
  112. $CIMG = $this->creatimage ($allImg, $toW, $toH, 0,0, ($ctoW-$toW)/2, ($ctoH-$toH)/2, $toW, $toH);
  113. return $this->echoimage ($CIMG, $toFile);
  114. Imagedestroy ($CIMG);
  115. Imagedestroy ($ALLIMG);
  116. }
  117. Create a thumbnail of the background fill
  118. function Backfill ($toFile, $toW, $toH, $BK 1=255, $BK 2=255, $BK 3=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, $BK 1, $BK 2, $BK 3); The background color of the fill
  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,0, $this->SRCW, $this->srch);
  149. /*
  150. if ($ftoW < $toW)
  151. {
  152. Imagecopymerge ($CIMG, $PROIMG, ($toW-$ftoW)/2,0,0,0, $ftoW, $ftoH, 100);
  153. }
  154. else if ($ftoH < $toH)
  155. {
  156. Imagecopymerge ($CIMG, $PROIMG, 0, ($toH-$ftoH)/2,0,0, $ftoW, $ftoH, 100);
  157. }
  158. */
  159. if ($ftoW < $toW)
  160. {
  161. Imagecopy ($CIMG, $PROIMG, ($toW-$ftoW)/2,0,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,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 picture, link---only output, do not save the file. file--Save As 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. ?>
Copy Code
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.