PageRenderTime 52ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/php/xuzhou58/xuzhou58.com/include/image.class.php

http://jqbird.googlecode.com/
PHP | 232 lines | 220 code | 12 blank | 0 comment | 45 complexity | c81c57826c318deae44bf00abbdb298c MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0, LGPL-2.1, GPL-2.0
  1. <?php
  2. class image
  3. {
  4. var $attachinfo;
  5. var $targetfile; //????
  6. var $imagecreatefromfunc;
  7. var $imagefunc;
  8. var $attach;
  9. var $animatedgif;
  10. var $watermarkquality;
  11. var $watermarktext;
  12. var $thumbstatus;
  13. var $watermarkstatus;
  14. function image($targetfile, $cfg_thumb, $cfg_watermarktext, $photo_waterpos, $photo_diaphaneity, $photo_wheight, $photo_wwidth, $cfg_watermarktype, $photo_marktrans,$trueMarkimg, $attach = array())
  15. {
  16. $this->__construct($targetfile, $cfg_thumb, $cfg_watermarktext, $photo_waterpos, $photo_diaphaneity, $photo_wheight, $photo_wwidth, $cfg_watermarktype, $photo_marktrans,$trueMarkimg, $attach);
  17. }
  18. function __construct($targetfile, $cfg_thumb, $cfg_watermarktext, $photo_waterpos, $photo_diaphaneity, $photo_wheight, $photo_wwidth, $cfg_watermarktype, $photo_marktrans,$trueMarkimg, $attach = array())
  19. {
  20. $this->thumbstatus = $cfg_thumb;
  21. $this->watermarktext = $cfg_watermarktext;
  22. $this->watermarkstatus = $photo_waterpos;
  23. $this->watermarkquality = $photo_marktrans;
  24. $this->watermarkminwidth = $photo_wwidth;
  25. $this->watermarkminheight = $photo_wheight;
  26. $this->watermarktype = $cfg_watermarktype;
  27. $this->watermarktrans = $photo_diaphaneity;
  28. $this->animatedgif = 0;
  29. $this->targetfile = $targetfile;
  30. $this->attachinfo = @getimagesize($targetfile);
  31. $this->attach = $attach;
  32. switch($this->attachinfo['mime'])
  33. {
  34. case 'image/jpeg':
  35. $this->imagecreatefromfunc = function_exists('imagecreatefromjpeg') ? 'imagecreatefromjpeg' : '';
  36. $this->imagefunc = function_exists('imagejpeg') ? 'imagejpeg' : '';
  37. break;
  38. case 'image/gif':
  39. $this->imagecreatefromfunc = function_exists('imagecreatefromgif') ? 'imagecreatefromgif' : '';
  40. $this->imagefunc = function_exists('imagegif') ? 'imagegif' : '';
  41. break;
  42. case 'image/png':
  43. $this->imagecreatefromfunc = function_exists('imagecreatefrompng') ? 'imagecreatefrompng' : '';
  44. $this->imagefunc = function_exists('imagepng') ? 'imagepng' : '';
  45. break;
  46. }//?????????????
  47. $this->attach['size'] = empty($this->attach['size']) ? @filesize($targetfile) : $this->attach['size'];
  48. if($this->attachinfo['mime'] == 'image/gif')
  49. {
  50. $fp = fopen($targetfile, 'rb');
  51. $targetfilecontent = fread($fp, $this->attach['size']);
  52. fclose($fp);
  53. $this->animatedgif = strpos($targetfilecontent, 'NETSCAPE2.0') === false ? 0 : 1;
  54. }
  55. }
  56. function thumb($thumbwidth, $thumbheight, $preview = 0)
  57. {
  58. $this->thumb_gd($thumbwidth, $thumbheight, $preview);
  59. if($this->thumbstatus == 2 && $this->watermarkstatus)
  60. {
  61. $this->image($this->targetfile, $this->attach);
  62. $this->attach['size'] = filesize($this->targetfile);
  63. }
  64. }
  65. function watermark($preview = 0)
  66. {
  67. if($this->watermarkminwidth && $this->attachinfo[0] <= $this->watermarkminwidth && $this->watermarkminheight && $this->attachinfo[1] <= $this->watermarkminheight)
  68. {
  69. return ;
  70. }
  71. $this->watermark_gd($preview);
  72. }
  73. function thumb_gd($thumbwidth, $thumbheight, $preview = 0)
  74. {
  75. if($this->thumbstatus && function_exists('imagecreatetruecolor') && function_exists('imagecopyresampled') && function_exists('imagejpeg'))
  76. {
  77. $imagecreatefromfunc = $this->imagecreatefromfunc;
  78. $imagefunc = $this->thumbstatus == 1 ? 'imagejpeg' : $this->imagefunc;
  79. list($imagewidth, $imageheight) = $this->attachinfo;
  80. if(!$this->animatedgif && ($imagewidth >= $thumbwidth || $imageheight >= $thumbheight))
  81. {
  82. $attach_photo = $imagecreatefromfunc($this->targetfile);
  83. $x_ratio = $thumbwidth / $imagewidth;
  84. $y_ratio = $thumbheight / $imageheight;
  85. if(($x_ratio * $imageheight) < $thumbheight)
  86. {
  87. $thumb['height'] = ceil($x_ratio * $imageheight);
  88. $thumb['width'] = $thumbwidth;
  89. }
  90. else
  91. {
  92. $thumb['width'] = ceil($y_ratio * $imagewidth);
  93. $thumb['height'] = $thumbheight;
  94. }
  95. $targetfile = !$preview ? ($this->thumbstatus == 1 ? $this->targetfile.'.thumb.jpg' : $this->targetfile) : './watermark_tmp.jpg';
  96. $thumb_photo = imagecreatetruecolor($thumb['width'], $thumb['height']);
  97. imagecopyresampled($thumb_photo, $attach_photo, 0, 0, 0, 0, $thumb['width'], $thumb['height'], $imagewidth, $imageheight);
  98. if($this->attachinfo['mime'] == 'image/jpeg')
  99. {
  100. $imagefunc($thumb_photo, $targetfile, 100);
  101. }
  102. else
  103. {
  104. $imagefunc($thumb_photo, $targetfile);
  105. }
  106. $this->attach['thumb'] = $this->thumbstatus == 1 ? 1 : 0;
  107. }
  108. }
  109. }
  110. function watermark_gd($preview = 0)
  111. {
  112. if($this->watermarkstatus && function_exists('imagecopy') && function_exists('imagealphablending') && function_exists('imagecopymerge'))
  113. {
  114. $imagecreatefunc = $this->imagecreatefromfunc;
  115. $imagefunc = $this->imagefunc;
  116. list($imagewidth, $imageheight) = $this->attachinfo;
  117. if($this->watermarktype < 2)
  118. {
  119. $watermark_file = $this->watermarktype == 1 ? DEDEROOT.'/data/mark/mark.png' : DEDEROOT.'/data/mark/mark.gif';
  120. $watermarkinfo = @getimagesize($watermark_file);
  121. $watermark_logo = $this->watermarktype == 1 ? @imagecreatefrompng($watermark_file) : @imagecreatefromgif($watermark_file);
  122. if(!$watermark_logo)
  123. {
  124. return ;
  125. }
  126. list($logowidth, $logoheight) = $watermarkinfo;
  127. }
  128. else
  129. {
  130. $box = @imagettfbbox($this->watermarktext['size'], $this->watermarktext['angle'], $this->watermarktext['fontpath'],$this->watermarktext['text']);
  131. $logowidth = max($box[2], $box[4]) - min($box[0], $box[6]);
  132. $logoheight = max($box[1], $box[3]) - min($box[5], $box[7]);
  133. $ax = min($box[0], $box[6]) * -1;
  134. $ay = min($box[5], $box[7]) * -1;
  135. }
  136. $wmwidth = $imagewidth - $logowidth;
  137. $wmheight = $imageheight - $logoheight;
  138. if(($this->watermarktype < 2 && is_readable($watermark_file) || $this->watermarktype == 2) && $wmwidth > 10 && $wmheight > 10 && !$this->animatedgif)
  139. {
  140. switch($this->watermarkstatus)
  141. {
  142. case 1:
  143. $x = +5;
  144. $y = +5;
  145. break;
  146. case 2:
  147. $x = ($imagewidth - $logowidth) / 2;
  148. $y = +5;
  149. break;
  150. case 3:
  151. $x = $imagewidth - $logowidth - 5;
  152. $y = +5;
  153. break;
  154. case 4:
  155. $x = +5;
  156. $y = ($imageheight - $logoheight) / 2;
  157. break;
  158. case 5:
  159. $x = ($imagewidth - $logowidth) / 2;
  160. $y = ($imageheight - $logoheight) / 2;
  161. break;
  162. case 6:
  163. $x = $imagewidth - $logowidth - 5;
  164. $y = ($imageheight - $logoheight) / 2;
  165. break;
  166. case 7:
  167. $x = +5;
  168. $y = $imageheight - $logoheight - 5;
  169. break;
  170. case 8:
  171. $x = ($imagewidth - $logowidth) / 2;
  172. $y = $imageheight - $logoheight - 5;
  173. break;
  174. case 9:
  175. $x = $imagewidth - $logowidth - 5;
  176. $y = $imageheight - $logoheight -5;
  177. break;
  178. }
  179. $dst_photo = @imagecreatetruecolor($imagewidth, $imageheight);
  180. $target_photo = $imagecreatefunc($this->targetfile);
  181. imagecopy($dst_photo, $target_photo, 0, 0, 0, 0, $imagewidth, $imageheight);
  182. if($this->watermarktype == 1)
  183. {
  184. imagecopy($dst_photo, $watermark_logo, $x, $y, 0, 0, $logowidth, $logoheight);
  185. }
  186. elseif($this->watermarktype == 2)
  187. {
  188. if(($this->watermarktext['shadowx'] || $this->watermarktext['shadowy']) && $this->watermarktext['shadowcolor'])
  189. {
  190. $shadowcolorrgb = explode(',', $this->watermarktext['shadowcolor']);
  191. $shadowcolor = imagecolorallocate($dst_photo, $shadowcolorrgb[0], $shadowcolorrgb[1], $shadowcolorrgb[2]);
  192. imagettftext($dst_photo, $this->watermarktext['size'], $this->watermarktext['angle'],
  193. $x + $ax + $this->watermarktext['shadowx'], $y + $ay + $this->watermarktext['shadowy'], $shadowcolor,
  194. $this->watermarktext['fontpath'], $this->watermarktext['text']);
  195. }
  196. $colorrgb = explode(',', $this->watermarktext['color']);
  197. $color = imagecolorallocate($dst_photo, $colorrgb[0], $colorrgb[1], $colorrgb[2]);
  198. imagettftext($dst_photo, $this->watermarktext['size'], $this->watermarktext['angle'],
  199. $x + $ax, $y + $ay, $color, $this->watermarktext['fontpath'], $this->watermarktext['text']);
  200. }
  201. else
  202. {
  203. imagealphablending($watermark_logo, true);
  204. imagecopymerge($dst_photo, $watermark_logo, $x, $y, 0, 0, $logowidth, $logoheight, $this->watermarktrans);
  205. }
  206. $targetfile = !$preview ? $this->targetfile : './watermark_tmp.jpg';
  207. if($this->attachinfo['mime'] == 'image/jpeg')
  208. {
  209. $imagefunc($dst_photo, $targetfile, $this->watermarkquality);
  210. }
  211. else
  212. {
  213. $imagefunc($dst_photo, $targetfile);
  214. }
  215. $this->attach['size'] = filesize($this->targetfile);
  216. }
  217. }
  218. }
  219. }
  220. ?>