ホームページ  >  記事  >  バックエンド開発  >  PHPで画像のサムネイルを作成する

PHPで画像のサムネイルを作成する

WBOY
WBOYオリジナル
2016-07-25 08:48:24985ブラウズ
    /**
  1. * サムネイルを生成するには画像をアップロードします
  2. *
  3. * GD2 ライブラリのサポートが必要です
  4. *
  5. * パラメータ new sumnails('サムネイル化される画像の元のアドレス'、'サムネイルの幅'、'サムネイルの高さ'は初期化中に必要です ' ,'(オプションのパラメータ) サムネイル保存パス');
  6. * 最後のパラメータが指定されていない場合、サムネイルはデフォルトで元画像のディレクトリ内の小さいフォルダに保存されます
  7. * 小さいフォルダが存在しない場合、小さなフォルダーが自動的に作成されます
  8. *
  9. * 初期化後、メソッドProduceを呼び出してサムネイルを作成する必要があります
  10. * $thumbnails = new summnails(''....);
  11. * $thumbnails->Produce() ;
  12. *
  13. * 元の画像、幅、高さ、画像 mime の関連情報を取得できます
  14. *
  15. * $thumbnails->getImageWidth() //int 画像幅
  16. * $thumbnails->getImageHeight( ); // int 画像の高さ
  17. * $thumbnails->getImageMime(); // 画像の文字列 mime
  18. *
  19. * $thumbnails->trueSize(); // これは幅と合計を含む配列です。縮小後の画像の高さの配列
  20. * $size = array('width'=>'','height'=>'');
  21. * 画像の幅と高さを取得します。スケーリング後
  22. * $size['width ']//比例サムネイルの幅
  23. * $size['height']//比例サムネイルの高さ
  24. *
  25. */
  26. class sumnails{
  27. private $imgSrc; //画像の保存パス
  28. private $saveSrc; //画像の保存パス、デフォルトは空です
  29. private $canvasWidth; //キャンバスの幅
  30. private $canvasHeight; //キャンバスの高さ
  31. private $im; //画像をコピーすることで返されるリソース
  32. /**
  33. * クラスを初期化し、関連設定を読み込みます
  34. *
  35. * @param $imgSrc サムネイル化する必要がある画像のパス
  36. * @param $canvasWidth サムネイルの幅
  37. * @param $canvasHeight サムネイルの高さ
  38. * /
  39. public function __construct($imgSrc,$canvasWidth,$canvasHeight,$saveSrc=null)
  40. {
  41. $this->imgSrc = $imgSrc;
  42. $this->canvasWidth = $canvasWidth;
  43. $this->gt; CanvasHeight = $ CanvasHeight;
  44. $this->saveSrc = $saveSrc;
  45. }
  46. /**
  47. * サムネイルを生成します
  48. */
  49. public function generated()
  50. {
  51. $this->createCanvas();
  52. $this- >judgeImage ();
  53. $this->copyImage();
  54. $this->headerImage();
  55. }
  56. /**
  57. * ロードされた画像の情報を取得します
  58. *
  59. * 長さ、幅、画像タイプが含まれます
  60. *
  61. * @return array 画像の長さ、幅、MIME を含む配列
  62. */
  63. private function getImageInfo()
  64. {
  65. return getimagesize( $this->imgSrc);
  66. }
  67. /**
  68. * 画像の長さを取得します
  69. *
  70. * @return int 画像の幅
  71. */
  72. public function getImageWidth()
  73. {
  74. $imageInfo = $this->getImageInfo();
  75. return $imageInfo['0'] ;
  76. }
  77. /**
  78. * 画像の高さを取得します
  79. *
  80. * @return int 画像の高さ
  81. */
  82. public function getImageHeight()
  83. {
  84. $imageInfo = $this->getImageInfo();
  85. return $imageInfo['1'];
  86. }
  87. /* *
  88. * 画像の種類を取得します
  89. *
  90. * @return 画像の MIME 値を返します
  91. */
  92. public function getImageMime()
  93. {
  94. $imageInfo = $this->getImageInfo();
  95. return $imageInfo['mime'];
  96. }
  97. /**
  98. * キャンバスを作成します
  99. *
  100. * 同時に、作成したキャンバスリソースを $this->im 属性に入れます
  101. */
  102. private function createCanvas ()
  103. {
  104. $size = $this->trueSize();
  105. $this->im = imagecreatetruecolor($size['width'],$size['height']);
  106. }
  107. /* *
  108. * 画像のMIME値を決定し、使用する機能を決定します
  109. *
  110. * 同時に、作成した画像リソースを$this->dmに入れます
  111. */
  112. プライベート関数 judgeImage()
  113. {
  114. $mime = $this->getImageMime();
  115. switch ($mime)
  116. {
  117. case 'image/png':$dm = imagecreatefrompng ($this ->imgSrc);
  118. ブレーク;
  119. case 'image/gif':$dm = imagecreatefromgif($this->imgSrc);
  120. ブレーク;
  121. case 'image/jpg':$dm = imagecreatefromjpeg ($this ->imgSrc);
  122. Break;
  123. case 'image/jpeg':$dm = imagecreatefromgjpeg($this->imgSrc);
  124. Break;
  125. }
  126. $this->dm = $dm;
  127. }
  128. /**
  129. * 縮小後の画像の幅と高さを決定します
  130. *
  131. * この幅と高さはキャンバスのサイズとしても使用されます
  132. *
  133. * @return 配列 均等に縮小した後の画像のサイズ
  134. */
  135. public function trueSize()
  136. {
  137. $proportionW = $this->getImageWidth() / $this->canvasWidth;
  138. $proportionH = $this->getImageHeight() / $this->canvasHeight;
  139. if( ($this->getImageWidth() < $this->canvasWidth) && ($this->getImageHeight() < $this->canvasHeight) )
  140. {
  141. $trueSize = array('width'=>$this->getImageWidth(),'height'=>$this->getImageHeight());
  142. }
  143. elseif($proportionW >= $ propertyH)
  144. {
  145. $trueSize = array('width'=>$this->canvasWidth,'height'=>$this->getImageHeight() / $proportionW);
  146. }
  147. else
  148. {
  149. $ trueSize = array('width'=>$this->getImageWidth() / $proportionH,'height'=>$this->canvasHeight);
  150. }
  151. return $trueSize;
  152. }
  153. /**
  154. * 画像を新しいキャンバスにコピーします
  155. *
  156. * 画像は比例的に拡大縮小され、変形されません
  157. */
  158. プライベート関数 copyImage()
  159. {
  160. $size = $this->trueSize();
  161. imagecopyresize($this->im, $this->dm , 0 , 0 , 0 , 0 , $size['width'] , $size['height'] , $this->getImageWidth() , $this->getImageheight());
  162. }
  163. /**
  164. * 画像をエクスポートします
  165. *
  166. * デフォルトでは、画像の名前は元の画像名と同じです
  167. *
  168. * パスは、大きな画像の現在のディレクトリの下にある小さなディレクトリです
  169. *
  170. * 小さなディレクトリの場合存在しないため、自動的に作成されます
  171. */
  172. public function headerImage()
  173. {
  174. $position = strrpos($this->imgSrc,'/');
  175. $imageName = substr($this->imgSrc,($position + 1));
  176. if($this ->saveSrc)
  177. {
  178. $imageFlode = $this->saveSrc.'/';
  179. }
  180. else
  181. {
  182. $imageFlode = substr($this->imgSrc,0,$position).'/small /';
  183. }
  184. if(!file_exists($imageFlode))
  185. {
  186. mkdir($imageFlode);
  187. }
  188. $saveSrc = $imageFlode.$imageName;
  189. imagejpeg($this->im,$saveSrc);
  190. }
  191. }
复制番号


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