Discuz教程网

PHP生成缩略图类

[复制链接]
authicon dly 发表于 2011-3-28 19:10:20 | 显示全部楼层 |阅读模式
  1. <?php

  2. /**
  3. * 功能:生成缩略图
  4. * 作者:phpox
  5. * 日期:Thu May 17 09:57:05 CST 2007
  6. */

  7. class CreatMiniature
  8. {
  9. //公共变量
  10. var $srcFile=""; //原图
  11. var $echoType; //输出图片类型,link--不保存为文件;file--保存为文件
  12. var $im=""; //临时变量
  13. var $srcW=""; //原图宽
  14. var $srcH=""; //原图高

  15. //设置变量及初始化
  16. function SetVar($srcFile,$echoType)
  17. {
  18. if (!file_exists($srcFile)){
  19. echo '源图片文件不存在!';
  20. exit();
  21. }
  22. $this->srcFile=$srcFile;
  23. $this->echoType=$echoType;

  24. $info = "";
  25. $data = GetImageSize($this->srcFile,$info);
  26. switch ($data[2])
  27. {
  28. case 1:
  29. if(!function_exists("imagecreatefromgif")){
  30. echo "你的GD库不能使用GIF格式的图片,请使用Jpeg或PNG格式!<a href='javascript:go(-1);'>返回</a>";
  31. exit();
  32. }
  33. $this->im = ImageCreateFromGIF($this->srcFile);
  34. break;
  35. case 2:
  36. if(!function_exists("imagecreatefromjpeg")){
  37. echo "你的GD库不能使用jpeg格式的图片,请使用其它格式的图片!<a href='javascript:go(-1);'>返回</a>";
  38. exit();
  39. }
  40. $this->im = ImageCreateFromJpeg($this->srcFile);
  41. break;
  42. case 3:
  43. $this->im = ImageCreateFromPNG($this->srcFile);
  44. break;
  45. }
  46. $this->srcW=ImageSX($this->im);
  47. $this->srcH=ImageSY($this->im);
  48. }

  49. //生成扭曲型缩图
  50. function Distortion($toFile,$toW,$toH)
  51. {
  52. $cImg=$this->CreatImage($this->im,$toW,$toH,0,0,0,0,$this->srcW,$this->srcH);
  53. return $this->EchoImage($cImg,$toFile);
  54. ImageDestroy($cImg);
  55. }

  56. //生成按比例缩放的缩图
  57. function Prorate($toFile,$toW,$toH)
  58. {
  59. $toWH=$toW/$toH;
  60. $srcWH=$this->srcW/$this->srcH;
  61. if($toWH<=$srcWH)
  62. {
  63. $ftoW=$toW;
  64. $ftoH=$ftoW*($this->srcH/$this->srcW);
  65. }
  66. else
  67. {
  68. $ftoH=$toH;
  69. $ftoW=$ftoH*($this->srcW/$this->srcH);
  70. }
  71. if($this->srcW>$toW||$this->srcH>$toH)
  72. {
  73. $cImg=$this->CreatImage($this->im,$ftoW,$ftoH,0,0,0,0,$this->srcW,$this->srcH);
  74. return $this->EchoImage($cImg,$toFile);
  75. ImageDestroy($cImg);
  76. }
  77. else
  78. {
  79. $cImg=$this->CreatImage($this->im,$this->srcW,$this->srcH,0,0,0,0,$this->srcW,$this->srcH);
  80. return $this->EchoImage($cImg,$toFile);
  81. ImageDestroy($cImg);
  82. }
  83. }

  84. //生成最小裁剪后的缩图
  85. function Cut($toFile,$toW,$toH)
  86. {
  87. $toWH=$toW/$toH;
  88. $srcWH=$this->srcW/$this->srcH;
  89. if($toWH<=$srcWH)
  90. {
  91. $ctoH=$toH;
  92. $ctoW=$ctoH*($this->srcW/$this->srcH);
  93. }
  94. else
  95. {
  96. $ctoW=$toW;
  97. $ctoH=$ctoW*($this->srcH/$this->srcW);
  98. }
  99. $allImg=$this->CreatImage($this->im,$ctoW,$ctoH,0,0,0,0,$this->srcW,$this->srcH);
  100. $cImg=$this->CreatImage($allImg,$toW,$toH,0,0,($ctoW-$toW)/2,($ctoH-$toH)/2,$toW,$toH);
  101. return $this->EchoImage($cImg,$toFile);
  102. ImageDestroy($cImg);
  103. ImageDestroy($allImg);
  104. }

  105. //生成背景填充的缩图
  106. function BackFill($toFile,$toW,$toH,$bk1=255,$bk2=255,$bk3=255)
  107. {
  108. $toWH=$toW/$toH;
  109. $srcWH=$this->srcW/$this->srcH;
  110. if($toWH<=$srcWH)
  111. {
  112. $ftoW=$toW;
  113. $ftoH=$ftoW*($this->srcH/$this->srcW);
  114. }
  115. else
  116. {
  117. $ftoH=$toH;
  118. $ftoW=$ftoH*($this->srcW/$this->srcH);
  119. }
  120. if(function_exists("imagecreatetruecolor"))
  121. {
  122. @$cImg=ImageCreateTrueColor($toW,$toH);
  123. if(!$cImg)
  124. {
  125. $cImg=ImageCreate($toW,$toH);
  126. }
  127. }
  128. else
  129. {
  130. $cImg=ImageCreate($toW,$toH);
  131. }
  132. $backcolor = imagecolorallocate($cImg, $bk1, $bk2, $bk3); //填充的背景颜色
  133. ImageFilledRectangle($cImg,0,0,$toW,$toH,$backcolor);
  134. if($this->srcW>$toW||$this->srcH>$toH)
  135. {
  136. $proImg=$this->CreatImage($this->im,$ftoW,$ftoH,0,0,0,0,$this->srcW,$this->srcH);
  137. if($ftoW<$toW)
  138. {
  139. ImageCopy($cImg,$proImg,($toW-$ftoW)/2,0,0,0,$ftoW,$ftoH);
  140. }
  141. else if($ftoH<$toH)
  142. {
  143. ImageCopy($cImg,$proImg,0,($toH-$ftoH)/2,0,0,$ftoW,$ftoH);
  144. }
  145. else
  146. {
  147. ImageCopy($cImg,$proImg,0,0,0,0,$ftoW,$ftoH);
  148. }
  149. }
  150. else
  151. {
  152. ImageCopyMerge($cImg,$this->im,($toW-$ftoW)/2,($toH-$ftoH)/2,0,0,$ftoW,$ftoH,100);
  153. }
  154. return $this->EchoImage($cImg,$toFile);
  155. ImageDestroy($cImg);
  156. }


  157. function CreatImage($img,$creatW,$creatH,$dstX,$dstY,$srcX,$srcY,$srcImgW,$srcImgH)
  158. {
  159. if(function_exists("imagecreatetruecolor"))
  160. {
  161. @$creatImg = ImageCreateTrueColor($creatW,$creatH);
  162. if($creatImg)
  163. ImageCopyResampled($creatImg,$img,$dstX,$dstY,$srcX,$srcY,$creatW,$creatH,$srcImgW,$srcImgH);
  164. else
  165. {
  166. $creatImg=ImageCreate($creatW,$creatH);
  167. ImageCopyResized($creatImg,$img,$dstX,$dstY,$srcX,$srcY,$creatW,$creatH,$srcImgW,$srcImgH);
  168. }
  169. }
  170. else
  171. {
  172. $creatImg=ImageCreate($creatW,$creatH);
  173. ImageCopyResized($creatImg,$img,$dstX,$dstY,$srcX,$srcY,$creatW,$creatH,$srcImgW,$srcImgH);
  174. }
  175. return $creatImg;
  176. }

  177. //输出图片,link---只输出,不保存文件。file--保存为文件
  178. function EchoImage($img,$to_File)
  179. {
  180. switch($this->echoType)
  181. {
  182. case "link":
  183. if(function_exists('imagejpeg')) return ImageJpeg($img);
  184. else return ImagePNG($img);
  185. break;
  186. case "file":
  187. if(function_exists('imagejpeg')) return ImageJpeg($img,$to_File);
  188. else return ImagePNG($img,$to_File);
  189. break;
  190. }
  191. }
  192. }
  193. ?>

复制代码






上一篇:PHP识别Robot(Spider,机器人,搜索引擎)函数
下一篇:PHP生成静态页面的类
authicon cutiemimi 发表于 2011-5-9 05:59:42 | 显示全部楼层
好东西,要下来看看
authicon 82xiaochong911 发表于 2011-5-10 17:59:52 | 显示全部楼层
好东东下下来看看
authicon D_hong 发表于 2011-5-13 06:59:42 | 显示全部楼层
看一下啊,嘻嘻
authicon 咫尺天 发表于 2011-5-13 17:59:59 | 显示全部楼层
这个不错呀
authicon huanglv80 发表于 2011-5-24 15:59:48 | 显示全部楼层
继续来索要
authicon 丁加丁 发表于 2011-5-25 22:59:56 | 显示全部楼层
继续来索要
authicon 婷婷爱牛牛 发表于 2011-5-28 07:00:01 | 显示全部楼层
这个不错呀
authicon 21585151 发表于 2011-6-17 07:01:25 | 显示全部楼层
楼主威武
authicon kikiya11 发表于 2011-6-17 23:59:59 | 显示全部楼层
看帖必回
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-3 07:55

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表