php產生圖片縮圖代碼類

來源:互聯網
上載者:User

產生多種類型的縮圖

  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
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.