国内流行的内容管理系统(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.

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