PHP智能把图片生成缩略图类

一个PHP智能把图片生成缩略图类,可自动根据你图片的大小生成等比例的图片,PHP智能把图片生成缩略图类代码如下:

  1. <?php
  2. /*************************************** *作者:落梦天蝎(beluckly)
  3. *开源代码phpfensi.com
  4. *类名:CreatMiniature
  5. *功能:生成多种类型的缩略图
  6. *基本参数:$srcFile,$echoType
  7. *方法用到的参数:
  8. $toFile,生成的文件
  9. $toW,生成的宽
  10. $toH,生成的高
  11. $bk1,背景颜色参数 以255为最高
  12. $bk2,背景颜色参数
  13. $bk3,背景颜色参数
  14. *例子:
  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. //公共变量
  26. var $srcFile=""; //原图
  27. var $echoType; //输出图片类型,link--不保存为文件;file--保存为文件
  28. var $im=""; //临时变量
  29. var $srcW=""; //原图宽
  30. var $srcH=""; //原图高
  31. //设置变量及初始化
  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. case 1:
  40. if(!function_exists("imagecreatefromgif")){
  41. echo "你的GD库不能使用GIF格式的图片,请使用Jpeg或PNG格式!<a href='javascript:go(-1);'>返回</a>";
  42. exit();
  43. }
  44. $this->im = ImageCreateFromGIF($this->srcFile);
  45. break;
  46. case 2:
  47. if(!function_exists("imagecreatefromjpeg")){
  48. echo "你的GD库不能使用jpeg格式的图片,请使用其它格式的图片!<a href='javascript:go(-1);'>返回</a>";
  49. exit();
  50. }
  51. $this->im = ImageCreateFromJpeg($this->srcFile);
  52. break;
  53. case 3:
  54. $this->im = ImageCreateFromPNG($this->srcFile);
  55. break;
  56. }
  57. $this->srcW=ImageSX($this->im);
  58. $this->srcH=ImageSY($this->im);
  59. }
  60. //生成扭曲型缩图
  61. function Distortion($toFile,$toW,$toH)
  62. {
  63. $cImg=$this->CreatImage($this->im,$toW,$toH,0,0,0,0,$this->srcW,$this->srcH);
  64. return $this->EchoImage($cImg,$toFile);
  65. ImageDestroy($cImg);
  66. }
  67. //生成按比例缩放的缩图
  68. function Prorate($toFile,$toW,$toH)
  69. {
  70. $toWH=$toW/$toH;
  71. $srcWH=$this->srcW/$this->srcH;
  72. if($toWH< =$srcWH)
  73. {
  74. $ftoW=$toW;
  75. $ftoH=$ftoW*($this->srcH/$this->srcW);
  76. }
  77. else {
  78. $ftoH=$toH;
  79. $ftoW=$ftoH*($this->srcW/$this->srcH);
  80. }
  81. if($this->srcW>$toW||$this->srcH>$toH)
  82. {
  83. $cImg=$this->CreatImage($this->im,$ftoW,$ftoH,0,0,0,0,$this->srcW,$this->srcH);
  84. return $this->EchoImage($cImg,$toFile);
  85. ImageDestroy($cImg);
  86. }
  87. else
  88. {
  89. $cImg=$this->CreatImage($this->im,$this->srcW,$this->srcH,0,0,0,0,$this->srcW,$this->srcH);
  90. return $this->EchoImage($cImg,$toFile);
  91. ImageDestroy($cImg);
  92. }
  93. }
  94. //生成最小裁剪后的缩图
  95. function Cut($toFile,$toW,$toH)
  96. {
  97. $toWH=$toW/$toH;
  98. $srcWH=$this->srcW/$this->srcH;
  99. if($toWH< =$srcWH)
  100. {
  101. $ctoH=$toH;
  102. $ctoW=$ctoH*($this->srcW/$this->srcH);
  103. }
  104. else
  105. {
  106. $ctoW=$toW;
  107. $ctoH=$ctoW*($this->srcH/$this->srcW);
  108. }
  109. $allImg=$this->CreatImage($this->im,$ctoW,$ctoH,0,0,0,0,$this->srcW,$this->srcH);
  110. $cImg=$this->CreatImage($allImg,$toW,$toH,0,0,($ctoW-$toW)/2,($ctoH-$toH)/2,$toW,$toH);
  111. return $this->EchoImage($cImg,$toFile);
  112. ImageDestroy($cImg);
  113. ImageDestroy($allImg);
  114. }
  115. //生成背景填充的缩图
  116. function BackFill($toFile,$toW,$toH,$bk1=255,$bk2=255,$bk3=255)
  117. {
  118. $toWH=$toW/$toH;
  119. $srcWH=$this->srcW/$this->srcH;
  120. if($toWH< =$srcWH)
  121. {
  122. $ftoW=$toW;
  123. $ftoH=$ftoW*($this->srcH/$this->srcW);
  124. }
  125. else
  126. {
  127. $ftoH=$toH;
  128. $ftoW=$ftoH*($this->srcW/$this->srcH);
  129. }
  130. if(function_exists("imagecreatetruecolor"))
  131. {
  132. @$cImg=ImageCreateTrueColor($toW,$toH);
  133. if(!$cImg)
  134. {
  135. $cImg=ImageCreate($toW,$toH);
  136. }
  137. }
  138. else
  139. {
  140. $cImg=ImageCreate($toW,$toH);
  141. }
  142. $backcolor = imagecolorallocate($cImg, $bk1, $bk2, $bk3); //填充的背景颜色
  143. ImageFilledRectangle($cImg,0,0,$toW,$toH,$backcolor);
  144. if($this->srcW>$toW||$this->srcH>$toH)
  145. {
  146. $proImg=$this->CreatImage($this->im,$ftoW,$ftoH,0,0,0,0,$this->srcW,$this->srcH);
  147. /*
  148. if($ftoW< $toW)
  149. {
  150. ImageCopyMerge($cImg,$proImg,($toW-$ftoW)/2,0,0,0,$ftoW,$ftoH,100);
  151. }
  152. else if($ftoH<$toH)
  153. {
  154. ImageCopyMerge($cImg,$proImg,0,($toH-$ftoH)/2,0,0,$ftoW,$ftoH,100);
  155. }
  156. */
  157. if($ftoW<$toW)
  158. {
  159. ImageCopy($cImg,$proImg,($toW-$ftoW)/2,0,0,0,$ftoW,$ftoH);
  160. }
  161. else if($ftoH<$toH)
  162. {
  163. ImageCopy($cImg,$proImg,0,($toH-$ftoH)/2,0,0,$ftoW,$ftoH);
  164. }
  165. else
  166. {
  167. ImageCopy($cImg,$proImg,0,0,0,0,$ftoW,$ftoH);
  168. }
  169. }
  170. else
  171. {
  172. ImageCopyMerge($cImg,$this->im,($toW-$ftoW)/2,($toH-$ftoH)/2,0,0,$ftoW,$ftoH,100);
  173. }
  174. return $this->EchoImage($cImg,$toFile);
  175. ImageDestroy($cImg);
  176. }
  177. function CreatImage($img,$creatW,$creatH,$dstX,$dstY,$srcX,$srcY,$srcImgW,$srcImgH)
  178. {
  179. if(function_exists("imagecreatetruecolor"))
  180. {
  181. @$creatImg = ImageCreateTrueColor($creatW,$creatH); if($creatImg)
  182. ImageCopyResampled($creatImg,$img,$dstX,$dstY,$srcX,$srcY,$creatW,$creatH,$srcImgW,$srcImgH);
  183. else
  184. {
  185. $creatImg=ImageCreate($creatW,$creatH);
  186. ImageCopyResized($creatImg,$img,$dstX,$dstY,$srcX,$srcY,$creatW,$creatH,$srcImgW,$srcImgH);
  187. }
  188. }
  189. else
  190. {
  191. $creatImg=ImageCreate($creatW,$creatH);
  192. ImageCopyResized($creatImg,$img,$dstX,$dstY,$srcX,$srcY,$creatW,$creatH,$srcImgW,$srcImgH);
  193. }
  194. return $creatImg;
  195. }
  196. //输出图片,link---只输出,不保存文件。file--保存为文件
  197. function EchoImage($img,$to_File)
  198. {
  199. switch($this->echoType) {
  200. case "link":
  201. if(function_exists('imagejpeg')) return ImageJpeg($img);
  202. else return ImagePNG($img);
  203. break;
  204. case "file":
  205. if(function_exists('imagejpeg')) return ImageJpeg($img,$to_File);
  206. else return ImagePNG($img,$to_File);
  207. break;
  208. }
  209. }
  210. }
  211. ?>