国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

275 行
13KB

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