首頁  >  文章  >  後端開發  >  php生成圖片縮圖程式碼類

php生成圖片縮圖程式碼類

WBOY
WBOY原創
2016-07-25 08:45:14851瀏覽

產生多種類型的縮圖

  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("imagecreatefrom")){
  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. exit(); ;
  50. 鍵;
  51. 鍵3:
  52. $this->im = ImageCreateFromPNG($this->srcFile); $this->im);
  53. $this->srcH=ImageSY($this->im); ,$toW,$toH​​ )
  54. {
  55. $cImg=$this->CreateImage($this->im,$toW,$toH​​,0, 0,0,0,$this->srcW,$this->srcH);
  56. 回傳$this -> EchoImage ( $ cImg , $ toFile ); 屬性函數Prorate($toFile,$toW,$toH​​)
  57. {
  58. $toWH=$toW/$toH;
  59. $srcWH=$this-> srcW/ $this->srcH;
  60. if($toWH {
  61. $ftoW=$toW;
  62. $ftoH=$ftoW*( $this->srcH/$ this-> srcW ); > }
  63. if ( $this -> srcW > $toW || $this -> srcH > $toH )
  64. {
  65. $cImg = $ this -> CreateImage ( $this -> im , $ftoW , $ftoH , 0 0 , 0 , 0 , $ this -> srcW , $ this -> srcH ) ;
  66. }
  67. else
  68. $cImg=$ this->CreateImage($this->im,$this->srcW,$this->srcH,0,0,0,0; ​​, $this->srcW , $this-> srcH ); / 傳回輸入檔案的金鑰
  69. function Cut($ toFile,$toW,$toH​​ )
  70. {
  71. $toWH=$toW/$toH;
  72. $ srcWH =$this- >srcW/$this->srcH;
  73. if($至WH9 =$srcWH)
  74. {
  75. $ctoH=$toH;
  76. $ctoW=$ctoH*( $this ->srcW/ $this->srcH);
  77. }
  78. else
  79. {
  80. $ctoW=$toW;
  81. $ctoH=$ctoW*($this->srcH/ $this - > srcW ) ; this - > srcH ); /2, $toW, $toH );
  82. return $this ->EchoImage($cImg,$toFile); }
  83. // 安裝預設檔
  84. 返回BackFill($toFile,$toW,$toH​​,$bk1=255,$bk2=255,$bk3=255)
  85. {
  86. $toWH=$toW/$toH;
  87. $srcWH =$this->srcW/$this->srcH;
  88. if($toWH {
  89. $ftoW=$toW;
  90. $ftoH=$ ftoW*($this ->srcH/$this->srcW)
  91. $ftoW=$ftoH*($this->; srcW/$this->srcH);
  92. }
  93. if(function_exists("createTrueColorImage"))
  94. {
  95. @$cImg=CreateTrueColorImage($toW,$ ); if ( !$cImg)
  96. {
  97. $cImg=CreateImage($toW,$toH​​); ($toW ,$toH​​於);
  98. }
  99. $backcolor = imageColorAllocate ($cImg, $bk1) , $bk2, $bk3); //公共配置規範
  100. ImageFilledRectangle($cImg,0,0,$toW,$toH​​,$backcolor);
  101. if($this->srcW>$toW||$this->srcH > ;$ toH)
  102. {
  103. $proImg=$this->CreateImage($this->im,$ftoW,$ftoH,0,0,0,0,$this->srcW,$this-> srcH ) , $ftoW , $ftoH , 100 ); / 2 , 0 , 0 , $ftoW , $ftoH , 100 ); ); $cImg, $proImg, 0,($toH-$ftoH)/2,0, 0,$ftoW,$ftoH); $proImg , 0 , 0 , 0 , $ ftoW , $ ftoH ); ftoW)/2,($toH-$ftoH)/2,0,0,$ftoW,$ftoH, 100);
  104. return $this->EchoImage($cImg,$toFile);
  105. ImageDestroy($cImg);
  106. }
  107. function CreatImage($img,$creatW,$creatH,$stX ,$dstY,$srcX,$srcY,$srcImgW,$srcImgH)
  108. {
  109. if(function_exists("imagecreatetruecolor"))
  110. {
  111. @$creatImg = ImageCreate. );
  112. if($creatImg)
  113. ImageCopyResampled($creatImg,$img,$dstX,$dstY,$srcX,$srcY,$creatW,$creatH,$srcImgW,$srcX,$srcY,$creatW,$creatH,$srcImgW,$srcImgH);
  114. {
  115. $creatImg=ImageCreate($creatW,$creatH);
  116. ImageCopyResized($creatImg,$img,$dstX,$dstY,$srcX,$srcY,$creatW,$creatH,$creatW$4,$,$creatW$Y,$creatW$4,$ ,$srcImgH);
  117. }
  118. }
  119. else
  120. {
  121. $creatImg=ImageCreate($creatW,$creatH);
  122. ImageCopyResized($creatImg,$3,$ $dstY,$srcX,$srcY,$creatW,$creatH,$srcImgW,$srcImgH);
  123. }
  124. return $creatImg;
  125. }
  126. // 輸出圖片,連結圖片,連結圖片,連結連結--只輸出,不儲存檔案。 "link":
  127. if(function_exists('imagejpeg')) return ImageJpeg($img);
  128. else return ImagePNG($img);
  129. ;
  130. case "file":break
  131. if(function_exists('imagejpeg')) return ImageJpeg($img,$to_File);
  132. else return ImagePNG($img,$to_File);
  133. 中斷;
  134. }
  135. }
  136. }
  137. 🎜>}
  138. ?>
  139. 複製程式碼
php

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn