ホームページ  >  記事  >  バックエンド開発  >  PHP は画像サムネイル コード クラスを生成します

PHP は画像サムネイル コード クラスを生成します

WBOY
WBOYオリジナル
2016-07-25 08:45:14822ブラウズ

複数の種類のサムネイルを生成する

    /****************************************
  1. *著者: 夢見る蠍座 (belucly)
  2. *完了時間: 2006-12-18
  3. *クラス名: CreatMiniature
  4. *機能: 複数種類のサムネイルを生成
  5. *基本パラメータ: $srcFile、$echoType
  6. *メソッドで使用されるパラメータ:
  7. $toFile、生成されたファイル
  8. $toW、生成された幅
  9. $toH、生成された高さ
  10. $bk1、背景色パラメータは最高の255です
  11. $bk2、背景色パラメータ
  12. $bk3、背景色パラメータ
  13. *例:
  14. include("thumb. php");
  15. $cm=new CreatMiniature();
  16. $cm->SetVar("1.jpg","file");
  17. $cm->Distortion("dis_bei.jpg",150,200);
  18. $cm->Prorate("pro_bei.jpg",150,200);
  19. $cm->Cut("cut_bei.jpg",150,200);
  20. $cm->BackFill("fill_bei.jpg",150,200);
  21. ******************************************/
  22. class CreatMiniature
  23. {
  24. //パブリック変数
  25. var $srcFile="" //元の画像
  26. var $echoType; , リンク -- ファイルとして保存しない -- ファイルとして保存
  27. var $im="" // 一時変数
  28. var $srcW="" // 元の画像の幅
  29. var $srcH=""; ; // 元の画像の高さ
  30. // 変数を設定して
  31. 関数を初期化 SetVar($srcFile,$echoType)
  32. {
  33. $this->srcFile=$srcFile;
  34. $this->echoType=$echoType;
  35. $info = "";
  36. $data = GetImageSize($this->srcFile,$info);
  37. switch ($data[2])
  38. {
  39. case 1:
  40. if(!function_exists("imagecreatefromgif")){
  41. echo " GD ライブラリでは GIF 形式の画像を使用できません。Jpeg または PNG 形式を使用してください! Return";
  42. exit();
  43. }
  44. $this->im = ImageCreateFromGIF($this->srcFile);
  45. Break;
  46. case 2:
  47. if(!function_exists("imagecreatefromjpeg")){
  48. echo "GD ライブラリは jpeg 形式の画像を使用できません。他の形式の画像を使用してください。戻り";
  49. exit();
  50. }
  51. $this->im = ImageCreateFromJpeg($this->srcFile);
  52. ブレーク;
  53. ケース 3:
  54. $this->im = ImageCreateFromPNG($this->srcFile);
  55. ブレーク;
  56. }
  57. $this->srcW=ImageSX($this->im);
  58. $ this->srcH=ImageSY($this->im);
  59. }
  60. // 扭曲型缩图
  61. 関数の生成 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. // 比例放散を生成する関数 Prorate($toFile,$toW,$toH​​)
  68. {
  69. $toWH=$toW/$toH;
  70. $srcWH=$this->srcW/$this->srcH;
  71. if($toWH {
  72. $ftoW=$toW;
  73. $ftoH=$ftoW*($this-> srcH/$this->srcW);
  74. }
  75. else
  76. {
  77. $ftoH=$toH;
  78. $ftoW=$ftoH*($this->srcW/$this->srcH);
  79. }
  80. if ($this->srcW>$toW||$this->srcH>$toH)
  81. {
  82. $cImg=$this->CreatImage($this->im,$ftoW,$ftoH,0, 0,0,0,$this->srcW,$this->srcH);
  83. return $this->EchoImage($cImg,$toFile);
  84. ImageDestroy($cImg);
  85. }
  86. else
  87. {
  88. $cImg=$this->CreatImage($this->im,$this->srcW,$this->srcH,0,0,0,0,$this->srcW,$this- >srcH);
  89. return $this->EchoImage($cImg,$toFile);
  90. ImageDestroy($cImg);
  91. }
  92. }
  93. // 最小裁剪後の缩图
  94. function Cut($toFile, $toW,$toH​​)
  95. {
  96. $toWH=$toW/$toH;
  97. $srcWH=$this->srcW/$this->srcH;
  98. if($toWH {
  99. $ctoH=$toH;
  100. $ctoW=$ctoH*($this->srcW/$this->srcH);
  101. }
  102. else
  103. {
  104. $ctoW=$toW;
  105. $ctoH=$ctoW*($this->srcH/$this->srcW);
  106. }
  107. $allImg=$this->CreatImage($this->im,$ctoW,$ctoH,0, 0,0,0,$this->srcW,$this->srcH);
  108. $cImg=$this->CreatImage($allImg,$toW,$toH​​,0,0,($ctoW-$ toW)/2,($ctoH-$toH)/2,$toW,$toH​​);
  109. return $this->EchoImage($cImg,$toFile);
  110. ImageDestroy($cImg);
  111. ImageDestroy($allImg );
  112. }
  113. // 背景充填の缩图
  114. function BackFill($toFile,$toW,$toH​​,$bk1=255,$bk2=255,$bk3=255)
  115. {
  116. $toWH=$toW /$toH;
  117. $srcWH=$this->srcW/$this->srcH;
  118. if($toWH {
  119. $ftoW=$toW;
  120. $ftoH=$ftoW*($ this->srcH/$this->srcW);
  121. }
  122. else
  123. {
  124. $ftoH=$toH;
  125. $ftoW=$ftoH*($this->srcW/$this->srcH);
  126. }
  127. if(function_exists("imagecreatetruecolor"))
  128. {
  129. @$cImg=ImageCreateTrueColor($toW,$toH​​);
  130. if(!$cImg)
  131. {
  132. $cImg=ImageCreate($toW,$toH​​);
  133. }
  134. }
  135. else
  136. {
  137. $cImg=ImageCreate($toW,$toH​​);
  138. }
  139. $backcolor = imagecolorallocate($cImg, $bk1, $bk2, $bk3); //塗りつぶされた背景色
  140. ImageFilledRectangle($cImg,0,0,$toW,$toH​​,$backcolor);
  141. if($this->srcW>$toW||$this->srcH>$toH)
  142. {
  143. $proImg=$this->CreatImage($this->im,$ftoW,$ftoH,0,0,0,0,$this->srcW,$this->srcH);
  144. /*
  145. if($ftoW< $toW)
  146. {
  147. ImageCopyMerge($cImg,$proImg,($toW-$ftoW)/2,0,0,0,$ftoW,$ftoH,100);
  148. }
  149. else if($ftoH<$toH)
  150. {
  151. ImageCopyMerge($cImg,$proImg,0,($toH-$ftoH)/2,0,0,$ftoW,$ftoH,100);
  152. }
  153. */
  154. if($ftoW<$toW)
  155. {
  156. ImageCopy($cImg,$proImg,($toW-$ftoW)/2,0,0,0,$ftoW,$ftoH);
  157. }
  158. else if($ ftoH<$toH)
  159. {
  160. ImageCopy($cImg,$proImg,0,($toH-$ftoH)/2,0,0,$ftoW,$ftoH);
  161. }
  162. else
  163. {
  164. ImageCopy($cImg) ,$proImg,0,0,0,0,$ftoW,$ftoH);
  165. }
  166. }
  167. else
  168. {
  169. ImageCopyMerge($cImg,$this->im,($toW-$ftoW)/2, ($toH-$ftoH)/2,0,0,$ftoW,$ftoH,100);
  170. }
  171. return $this->EchoImage($cImg,$toFile);
  172. ImageDestroy($cImg);
  173. }
  174. function CreatImage($img,$creatW,$creatH,$dstX,$dstY,$srcX,$srcY ,$srcImgW,$srcImgH)
  175. {
  176. if(function_exists("imagecreatetruecolor"))
  177. {
  178. @$creatImg = ImageCreateTrueColor($creatW,$creatH);
  179. if($creatImg)
  180. ImageCopyResampled($creatImg,$img, $dstX,$dstY,$srcX,$srcY,$creatW,$creatH,$srcImgW,$srcImgH);
  181. else
  182. {
  183. $creatImg=ImageCreate($creatW,$creatH);
  184. ImageCopyResize($creatImg,$img) ,$dstX,$dstY,$srcX,$srcY,$creatW,$creatH,$srcImgW,$srcImgH);
  185. }
  186. }
  187. else
  188. {
  189. $creatImg=ImageCreate($creatW,$creatH);
  190. ImageCopyResize( $creatImg,$img,$dstX,$dstY,$srcX,$srcY,$creatW,$creatH,$srcImgW,$srcImgH);
  191. }
  192. return $creatImg;
  193. }
  194. //出力画像、リンク-- file-- ファイルとして保存
  195. function EchoImage($img,$to_File)
  196. {
  197. switch($this->echoType)
  198. {
  199. case "link":
  200. if(function_exists(' imagejpeg')) return ImageJpeg($img);
  201. else return ImagePNG($img);
  202. Break;
  203. case "file":
  204. if(function_exists('imagejpeg')) return ImageJpeg($img,$to_File);
  205. else return ImagePNG($img,$to_File);
  206. Break;
  207. }
  208. }
  209. }
  210. ?>
复制發

php


声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。