Discuz教程网

[二次开发] DZ 的GD类~ 作者:DZ-monkeye 整理加注:haierspi

[复制链接]
authicon dly 发表于 2012-6-23 18:35:58 | 显示全部楼层 |阅读模式
IMAGE类变量含义 Haierspi原创
PHP代码

<?php
//DZ IMAGE类包含变量含义 >Haierspi
$imagelib = 0;
//水印模式 0为使用GD库 1为外置IM处理
$imageimpath=”;
//IM执行路径
$thumbstatus=0;
//缩略图功能状态 0功能不启用 1自动缩略图 2指定大小的缩略图
$thumbwidth = ”;;
//缩略图宽度
$thumbheight = ”;;
//缩略图高度
$watermarkstatus = ”;;
//水印状态
/*
0 不启用缩略图
以下是各个位置对应的值:
1 2 3
4 5 6
7 8 9
比如值为1 则在图片左上角添加水印
*/
$watermarkminwidth = 300;
$watermarkminheight = 300;
// 水印功能开启的最小宽度和最小高度 当低于这个值时 不启用水印
$watermarktrans = 50;
//水印效果融合度 这个对GIF图片有效
$watermarkquality = 100;
//水印图片压缩质量
[separator]

$watermarktype =1;
//水印类型 0 gif ;1 PNG ;2 文本水印
$watermarktext = array();
//文字水印的一些参数 这个变量是ARRAY 值对应以下:
$watermarktext[text] = ”;
//文字水印内容
$watermarktext[fontpath] = ”;
//文字水印字体路径
$watermarktext[size] = ”;
//文字水印字体大小
$watermarktext[angle] = ”;
//文字水印字体角度
$watermarktext[color] = ”;
//文字水印字体颜色
$watermarktext[shadowx] = ”;
//文字水印投影X偏移
$watermarktext[shadowy] = ”;
//文字水印投影Y偏移
$watermarktext[shadowcolor] = ”;
//文字水印投影颜色

//IM 变量含义 略… 自己想吧 呵呵
?>

