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

262 lines
12KB

  1. <?php
  2. if (!defined('DEDEINC')) exit('dedebiz');
  3. /**
  4. * 图像处理类
  5. *
  6. * @version $Id: image.class.php 1 18:10 2010年7月5日Z tianya $
  7. * @package DedeBIZ.Libraries
  8. * @copyright Copyright (c) 2022, DedeBIZ.COM
  9. * @license https://www.dedebiz.com/license
  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. 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. $fp = fopen($targetfile, 'rb');
  61. $targetfilecontent = fread($fp, $this->attach['size']);
  62. fclose($fp);
  63. $this->animatedgif = strpos($targetfilecontent, 'NETSCAPE2.0') === false ? 0 : 1;
  64. }
  65. }
  66. /**
  67. * 生成缩略图
  68. *
  69. * @access public
  70. * @param int $thumbwidth 图片宽度
  71. * @param int $thumbheight 图片高度
  72. * @param int $preview 是否预览
  73. * @return void
  74. */
  75. function thumb($thumbwidth, $thumbheight, $preview = 0)
  76. {
  77. $this->thumb_gd($thumbwidth, $thumbheight, $preview);
  78. if ($this->thumbstatus == 2 && $this->watermarkstatus) {
  79. $this->image($this->targetfile, $this->attach);
  80. $this->attach['size'] = filesize($this->targetfile);
  81. }
  82. }
  83. /**
  84. * 图片水印
  85. *
  86. * @access public
  87. * @param int $preview 是否预览
  88. * @return void
  89. */
  90. function watermark($preview = 0)
  91. {
  92. if ($this->watermarkminwidth && $this->attachinfo[0] <= $this->watermarkminwidth && $this->watermarkminheight && $this->attachinfo[1] <= $this->watermarkminheight) {
  93. return;
  94. }
  95. $this->watermark_gd($preview);
  96. }
  97. /**
  98. * 使用gd生成缩略图
  99. *
  100. * @access public
  101. * @param int $thumbwidth 图片宽度
  102. * @param int $thumbheight 图片高度
  103. * @param int $preview 是否预览
  104. * @return void
  105. */
  106. function thumb_gd($thumbwidth, $thumbheight, $preview = 0)
  107. {
  108. if ($this->thumbstatus && function_exists('imagecreatetruecolor') && function_exists('imagecopyresampled') && function_exists('imagejpeg')) {
  109. $imagecreatefromfunc = $this->imagecreatefromfunc;
  110. $imagefunc = $this->thumbstatus == 1 ? 'imagejpeg' : $this->imagefunc;
  111. list($imagewidth, $imageheight) = $this->attachinfo;
  112. if (!$this->animatedgif && ($imagewidth >= $thumbwidth || $imageheight >= $thumbheight)) {
  113. $attach_photo = $imagecreatefromfunc($this->targetfile);
  114. $x_ratio = $thumbwidth / $imagewidth;
  115. $y_ratio = $thumbheight / $imageheight;
  116. if (($x_ratio * $imageheight) < $thumbheight) {
  117. $thumb['height'] = ceil($x_ratio * $imageheight);
  118. $thumb['width'] = $thumbwidth;
  119. } else {
  120. $thumb['width'] = ceil($y_ratio * $imagewidth);
  121. $thumb['height'] = $thumbheight;
  122. }
  123. $targetfile = !$preview ? ($this->thumbstatus == 1 ? $this->targetfile.'.thumb.jpg' : $this->targetfile) : './watermark_tmp.jpg';
  124. $thumb_photo = imagecreatetruecolor($thumb['width'], $thumb['height']);
  125. imagecopyresampled($thumb_photo, $attach_photo, 0, 0, 0, 0, $thumb['width'], $thumb['height'], $imagewidth, $imageheight);
  126. if ($this->attachinfo['mime'] == 'image/jpeg') {
  127. $imagefunc($thumb_photo, $targetfile, 100);
  128. } else {
  129. $imagefunc($thumb_photo, $targetfile);
  130. }
  131. $this->attach['thumb'] = $this->thumbstatus == 1 ? 1 : 0;
  132. }
  133. }
  134. }
  135. /**
  136. * 使用gd进行水印
  137. *
  138. * @access public
  139. * @param int $preview 是否预览
  140. * @return void
  141. */
  142. function watermark_gd($preview = 0)
  143. {
  144. if ($this->watermarkstatus && function_exists('imagecopy') && function_exists('imagealphablending') && function_exists('imagecopymerge')) {
  145. $imagecreatefunc = $this->imagecreatefromfunc;
  146. $imagefunc = $this->imagefunc;
  147. list($imagewidth, $imageheight) = $this->attachinfo;
  148. if ($this->watermarktype < 2) {
  149. $watermark_file = $this->watermarktype == 1 ? DEDEDATA.'/mark/mark.png' : DEDEDATA.'/mark/mark.gif';
  150. $watermarkinfo = @getimagesize($watermark_file);
  151. $watermark_logo = $this->watermarktype == 1 ? @imagecreatefrompng($watermark_file) : @imagecreatefromgif($watermark_file);
  152. if (!$watermark_logo) {
  153. return;
  154. }
  155. list($logowidth, $logoheight) = $watermarkinfo;
  156. } else {
  157. $box = @imagettfbbox($this->watermarktext['size'], $this->watermarktext['angle'], $this->watermarktext['fontpath'], $this->watermarktext['text']);
  158. $logowidth = max($box[2], $box[4]) - min($box[0], $box[6]);
  159. $logoheight = max($box[1], $box[3]) - min($box[5], $box[7]);
  160. $ax = min($box[0], $box[6]) * -1;
  161. $ay = min($box[5], $box[7]) * -1;
  162. }
  163. $wmwidth = $imagewidth - $logowidth;
  164. $wmheight = $imageheight - $logoheight;
  165. if (($this->watermarktype < 2 && is_readable($watermark_file) || $this->watermarktype == 2) && $wmwidth > 10 && $wmheight > 10 && !$this->animatedgif) {
  166. switch ($this->watermarkstatus) {
  167. case 1:
  168. $x = +5;
  169. $y = +5;
  170. break;
  171. case 2:
  172. $x = ($imagewidth - $logowidth) / 2;
  173. $y = +5;
  174. break;
  175. case 3:
  176. $x = $imagewidth - $logowidth - 5;
  177. $y = +5;
  178. break;
  179. case 4:
  180. $x = +5;
  181. $y = ($imageheight - $logoheight) / 2;
  182. break;
  183. case 5:
  184. $x = ($imagewidth - $logowidth) / 2;
  185. $y = ($imageheight - $logoheight) / 2;
  186. break;
  187. case 6:
  188. $x = $imagewidth - $logowidth - 5;
  189. $y = ($imageheight - $logoheight) / 2;
  190. break;
  191. case 7:
  192. $x = +5;
  193. $y = $imageheight - $logoheight - 5;
  194. break;
  195. case 8:
  196. $x = ($imagewidth - $logowidth) / 2;
  197. $y = $imageheight - $logoheight - 5;
  198. break;
  199. case 9:
  200. $x = $imagewidth - $logowidth - 5;
  201. $y = $imageheight - $logoheight - 5;
  202. break;
  203. }
  204. $dst_photo = @imagecreatetruecolor($imagewidth, $imageheight);
  205. $target_photo = $imagecreatefunc($this->targetfile);
  206. imagecopy($dst_photo, $target_photo, 0, 0, 0, 0, $imagewidth, $imageheight);
  207. if ($this->watermarktype == 1) {
  208. imagecopy($dst_photo, $watermark_logo, $x, $y, 0, 0, $logowidth, $logoheight);
  209. } elseif ($this->watermarktype == 2) {
  210. if (($this->watermarktext['shadowx'] || $this->watermarktext['shadowy']) && $this->watermarktext['shadowcolor']) {
  211. $shadowcolorrgb = explode(',', $this->watermarktext['shadowcolor']);
  212. $shadowcolor = imagecolorallocate($dst_photo, $shadowcolorrgb[0], $shadowcolorrgb[1], $shadowcolorrgb[2]);
  213. imagettftext(
  214. $dst_photo,
  215. $this->watermarktext['size'],
  216. $this->watermarktext['angle'],
  217. $x + $ax + $this->watermarktext['shadowx'],
  218. $y + $ay + $this->watermarktext['shadowy'],
  219. $shadowcolor,
  220. $this->watermarktext['fontpath'],
  221. $this->watermarktext['text']
  222. );
  223. }
  224. $colorrgb = explode(',', $this->watermarktext['color']);
  225. $color = imagecolorallocate($dst_photo, $colorrgb[0], $colorrgb[1], $colorrgb[2]);
  226. imagettftext(
  227. $dst_photo,
  228. $this->watermarktext['size'],
  229. $this->watermarktext['angle'],
  230. $x + $ax,
  231. $y + $ay,
  232. $color,
  233. $this->watermarktext['fontpath'],
  234. $this->watermarktext['text']
  235. );
  236. } else {
  237. imagealphablending($watermark_logo, true);
  238. imagecopymerge($dst_photo, $watermark_logo, $x, $y, 0, 0, $logowidth, $logoheight, $this->watermarktrans);
  239. }
  240. $targetfile = !$preview ? $this->targetfile : './watermark_tmp.jpg';
  241. if ($this->attachinfo['mime'] == 'image/jpeg') {
  242. $imagefunc($dst_photo, $targetfile, $this->watermarkquality);
  243. } else {
  244. $imagefunc($dst_photo, $targetfile);
  245. }
  246. $this->attach['size'] = filesize($this->targetfile);
  247. }
  248. }
  249. }
  250. }//End Class