PageRenderTime 84ms CodeModel.GetById 2ms RepoModel.GetById 1ms app.codeStats 0ms

/include/image.class.php

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