PageRenderTime 60ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/ php-ppcms/includes/classes/lib.image.thumb.class.php

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