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

271 lines
13KB

  1. <?php
  2. if (!defined('DEDEINC')) exit ('dedebiz');
  3. /**
  4. * 图像处理
  5. *
  6. * @version $id:image.class.php 18:10 2010年7月5日 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(
  80. $this->targetfile,
  81. $this->attach,
  82. $this->watermarktext,
  83. $this->watermarkstatus,
  84. $this->watermarktrans,
  85. $this->watermarkminheight,
  86. $this->watermarkminwidth,
  87. $this->watermarktype,
  88. $this->watermarktrans,
  89. true,
  90. $this->attach
  91. );
  92. $this->attach['size'] = filesize($this->targetfile);
  93. }
  94. }
  95. /**
  96. * 图片水印
  97. *
  98. * @access public
  99. * @param int $preview 是否预览
  100. * @return void
  101. */
  102. function watermark($preview = 0)
  103. {
  104. if ($this->watermarkminwidth && $this->attachinfo[0] <= $this->watermarkminwidth && $this->watermarkminheight && $this->attachinfo[1] <= $this->watermarkminheight) {
  105. return;
  106. }
  107. $this->watermark_gd($preview);
  108. }
  109. /**
  110. * 使用gd生成缩略图
  111. *
  112. * @access public
  113. * @param int $thumbwidth 图片宽度
  114. * @param int $thumbheight 图片高度
  115. * @param int $preview 是否预览
  116. * @return void
  117. */
  118. function thumb_gd($thumbwidth, $thumbheight, $preview = 0)
  119. {
  120. if ($this->thumbstatus && function_exists('imagecreatetruecolor') && function_exists('imagecopyresampled') && function_exists('imagejpeg')) {
  121. $imagecreatefromfunc = $this->imagecreatefromfunc;
  122. $imagefunc = $this->thumbstatus == 1 ? 'imagejpeg' : $this->imagefunc;
  123. list($imagewidth, $imageheight) = $this->attachinfo;
  124. if (!$this->animatedgif && ($imagewidth >= $thumbwidth || $imageheight >= $thumbheight)) {
  125. $attach_photo = $imagecreatefromfunc($this->targetfile);
  126. $x_ratio = $thumbwidth / $imagewidth;
  127. $y_ratio = $thumbheight / $imageheight;
  128. if (($x_ratio * $imageheight) < $thumbheight) {
  129. $thumb['height'] = ceil($x_ratio * $imageheight);
  130. $thumb['width'] = $thumbwidth;
  131. } else {
  132. $thumb['width'] = ceil($y_ratio * $imagewidth);
  133. $thumb['height'] = $thumbheight;
  134. }
  135. $targetfile = !$preview ? ($this->thumbstatus == 1 ? $this->targetfile.'.thumb.jpg' : $this->targetfile) : './watermark_tmp.jpg';
  136. $thumb_photo = imagecreatetruecolor($thumb['width'], $thumb['height']);
  137. $alpha = imagecolorallocatealpha($thumb_photo, 0, 0, 0, 127);
  138. imagefill($thumb_photo, 0, 0, $alpha);
  139. imagealphablending($thumb_photo, true);
  140. imagesavealpha($thumb_photo, true);
  141. imagecopyresampled($thumb_photo, $attach_photo, 0, 0, 0, 0, $thumb['width'], $thumb['height'], $imagewidth, $imageheight);
  142. if ($this->attachinfo['mime'] == 'image/jpeg') {
  143. $imagefunc($thumb_photo, $targetfile, 100);
  144. } else {
  145. $imagefunc($thumb_photo, $targetfile);
  146. }
  147. $this->attach['thumb'] = $this->thumbstatus == 1 ? 1 : 0;
  148. }
  149. }
  150. }
  151. /**
  152. * 使用gd进行水印
  153. *
  154. * @access public
  155. * @param int $preview 是否预览
  156. * @return void
  157. */
  158. function watermark_gd($preview = 0)
  159. {
  160. if ($this->watermarkstatus && function_exists('imagecopy') && function_exists('imagealphablending') && function_exists('imagecopymerge')) {
  161. $imagecreatefunc = $this->imagecreatefromfunc;
  162. $imagefunc = $this->imagefunc;
  163. list($imagewidth, $imageheight) = $this->attachinfo;
  164. if ($this->watermarktype < 2) {
  165. $watermark_file = $this->watermarktype == 1 ? DEDEDATA.'/mark/mark.png' : DEDEDATA.'/mark/mark.gif';
  166. $watermarkinfo = @getimagesize($watermark_file);
  167. $watermark_logo = $this->watermarktype == 1 ? @imagecreatefrompng($watermark_file) : @imagecreatefromgif ($watermark_file);
  168. if (!$watermark_logo) {
  169. return;
  170. }
  171. list($logowidth, $logoheight) = $watermarkinfo;
  172. } else {
  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. switch ($this->watermarkstatus) {
  183. case 1:
  184. $x = +5;
  185. $y = +5;
  186. break;
  187. case 2:
  188. $x = ($imagewidth - $logowidth) / 2;
  189. $y = +5;
  190. break;
  191. case 3:
  192. $x = $imagewidth - $logowidth - 5;
  193. $y = +5;
  194. break;
  195. case 4:
  196. $x = +5;
  197. $y = ($imageheight - $logoheight) / 2;
  198. break;
  199. case 5:
  200. $x = ($imagewidth - $logowidth) / 2;
  201. $y = ($imageheight - $logoheight) / 2;
  202. break;
  203. case 6:
  204. $x = $imagewidth - $logowidth - 5;
  205. $y = ($imageheight - $logoheight) / 2;
  206. break;
  207. case 7:
  208. $x = +5;
  209. $y = $imageheight - $logoheight - 5;
  210. break;
  211. case 8:
  212. $x = ($imagewidth - $logowidth) / 2;
  213. $y = $imageheight - $logoheight - 5;
  214. break;
  215. case 9:
  216. $x = $imagewidth - $logowidth - 5;
  217. $y = $imageheight - $logoheight - 5;
  218. break;
  219. }
  220. $dst_photo = @imagecreatetruecolor($imagewidth, $imageheight);
  221. $alpha = imagecolorallocatealpha($dst_photo, 0, 0, 0, 127);
  222. imagefill($dst_photo, 0, 0, $alpha);
  223. imagealphablending($dst_photo, true);
  224. imagesavealpha($dst_photo, true);
  225. $target_photo = $imagecreatefunc($this->targetfile);
  226. imagecopy($dst_photo, $target_photo, 0, 0, 0, 0, $imagewidth, $imageheight);
  227. if ($this->watermarktype == 1) {
  228. imagecopy($dst_photo, $watermark_logo, $x, $y, 0, 0, $logowidth, $logoheight);
  229. } elseif ($this->watermarktype == 2) {
  230. if (($this->watermarktext['shadowx'] || $this->watermarktext['shadowy']) && $this->watermarktext['shadowcolor']) {
  231. $shadowcolorrgb = explode(',', $this->watermarktext['shadowcolor']);
  232. $shadowcolor = imagecolorallocate($dst_photo, $shadowcolorrgb[0], $shadowcolorrgb[1], $shadowcolorrgb[2]);
  233. imagettftext(
  234. $dst_photo,
  235. $this->watermarktext['size'],
  236. $this->watermarktext['angle'],
  237. $x + $ax + $this->watermarktext['shadowx'],
  238. $y + $ay + $this->watermarktext['shadowy'],
  239. $shadowcolor,
  240. $this->watermarktext['fontpath'],
  241. $this->watermarktext['text']
  242. );
  243. }
  244. $colorrgb = explode(',', $this->watermarktext['color']);
  245. $color = imagecolorallocate($dst_photo, $colorrgb[0], $colorrgb[1], $colorrgb[2]);
  246. imagettftext(
  247. $dst_photo,
  248. $this->watermarktext['size'],
  249. $this->watermarktext['angle'],
  250. $x + $ax,
  251. $y + $ay,
  252. $color,
  253. $this->watermarktext['fontpath'],
  254. $this->watermarktext['text']
  255. );
  256. } else {
  257. imagealphablending($watermark_logo, true);
  258. imagecopymerge($dst_photo, $watermark_logo, $x, $y, 0, 0, $logowidth, $logoheight, $this->watermarktrans);
  259. }
  260. $targetfile = !$preview ? $this->targetfile : './watermark_tmp.jpg';
  261. if ($this->attachinfo['mime'] == 'image/jpeg') {
  262. $imagefunc($dst_photo, $targetfile, $this->watermarkquality);
  263. } else {
  264. $imagefunc($dst_photo, $targetfile);
  265. }
  266. $this->attach['size'] = filesize($this->targetfile);
  267. }
  268. }
  269. }
  270. }//End Class
  271. ?>