class_image.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Comsenz Inc.
  4. * This is NOT a freeware, use is subject to license terms
  5. *
  6. * $Id: class_image.php 36349 2017-01-16 03:05:23Z nemohou $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. class image {
  12. var $source = '';
  13. var $target = '';
  14. var $imginfo = array();
  15. var $imagecreatefromfunc = '';
  16. var $imagefunc = '';
  17. var $tmpfile = '';
  18. var $libmethod = 0;
  19. var $param = array();
  20. var $errorcode = 0;
  21. var $extension = array();
  22. function __construct() {
  23. global $_G;
  24. $this->extension['gd'] = extension_loaded('gd');
  25. $this->extension['imagick'] = extension_loaded('imagick');
  26. $this->param = array(
  27. 'imagelib' => $_G['setting']['imagelib'],
  28. 'imageimpath' => $_G['setting']['imageimpath'],
  29. 'thumbquality' => $_G['setting']['thumbquality'],
  30. 'watermarkstatus' => dunserialize($_G['setting']['watermarkstatus']),
  31. 'watermarkminwidth' => dunserialize($_G['setting']['watermarkminwidth']),
  32. 'watermarkminheight' => dunserialize($_G['setting']['watermarkminheight']),
  33. 'watermarktype' => $_G['setting']['watermarktype'],
  34. 'watermarktext' => $_G['setting']['watermarktext'],
  35. 'watermarktrans' => dunserialize($_G['setting']['watermarktrans']),
  36. 'watermarkquality' => dunserialize($_G['setting']['watermarkquality']),
  37. );
  38. }
  39. function Thumb($source, $target, $thumbwidth, $thumbheight, $thumbtype = 1, $nosuffix = 0) {
  40. $return = $this->init('thumb', $source, $target, $nosuffix);
  41. if($return <= 0) {
  42. return $this->returncode($return);
  43. }
  44. if($this->imginfo['animated']) {
  45. return $this->returncode(0);
  46. }
  47. $this->param['thumbwidth'] = intval($thumbwidth);
  48. if(!$thumbheight || $thumbheight > $this->imginfo['height']) {
  49. $thumbheight = $thumbwidth > $this->imginfo['width'] ? $this->imginfo['height'] : $this->imginfo['height']*($thumbwidth/$this->imginfo['width']);
  50. }
  51. $this->param['thumbheight'] = intval($thumbheight);
  52. $this->param['thumbtype'] = $thumbtype;
  53. if($thumbwidth < 100 && $thumbheight < 100) {
  54. $this->param['thumbquality'] = 100;
  55. }
  56. $return = !$this->libmethod ? $this->Thumb_GD() : $this->Thumb_IM();
  57. $return = !$nosuffix ? $return : 0;
  58. return $this->sleep($return);
  59. }
  60. function Cropper($source, $target, $dstwidth, $dstheight, $srcx = 0, $srcy = 0, $srcwidth = 0, $srcheight = 0) {
  61. $return = $this->init('thumb', $source, $target, 1);
  62. if($return <= 0) {
  63. return $this->returncode($return);
  64. }
  65. if($dstwidth < 0 || $dstheight < 0) {
  66. return $this->returncode(false);
  67. }
  68. $this->param['dstwidth'] = intval($dstwidth);
  69. $this->param['dstheight'] = intval($dstheight);
  70. $this->param['srcx'] = intval($srcx);
  71. $this->param['srcy'] = intval($srcy);
  72. $this->param['srcwidth'] = intval($srcwidth ? $srcwidth : $dstwidth);
  73. $this->param['srcheight'] = intval($srcheight ? $srcheight : $dstheight);
  74. $return = !$this->libmethod ? $this->Cropper_GD() : $this->Cropper_IM();
  75. }
  76. function Watermark($source, $target = '', $type = 'forum') {
  77. $return = $this->init('watermask', $source, $target);
  78. if($return <= 0) {
  79. return $this->returncode($return);
  80. }
  81. if(!$this->param['watermarkstatus'][$type] || ($this->param['watermarkminwidth'][$type] && $this->imginfo['width'] <= $this->param['watermarkminwidth'][$type] && $this->param['watermarkminheight'][$type] && $this->imginfo['height'] <= $this->param['watermarkminheight'][$type])) {
  82. return $this->returncode(0);
  83. }
  84. $this->param['watermarkfile'][$type] = './static/image/common/'.($this->param['watermarktype'][$type] == 'png' ? 'watermark.png' : 'watermark.gif');
  85. if(!is_readable($this->param['watermarkfile'][$type]) || ($this->param['watermarktype'][$type] == 'text' && (!file_exists($this->param['watermarktext']['fontpath'][$type]) || !is_file($this->param['watermarktext']['fontpath'][$type])))) {
  86. return $this->returncode(-3);
  87. }
  88. $return = !$this->libmethod ? $this->Watermark_GD($type) : $this->Watermark_IM($type);
  89. return $this->sleep($return);
  90. }
  91. function error() {
  92. return $this->errorcode;
  93. }
  94. function init($method, $source, $target, $nosuffix = 0) {
  95. global $_G;
  96. $this->errorcode = 0;
  97. if(empty($source)) {
  98. return -2;
  99. }
  100. $parse = parse_url($source);
  101. if(isset($parse['host'])) {
  102. if(empty($target)) {
  103. return -2;
  104. }
  105. $data = dfsockopen($source);
  106. $this->tmpfile = $source = tempnam($_G['setting']['attachdir'].'./temp/', 'tmpimg_');
  107. if(!$data || $source === FALSE) {
  108. return -2;
  109. }
  110. file_put_contents($source, $data);
  111. }
  112. if($method == 'thumb') {
  113. $target = empty($target) ? (!$nosuffix ? getimgthumbname($source) : $source) : $_G['setting']['attachdir'].'./'.$target;
  114. } elseif($method == 'watermask') {
  115. $target = empty($target) ? $source : $_G['setting']['attachdir'].'./'.$target;
  116. }
  117. $targetpath = dirname($target);
  118. dmkdir($targetpath);
  119. clearstatcache();
  120. if(!is_readable($source) || !is_writable($targetpath)) {
  121. return -2;
  122. }
  123. $imginfo = @getimagesize($source);
  124. if($imginfo === FALSE) {
  125. return -1;
  126. }
  127. $this->source = $source;
  128. $this->target = $target;
  129. $this->imginfo['width'] = $imginfo[0];
  130. $this->imginfo['height'] = $imginfo[1];
  131. $this->imginfo['mime'] = $imginfo['mime'];
  132. $this->imginfo['size'] = @filesize($source);
  133. $this->libmethod = $this->param['imagelib'];
  134. if(!$this->param['imagelib'] && $this->extension['gd']) {
  135. $this->libmethod = 0;
  136. } elseif($this->param['imagelib'] && $this->extension['imagick']) {
  137. $this->libmethod = 1;
  138. } else {
  139. return -4;
  140. }
  141. if(!$this->libmethod) {
  142. switch($this->imginfo['mime']) {
  143. case 'image/jpeg':
  144. $this->imagecreatefromfunc = function_exists('imagecreatefromjpeg') ? 'imagecreatefromjpeg' : '';
  145. $this->imagefunc = function_exists('imagejpeg') ? 'imagejpeg' : '';
  146. break;
  147. case 'image/gif':
  148. $this->imagecreatefromfunc = function_exists('imagecreatefromgif') ? 'imagecreatefromgif' : '';
  149. $this->imagefunc = function_exists('imagegif') ? 'imagegif' : '';
  150. break;
  151. case 'image/png':
  152. $this->imagecreatefromfunc = function_exists('imagecreatefrompng') ? 'imagecreatefrompng' : '';
  153. $this->imagefunc = function_exists('imagepng') ? 'imagepng' : '';
  154. break;
  155. }
  156. } else {
  157. $this->imagecreatefromfunc = $this->imagefunc = TRUE;
  158. }
  159. if(!$this->libmethod && $this->imginfo['mime'] == 'image/gif') {
  160. if(!$this->imagecreatefromfunc) {
  161. return -4;
  162. }
  163. if(!($fp = @fopen($source, 'rb'))) {
  164. return -2;
  165. }
  166. $content = fread($fp, $this->imginfo['size']);
  167. fclose($fp);
  168. $this->imginfo['animated'] = strpos($content, 'NETSCAPE2.0') === FALSE ? 0 : 1;
  169. }
  170. return $this->imagecreatefromfunc ? 1 : -4;
  171. }
  172. function sleep($return) {
  173. if($this->tmpfile) {
  174. @unlink($this->tmpfile);
  175. }
  176. $this->imginfo['size'] = @filesize($this->target);
  177. return $this->returncode($return);
  178. }
  179. function returncode($return) {
  180. if($return > 0 && file_exists($this->target)) {
  181. return true;
  182. } else {
  183. if($this->tmpfile) {
  184. @unlink($this->tmpfile);
  185. }
  186. $this->errorcode = $return;
  187. return false;
  188. }
  189. }
  190. function sizevalue($method) {
  191. $x = $y = $w = $h = 0;
  192. if($method > 0) {
  193. $imgratio = $this->imginfo['width'] / $this->imginfo['height'];
  194. $thumbratio = $this->param['thumbwidth'] / $this->param['thumbheight'];
  195. if($imgratio >= 1 && $imgratio >= $thumbratio || $imgratio < 1 && $imgratio > $thumbratio) {
  196. $h = $this->imginfo['height'];
  197. $w = $h * $thumbratio;
  198. $x = ($this->imginfo['width'] - $thumbratio * $this->imginfo['height']) / 2;
  199. } elseif($imgratio >= 1 && $imgratio <= $thumbratio || $imgratio < 1 && $imgratio <= $thumbratio) {
  200. $w = $this->imginfo['width'];
  201. $h = $w / $thumbratio;
  202. }
  203. } else {
  204. $x_ratio = $this->param['thumbwidth'] / $this->imginfo['width'];
  205. $y_ratio = $this->param['thumbheight'] / $this->imginfo['height'];
  206. if(($x_ratio * $this->imginfo['height']) < $this->param['thumbheight']) {
  207. $h = ceil($x_ratio * $this->imginfo['height']);
  208. $w = $this->param['thumbwidth'];
  209. } else {
  210. $w = ceil($y_ratio * $this->imginfo['width']);
  211. $h = $this->param['thumbheight'];
  212. }
  213. }
  214. return array($x, $y, $w, $h);
  215. }
  216. function loadsource() {
  217. $imagecreatefromfunc = &$this->imagecreatefromfunc;
  218. $im = @$imagecreatefromfunc($this->source);
  219. if(!$im) {
  220. if(!function_exists('imagecreatefromstring')) {
  221. return -4;
  222. }
  223. $fp = @fopen($this->source, 'rb');
  224. $contents = @fread($fp, filesize($this->source));
  225. fclose($fp);
  226. $im = @imagecreatefromstring($contents);
  227. if($im == FALSE) {
  228. return -1;
  229. }
  230. }
  231. return $im;
  232. }
  233. function Thumb_GD() {
  234. if(!function_exists('imagecreatetruecolor') || !function_exists('imagecopyresampled') || !function_exists('imagejpeg') || !function_exists('imagecopymerge')) {
  235. return -4;
  236. }
  237. $imagefunc = &$this->imagefunc;
  238. $attach_photo = $this->loadsource();
  239. if($attach_photo < 0) {
  240. return $attach_photo;
  241. }
  242. $copy_photo = imagecreatetruecolor($this->imginfo['width'], $this->imginfo['height']);
  243. imagecopy($copy_photo, $attach_photo ,0, 0, 0, 0, $this->imginfo['width'], $this->imginfo['height']);
  244. $attach_photo = $copy_photo;
  245. $thumb_photo = null;
  246. switch($this->param['thumbtype']) {
  247. case 'fixnone':
  248. case 1:
  249. if($this->imginfo['width'] >= $this->param['thumbwidth'] || $this->imginfo['height'] >= $this->param['thumbheight']) {
  250. $thumb = array();
  251. list(,,$thumb['width'], $thumb['height']) = $this->sizevalue(0);
  252. $cx = $this->imginfo['width'];
  253. $cy = $this->imginfo['height'];
  254. $thumb_photo = imagecreatetruecolor($thumb['width'], $thumb['height']);
  255. imagecopyresampled($thumb_photo, $attach_photo ,0, 0, 0, 0, $thumb['width'], $thumb['height'], $cx, $cy);
  256. }
  257. break;
  258. case 'fixwr':
  259. case 2:
  260. if(!($this->imginfo['width'] <= $this->param['thumbwidth'] || $this->imginfo['height'] <= $this->param['thumbheight'])) {
  261. list($startx, $starty, $cutw, $cuth) = $this->sizevalue(1);
  262. $dst_photo = imagecreatetruecolor($cutw, $cuth);
  263. imagecopymerge($dst_photo, $attach_photo, 0, 0, $startx, $starty, $cutw, $cuth, 100);
  264. $thumb_photo = imagecreatetruecolor($this->param['thumbwidth'], $this->param['thumbheight']);
  265. imagecopyresampled($thumb_photo, $dst_photo ,0, 0, 0, 0, $this->param['thumbwidth'], $this->param['thumbheight'], $cutw, $cuth);
  266. } else {
  267. $thumb_photo = imagecreatetruecolor($this->param['thumbwidth'], $this->param['thumbheight']);
  268. $bgcolor = imagecolorallocate($thumb_photo, 255, 255, 255);
  269. imagefill($thumb_photo, 0, 0, $bgcolor);
  270. $startx = ($this->param['thumbwidth'] - $this->imginfo['width']) / 2;
  271. $starty = ($this->param['thumbheight'] - $this->imginfo['height']) / 2;
  272. imagecopymerge($thumb_photo, $attach_photo, $startx, $starty, 0, 0, $this->imginfo['width'], $this->imginfo['height'], 100);
  273. }
  274. break;
  275. }
  276. clearstatcache();
  277. if($thumb_photo) {
  278. if($this->imginfo['mime'] == 'image/jpeg') {
  279. @$imagefunc($thumb_photo, $this->target, $this->param['thumbquality']);
  280. } else {
  281. @$imagefunc($thumb_photo, $this->target);
  282. }
  283. return 1;
  284. } else {
  285. return 0;
  286. }
  287. }
  288. function Thumb_IM() {
  289. switch($this->param['thumbtype']) {
  290. case 'fixnone':
  291. case 1:
  292. if($this->imginfo['width'] >= $this->param['thumbwidth'] || $this->imginfo['height'] >= $this->param['thumbheight']) {
  293. $im = new Imagick();
  294. $im->readImage(realpath($this->source));
  295. $im->setImageCompressionQuality($this->param['thumbquality']);
  296. $im->thumbnailImage($this->param['thumbwidth'], $this->param['thumbheight'], true);
  297. if(!$im->writeImage($this->target)) {
  298. $im->destroy();
  299. return -3;
  300. }
  301. $im->destroy();
  302. }
  303. break;
  304. case 'fixwr':
  305. case 2:
  306. if(!($this->imginfo['width'] <= $this->param['thumbwidth'] || $this->imginfo['height'] <= $this->param['thumbheight'])) {
  307. list($startx, $starty, $cutw, $cuth) = $this->sizevalue(1);
  308. $im = new Imagick();
  309. $im->readImage(realpath($this->source));
  310. $im->setImageCompressionQuality($this->param['thumbquality']);
  311. $im->cropImage($cutw, $cuth, $startx, $starty);
  312. if(!$im->writeImage($this->target)) {
  313. $im->destroy();
  314. return -3;
  315. }
  316. $im->readImage(realpath($this->target));
  317. $im->setImageCompressionQuality($this->param['thumbquality']);
  318. $im->thumbnailImage($this->param['thumbwidth'], $this->param['thumbheight']);
  319. $im->resizeImage($this->param['thumbwidth'], $this->param['thumbheight']);
  320. $im->setGravity(imagick::GRAVITY_CENTER );
  321. $im->extentImage($this->param['thumbwidth'], $this->param['thumbheight']);
  322. if(!$im->writeImage($this->target)) {
  323. $im->destroy();
  324. return -3;
  325. }
  326. $im->destroy();
  327. } else {
  328. $startx = -($this->param['thumbwidth'] - $this->imginfo['width']) / 2;
  329. $starty = -($this->param['thumbheight'] - $this->imginfo['height']) / 2;
  330. $im = new Imagick();
  331. $im->readImage(realpath($this->source));
  332. $im->setImageCompressionQuality($this->param['thumbquality']);
  333. $im->cropImage($this->param['thumbwidth'], $this->param['thumbheight'], $startx, $starty);
  334. if(!$im->writeImage($this->target)) {
  335. $im->destroy();
  336. return -3;
  337. }
  338. $im->readImage(realpath($this->target));
  339. $im->setImageCompressionQuality($this->param['thumbquality']);
  340. $im->thumbnailImage($this->param['thumbwidth'], $this->param['thumbheight']);
  341. $im->setGravity(imagick::GRAVITY_CENTER );
  342. $im->extentImage($this->param['thumbwidth'], $this->param['thumbheight']);
  343. if(!$im->writeImage($this->target)) {
  344. $im->destroy();
  345. return -3;
  346. }
  347. $im->destroy();
  348. }
  349. break;
  350. }
  351. return 1;
  352. }
  353. function Cropper_GD() {
  354. $image = $this->loadsource();
  355. if($image < 0) {
  356. return $image;
  357. }
  358. $newimage = imagecreatetruecolor($this->param['dstwidth'], $this->param['dstheight']);
  359. imagecopyresampled($newimage, $image, 0, 0, $this->param['srcx'], $this->param['srcy'], $this->param['dstwidth'], $this->param['dstheight'], $this->param['srcwidth'], $this->param['srcheight']);
  360. ImageJpeg($newimage, $this->target, 100);
  361. imagedestroy($newimage);
  362. imagedestroy($image);
  363. return true;
  364. }
  365. function Cropper_IM() {
  366. $im = new Imagick();
  367. $im->readImage(realpath($this->source));
  368. $im->cropImage($this->param['srcwidth'], $this->param['srcheight'], $this->param['srcx'], $this->param['srcy']);
  369. $im->thumbnailImage($this->param['dstwidth'], $this->param['dstheight']);
  370. $result = $im->writeImage($this->target);
  371. $im->destroy();
  372. if(!$result) {
  373. return -3;
  374. }
  375. }
  376. function Watermark_GD($type = 'forum') {
  377. if(!function_exists('imagecreatetruecolor')) {
  378. return -4;
  379. }
  380. $imagefunc = &$this->imagefunc;
  381. if($this->param['watermarktype'][$type] != 'text') {
  382. if(!function_exists('imagecopy') || !function_exists('imagecreatefrompng') || !function_exists('imagecreatefromgif') || !function_exists('imagealphablending') || !function_exists('imagecopymerge')) {
  383. return -4;
  384. }
  385. $watermarkinfo = @getimagesize($this->param['watermarkfile'][$type]);
  386. if($watermarkinfo === FALSE) {
  387. return -3;
  388. }
  389. $watermark_logo = $this->param['watermarktype'][$type] == 'png' ? @imageCreateFromPNG($this->param['watermarkfile'][$type]) : @imageCreateFromGIF($this->param['watermarkfile'][$type]);
  390. if(!$watermark_logo) {
  391. return 0;
  392. }
  393. list($logo_w, $logo_h) = $watermarkinfo;
  394. } else {
  395. if(!function_exists('imagettfbbox') || !function_exists('imagettftext') || !function_exists('imagecolorallocate')) {
  396. return -4;
  397. }
  398. if(!class_exists('Chinese')) {
  399. include libfile('class/chinese');
  400. }
  401. $watermarktextcvt = pack("H*", $this->param['watermarktext']['text'][$type]);
  402. $box = imagettfbbox($this->param['watermarktext']['size'][$type], $this->param['watermarktext']['angle'][$type], $this->param['watermarktext']['fontpath'][$type], $watermarktextcvt);
  403. $logo_h = max($box[1], $box[3]) - min($box[5], $box[7]);
  404. $logo_w = max($box[2], $box[4]) - min($box[0], $box[6]);
  405. $ax = min($box[0], $box[6]) * -1;
  406. $ay = min($box[5], $box[7]) * -1;
  407. }
  408. $wmwidth = $this->imginfo['width'] - $logo_w;
  409. $wmheight = $this->imginfo['height'] - $logo_h;
  410. if($wmwidth > 10 && $wmheight > 10 && !$this->imginfo['animated']) {
  411. switch($this->param['watermarkstatus'][$type]) {
  412. case 1:
  413. $x = 5;
  414. $y = 5;
  415. break;
  416. case 2:
  417. $x = ($this->imginfo['width'] - $logo_w) / 2;
  418. $y = 5;
  419. break;
  420. case 3:
  421. $x = $this->imginfo['width'] - $logo_w - 5;
  422. $y = 5;
  423. break;
  424. case 4:
  425. $x = 5;
  426. $y = ($this->imginfo['height'] - $logo_h) / 2;
  427. break;
  428. case 5:
  429. $x = ($this->imginfo['width'] - $logo_w) / 2;
  430. $y = ($this->imginfo['height'] - $logo_h) / 2;
  431. break;
  432. case 6:
  433. $x = $this->imginfo['width'] - $logo_w;
  434. $y = ($this->imginfo['height'] - $logo_h) / 2;
  435. break;
  436. case 7:
  437. $x = 5;
  438. $y = $this->imginfo['height'] - $logo_h - 5;
  439. break;
  440. case 8:
  441. $x = ($this->imginfo['width'] - $logo_w) / 2;
  442. $y = $this->imginfo['height'] - $logo_h - 5;
  443. break;
  444. case 9:
  445. $x = $this->imginfo['width'] - $logo_w - 5;
  446. $y = $this->imginfo['height'] - $logo_h - 5;
  447. break;
  448. }
  449. if($this->imginfo['mime'] != 'image/png') {
  450. $color_photo = imagecreatetruecolor($this->imginfo['width'], $this->imginfo['height']);
  451. }
  452. $dst_photo = $this->loadsource();
  453. if($dst_photo < 0) {
  454. return $dst_photo;
  455. }
  456. imagealphablending($dst_photo, true);
  457. imagesavealpha($dst_photo, true);
  458. if($this->imginfo['mime'] != 'image/png') {
  459. imageCopy($color_photo, $dst_photo, 0, 0, 0, 0, $this->imginfo['width'], $this->imginfo['height']);
  460. $dst_photo = $color_photo;
  461. }
  462. if($this->param['watermarktype'][$type] == 'png') {
  463. imageCopy($dst_photo, $watermark_logo, $x, $y, 0, 0, $logo_w, $logo_h);
  464. } elseif($this->param['watermarktype'][$type] == 'text') {
  465. if(($this->param['watermarktext']['shadowx'][$type] || $this->param['watermarktext']['shadowy'][$type]) && $this->param['watermarktext']['shadowcolor'][$type]) {
  466. $shadowcolorrgb = explode(',', $this->param['watermarktext']['shadowcolor'][$type]);
  467. $shadowcolor = imagecolorallocate($dst_photo, $shadowcolorrgb[0], $shadowcolorrgb[1], $shadowcolorrgb[2]);
  468. imagettftext($dst_photo, $this->param['watermarktext']['size'][$type], $this->param['watermarktext']['angle'][$type], $x + $ax + $this->param['watermarktext']['shadowx'][$type], $y + $ay + $this->param['watermarktext']['shadowy'][$type], $shadowcolor, $this->param['watermarktext']['fontpath'][$type], $watermarktextcvt);
  469. }
  470. $colorrgb = explode(',', $this->param['watermarktext']['color'][$type]);
  471. $color = imagecolorallocate($dst_photo, $colorrgb[0], $colorrgb[1], $colorrgb[2]);
  472. imagettftext($dst_photo, $this->param['watermarktext']['size'][$type], $this->param['watermarktext']['angle'][$type], $x + $ax, $y + $ay, $color, $this->param['watermarktext']['fontpath'][$type], $watermarktextcvt);
  473. } else {
  474. imageAlphaBlending($watermark_logo, true);
  475. imageCopyMerge($dst_photo, $watermark_logo, $x, $y, 0, 0, $logo_w, $logo_h, $this->param['watermarktrans'][$type]);
  476. }
  477. clearstatcache();
  478. if($this->imginfo['mime'] == 'image/jpeg') {
  479. @$imagefunc($dst_photo, $this->target, $this->param['watermarkquality'][$type]);
  480. } else {
  481. @$imagefunc($dst_photo, $this->target);
  482. }
  483. }
  484. return 1;
  485. }
  486. function Watermark_IM($type = 'forum') {
  487. switch($this->param['watermarkstatus'][$type]) {
  488. case 1:
  489. $gravity = imagick::GRAVITY_NORTHWEST;
  490. break;
  491. case 2:
  492. $gravity = imagick::GRAVITY_NORTH;
  493. break;
  494. case 3:
  495. $gravity = imagick::GRAVITY_NORTHEAST;
  496. break;
  497. case 4:
  498. $gravity = imagick::GRAVITY_WEST;
  499. break;
  500. case 5:
  501. $gravity = imagick::GRAVITY_CENTER;
  502. break;
  503. case 6:
  504. $gravity = imagick::GRAVITY_EAST;
  505. break;
  506. case 7:
  507. $gravity = imagick::GRAVITY_SOUTHWEST;
  508. break;
  509. case 8:
  510. $gravity = imagick::GRAVITY_SOUTH;
  511. break;
  512. case 9:
  513. $gravity = imagick::GRAVITY_SOUTHEAST;
  514. break;
  515. }
  516. if($this->param['watermarktype'][$type] != 'text') {
  517. $watermark = new Imagick(realpath($this->param['watermarkfile'][$type]));
  518. if($this->param['watermarktype'][$type] != 'png' && $this->param['watermarktrans'][$type] != '100') {
  519. $watermark->setImageOpacity($this->param['watermarktrans'][$type]);
  520. }
  521. $canvas = new Imagick(realpath($this->source));
  522. $canvas->setImageCompressionQuality($this->param['watermarkquality'][$type]);
  523. $dw = new ImagickDraw();
  524. $dw->setGravity($gravity);
  525. $dw->composite($watermark->getImageCompose(), 0, 0, 0, 0, $watermark);
  526. $canvas->drawImage($dw);
  527. $result = $canvas->writeImage($this->target);
  528. $watermark->destroy();
  529. $canvas->destroy();
  530. $dw->destroy();
  531. if(!$result) {
  532. return -3;
  533. }
  534. } else {
  535. $watermarktextcvt = escapeshellcmd(pack("H*", $this->param['watermarktext']['text'][$type]));
  536. $angle = -$this->param['watermarktext']['angle'][$type];
  537. $translate = $this->param['watermarktext']['translatex'][$type] || $this->param['watermarktext']['translatey'][$type] ? ' translate '.intval($this->param['watermarktext']['translatex'][$type]).','.intval($this->param['watermarktext']['translatey'][$type]) : '';
  538. $skewX = $this->param['watermarktext']['skewx'][$type] ? ' skewX '.intval($this->param['watermarktext']['skewx'][$type]) : '';
  539. $skewY = $this->param['watermarktext']['skewy'][$type] ? ' skewY '.intval($this->param['watermarktext']['skewy'][$type]) : '';
  540. $canvas = new Imagick(realpath($this->source));
  541. $canvas->setImageCompressionQuality($this->param['watermarkquality'][$type]);
  542. $dw = new ImagickDraw();
  543. $dw->setFont($this->param['watermarktext']['fontpath'][$type]);
  544. $dw->setFontSize($this->param['watermarktext']['size'][$type]);
  545. if(($this->param['watermarktext']['shadowx'][$type] || $this->param['watermarktext']['shadowy'][$type]) && $this->param['watermarktext']['shadowcolor'][$type]) {
  546. $dw->setFillColor(new ImagickPixel($this->param['watermarktext']['shadowcolor'][$type]));
  547. $dw->setGravity($gravity);
  548. if($translate) {
  549. $dw->translate($this->param['watermarktext']['translatex'][$type], $this->param['watermarktext']['translatey'][$type]);
  550. }
  551. if($skewX) {
  552. $dw->skewX($this->param['watermarktext']['skewx'][$type]);
  553. }
  554. if($skewY) {
  555. $dw->skewY($this->param['watermarktext']['skewy'][$type]);
  556. }
  557. $dw->annotation($this->param['watermarktext']['shadowx'][$type], $this->param['watermarktext']['shadowy'][$type], escapeshellcmd(pack("H*", $this->param['watermarktext']['text'][$type])));
  558. $canvas->drawImage($dw);
  559. }
  560. $dw->setFillColor(new ImagickPixel($this->param['watermarktext']['shadowcolor'][$type]));
  561. $dw->setGravity($gravity);
  562. if($translate) {
  563. $dw->translate($this->param['watermarktext']['translatex'][$type], $this->param['watermarktext']['translatey'][$type]);
  564. }
  565. if($skewX) {
  566. $dw->skewX($this->param['watermarktext']['skewx'][$type]);
  567. }
  568. if($skewY) {
  569. $dw->skewY($this->param['watermarktext']['skewy'][$type]);
  570. }
  571. $dw->rotate($angle);
  572. $dw->annotation(0, 0, escapeshellcmd(pack("H*", $this->param['watermarktext']['text'][$type])));
  573. $canvas->drawImage($dw);
  574. $result = $canvas->writeImage($this->target);
  575. $canvas->destroy();
  576. $dw->destroy();
  577. if(!$result) {
  578. return -3;
  579. }
  580. }
  581. return 1;
  582. }
  583. function IM_filter($str) {
  584. return escapeshellarg(str_replace(' ', '', $str));
  585. }
  586. }