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

261 lines
12KB

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