PageRenderTime 88ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 1ms

/trunk/jiaju001/bbs/include/image.class.php

https://gitlab.com/BGCX262/zyyhong-svn-to-git
PHP | 294 lines | 261 code | 27 blank | 6 comment | 73 complexity | fbaf9820e0cd069d5046ba1ecaf5ee30 MD5 | raw file
  1. <?
  2. /*
  3. [Discuz!] (C)2001-2007 Comsenz Inc.
  4. This is NOT a freeware, use is subject to license terms
  5. $Id: image.class.php 10590 2007-09-07 08:42:50Z monkey $
  6. */
  7. if(!defined('IN_DISCUZ')) {
  8. exit('Access Denied');
  9. }
  10. class Image {
  11. var $attachinfo = '';
  12. var $srcfile = '';
  13. var $targetfile = '';
  14. var $imagecreatefromfunc = '';
  15. var $imagefunc = '';
  16. var $attach = array();
  17. var $animatedgif = 0;
  18. function Image($srcfile, $targetfile, $attach = array()) {
  19. global $imagelib, $watermarktext;
  20. $this->srcfile = $srcfile;
  21. $this->targetfile = $targetfile;
  22. $this->attach = $attach;
  23. $this->attachinfo = @getimagesize($targetfile);
  24. if(!$imagelib) {
  25. switch($this->attachinfo['mime']) {
  26. case 'image/jpeg':
  27. $this->imagecreatefromfunc = function_exists('imagecreatefromjpeg') ? 'imagecreatefromjpeg' : '';
  28. $this->imagefunc = function_exists('imagejpeg') ? 'imagejpeg' : '';
  29. break;
  30. case 'image/gif':
  31. $this->imagecreatefromfunc = function_exists('imagecreatefromgif') ? 'imagecreatefromgif' : '';
  32. $this->imagefunc = function_exists('imagegif') ? 'imagegif' : '';
  33. break;
  34. case 'image/png':
  35. $this->imagecreatefromfunc = function_exists('imagecreatefrompng') ? 'imagecreatefrompng' : '';
  36. $this->imagefunc = function_exists('imagepng') ? 'imagepng' : '';
  37. break;
  38. }
  39. } else {
  40. $this->imagecreatefromfunc = $this->imagefunc = TRUE;
  41. }
  42. $this->attach['size'] = empty($this->attach['size']) ? @filesize($targetfile) : $this->attach['size'];
  43. if($this->attachinfo['mime'] == 'image/gif') {
  44. $fp = fopen($targetfile, 'rb');
  45. $targetfilecontent = fread($fp, $this->attach['size']);
  46. fclose($fp);
  47. $this->animatedgif = strpos($targetfilecontent, 'NETSCAPE2.0') === FALSE ? 0 : 1;
  48. }
  49. }
  50. function Thumb($thumbwidth, $thumbheight, $preview = 0) {
  51. global $imagelib, $imageimpath, $thumbstatus, $watermarkstatus;
  52. $imagelib && $imageimpath ? $this->Thumb_IM($thumbwidth, $thumbheight, $preview) : $this->Thumb_GD($thumbwidth, $thumbheight, $preview);
  53. if($thumbstatus == 2 && $watermarkstatus) {
  54. $this->Image($this->srcfile, $this->targetfile, $this->attach);
  55. $this->attach['size'] = filesize($this->targetfile);
  56. }
  57. }
  58. function Watermark($preview = 0) {
  59. global $imagelib, $imageimpath, $watermarktype, $watermarktext, $watermarkminwidth, $watermarkminheight;
  60. if(($watermarkminwidth && $this->attachinfo[0] <= $watermarkminwidth && $watermarkminheight && $this->attachinfo[1] <= $watermarkminheight) || ($watermarktype == 2 && (!file_exists($watermarktext['fontpath']) || !is_file($watermarktext['fontpath'])))) {
  61. return;
  62. }
  63. $imagelib && $imageimpath ? $this->Watermark_IM($preview) : $this->Watermark_GD($preview);
  64. }
  65. function Thumb_GD($thumbwidth, $thumbheight, $preview = 0) {
  66. global $thumbstatus;
  67. if($thumbstatus && function_exists('imagecreatetruecolor') && function_exists('imagecopyresampled') && function_exists('imagejpeg')) {
  68. $imagecreatefromfunc = $this->imagecreatefromfunc;
  69. $imagefunc = $thumbstatus == 1 ? 'imagejpeg' : $this->imagefunc;
  70. list($img_w, $img_h) = $this->attachinfo;
  71. if(!$this->animatedgif && ($img_w >= $thumbwidth || $img_h >= $thumbheight)) {
  72. $attach_photo = $imagecreatefromfunc($this->targetfile);
  73. $x_ratio = $thumbwidth / $img_w;
  74. $y_ratio = $thumbheight / $img_h;
  75. if(($x_ratio * $img_h) < $thumbheight) {
  76. $thumb['height'] = ceil($x_ratio * $img_h);
  77. $thumb['width'] = $thumbwidth;
  78. } else {
  79. $thumb['width'] = ceil($y_ratio * $img_w);
  80. $thumb['height'] = $thumbheight;
  81. }
  82. $targetfile = !$preview ? ($thumbstatus == 1 ? $this->targetfile.'.thumb.jpg' : $this->targetfile) : DISCUZ_ROOT.'./forumdata/watermark_temp.jpg';
  83. $thumb_photo = imagecreatetruecolor($thumb['width'], $thumb['height']);
  84. imageCopyreSampled($thumb_photo, $attach_photo ,0, 0, 0, 0, $thumb['width'], $thumb['height'], $img_w, $img_h);
  85. if($this->attachinfo['mime'] == 'image/jpeg') {
  86. $imagefunc($thumb_photo, $targetfile, 100);
  87. } else {
  88. $imagefunc($thumb_photo, $targetfile);
  89. }
  90. $this->attach['thumb'] = $thumbstatus == 1 ? 1 : 0;
  91. }
  92. }
  93. }
  94. function Watermark_GD($preview = 0) {
  95. global $watermarkstatus, $watermarktype, $watermarktrans, $watermarkquality, $watermarktext;
  96. $watermarkstatus = $GLOBALS['forum']['disablewatermark'] ? 0 : $watermarkstatus;
  97. if($watermarkstatus && function_exists('imagecopy') && function_exists('imagealphablending') && function_exists('imagecopymerge')) {
  98. $imagecreatefromfunc = $this->imagecreatefromfunc;
  99. $imagefunc = $this->imagefunc;
  100. list($img_w, $img_h) = $this->attachinfo;
  101. if($watermarktype < 2) {
  102. $watermark_file = $watermarktype == 1 ? './images/common/watermark.png' : './images/common/watermark.gif';
  103. $watermarkinfo = @getimagesize($watermark_file);
  104. $watermark_logo = $watermarktype == 1 ? @imageCreateFromPNG($watermark_file) : @imageCreateFromGIF($watermark_file);
  105. if(!$watermark_logo) {
  106. return;
  107. }
  108. list($logo_w, $logo_h) = $watermarkinfo;
  109. } else {
  110. $watermarktextcvt = pack("H*", $watermarktext['text']);
  111. $box = imagettfbbox($watermarktext['size'], $watermarktext['angle'], $watermarktext['fontpath'], $watermarktextcvt);
  112. $logo_h = max($box[1], $box[3]) - min($box[5], $box[7]);
  113. $logo_w = max($box[2], $box[4]) - min($box[0], $box[6]);
  114. $ax = min($box[0], $box[6]) * -1;
  115. $ay = min($box[5], $box[7]) * -1;
  116. }
  117. $wmwidth = $img_w - $logo_w;
  118. $wmheight = $img_h - $logo_h;
  119. if(($watermarktype < 2 && is_readable($watermark_file) || $watermarktype == 2) && $wmwidth > 10 && $wmheight > 10 && !$this->animatedgif) {
  120. switch($watermarkstatus) {
  121. case 1:
  122. $x = +5;
  123. $y = +5;
  124. break;
  125. case 2:
  126. $x = ($img_w - $logo_w) / 2;
  127. $y = +5;
  128. break;
  129. case 3:
  130. $x = $img_w - $logo_w - 5;
  131. $y = +5;
  132. break;
  133. case 4:
  134. $x = +5;
  135. $y = ($img_h - $logo_h) / 2;
  136. break;
  137. case 5:
  138. $x = ($img_w - $logo_w) / 2;
  139. $y = ($img_h - $logo_h) / 2;
  140. break;
  141. case 6:
  142. $x = $img_w - $logo_w;
  143. $y = ($img_h - $logo_h) / 2;
  144. break;
  145. case 7:
  146. $x = +5;
  147. $y = $img_h - $logo_h - 5;
  148. break;
  149. case 8:
  150. $x = ($img_w - $logo_w) / 2;
  151. $y = $img_h - $logo_h - 5;
  152. break;
  153. case 9:
  154. $x = $img_w - $logo_w - 5;
  155. $y = $img_h - $logo_h - 5;
  156. break;
  157. }
  158. $dst_photo = imagecreatetruecolor($img_w, $img_h);
  159. $target_photo = @$imagecreatefromfunc($this->targetfile);
  160. imageCopy($dst_photo, $target_photo, 0, 0, 0, 0, $img_w, $img_h);
  161. if($watermarktype == 1) {
  162. imageCopy($dst_photo, $watermark_logo, $x, $y, 0, 0, $logo_w, $logo_h);
  163. } elseif($watermarktype == 2) {
  164. if(($watermarktext['shadowx'] || $watermarktext['shadowy']) && $watermarktext['shadowcolor']) {
  165. $shadowcolorrgb = explode(',', $watermarktext['shadowcolor']);
  166. $shadowcolor = imagecolorallocate($dst_photo, $shadowcolorrgb[0], $shadowcolorrgb[1], $shadowcolorrgb[2]);
  167. imagettftext($dst_photo, $watermarktext['size'], $watermarktext['angle'], $x + $ax + $watermarktext['shadowx'], $y + $ay + $watermarktext['shadowy'], $shadowcolor, $watermarktext['fontpath'], $watermarktextcvt);
  168. }
  169. $colorrgb = explode(',', $watermarktext['color']);
  170. $color = imagecolorallocate($dst_photo, $colorrgb[0], $colorrgb[1], $colorrgb[2]);
  171. imagettftext($dst_photo, $watermarktext['size'], $watermarktext['angle'], $x + $ax, $y + $ay, $color, $watermarktext['fontpath'], $watermarktextcvt);
  172. } else {
  173. imageAlphaBlending($watermark_logo, true);
  174. imageCopyMerge($dst_photo, $watermark_logo, $x, $y, 0, 0, $logo_w, $logo_h, $watermarktrans);
  175. }
  176. $targetfile = !$preview ? $this->targetfile : DISCUZ_ROOT.'./forumdata/watermark_temp.jpg';
  177. if($this->attachinfo['mime'] == 'image/jpeg') {
  178. $imagefunc($dst_photo, $targetfile, $watermarkquality);
  179. } else {
  180. $imagefunc($dst_photo, $targetfile);
  181. }
  182. $this->attach['size'] = filesize($this->targetfile);
  183. }
  184. }
  185. }
  186. function Thumb_IM($thumbwidth, $thumbheight, $preview = 0) {
  187. global $thumbstatus, $imageimpath;
  188. if($thumbstatus) {
  189. list($img_w, $img_h) = $this->attachinfo;
  190. $targetfile = !$preview ? ($thumbstatus == 1 ? $this->targetfile.'.thumb.jpg' : $this->targetfile) : DISCUZ_ROOT.'./forumdata/watermark_temp.jpg';
  191. if(!$this->animatedgif && ($img_w >= $thumbwidth || $img_h >= $thumbheight)) {
  192. $exec_str = $imageimpath.'/convert -geometry '.$thumbwidth.'x'.$thumbheight.' '.$this->targetfile." ".$targetfile;
  193. @exec($exec_str, $output, $return);
  194. if(empty($return) && empty($output)) {
  195. $this->attach['thumb'] = $thumbstatus == 1 ? 1 : 0;
  196. }
  197. }
  198. }
  199. }
  200. function Watermark_IM($preview = 0) {
  201. global $watermarkstatus, $watermarktype, $watermarktrans, $watermarkquality, $watermarktext, $imageimpath;
  202. switch($watermarkstatus) {
  203. case 1:
  204. $gravity = 'NorthWest';
  205. break;
  206. case 2:
  207. $gravity = 'North';
  208. break;
  209. case 3:
  210. $gravity = 'NorthEast';
  211. break;
  212. case 4:
  213. $gravity = 'West';
  214. break;
  215. case 5:
  216. $gravity = 'Center';
  217. break;
  218. case 6:
  219. $gravity = 'East';
  220. break;
  221. case 7:
  222. $gravity = 'SouthWest';
  223. break;
  224. case 8:
  225. $gravity = 'South';
  226. break;
  227. case 9:
  228. $gravity = 'SouthEast';
  229. break;
  230. }
  231. $targetfile = !$preview ? $this->targetfile : DISCUZ_ROOT.'./forumdata/watermark_temp.jpg';
  232. if($watermarktype < 2) {
  233. $watermark_file = $watermarktype == 1 ? DISCUZ_ROOT.'./images/common/watermark.png' : DISCUZ_ROOT.'./images/common/watermark.gif';
  234. $exec_str = $imageimpath.'/composite'.
  235. ($watermarktype != 1 && $watermarktrans != '100' ? ' -watermark '.$watermarktrans.'%' : '').
  236. ' -gravity '.$gravity.
  237. ' '.$watermark_file.' '.$this->targetfile.' '.$targetfile;
  238. } else {
  239. $watermarktextcvt = str_replace(array("\n", "\r", "'"), array('', '', '\''), pack("H*", $watermarktext['text']));
  240. $watermarktext['angle'] = -$watermarktext['angle'];
  241. $translate = $watermarktext['translatex'] || $watermarktext['translatey'] ? ' translate '.$watermarktext['translatex'].','.$watermarktext['translatey'] : '';
  242. $skewX = $watermarktext['skewx'] ? ' skewX '.$watermarktext['skewx'] : '';
  243. $skewY = $watermarktext['skewy'] ? ' skewY '.$watermarktext['skewy'] : '';
  244. $exec_str = $imageimpath.'/convert'.
  245. ' -font "'.$watermarktext['fontpath'].'"'.
  246. ' -pointsize '.$watermarktext['size'].
  247. (($watermarktext['shadowx'] || $watermarktext['shadowy']) && $watermarktext['shadowcolor'] ?
  248. ' -fill "rgb('.$watermarktext['shadowcolor'].')"'.
  249. ' -draw "'.
  250. ' gravity '.$gravity.$translate.$skewX.$skewY.
  251. ' rotate '.$watermarktext['angle'].
  252. ' text '.$watermarktext['shadowx'].','.$watermarktext['shadowy'].' \''.$watermarktextcvt.'\'"' : '').
  253. ' -fill "rgb('.$watermarktext['color'].')"'.
  254. ' -draw "'.
  255. ' gravity '.$gravity.$translate.$skewX.$skewY.
  256. ' rotate '.$watermarktext['angle'].
  257. ' text 0,0 \''.$watermarktextcvt.'\'"'.
  258. ' '.$this->targetfile.' '.$targetfile;
  259. }
  260. @exec($exec_str, $output, $return);
  261. if(empty($return) && empty($output)) {
  262. $this->attach['size'] = filesize($this->targetfile);
  263. }
  264. }
  265. }