IMAGE 类代码
PHP代码
  1. class Image {
  2. var $attachinfo = '';
  3. var $srcfile = '';
  4. var $targetfile = '';
  5. var $imagecreatefromfunc = '';
  6. var $imagefunc = '';
  7. var $attach = array();
  8. var $animatedgif = 0;
  9. function Image($srcfile, $targetfile, $attach = array()) {
  10. global $imagelib, $watermarktext;
  11. $this->srcfile = $srcfile;
  12. $this->targetfile = $targetfile;
  13. $this->attach = $attach;
  14. $this->attachinfo = @getimagesize($targetfile);//返回图片信息
  15. if(!$imagelib) {
  16. switch($this->attachinfo['mime']) {
  17. case 'image/jpeg':
  18. $this->imagecreatefromfunc = function_exists('imagecreatefromjpeg') ? 'imagecreatefromjpeg' : '';
  19. $this->imagefunc = function_exists('imagejpeg') ? 'imagejpeg' : '';
  20. break;
  21. case 'image/gif':
  22. $this->imagecreatefromfunc = function_exists('imagecreatefromgif') ? 'imagecreatefromgif' : '';
  23. $this->imagefunc = function_exists('imagegif') ? 'imagegif' : '';
  24. break;
  25. case 'image/png':
  26. $this->imagecreatefromfunc = function_exists('imagecreatefrompng') ? 'imagecreatefrompng' : '';
  27. $this->imagefunc = function_exists('imagepng') ? 'imagepng' : '';
  28. break;
  29. }
  30. } else {
  31. $this->imagecreatefromfunc = $this->imagefunc = TRUE;
  32. }
  33. $this->attach['size'] = emptyempty($this->attach['size']) ? @filesize($targetfile) : $this->attach['size'];
  34. //取得图像大小
  35. if($this->attachinfo['mime'] == 'image/gif') {
  36. $fp = fopen($targetfile, 'rb');
  37. $targetfilecontent = fread($fp, $this->attach['size']);
  38. fclose($fp);
  39. $this->animatedgif = strpos($targetfilecontent, 'NETSCAPE2.0') === FALSE ? 0 : 1;
  40. //动画的GIF 里确实有 “NETSCAPE2.0” 这个字符
  41. //静态GIF 里没有 “NETSCAPE2.0” 字符,哈哈 郁闷了我好长时间
  42. }
  43. }
  44. //缩略图函数
  45. function Thumb($thumbwidth, $thumbheight, $preview = 0) {
  46. global $imagelib, $imageimpath, $thumbstatus, $watermarkstatus;
  47. //是否执行水印功能 $imageimpath 这个是ImageMagick的安装路径
  48. $imagelib && $imageimpath ? $this->Thumb_IM($thumbwidth, $thumbheight, $preview) : $this->Thumb_GD($thumbwidth, $thumbheight, $preview);
  49. if($thumbstatus == 2 && $watermarkstatus) {
  50. $this->Image($this->srcfile, $this->targetfile, $this->attach);
  51. $this->attach['size'] = filesize($this->targetfile);
  52. }
  53. }
  54. //水印函数
  55. function Watermark($preview = 0) {
  56. global $imagelib, $imageimpath, $watermarktype, $watermarktext, $watermarkminwidth, $watermarkminheight;
  57. if(($watermarkminwidth && $this->attachinfo[0] <= $watermarkminwidth && $watermarkminheight && $this->attachinfo[1] <= $watermarkminheight) || ($watermarktype == 2 && (!file_exists($watermarktext['fontpath']) || !is_file($watermarktext['fontpath'])))) {
  58. return;
  59. }
  60. $imagelib && $imageimpath ? $this->Watermark_IM($preview) : $this->Watermark_GD($preview);
  61. }
  62. //GD库函数
  63. function Thumb_GD($thumbwidth, $thumbheight, $preview = 0) {
  64. global $thumbstatus;
  65. if($thumbstatus && function_exists('imagecreatetruecolor') && function_exists('imagecopyresampled') && function_exists('imagejpeg')) {
  66. $imagecreatefromfunc = $this->imagecreatefromfunc;
  67. $imagefunc = $thumbstatus == 1 ? 'imagejpeg' : $this->imagefunc;
  68. list($img_w, $img_h) = $this->attachinfo;
  69. if(!$this->animatedgif && ($img_w >= $thumbwidth || $img_h >= $thumbheight)) {
  70. $attach_photo = $imagecreatefromfunc($this->targetfile);
  71. $x_ratio = $thumbwidth / $img_w;
  72. $y_ratio = $thumbheight / $img_h;
  73. if(($x_ratio * $img_h) < $thumbheight) {
  74. $thumb['height'] = ceil($x_ratio * $img_h);
  75. $thumb['width'] = $thumbwidth;
  76. } else {
  77. $thumb['width'] = ceil($y_ratio * $img_w);
  78. $thumb['height'] = $thumbheight;
  79. }
  80. $targetfile = !$preview ? ($thumbstatus == 1 ? $this->targetfile.'' : $this->targetfile) : DISCUZ_ROOT.'./forumdata/watermark_temp.jpg';
  81. $thumb_photo = imagecreatetruecolor($thumb['width'], $thumb['height']);
  82. imageCopyreSampled($thumb_photo, $attach_photo ,0, 0, 0, 0, $thumb['width'], $thumb['height'], $img_w, $img_h);
  83. if($this->attachinfo['mime'] == 'image/jpeg') {
  84. $imagefunc($thumb_photo, $targetfile, 100);
  85. } else {
  86. $imagefunc($thumb_photo, $targetfile);
  87. }
  88. $this->attach['thumb'] = $thumbstatus == 1 ? 1 : 0;
  89. }
  90. }
  91. }
  92. function Watermark_GD($preview = 0) {
  93. global $watermarkstatus, $watermarktype, $watermarktrans, $watermarkquality, $watermarktext;
  94. $watermarkstatus = $GLOBALS['forum']['disablewatermark'] ? 0 : $watermarkstatus;
  95. if($watermarkstatus && function_exists('imagecopy') && function_exists('imagealphablending') && function_exists('imagecopymerge')) {
  96. $imagecreatefromfunc = $this->imagecreatefromfunc;
  97. $imagefunc = $this->imagefunc;
  98. list($img_w, $img_h) = $this->attachinfo;
  99. if($watermarktype < 2) {
  100. $watermark_file = $watermarktype == 1 ? './images/common/watermark.png' : './images/common/watermark.gif';
  101. $watermarkinfo = @getimagesize($watermark_file);
  102. $watermark_logo = $watermarktype == 1 ? @imageCreateFromPNG($watermark_file) : @imageCreateFromGIF($watermark_file);
  103. if(!$watermark_logo) {
  104. return;
  105. }
  106. list($logo_w, $logo_h) = $watermarkinfo;
  107. } else {
  108. $box = imagettfbbox($watermarktext['size'], $watermarktext['angle'], $watermarktext['fontpath'], $watermarktext['text']);
  109. $logo_h = max($box[1], $box[3]) - min($box[5], $box[7]);
  110. $logo_w = max($box[2], $box[4]) - min($box[0], $box[6]);
  111. $ax = min($box[0], $box[6]) * -1;
  112. $ay = min($box[5], $box[7]) * -1;
  113. }
  114. $wmwidth = $img_w - $logo_w;
  115. $wmheight = $img_h - $logo_h;
  116. if(($watermarktype < 2 && is_readable($watermark_file) || $watermarktype == 2) && $wmwidth > 10 && $wmheight > 10 && !$this->animatedgif) {
  117. switch($watermarkstatus) {
  118. case 1:
  119. $x = +5;
  120. $y = +5;
  121. break;
  122. case 2:
  123. $x = ($img_w - $logo_w) / 2;
  124. $y = +5;
  125. break;
  126. case 3:
  127. $x = $img_w - $logo_w - 5;
  128. $y = +5;
  129. break;
  130. case 4:
  131. $x = +5;
  132. $y = ($img_h - $logo_h) / 2;
  133. break;
  134. case 5:
  135. $x = ($img_w - $logo_w) / 2;
  136. $y = ($img_h - $logo_h) / 2;
  137. break;
  138. case 6:
  139. $x = $img_w - $logo_w;
  140. $y = ($img_h - $logo_h) / 2;
  141. break;
  142. case 7:
  143. $x = +5;
  144. $y = $img_h - $logo_h - 5;
  145. break;
  146. case 8:
  147. $x = ($img_w - $logo_w) / 2;
  148. $y = $img_h - $logo_h - 5;
  149. break;
  150. case 9:
  151. $x = $img_w - $logo_w - 5;
  152. $y = $img_h - $logo_h - 5;
  153. break;
  154. }
  155. $dst_photo = imagecreatetruecolor($img_w, $img_h);
  156. $target_photo = @$imagecreatefromfunc($this->targetfile);
  157. imageCopy($dst_photo, $target_photo, 0, 0, 0, 0, $img_w, $img_h);
  158. if($watermarktype == 1) {
  159. imageCopy($dst_photo, $watermark_logo, $x, $y, 0, 0, $logo_w, $logo_h);
  160. } elseif($watermarktype == 2) {
  161. if(($watermarktext['shadowx'] || $watermarktext['shadowy']) && $watermarktext['shadowcolor']) {
  162. $shadowcolorrgb = explode(',', $watermarktext['shadowcolor']);
  163. $shadowcolor = imagecolorallocate($dst_photo, $shadowcolorrgb[0], $shadowcolorrgb[1], $shadowcolorrgb[2]);
  164. imagettftext($dst_photo, $watermarktext['size'], $watermarktext['angle'], $x + $ax + $watermarktext['shadowx'], $y + $ay + $watermarktext['shadowy'], $shadowcolor, $watermarktext['fontpath'], $watermarktext['text']);
  165. }
  166. $colorrgb = explode(',', $watermarktext['color']);
  167. $color = imagecolorallocate($dst_photo, $colorrgb[0], $colorrgb[1], $colorrgb[2]);
  168. imagettftext($dst_photo, $watermarktext['size'], $watermarktext['angle'], $x + $ax, $y + $ay, $color, $watermarktext['fontpath'], $watermarktext['text']);
  169. } else {
  170. imageAlphaBlending($watermark_logo, true);
  171. imageCopyMerge($dst_photo, $watermark_logo, $x, $y, 0, 0, $logo_w, $logo_h, $watermarktrans);
  172. }
  173. $targetfile = !$preview ? $this->targetfile : DISCUZ_ROOT.'./forumdata/watermark_temp.jpg';
  174. if($this->attachinfo['mime'] == 'image/jpeg') {
  175. $imagefunc($dst_photo, $targetfile, $watermarkquality);
  176. } else {
  177. $imagefunc($dst_photo, $targetfile);
  178. }
  179. $this->attach['size'] = filesize($this->targetfile);
  180. }
  181. }
  182. }
  183. //ImageMagick的函数定义 Magickwand
  184. function Thumb_IM($thumbwidth, $thumbheight, $preview = 0) {
  185. global $thumbstatus, $imageimpath;
  186. if($thumbstatus) {
  187. list($img_w, $img_h) = $this->attachinfo;
  188. $targetfile = !$preview ? ($thumbstatus == 1 ? $this->targetfile.'' : $this->targetfile) : DISCUZ_ROOT.'./forumdata/watermark_temp.jpg';
  189. if(!$this->animatedgif && ($img_w >= $thumbwidth || $img_h >= $thumbheight)) {
  190. $exec_str = $imageimpath.'/convert -geometry '.$thumbwidth.'x'.$thumbheight.' '.$this->targetfile." ".$targetfile;
  191. @exec($exec_str, $output, $return);
  192. if(emptyempty($return) && emptyempty($output)) {
  193. $this->attach['thumb'] = $thumbstatus == 1 ? 1 : 0;
  194. }
  195. }
  196. }
  197. }
  198. function Watermark_IM($preview = 0) {
  199. global $watermarkstatus, $watermarktype, $watermarktrans, $watermarkquality, $watermarktext, $imageimpath;
  200. switch($watermarkstatus) {
  201. case 1:
  202. $gravity = 'NorthWest';
  203. break;
  204. case 2:
  205. $gravity = 'North';
  206. break;
  207. case 3:
  208. $gravity = 'NorthEast';
  209. break;
  210. case 4:
  211. $gravity = 'West';
  212. break;
  213. case 5:
  214. $gravity = 'Center';
  215. break;
  216. case 6:
  217. $gravity = 'East';
  218. break;
  219. case 7:
  220. $gravity = 'SouthWest';
  221. break;
  222. case 8:
  223. $gravity = 'South';
  224. break;
  225. case 9:
  226. $gravity = 'SouthEast';
  227. break;
  228. }
  229. $targetfile = !$preview ? $this->targetfile : DISCUZ_ROOT.'./forumdata/watermark_temp.jpg';
  230. if($watermarktype < 2) {
  231. $watermark_file = $watermarktype == 1 ? DISCUZ_ROOT.'./images/common/watermark.png' : DISCUZ_ROOT.'./images/common/watermark.gif';
  232. $exec_str = $imageimpath.'/composite'.
  233. ($watermarktype != 1 && $watermarktrans != '100' ? ' -watermark '.$watermarktrans.'%' : '').
  234. ' -gravity '.$gravity.
  235. ' '.$watermark_file.' '.$this->targetfile.' '.$targetfile;
  236. } else {
  237. $watermarktext['text'] = str_replace(array("\n", "\r", "'"), array('', '', '\''), $watermarktext['text']);
  238. $watermarktext['angle'] = -$watermarktext['angle'];
  239. $translate = $watermarktext['translatex'] || $watermarktext['translatey'] ? ' translate '.$watermarktext['translatex'].','.$watermarktext['translatey'] : '';
  240. $skewX = $watermarktext['skewx'] ? ' skewX '.$watermarktext['skewx'] : '';
  241. $skewY = $watermarktext['skewy'] ? ' skewY '.$watermarktext['skewy'] : '';
  242. $exec_str = $imageimpath.'/convert'.
  243. ' -font "'.$watermarktext['fontpath'].'"'.
  244. ' -pointsize '.$watermarktext['size'].
  245. (($watermarktext['shadowx'] || $watermarktext['shadowy']) && $watermarktext['shadowcolor'] ?
  246. ' -fill "rgb('.$watermarktext['shadowcolor'].')"'.
  247. ' -draw "'.
  248. ' gravity '.$gravity.$translate.$skewX.$skewY.
  249. ' rotate '.$watermarktext['angle'].
  250. ' text '.$watermarktext['shadowx'].','.$watermarktext['shadowy'].' \''.$watermarktext['text'].'\'"' : '').
  251. ' -fill "rgb('.$watermarktext['color'].')"'.
  252. ' -draw "'.
  253. ' gravity '.$gravity.$translate.$skewX.$skewY.
  254. ' rotate '.$watermarktext['angle'].
  255. ' text 0,0 \''.$watermarktext['text'].'\'"'.
  256. ' '.$this->targetfile.' '.$targetfile;
  257. }
  258. @exec($exec_str, $output, $return);
  259. if(emptyempty($return) && emptyempty($output)) {
  260. $this->attach['size'] = filesize($this->targetfile);
  261. }
  262. }
  263. }
复制代码


游客,如果您要查看本帖隐藏内容请回复



上一篇:discuz 7.0/7.1/7.2 和 jQuery 冲突兼容问题
下一篇:linux下安装7zip

相关帖子

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2024-5-12 03:16

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表