PHP建立圖片縮圖

來源:互聯網
上載者:User
<無詳細內容>
  1. /**
  2. * 上傳圖片產生縮圖
  3. *
  4. * 需要GD2庫的支援
  5. *
  6. * 初始化時需要參數new thumbnails('需要縮減的圖片的原始地址','縮圖的寬度','縮圖的高度','(選擇性參數)縮圖的儲存路徑');
  7. * 如果最後一個參數不指定,那麼縮圖就預設儲存在原始圖片的所在目錄裡的small檔案夾裡,
  8. * 如果不存在small檔案夾,則會自動建立small檔案夾
  9. *
  10. * 初始化之後需要調用方法produce建立縮圖
  11. * $thumbnails = new thumbnails(''....);
  12. * $thumbnails->produce();
  13. *
  14. * 其中可以擷取原始圖片的相關資訊,寬度、高度、和圖片mime
  15. *
  16. * $thumbnails->getImageWidth(); //int 圖片寬度
  17. * $thumbnails->getImageHeight(); // int 圖片高度
  18. * $thumbnails->getImageMime(); // string 圖片的mime
  19. *
  20. * $thumbnails->trueSize(); //array 這是一個包含圖片等比例縮減之後的寬度和高度值的數組
  21. * $size = array('width'=>'','height'=>'');
  22. * 擷取圖片等比縮減之後的寬度和高度
  23. * $size['width']//等比縮圖的寬度
  24. * $size['height']//等比縮圖的高度
  25. *
  26. */
  27. class thumbnails{
  28. private $imgSrc; //圖片的路徑
  29. private $saveSrc; //圖片的儲存路徑,預設為空白
  30. private $canvasWidth; //畫布的寬度
  31. private $canvasHeight; //畫布的高度
  32. private $im; //畫布資源
  33. private $dm; //複製圖片返回的資源
  34. /**
  35. * 初始化類,載入相關設定
  36. *
  37. * @param $imgSrc 需要縮減的圖片的路徑
  38. * @param $canvasWidth 縮圖的寬度
  39. * @param $canvasHeight 縮圖的高度
  40. */
  41. public function __construct($imgSrc,$canvasWidth,$canvasHeight,$saveSrc=null)
  42. {
  43. $this->imgSrc = $imgSrc;
  44. $this->canvasWidth = $canvasWidth;
  45. $this->canvasHeight = $canvasHeight;
  46. $this->saveSrc = $saveSrc;
  47. }
  48. /**
  49. * 產生縮圖
  50. */
  51. public function produce()
  52. {
  53. $this->createCanvas();
  54. $this->judgeImage();
  55. $this->copyImage();
  56. $this->headerImage();
  57. }
  58. /**
  59. * 擷取載入圖片的資訊
  60. *
  61. * 包含長度、寬度、圖片類型
  62. *
  63. * @return array 包含圖片長度、寬度、mime的數組
  64. */
  65. private function getImageInfo()
  66. {
  67. return getimagesize($this->imgSrc);
  68. }
  69. /**
  70. * 擷取圖片的長度
  71. *
  72. * @return int 圖片的寬度
  73. */
  74. public function getImageWidth()
  75. {
  76. $imageInfo = $this->getImageInfo();
  77. return $imageInfo['0'];
  78. }
  79. /**
  80. * 擷取圖片高度
  81. *
  82. * @return int 圖片的高度
  83. */
  84. public function getImageHeight()
  85. {
  86. $imageInfo = $this->getImageInfo();
  87. return $imageInfo['1'];
  88. }
  89. /**
  90. * 擷取圖片的類型
  91. *
  92. * @return 圖片的mime值
  93. */
  94. public function getImageMime()
  95. {
  96. $imageInfo = $this->getImageInfo();
  97. return $imageInfo['mime'];
  98. }
  99. /**
  100. * 建立畫布
  101. *
  102. * 同時將建立的畫布資源放入屬性$this->im中
  103. */
  104. private function createCanvas()
  105. {
  106. $size = $this->trueSize();
  107. $this->im = imagecreatetruecolor($size['width'],$size['height']);
  108. }
  109. /**
  110. * 判斷圖片的mime值,確定使用的函數
  111. *
  112. * 同時將建立的圖片資源放入$this->dm中
  113. */
  114. private function judgeImage()
  115. {
  116. $mime = $this->getImageMime();
  117. switch ($mime)
  118. {
  119. case 'image/png':$dm = imagecreatefrompng($this->imgSrc);
  120. break;
  121. case 'image/gif':$dm = imagecreatefromgif($this->imgSrc);
  122. break;
  123. case 'image/jpg':$dm = imagecreatefromjpeg($this->imgSrc);
  124. break;
  125. case 'image/jpeg':$dm = imagecreatefromgjpeg($this->imgSrc);
  126. break;
  127. }
  128. $this->dm = $dm;
  129. }
  130. /**
  131. * 判斷圖片縮減後的寬度和高度
  132. *
  133. * 此寬度和高度也作為畫布的尺寸
  134. *
  135. * @return array 圖片經過等比例縮減之後的尺寸
  136. */
  137. public function trueSize()
  138. {
  139. $proportionW = $this->getImageWidth() / $this->canvasWidth;
  140. $proportionH = $this->getImageHeight() / $this->canvasHeight;
  141. if( ($this->getImageWidth() < $this->canvasWidth) && ($this->getImageHeight() < $this->canvasHeight) )
  142. {
  143. $trueSize = array('width'=>$this->getImageWidth(),'height'=>$this->getImageHeight());
  144. }
  145. elseif($proportionW >= $proportionH)
  146. {
  147. $trueSize = array('width'=>$this->canvasWidth,'height'=>$this->getImageHeight() / $proportionW);
  148. }
  149. else
  150. {
  151. $trueSize = array('width'=>$this->getImageWidth() / $proportionH,'height'=>$this->canvasHeight);
  152. }
  153. return $trueSize;
  154. }
  155. /**
  156. * 將圖片複製到新的畫布上面
  157. *
  158. * 圖片會被等比例的縮放,不會變形
  159. */
  160. private function copyImage()
  161. {
  162. $size = $this->trueSize();
  163. imagecopyresized($this->im, $this->dm , 0 , 0 , 0 , 0 , $size['width'] , $size['height'] , $this->getImageWidth() , $this->getImageheight());
  164. }
  165. /**
  166. * 將圖片輸出
  167. *
  168. * 圖片的名稱預設和原圖片名稱相同
  169. *
  170. * 路徑為大圖片目前的目錄下的small目錄內
  171. *
  172. * 如果small目錄不存在,則會自動建立
  173. */
  174. public function headerImage()
  175. {
  176. $position = strrpos($this->imgSrc,'/');
  177. $imageName = substr($this->imgSrc,($position + 1));
  178. if($this->saveSrc)
  179. {
  180. $imageFlode = $this->saveSrc.'/';
  181. }
  182. else
  183. {
  184. $imageFlode = substr($this->imgSrc,0,$position).'/small/';
  185. }
  186. if(!file_exists($imageFlode))
  187. {
  188. mkdir($imageFlode);
  189. }
  190. $saveSrc = $imageFlode.$imageName;
  191. imagejpeg($this->im,$saveSrc);
  192. }
  193. }
複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.