PHP로 이미지 썸네일 만들기

WBOY
풀어 주다: 2016-07-25 08:48:24
원래의
994명이 탐색했습니다.
  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 $imgSrc; //圖片的路徑
  30. private $saveSrc; //圖片的儲存路徑,預設為空
  31. private $canvasWidth; //畫布的寬度
  32. private $canvasHeight; //畫布的高度
  33. private $im; //畫布資源
  34. private $dm; //複製圖片回傳的資源
  35. /**
  36. * 初始化類,載入相關設定
  37. *
  38. * @param $imgSrc 需要縮略的圖片的路徑
  39. * @param $canvasWidth 縮圖的寬度
  40. * @param $canvasHeight 縮縮圖略圖的高度
  41. */
  42. public function __construct($imgSrc,$canvasWidth,$canvasHeight,$saveSrc=null) {
  43. $this->imgSrc = $imgSrc;
  44. $this->canvasWidth = $canvasWidth;
  45. $this->canvasHeight = $canvasHeight;
  46. $this->saveSrc = $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. $imageInfo = $this->getImageInfo();
  76. return $imageInfo['0'];
  77. }
  78. /**
  79. * 取得圖片高度
  80. *
  81. * @return int 圖片的高度
  82. */
  83. public function getImageHeight()
  84. {
  85. $imageInfo = $this->getImageInfo();
  86. return $imageInfo['1'];
  87. }
  88. /**
  89. * 取得圖片的種類
  90. *
  91. * @return 圖片的mime值
  92. */
  93. public function getImageMime()
  94. {
  95. $imageInfo = $this->getImageInfo();
  96. return $imageInfo['mime'];
  97. }
  98. /***/
  99. private function createCanvas()
  100. {
  101. $size = $this->trueSize();
  102. $this->im = imagecreatetruecolor($size['width'],$size ['height']);
  103. }
  104. /**
  105. * 建立畫布
  106. *
  107. * 同時將建立的畫布資源放入屬性$this->im中
  108. */
  109. private function judgeImage()
  110. {
  111. $mime = $this->getImageMime() ;
  112. switch ($mime)
  113. {
  114. case 'image/png':$dm = imagecreatefrompng($this->imgSrc);
  115. break;
  116. case 'image/ gif':$dm = imagecreatefromgif($this->imgSrc);
  117. break;
  118. case 'image/jpg':$dm = imagecreatefromjpeg($this->imgSrc);
  119. break;
  120. case 'image/jpeg':$dm = imagecreatefromgjpeg($this->imgSrc);
  121. break;
  122. }
  123. $this->dm = $dm;
  124. }
  125. /**
  126. * 축소 후 이미지의 너비와 높이를 결정
  127. *
  128. * 이 너비와 높이는 캔버스의 크기로도 사용됩니다.
  129. *
  130. * @return array 같은 비율로 축소한 후의 이미지
  131. */
  132. 공개 함수 trueSize()
  133. {
  134. $proportionW = $this->getImageWidth() / $this->canvasWidth;
  135. $proportionH = $this->getImageHeight() / $this->canvasHeight;
  136. if( ($this->getImageWidth() < $this->canvasWidth) && ($this ->getImageHeight() < $this->canvasHeight) )
  137. {
  138. $trueSize = array('width'=>$this->getImageWidth(),'height'=>$ this->getImageHeight());
  139. }
  140. elseif($proportionW >= $proportionH)
  141. {
  142. $trueSize = array('width'=>$this->canvasWidth ,'height'=>$this->getImageHeight() / $proportionW);
  143. }
  144. else
  145. {
  146. $trueSize = array('width'=>$this-> ;getImageWidth() / $proportionH,'height'=>$this->canvasHeight);
  147. }
  148. return $trueSize;
  149. }
  150. /**
  151. * 이미지를 새 캔버스에 복사
  152. *
  153. * 이미지의 크기는 비율에 맞게 조정되며 변형되지 않습니다
  154. * /
  155. 비공개 함수 copyImage()
  156. {
  157. $size = $this->trueSize();
  158. imagecopyreized($this->im, $this->dm , 0 , 0 , 0 , 0 , $size['width'] , $size['height'] , $this->getImageWidth() , $this->getImageheight());
  159. }
  160. /**
  161. * 이미지 출력
  162. *
  163. * 이미지 이름은 기본적으로 원본 이미지 이름과 동일합니다.
  164. *
  165. * 경로는 현재 디렉터리 아래의 작은 디렉터리입니다. 큰 이미지
  166. *
  167. * 작은 디렉토리가 없으면 자동으로 생성됩니다
  168. */
  169. 공용 함수 headerImage()
  170. {
  171. $position = strrpos($this->imgSrc,'/');
  172. $imageName = substr($this ->imgSrc,($위치 1));
  173. if($this->saveSrc)
  174. {
  175. $imageFlode = $this->saveSrc.'/';
  176. }
  177. else
  178. {
  179. $imageFlode = substr($this->imgSrc,0,$position).'/small/';
  180. }
  181. if(!file_exists($imageFlode))
  182. {
  183. mkdir($imageFlode);
  184. }
  185. $saveSrc = $imageFlode.$imageName;
  186. imagejpeg($this->im,$saveSrc);
  187. }
  188. }
复代代码


관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!