PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/source/class/class_image.php

https://github.com/kuaileshike/upload
PHP | 563 lines | 512 code | 45 blank | 6 comment | 127 complexity | 25e2cc3a2395f215a6b118fa2662346f MD5 | raw file
  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 30998 2012-07-06 07:22:08Z zhangguosheng $
  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. function image() {
  22. global $_G;
  23. $this->param = array(
  24. 'imagelib' => $_G['setting']['imagelib'],
  25. 'imageimpath' => $_G['setting']['imageimpath'],
  26. 'thumbquality' => $_G['setting']['thumbquality'],
  27. 'watermarkstatus' => dunserialize($_G['setting']['watermarkstatus']),
  28. 'watermarkminwidth' => dunserialize($_G['setting']['watermarkminwidth']),
  29. 'watermarkminheight' => dunserialize($_G['setting']['watermarkminheight']),
  30. 'watermarktype' => $_G['setting']['watermarktype'],
  31. 'watermarktext' => $_G['setting']['watermarktext'],
  32. 'watermarktrans' => dunserialize($_G['setting']['watermarktrans']),
  33. 'watermarkquality' => dunserialize($_G['setting']['watermarkquality']),
  34. );
  35. }
  36. function Thumb($source, $target, $thumbwidth, $thumbheight, $thumbtype = 1, $nosuffix = 0) {
  37. $return = $this->init('thumb', $source, $target, $nosuffix);
  38. if($return <= 0) {
  39. return $this->returncode($return);
  40. }
  41. if($this->imginfo['animated']) {
  42. return $this->returncode(0);
  43. }
  44. $this->param['thumbwidth'] = $thumbwidth;
  45. if(!$thumbheight || $thumbheight > $this->imginfo['height']) {
  46. $thumbheight = $thumbwidth > $this->imginfo['width'] ? $this->imginfo['height'] : $this->imginfo['height']*($thumbwidth/$this->imginfo['width']);
  47. }
  48. $this->param['thumbheight'] = $thumbheight;
  49. $this->param['thumbtype'] = $thumbtype;
  50. if($thumbwidth < 100 && $thumbheight < 100) {
  51. $this->param['thumbquality'] = 100;
  52. }
  53. $return = !$this->libmethod ? $this->Thumb_GD() : $this->Thumb_IM();
  54. $return = !$nosuffix ? $return : 0;
  55. return $this->sleep($return);
  56. }
  57. function Cropper($source, $target, $dstwidth, $dstheight, $srcx = 0, $srcy = 0, $srcwidth = 0, $srcheight = 0) {
  58. $return = $this->init('thumb', $source, $target, 1);
  59. if($return <= 0) {
  60. return $this->returncode($return);
  61. }
  62. if($dstwidth < 0 || $dstheight < 0) {
  63. return $this->returncode(false);
  64. }
  65. $this->param['dstwidth'] = $dstwidth;
  66. $this->param['dstheight'] = $dstheight;
  67. $this->param['srcx'] = $srcx;
  68. $this->param['srcy'] = $srcy;
  69. $this->param['srcwidth'] = $srcwidth ? $srcwidth : $dstwidth;
  70. $this->param['srcheight'] = $srcheight ? $srcheight : $dstheight;
  71. $return = !$this->libmethod ? $this->Cropper_GD() : $this->Cropper_IM();
  72. }
  73. function Watermark($source, $target = '', $type = 'forum') {
  74. $return = $this->init('watermask', $source, $target);
  75. if($return <= 0) {
  76. return $this->returncode($return);
  77. }
  78. 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])) {
  79. return $this->returncode(0);
  80. }
  81. $this->param['watermarkfile'][$type] = './static/image/common/'.($this->param['watermarktype'][$type] == 'png' ? 'watermark.png' : 'watermark.gif');
  82. 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])))) {
  83. return $this->returncode(-3);
  84. }
  85. $return = !$this->libmethod ? $this->Watermark_GD($type) : $this->Watermark_IM($type);
  86. return $this->sleep($return);
  87. }
  88. function error() {
  89. return $this->errorcode;
  90. }
  91. function init($method, $source, $target, $nosuffix = 0) {
  92. global $_G;
  93. $this->errorcode = 0;
  94. if(empty($source)) {
  95. return -2;
  96. }
  97. $parse = parse_url($source);
  98. if(isset($parse['host'])) {
  99. if(empty($target)) {
  100. return -2;
  101. }
  102. $data = dfsockopen($source);
  103. $this->tmpfile = $source = tempnam($_G['setting']['attachdir'].'./temp/', 'tmpimg_');
  104. file_put_contents($source, $data);
  105. if(!$data || $source === FALSE) {
  106. return -2;
  107. }
  108. }
  109. if($method == 'thumb') {
  110. $target = empty($target) ? (!$nosuffix ? getimgthumbname($source) : $source) : $_G['setting']['attachdir'].'./'.$target;
  111. } elseif($method == 'watermask') {
  112. $target = empty($target) ? $source : $_G['setting']['attachdir'].'./'.$target;
  113. }
  114. $targetpath = dirname($target);
  115. dmkdir($targetpath);
  116. clearstatcache();
  117. if(!is_readable($source) || !is_writable($targetpath)) {
  118. return -2;
  119. }
  120. $imginfo = @getimagesize($source);
  121. if($imginfo === FALSE) {
  122. return -1;
  123. }
  124. $this->source = $source;
  125. $this->target = $target;
  126. $this->imginfo['width'] = $imginfo[0];
  127. $this->imginfo['height'] = $imginfo[1];
  128. $this->imginfo['mime'] = $imginfo['mime'];
  129. $this->imginfo['size'] = @filesize($source);
  130. $this->libmethod = $this->param['imagelib'] && $this->param['imageimpath'];
  131. if(!$this->libmethod) {
  132. switch($this->imginfo['mime']) {
  133. case 'image/jpeg':
  134. $this->imagecreatefromfunc = function_exists('imagecreatefromjpeg') ? 'imagecreatefromjpeg' : '';
  135. $this->imagefunc = function_exists('imagejpeg') ? 'imagejpeg' : '';
  136. break;
  137. case 'image/gif':
  138. $this->imagecreatefromfunc = function_exists('imagecreatefromgif') ? 'imagecreatefromgif' : '';
  139. $this->imagefunc = function_exists('imagegif') ? 'imagegif' : '';
  140. break;
  141. case 'image/png':
  142. $this->imagecreatefromfunc = function_exists('imagecreatefrompng') ? 'imagecreatefrompng' : '';
  143. $this->imagefunc = function_exists('imagepng') ? 'imagepng' : '';
  144. break;
  145. }
  146. } else {
  147. $this->imagecreatefromfunc = $this->imagefunc = TRUE;
  148. }
  149. if(!$this->libmethod && $this->imginfo['mime'] == 'image/gif') {
  150. if(!$this->imagecreatefromfunc) {
  151. return -4;
  152. }
  153. if(!($fp = @fopen($source, 'rb'))) {
  154. return -2;
  155. }
  156. $content = fread($fp, $this->imginfo['size']);
  157. fclose($fp);
  158. $this->imginfo['animated'] = strpos($content, 'NETSCAPE2.0') === FALSE ? 0 : 1;
  159. }
  160. return $this->imagecreatefromfunc ? 1 : -4;
  161. }
  162. function sleep($return) {
  163. if($this->tmpfile) {
  164. @unlink($this->tmpfile);
  165. }
  166. $this->imginfo['size'] = @filesize($this->target);
  167. return $this->returncode($return);
  168. }
  169. function returncode($return) {
  170. if($return > 0 && file_exists($this->target)) {
  171. return true;
  172. } else {
  173. $this->errorcode = $return;
  174. return false;
  175. }
  176. }
  177. function sizevalue($method) {
  178. $x = $y = $w = $h = 0;
  179. if($method > 0) {
  180. $imgratio = $this->imginfo['width'] / $this->imginfo['height'];
  181. $thumbratio = $this->param['thumbwidth'] / $this->param['thumbheight'];
  182. if($imgratio >= 1 && $imgratio >= $thumbratio || $imgratio < 1 && $imgratio > $thumbratio) {
  183. $h = $this->imginfo['height'];
  184. $w = $h * $thumbratio;
  185. $x = ($this->imginfo['width'] - $thumbratio * $this->imginfo['height']) / 2;
  186. } elseif($imgratio >= 1 && $imgratio <= $thumbratio || $imgratio < 1 && $imgratio <= $thumbratio) {
  187. $w = $this->imginfo['width'];
  188. $h = $w / $thumbratio;
  189. }
  190. } else {
  191. $x_ratio = $this->param['thumbwidth'] / $this->imginfo['width'];
  192. $y_ratio = $this->param['thumbheight'] / $this->imginfo['height'];
  193. if(($x_ratio * $this->imginfo['height']) < $this->param['thumbheight']) {
  194. $h = ceil($x_ratio * $this->imginfo['height']);
  195. $w = $this->param['thumbwidth'];
  196. } else {
  197. $w = ceil($y_ratio * $this->imginfo['width']);
  198. $h = $this->param['thumbheight'];
  199. }
  200. }
  201. return array($x, $y, $w, $h);
  202. }
  203. function loadsource() {
  204. $imagecreatefromfunc = &$this->imagecreatefromfunc;
  205. $im = @$imagecreatefromfunc($this->source);
  206. if(!$im) {
  207. if(!function_exists('imagecreatefromstring')) {
  208. return -4;
  209. }
  210. $fp = @fopen($this->source, 'rb');
  211. $contents = @fread($fp, filesize($this->source));
  212. fclose($fp);
  213. $im = @imagecreatefromstring($contents);
  214. if($im == FALSE) {
  215. return -1;
  216. }
  217. }
  218. return $im;
  219. }
  220. function Thumb_GD() {
  221. if(!function_exists('imagecreatetruecolor') || !function_exists('imagecopyresampled') || !function_exists('imagejpeg') || !function_exists('imagecopymerge')) {
  222. return -4;
  223. }
  224. $imagefunc = &$this->imagefunc;
  225. $attach_photo = $this->loadsource();
  226. if($attach_photo < 0) {
  227. return $attach_photo;
  228. }
  229. $copy_photo = imagecreatetruecolor($this->imginfo['width'], $this->imginfo['height']);
  230. $bg = imagecolorallocate($copy_photo, 255, 255, 255);
  231. imagefill($copy_photo, 0, 0, $bg);
  232. imagecopy($copy_photo, $attach_photo ,0, 0, 0, 0, $this->imginfo['width'], $this->imginfo['height']);
  233. $attach_photo = $copy_photo;
  234. $thumb_photo = null;
  235. switch($this->param['thumbtype']) {
  236. case 'fixnone':
  237. case 1:
  238. if($this->imginfo['width'] >= $this->param['thumbwidth'] || $this->imginfo['height'] >= $this->param['thumbheight']) {
  239. $thumb = array();
  240. list(,,$thumb['width'], $thumb['height']) = $this->sizevalue(0);
  241. $cx = $this->imginfo['width'];
  242. $cy = $this->imginfo['height'];
  243. $thumb_photo = imagecreatetruecolor($thumb['width'], $thumb['height']);
  244. imagecopyresampled($thumb_photo, $attach_photo ,0, 0, 0, 0, $thumb['width'], $thumb['height'], $cx, $cy);
  245. }
  246. break;
  247. case 'fixwr':
  248. case 2:
  249. if(!($this->imginfo['width'] <= $this->param['thumbwidth'] || $this->imginfo['height'] <= $this->param['thumbheight'])) {
  250. list($startx, $starty, $cutw, $cuth) = $this->sizevalue(1);
  251. $dst_photo = imagecreatetruecolor($cutw, $cuth);
  252. imagecopymerge($dst_photo, $attach_photo, 0, 0, $startx, $starty, $cutw, $cuth, 100);
  253. $thumb_photo = imagecreatetruecolor($this->param['thumbwidth'], $this->param['thumbheight']);
  254. imagecopyresampled($thumb_photo, $dst_photo ,0, 0, 0, 0, $this->param['thumbwidth'], $this->param['thumbheight'], $cutw, $cuth);
  255. } else {
  256. $thumb_photo = imagecreatetruecolor($this->param['thumbwidth'], $this->param['thumbheight']);
  257. $bgcolor = imagecolorallocate($thumb_photo, 255, 255, 255);
  258. imagefill($thumb_photo, 0, 0, $bgcolor);
  259. $startx = ($this->param['thumbwidth'] - $this->imginfo['width']) / 2;
  260. $starty = ($this->param['thumbheight'] - $this->imginfo['height']) / 2;
  261. imagecopymerge($thumb_photo, $attach_photo, $startx, $starty, 0, 0, $this->imginfo['width'], $this->imginfo['height'], 100);
  262. }
  263. break;
  264. }
  265. clearstatcache();
  266. if($thumb_photo) {
  267. if($this->imginfo['mime'] == 'image/jpeg') {
  268. @$imagefunc($thumb_photo, $this->target, $this->param['thumbquality']);
  269. } else {
  270. @$imagefunc($thumb_photo, $this->target);
  271. }
  272. return 1;
  273. } else {
  274. return 0;
  275. }
  276. }
  277. function Thumb_IM() {
  278. switch($this->param['thumbtype']) {
  279. case 'fixnone':
  280. case 1:
  281. if($this->imginfo['width'] > $this->param['thumbwidth'] || $this->imginfo['height'] > $this->param['thumbheight']) {
  282. $exec_str = $this->param['imageimpath'].'/convert -quality '.intval($this->param['thumbquality']).' -geometry '.$this->param['thumbwidth'].'x'.$this->param['thumbheight'].' '.$this->source.' '.$this->target;
  283. $return = exec($exec_str);
  284. if(!file_exists($this->target)) {
  285. return -3;
  286. }
  287. }
  288. break;
  289. case 'fixwr':
  290. case 2:
  291. if(!($this->imginfo['width'] <= $this->param['thumbwidth'] || $this->imginfo['height'] <= $this->param['thumbheight'])) {
  292. list($startx, $starty, $cutw, $cuth) = $this->sizevalue(1);
  293. $exec_str = $this->param['imageimpath'].'/convert -quality '.intval($this->param['thumbquality']).' -crop '.$cutw.'x'.$cuth.'+'.$startx.'+'.$starty.' '.$this->source.' '.$this->target;
  294. exec($exec_str);
  295. if(!file_exists($this->target)) {
  296. return -3;
  297. }
  298. $exec_str = $this->param['imageimpath'].'/convert -quality '.intval($this->param['thumbquality']).' -thumbnail \''.$this->param['thumbwidth'].'x'.$this->param['thumbheight'].'\' -resize '.$this->param['thumbwidth'].'x'.$this->param['thumbheight'].' -gravity center -extent '.$this->param['thumbwidth'].'x'.$this->param['thumbheight'].' '.$this->target.' '.$this->target;
  299. exec($exec_str);
  300. if(!file_exists($this->target)) {
  301. return -3;
  302. }
  303. } else {
  304. $startx = -($this->param['thumbwidth'] - $this->imginfo['width']) / 2;
  305. $starty = -($this->param['thumbheight'] - $this->imginfo['height']) / 2;
  306. $exec_str = $this->param['imageimpath'].'/convert -quality '.intval($this->param['thumbquality']).' -crop '.$this->param['thumbwidth'].'x'.$this->param['thumbheight'].'+'.$startx.'+'.$starty.' '.$this->source.' '.$this->target;
  307. exec($exec_str);
  308. if(!file_exists($this->target)) {
  309. return -3;
  310. }
  311. $exec_str = $this->param['imageimpath'].'/convert -quality '.intval($this->param['thumbquality']).' -thumbnail \''.$this->param['thumbwidth'].'x'.$this->param['thumbheight'].'\' -gravity center -extent '.$this->param['thumbwidth'].'x'.$this->param['thumbheight'].' '.$this->target.' '.$this->target;
  312. exec($exec_str);
  313. if(!file_exists($this->target)) {
  314. return -3;
  315. }
  316. }
  317. break;
  318. }
  319. return 1;
  320. }
  321. function Cropper_GD() {
  322. $image = $this->loadsource();
  323. if($image < 0) {
  324. return $image;
  325. }
  326. $newimage = imagecreatetruecolor($this->param['dstwidth'], $this->param['dstheight']);
  327. imagecopyresampled($newimage, $image, 0, 0, $this->param['srcx'], $this->param['srcy'], $this->param['dstwidth'], $this->param['dstheight'], $this->param['srcwidth'], $this->param['srcheight']);
  328. ImageJpeg($newimage, $this->target, 100);
  329. imagedestroy($newimage);
  330. imagedestroy($image);
  331. return true;
  332. }
  333. function Cropper_IM() {
  334. $exec_str = $this->param['imageimpath'].'/convert -quality 100 '.
  335. '-crop '.$this->param['srcwidth'].'x'.$this->param['srcheight'].'+'.$this->param['srcx'].'+'.$this->param['srcy'].' '.
  336. '-geometry '.$this->param['dstwidth'].'x'.$this->param['dstheight'].' '.$this->source.' '.$this->target;
  337. exec($exec_str);
  338. if(!file_exists($this->target)) {
  339. return -3;
  340. }
  341. }
  342. function Watermark_GD($type = 'forum') {
  343. if(!function_exists('imagecreatetruecolor')) {
  344. return -4;
  345. }
  346. $imagefunc = &$this->imagefunc;
  347. if($this->param['watermarktype'][$type] != 'text') {
  348. if(!function_exists('imagecopy') || !function_exists('imagecreatefrompng') || !function_exists('imagecreatefromgif') || !function_exists('imagealphablending') || !function_exists('imagecopymerge')) {
  349. return -4;
  350. }
  351. $watermarkinfo = @getimagesize($this->param['watermarkfile'][$type]);
  352. if($watermarkinfo === FALSE) {
  353. return -3;
  354. }
  355. $watermark_logo = $this->param['watermarktype'][$type] == 'png' ? @imageCreateFromPNG($this->param['watermarkfile'][$type]) : @imageCreateFromGIF($this->param['watermarkfile'][$type]);
  356. if(!$watermark_logo) {
  357. return 0;
  358. }
  359. list($logo_w, $logo_h) = $watermarkinfo;
  360. } else {
  361. if(!function_exists('imagettfbbox') || !function_exists('imagettftext') || !function_exists('imagecolorallocate')) {
  362. return -4;
  363. }
  364. if(!class_exists('Chinese')) {
  365. include libfile('class/chinese');
  366. }
  367. $watermarktextcvt = pack("H*", $this->param['watermarktext']['text'][$type]);
  368. $box = imagettfbbox($this->param['watermarktext']['size'][$type], $this->param['watermarktext']['angle'][$type], $this->param['watermarktext']['fontpath'][$type], $watermarktextcvt);
  369. $logo_h = max($box[1], $box[3]) - min($box[5], $box[7]);
  370. $logo_w = max($box[2], $box[4]) - min($box[0], $box[6]);
  371. $ax = min($box[0], $box[6]) * -1;
  372. $ay = min($box[5], $box[7]) * -1;
  373. }
  374. $wmwidth = $this->imginfo['width'] - $logo_w;
  375. $wmheight = $this->imginfo['height'] - $logo_h;
  376. if($wmwidth > 10 && $wmheight > 10 && !$this->imginfo['animated']) {
  377. switch($this->param['watermarkstatus'][$type]) {
  378. case 1:
  379. $x = 5;
  380. $y = 5;
  381. break;
  382. case 2:
  383. $x = ($this->imginfo['width'] - $logo_w) / 2;
  384. $y = 5;
  385. break;
  386. case 3:
  387. $x = $this->imginfo['width'] - $logo_w - 5;
  388. $y = 5;
  389. break;
  390. case 4:
  391. $x = 5;
  392. $y = ($this->imginfo['height'] - $logo_h) / 2;
  393. break;
  394. case 5:
  395. $x = ($this->imginfo['width'] - $logo_w) / 2;
  396. $y = ($this->imginfo['height'] - $logo_h) / 2;
  397. break;
  398. case 6:
  399. $x = $this->imginfo['width'] - $logo_w;
  400. $y = ($this->imginfo['height'] - $logo_h) / 2;
  401. break;
  402. case 7:
  403. $x = 5;
  404. $y = $this->imginfo['height'] - $logo_h - 5;
  405. break;
  406. case 8:
  407. $x = ($this->imginfo['width'] - $logo_w) / 2;
  408. $y = $this->imginfo['height'] - $logo_h - 5;
  409. break;
  410. case 9:
  411. $x = $this->imginfo['width'] - $logo_w - 5;
  412. $y = $this->imginfo['height'] - $logo_h - 5;
  413. break;
  414. }
  415. if($this->imginfo['mime'] != 'image/png') {
  416. $color_photo = imagecreatetruecolor($this->imginfo['width'], $this->imginfo['height']);
  417. }
  418. $dst_photo = $this->loadsource();
  419. if($dst_photo < 0) {
  420. return $dst_photo;
  421. }
  422. imagealphablending($dst_photo, true);
  423. imagesavealpha($dst_photo, true);
  424. if($this->imginfo['mime'] != 'image/png') {
  425. imageCopy($color_photo, $dst_photo, 0, 0, 0, 0, $this->imginfo['width'], $this->imginfo['height']);
  426. $dst_photo = $color_photo;
  427. }
  428. if($this->param['watermarktype'][$type] == 'png') {
  429. imageCopy($dst_photo, $watermark_logo, $x, $y, 0, 0, $logo_w, $logo_h);
  430. } elseif($this->param['watermarktype'][$type] == 'text') {
  431. if(($this->param['watermarktext']['shadowx'][$type] || $this->param['watermarktext']['shadowy'][$type]) && $this->param['watermarktext']['shadowcolor'][$type]) {
  432. $shadowcolorrgb = explode(',', $this->param['watermarktext']['shadowcolor'][$type]);
  433. $shadowcolor = imagecolorallocate($dst_photo, $shadowcolorrgb[0], $shadowcolorrgb[1], $shadowcolorrgb[2]);
  434. 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);
  435. }
  436. $colorrgb = explode(',', $this->param['watermarktext']['color'][$type]);
  437. $color = imagecolorallocate($dst_photo, $colorrgb[0], $colorrgb[1], $colorrgb[2]);
  438. imagettftext($dst_photo, $this->param['watermarktext']['size'][$type], $this->param['watermarktext']['angle'][$type], $x + $ax, $y + $ay, $color, $this->param['watermarktext']['fontpath'][$type], $watermarktextcvt);
  439. } else {
  440. imageAlphaBlending($watermark_logo, true);
  441. imageCopyMerge($dst_photo, $watermark_logo, $x, $y, 0, 0, $logo_w, $logo_h, $this->param['watermarktrans'][$type]);
  442. }
  443. clearstatcache();
  444. if($this->imginfo['mime'] == 'image/jpeg') {
  445. @$imagefunc($dst_photo, $this->target, $this->param['watermarkquality'][$type]);
  446. } else {
  447. @$imagefunc($dst_photo, $this->target);
  448. }
  449. }
  450. return 1;
  451. }
  452. function Watermark_IM($type = 'forum') {
  453. switch($this->param['watermarkstatus'][$type]) {
  454. case 1:
  455. $gravity = 'NorthWest';
  456. break;
  457. case 2:
  458. $gravity = 'North';
  459. break;
  460. case 3:
  461. $gravity = 'NorthEast';
  462. break;
  463. case 4:
  464. $gravity = 'West';
  465. break;
  466. case 5:
  467. $gravity = 'Center';
  468. break;
  469. case 6:
  470. $gravity = 'East';
  471. break;
  472. case 7:
  473. $gravity = 'SouthWest';
  474. break;
  475. case 8:
  476. $gravity = 'South';
  477. break;
  478. case 9:
  479. $gravity = 'SouthEast';
  480. break;
  481. }
  482. if($this->param['watermarktype'][$type] != 'text') {
  483. $exec_str = $this->param['imageimpath'].'/composite'.
  484. ($this->param['watermarktype'][$type] != 'png' && $this->param['watermarktrans'][$type] != '100' ? ' -watermark '.$this->param['watermarktrans'][$type] : '').
  485. ' -quality '.$this->param['watermarkquality'][$type].
  486. ' -gravity '.$gravity.
  487. ' '.$this->param['watermarkfile'][$type].' '.$this->source.' '.$this->target;
  488. } else {
  489. $watermarktextcvt = str_replace(array("\n", "\r", "'"), array('', '', '\''), pack("H*", $this->param['watermarktext']['text'][$type]));
  490. $angle = -$this->param['watermarktext']['angle'][$type];
  491. $translate = $this->param['watermarktext']['translatex'][$type] || $this->param['watermarktext']['translatey'][$type] ? ' translate '.$this->param['watermarktext']['translatex'][$type].','.$this->param['watermarktext']['translatey'][$type] : '';
  492. $skewX = $this->param['watermarktext']['skewx'][$type] ? ' skewX '.$this->param['watermarktext']['skewx'][$type] : '';
  493. $skewY = $this->param['watermarktext']['skewy'][$type] ? ' skewY '.$this->param['watermarktext']['skewy'][$type] : '';
  494. $exec_str = $this->param['imageimpath'].'/convert'.
  495. ' -quality '.$this->param['watermarkquality'][$type].
  496. ' -font "'.$this->param['watermarktext']['fontpath'][$type].'"'.
  497. ' -pointsize '.$this->param['watermarktext']['size'][$type].
  498. (($this->param['watermarktext']['shadowx'][$type] || $this->param['watermarktext']['shadowy'][$type]) && $this->param['watermarktext']['shadowcolor'][$type] ?
  499. ' -fill "rgb('.$this->param['watermarktext']['shadowcolor'][$type].')"'.
  500. ' -draw "'.
  501. ' gravity '.$gravity.$translate.$skewX.$skewY.
  502. ' rotate '.$angle.
  503. ' text '.$this->param['watermarktext']['shadowx'][$type].','.$this->param['watermarktext']['shadowy'][$type].' \''.$watermarktextcvt.'\'"' : '').
  504. ' -fill "rgb('.$this->param['watermarktext']['color'][$type].')"'.
  505. ' -draw "'.
  506. ' gravity '.$gravity.$translate.$skewX.$skewY.
  507. ' rotate '.$angle.
  508. ' text 0,0 \''.$watermarktextcvt.'\'"'.
  509. ' '.$this->source.' '.$this->target;
  510. }
  511. exec($exec_str);
  512. if(!file_exists($this->target)) {
  513. return -3;
  514. }
  515. return 1;
  516. }
  517. }