Heim > Backend-Entwicklung > PHP-Tutorial > php生成图片缩略图代码类

php生成图片缩略图代码类

WBOY
Freigeben: 2016-07-25 08:45:14
Original
897 Leute haben es durchsucht

生成多种类型的缩略图

  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 {
  74. $ftoW=$toW;
  75. $ftoH=$ftoW*($this->srcH/$this->srcW);
  76. }
  77. else
  78. {
  79. $ftoH=$toH;
  80. $ftoW=$ftoH*($this->srcW/$this->srcH);
  81. }
  82. if($this->srcW>$toW||$this->srcH>$toH)
  83. {
  84. $cImg=$this->CreatImage($this->im,$ftoW,$ftoH,0,0,0,0,$this->srcW,$this->srcH);
  85. return $this->EchoImage($cImg,$toFile);
  86. ImageDestroy($cImg);
  87. }
  88. else
  89. {
  90. $cImg=$this->CreatImage($this->im,$this->srcW,$this->srcH,0,0,0,0,$this->srcW,$this->srcH);
  91. return $this->EchoImage($cImg,$toFile);
  92. ImageDestroy($cImg);
  93. }
  94. }
  95. //生成最小裁剪后的缩图
  96. function Cut($toFile,$toW,$toH)
  97. {
  98. $toWH=$toW/$toH;
  99. $srcWH=$this->srcW/$this->srcH;
  100. if($toWH {
  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 {
  121. $ftoW=$toW;
  122. $ftoH=$ftoW*($this->srcH/$this->srcW);
  123. }
  124. else
  125. {
  126. $ftoH=$toH;
  127. $ftoW=$ftoH*($this->srcW/$this->srcH);
  128. }
  129. if(function_exists("imagecreatetruecolor"))
  130. {
  131. @$cImg=ImageCreateTrueColor($toW,$toH);
  132. if(!$cImg)
  133. {
  134. $cImg=ImageCreate($toW,$toH);
  135. }
  136. }
  137. else
  138. {
  139. $cImg=ImageCreate($toW,$toH);
  140. }
  141. $backcolor = imagecolorallocate($cImg, $bk1, $bk2, $bk3); //填充的背景颜色
  142. ImageFilledRectangle($cImg,0,0,$toW,$toH,$backcolor);
  143. if($this->srcW>$toW||$this->srcH>$toH)
  144. {
  145. $proImg=$this->CreatImage($this->im,$ftoW,$ftoH,0,0,0,0,$this->srcW,$this->srcH);
  146. /*
  147. if($ftoW {
  148. ImageCopyMerge($cImg,$proImg,($toW-$ftoW)/2,0,0,0,$ftoW,$ftoH,100);
  149. }
  150. else if($ftoH {
  151. ImageCopyMerge($cImg,$proImg,0,($toH-$ftoH)/2,0,0,$ftoW,$ftoH,100);
  152. }
  153. */
  154. if($ftoW {
  155. ImageCopy($cImg,$proImg,($toW-$ftoW)/2,0,0,0,$ftoW,$ftoH);
  156. }
  157. else if($ftoH {
  158. ImageCopy($cImg,$proImg,0,($toH-$ftoH)/2,0,0,$ftoW,$ftoH);
  159. }
  160. else
  161. {
  162. ImageCopy($cImg,$proImg,0,0,0,0,$ftoW,$ftoH);
  163. }
  164. }
  165. else
  166. {
  167. ImageCopyMerge($cImg,$this->im,($toW-$ftoW)/2,($toH-$ftoH)/2,0,0,$ftoW,$ftoH,100);
  168. }
  169. return $this->EchoImage($cImg,$toFile);
  170. ImageDestroy($cImg);
  171. }
  172. function CreatImage($img,$creatW,$creatH,$dstX,$dstY,$srcX,$srcY,$srcImgW,$srcImgH)
  173. {
  174. if(function_exists("imagecreatetruecolor"))
  175. {
  176. @$creatImg = ImageCreateTrueColor($creatW,$creatH);
  177. if($creatImg)
  178. ImageCopyResampled($creatImg,$img,$dstX,$dstY,$srcX,$srcY,$creatW,$creatH,$srcImgW,$srcImgH);
  179. else
  180. {
  181. $creatImg=ImageCreate($creatW,$creatH);
  182. ImageCopyResized($creatImg,$img,$dstX,$dstY,$srcX,$srcY,$creatW,$creatH,$srcImgW,$srcImgH);
  183. }
  184. }
  185. else
  186. {
  187. $creatImg=ImageCreate($creatW,$creatH);
  188. ImageCopyResized($creatImg,$img,$dstX,$dstY,$srcX,$srcY,$creatW,$creatH,$srcImgW,$srcImgH);
  189. }
  190. return $creatImg;
  191. }
  192. //输出图片,link---只输出,不保存文件。file--保存为文件
  193. function EchoImage($img,$to_File)
  194. {
  195. switch($this->echoType)
  196. {
  197. case "link":
  198. if(function_exists('imagejpeg')) return ImageJpeg($img);
  199. else return ImagePNG($img);
  200. break;
  201. case "file":
  202. if(function_exists('imagejpeg')) return ImageJpeg($img,$to_File);
  203. else return ImagePNG($img,$to_File);
  204. break;
  205. }
  206. }
  207. }
  208. ?>
复制代码

php


Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage