国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

275 lines
12KB

  1. <?php if(!defined('DEDEINC')) exit('dedecms');
  2. /**
  3. * 图像处理类
  4. *
  5. * @version $Id: image.class.php 1 18:10 2010年7月5日Z tianya $
  6. * @package DedeCMS.Libraries
  7. * @copyright Copyright (c) 2007 - 2018, DesDev, Inc.
  8. * @copyright Copyright (c) 2020, DedeBIZ.COM
  9. * @license https://www.dedebiz.com/license/v6
  10. * @link https://www.dedebiz.com
  11. */
  12. class image
  13. {
  14. var $attachinfo;
  15. var $targetfile; //图片路径
  16. var $imagecreatefromfunc;
  17. var $imagefunc;
  18. var $attach;
  19. var $animatedgif;
  20. var $watermarkquality;
  21. var $watermarktext;
  22. var $thumbstatus;
  23. var $watermarkstatus;
  24. // 析构函数,兼容PHP4
  25. function image($targetfile, $cfg_thumb, $cfg_watermarktext, $photo_waterpos, $photo_diaphaneity, $photo_wheight, $photo_wwidth, $cfg_watermarktype, $photo_marktrans,$trueMarkimg, $attach = array())
  26. {
  27. $this->__construct($targetfile, $cfg_thumb, $cfg_watermarktext, $photo_waterpos, $photo_diaphaneity, $photo_wheight, $photo_wwidth, $cfg_watermarktype, $photo_marktrans,$trueMarkimg, $attach);
  28. }
  29. // 析构函数
  30. function __construct($targetfile, $cfg_thumb, $cfg_watermarktext, $photo_waterpos, $photo_diaphaneity, $photo_wheight, $photo_wwidth, $cfg_watermarktype, $photo_marktrans,$trueMarkimg, $attach = array())
  31. {
  32. $this->thumbstatus = $cfg_thumb;
  33. $this->watermarktext = $cfg_watermarktext;
  34. $this->watermarkstatus = $photo_waterpos;
  35. $this->watermarkquality = $photo_marktrans;
  36. $this->watermarkminwidth = $photo_wwidth;
  37. $this->watermarkminheight = $photo_wheight;
  38. $this->watermarktype = $cfg_watermarktype;
  39. $this->watermarktrans = $photo_diaphaneity;
  40. $this->animatedgif = 0;
  41. $this->targetfile = $targetfile;
  42. $this->attachinfo = @getimagesize($targetfile);
  43. $this->attach = $attach;
  44. switch($this->attachinfo['mime'])
  45. {
  46. case 'image/jpeg':
  47. $this->imagecreatefromfunc = function_exists('imagecreatefromjpeg') ? 'imagecreatefromjpeg' : '';
  48. $this->imagefunc = function_exists('imagejpeg') ? 'imagejpeg' : '';
  49. break;
  50. case 'image/gif':
  51. $this->imagecreatefromfunc = function_exists('imagecreatefromgif') ? 'imagecreatefromgif' : '';
  52. $this->imagefunc = function_exists('imagegif') ? 'imagegif' : '';
  53. break;
  54. case 'image/png':
  55. $this->imagecreatefromfunc = function_exists('imagecreatefrompng') ? 'imagecreatefrompng' : '';
  56. $this->imagefunc = function_exists('imagepng') ? 'imagepng' : '';
  57. break;
  58. }//为空则匹配类型的函数不存在
  59. $this->attach['size'] = empty($this->attach['size']) ? @filesize($targetfile) : $this->attach['size'];
  60. if($this->attachinfo['mime'] == 'image/gif')
  61. {
  62. $fp = fopen($targetfile, 'rb');
  63. $targetfilecontent = fread($fp, $this->attach['size']);
  64. fclose($fp);
  65. $this->animatedgif = strpos($targetfilecontent, 'NETSCAPE2.0') === false ? 0 : 1;
  66. }
  67. }
  68. /**
  69. * 生成缩略图
  70. *
  71. * @access public
  72. * @param int $thumbwidth 图片宽度
  73. * @param int $thumbheight 图片高度
  74. * @param int $preview 是否预览
  75. * @return void
  76. */
  77. function thumb($thumbwidth, $thumbheight, $preview = 0)
  78. {
  79. $this->thumb_gd($thumbwidth, $thumbheight, $preview);
  80. if($this->thumbstatus == 2 && $this->watermarkstatus)
  81. {
  82. $this->image($this->targetfile, $this->attach);
  83. $this->attach['size'] = filesize($this->targetfile);
  84. }
  85. }
  86. /**
  87. * 图片水印
  88. *
  89. * @access public
  90. * @param int $preview 是否预览
  91. * @return void
  92. */
  93. function watermark($preview = 0)
  94. {
  95. if($this->watermarkminwidth && $this->attachinfo[0] <= $this->watermarkminwidth && $this->watermarkminheight && $this->attachinfo[1] <= $this->watermarkminheight)
  96. {
  97. return ;
  98. }
  99. $this->watermark_gd($preview);
  100. }
  101. /**
  102. * 使用gd生成缩略图
  103. *
  104. * @access public
  105. * @param int $thumbwidth 图片宽度
  106. * @param int $thumbheight 图片高度
  107. * @param int $preview 是否预览
  108. * @return void
  109. */
  110. function thumb_gd($thumbwidth, $thumbheight, $preview = 0)
  111. {
  112. if($this->thumbstatus && function_exists('imagecreatetruecolor') && function_exists('imagecopyresampled') && function_exists('imagejpeg'))
  113. {
  114. $imagecreatefromfunc = $this->imagecreatefromfunc;
  115. $imagefunc = $this->thumbstatus == 1 ? 'imagejpeg' : $this->imagefunc;
  116. list($imagewidth, $imageheight) = $this->attachinfo;
  117. if(!$this->animatedgif && ($imagewidth >= $thumbwidth || $imageheight >= $thumbheight))
  118. {
  119. $attach_photo = $imagecreatefromfunc($this->targetfile);
  120. $x_ratio = $thumbwidth / $imagewidth;
  121. $y_ratio = $thumbheight / $imageheight;
  122. if(($x_ratio * $imageheight) < $thumbheight)
  123. {
  124. $thumb['height'] = ceil($x_ratio * $imageheight);
  125. $thumb['width'] = $thumbwidth;
  126. }
  127. else
  128. {
  129. $thumb['width'] = ceil($y_ratio * $imagewidth);
  130. $thumb['height'] = $thumbheight;
  131. }
  132. $targetfile = !$preview ? ($this->thumbstatus == 1 ? $this->targetfile.'.thumb.jpg' : $this->targetfile) : './watermark_tmp.jpg';
  133. $thumb_photo = imagecreatetruecolor($thumb['width'], $thumb['height']);
  134. imagecopyresampled($thumb_photo, $attach_photo, 0, 0, 0, 0, $thumb['width'], $thumb['height'], $imagewidth, $imageheight);
  135. if($this->attachinfo['mime'] == 'image/jpeg')
  136. {
  137. $imagefunc($thumb_photo, $targetfile, 100);
  138. }
  139. else
  140. {
  141. $imagefunc($thumb_photo, $targetfile);
  142. }
  143. $this->attach['thumb'] = $this->thumbstatus == 1 ? 1 : 0;
  144. }
  145. }
  146. }
  147. /**
  148. * 使用gd进行水印
  149. *
  150. * @access public
  151. * @param int $preview 是否预览
  152. * @return void
  153. */
  154. function watermark_gd($preview = 0)
  155. {
  156. if($this->watermarkstatus && function_exists('imagecopy') && function_exists('imagealphablending') && function_exists('imagecopymerge'))
  157. {
  158. $imagecreatefunc = $this->imagecreatefromfunc;
  159. $imagefunc = $this->imagefunc;
  160. list($imagewidth, $imageheight) = $this->attachinfo;
  161. if($this->watermarktype < 2)
  162. {
  163. $watermark_file = $this->watermarktype == 1 ? DEDEDATA.'/mark/mark.png' : DEDEDATA.'/mark/mark.gif';
  164. $watermarkinfo = @getimagesize($watermark_file);
  165. $watermark_logo = $this->watermarktype == 1 ? @imagecreatefrompng($watermark_file) : @imagecreatefromgif($watermark_file);
  166. if(!$watermark_logo)
  167. {
  168. return ;
  169. }
  170. list($logowidth, $logoheight) = $watermarkinfo;
  171. }
  172. else
  173. {
  174. $box = @imagettfbbox($this->watermarktext['size'], $this->watermarktext['angle'], $this->watermarktext['fontpath'],$this->watermarktext['text']);
  175. $logowidth = max($box[2], $box[4]) - min($box[0], $box[6]);
  176. $logoheight = max($box[1], $box[3]) - min($box[5], $box[7]);
  177. $ax = min($box[0], $box[6]) * -1;
  178. $ay = min($box[5], $box[7]) * -1;
  179. }
  180. $wmwidth = $imagewidth - $logowidth;
  181. $wmheight = $imageheight - $logoheight;
  182. if(($this->watermarktype < 2 && is_readable($watermark_file) || $this->watermarktype == 2) && $wmwidth > 10 && $wmheight > 10 && !$this->animatedgif)
  183. {
  184. switch($this->watermarkstatus)
  185. {
  186. case 1:
  187. $x = +5;
  188. $y = +5;
  189. break;
  190. case 2:
  191. $x = ($imagewidth - $logowidth) / 2;
  192. $y = +5;
  193. break;
  194. case 3:
  195. $x = $imagewidth - $logowidth - 5;
  196. $y = +5;
  197. break;
  198. case 4:
  199. $x = +5;
  200. $y = ($imageheight - $logoheight) / 2;
  201. break;
  202. case 5:
  203. $x = ($imagewidth - $logowidth) / 2;
  204. $y = ($imageheight - $logoheight) / 2;
  205. break;
  206. case 6:
  207. $x = $imagewidth - $logowidth - 5;
  208. $y = ($imageheight - $logoheight) / 2;
  209. break;
  210. case 7:
  211. $x = +5;
  212. $y = $imageheight - $logoheight - 5;
  213. break;
  214. case 8:
  215. $x = ($imagewidth - $logowidth) / 2;
  216. $y = $imageheight - $logoheight - 5;
  217. break;
  218. case 9:
  219. $x = $imagewidth - $logowidth - 5;
  220. $y = $imageheight - $logoheight -5;
  221. break;
  222. }
  223. $dst_photo = @imagecreatetruecolor($imagewidth, $imageheight);
  224. $target_photo = $imagecreatefunc($this->targetfile);
  225. imagecopy($dst_photo, $target_photo, 0, 0, 0, 0, $imagewidth, $imageheight);
  226. if($this->watermarktype == 1)
  227. {
  228. imagecopy($dst_photo, $watermark_logo, $x, $y, 0, 0, $logowidth, $logoheight);
  229. }
  230. elseif($this->watermarktype == 2)
  231. {
  232. if(($this->watermarktext['shadowx'] || $this->watermarktext['shadowy']) && $this->watermarktext['shadowcolor'])
  233. {
  234. $shadowcolorrgb = explode(',', $this->watermarktext['shadowcolor']);
  235. $shadowcolor = imagecolorallocate($dst_photo, $shadowcolorrgb[0], $shadowcolorrgb[1], $shadowcolorrgb[2]);
  236. imagettftext($dst_photo, $this->watermarktext['size'], $this->watermarktext['angle'],
  237. $x + $ax + $this->watermarktext['shadowx'], $y + $ay + $this->watermarktext['shadowy'], $shadowcolor,
  238. $this->watermarktext['fontpath'], $this->watermarktext['text']);
  239. }
  240. $colorrgb = explode(',', $this->watermarktext['color']);
  241. $color = imagecolorallocate($dst_photo, $colorrgb[0], $colorrgb[1], $colorrgb[2]);
  242. imagettftext($dst_photo, $this->watermarktext['size'], $this->watermarktext['angle'],
  243. $x + $ax, $y + $ay, $color, $this->watermarktext['fontpath'], $this->watermarktext['text']);
  244. }
  245. else
  246. {
  247. imagealphablending($watermark_logo, true);
  248. imagecopymerge($dst_photo, $watermark_logo, $x, $y, 0, 0, $logowidth, $logoheight, $this->watermarktrans);
  249. }
  250. $targetfile = !$preview ? $this->targetfile : './watermark_tmp.jpg';
  251. if($this->attachinfo['mime'] == 'image/jpeg')
  252. {
  253. $imagefunc($dst_photo, $targetfile, $this->watermarkquality);
  254. }
  255. else
  256. {
  257. $imagefunc($dst_photo, $targetfile);
  258. }
  259. $this->attach['size'] = filesize($this->targetfile);
  260. }
  261. }
  262. }
  263. }//End Class