Distortion("dis_bei.jpg",150,200); $cm->Prorate("pro_bei.jpg",150,200);">
当前位置:Gxlcms > PHP教程 > php生成图片缩略图代码类

php生成图片缩略图代码类

时间:2021-07-01 10:21:17 帮助过:35人阅读

生成多种类型的缩略图

  1. /***************************************
  2. *作者:落梦天蝎(beluckly)
  3. *完成时间:2006-12-18
  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. {
  40. case 1:
  41. if(!function_exists("imagecreatefromgif")){
  42. echo "你的GD库不能使用GIF格式的图片,请使用Jpeg或PNG格式!返回";
  43. exit();
  44. }
  45. $this->im = ImageCreateFromGIF($this->srcFile);
  46. break;
  47. case 2:
  48. if(!function_exists("imagecreatefromjpeg")){
  49. echo "你的GD库不能使用jpeg格式的图片,请使用其它格式的图片!返回";
  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

人气教程排行