Heim  >  Artikel  >  Backend-Entwicklung  >  PHP创建图片缩略图

PHP创建图片缩略图

WBOY
WBOYOriginal
2016-07-25 08:48:24987Durchsuche
  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() canvasWidth) && ($this->getImageHeight() 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. }
复制代码


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn