Home > Backend Development > PHP Tutorial > PHP generates image thumbnail code class

PHP generates image thumbnail code class

WBOY
Release: 2016-07-25 08:45:14
Original
885 people have browsed it

Generate multiple types of thumbnails

  1. /***************************************
  2. *Author: Dreaming Scorpio (beluckly)
  3. *Complete Time: 2006-12-18
  4. *Class name: CreatMiniature
  5. *Function: Generate multiple types of thumbnails
  6. *Basic parameters: $srcFile, $echoType
  7. *Parameters used in the method:
  8. $toFile, generated file
  9. $toW, the generated width
  10. $toH, the generated height
  11. $bk1, the background color parameter is 255 as the highest
  12. $bk2, the background color parameter
  13. $bk3, the 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->Cut("cut_bei.jpg",150,200);
  21. $cm->BackFill("fill_bei.jpg",150,200);
  22. *******************************************/
  23. class CreatMiniature
  24. {
  25. //Public variable
  26. var $srcFile=""; //Original image
  27. var $echoType; //Output image type , link--do not save as a file; file--save as a file
  28. var $im=""; //Temporary variable
  29. var $srcW=""; //Original image width
  30. var $srcH=""; // Original image height
  31. //Set variables and initialize
  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 images, 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 pictures in other formats!返回";
  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. //生成扭曲型缩图
  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. //生成按比例缩放的缩图
  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. //生成最小裁剪后的缩图
  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. //生成背景填充的缩图
  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); //填充的背景颜色
  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. //输出图片,link---只输出,不保存文件。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


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