image.class.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. /**
  3. * 图像处理
  4. */
  5. class image {
  6. var $w_pct = 100;
  7. var $w_quality = 80;
  8. var $w_minwidth = 300;
  9. var $w_minheight = 300;
  10. var $thumb_enable;
  11. var $watermark_enable;
  12. var $interlace = 0;
  13. var $siteinfo = array();
  14. function __construct($thumb_enable = 0 ,$siteid) {
  15. $this->thumb_enable = $thumb_enable;
  16. if($siteid) {
  17. $this->siteinfo = getcache('sitelist', 'commons');
  18. $this->site_setting = string2array($this->siteinfo[$siteid]['setting']);
  19. $this->watermark_enable = $this->site_setting['watermark_enable'];
  20. $this->set($this->site_setting['watermark_img'],$this->site_setting['watermark_pos'],$this->site_setting['watermark_minwidth'],$this->site_setting['watermark_minheight'],$this->site_setting['watermark_quality'],$this->site_setting['watermark_pct']);
  21. }
  22. }
  23. function set($w_img, $w_pos, $w_minwidth = 300, $w_minheight = 300, $w_quality = 80, $w_pct = 100) {
  24. $this->w_img = $w_img;
  25. $this->w_pos = $w_pos;
  26. $this->w_minwidth = $w_minwidth;
  27. $this->w_minheight = $w_minheight;
  28. $this->w_quality = $w_quality;
  29. $this->w_pct = $w_pct;
  30. }
  31. function info($img) {
  32. $imageinfo = getimagesize($img);
  33. if($imageinfo === false) return false;
  34. $imagetype = strtolower(substr(image_type_to_extension($imageinfo[2]),1));
  35. $imagesize = filesize($img);
  36. $info = array(
  37. 'width'=>$imageinfo[0],
  38. 'height'=>$imageinfo[1],
  39. 'type'=>$imagetype,
  40. 'size'=>$imagesize,
  41. 'mime'=>$imageinfo['mime']
  42. );
  43. return $info;
  44. }
  45. function getpercent($srcwidth,$srcheight,$dstw,$dsth) {
  46. if (empty($srcwidth) || empty($srcheight) || ($srcwidth <= $dstW && $srcheight <= $dstH)) $w = $srcwidth ;$h = $srcheight;
  47. if ((empty($dstw) || $dstw == 0) && $dsth > 0 && $srcheight > $dsth) {
  48. $h = $dsth;
  49. $w = round($dsth / $srcheight * $srcwidth);
  50. } elseif ((empty($dsth) || $dsth == 0) && $dstw > 0 && $srcwidth > $dstw) {
  51. $w = $dstw;
  52. $h = round($dstw / $srcwidth * $srcheight);
  53. } elseif ($dstw > 0 && $dsth > 0) {
  54. if (($srcwidth / $dstw) < ($srcheight / $dsth)) {
  55. $w = round($dsth / $srcheight * $srcwidth);
  56. $h = $dsth;
  57. } elseif (($srcwidth / $dstw) > ($srcheight / $dsth)) {
  58. $w = $dstw;
  59. $h = round($dstw / $srcwidth * $srcheight );
  60. } else {
  61. $h = $dstw;
  62. $w = $dsth;
  63. }
  64. }
  65. $array['w'] = $w;
  66. $array['h'] = $h;
  67. return $array;
  68. }
  69. function thumb($image, $filename = '', $maxwidth = 200, $maxheight = 200, $suffix='', $autocut = 0, $ftp = 0) {
  70. if(!$this->thumb_enable || !$this->check($image)) return false;
  71. $info = image::info($image);
  72. if($info === false) return false;
  73. $srcwidth = $info['width'];
  74. $srcheight = $info['height'];
  75. $pathinfo = pathinfo($image);
  76. $type = $info['type'];
  77. if(!$type) $type = $pathinfo['extension'];
  78. $type = strtolower($type);
  79. unset($info);
  80. $creat_arr = $this->getpercent($srcwidth,$srcheight,$maxwidth,$maxheight);
  81. $createwidth = $width = $creat_arr['w'];
  82. $createheight = $height = $creat_arr['h'];
  83. $psrc_x = $psrc_y = 0;
  84. if($autocut && $maxwidth > 0 && $maxheight > 0) {
  85. if($maxwidth/$maxheight<$srcwidth/$srcheight && $maxheight>=$height) {
  86. $width = $maxheight/$height*$width;
  87. $height = $maxheight;
  88. }elseif($maxwidth/$maxheight>$srcwidth/$srcheight && $maxwidth>=$width) {
  89. $height = $maxwidth/$width*$height;
  90. $width = $maxwidth;
  91. }
  92. $createwidth = $maxwidth;
  93. $createheight = $maxheight;
  94. }
  95. $createfun = 'imagecreatefrom'.($type=='jpg' ? 'jpeg' : $type);
  96. $srcimg = $createfun($image);
  97. if($type != 'gif' && function_exists('imagecreatetruecolor'))
  98. $thumbimg = imagecreatetruecolor($createwidth, $createheight);
  99. else
  100. $thumbimg = imagecreate($width, $height);
  101. if(function_exists('imagecopyresampled'))
  102. imagecopyresampled($thumbimg, $srcimg, 0, 0, $psrc_x, $psrc_y, $width, $height, $srcwidth, $srcheight);
  103. else
  104. imagecopyresized($thumbimg, $srcimg, 0, 0, $psrc_x, $psrc_y, $width, $height, $srcwidth, $srcheight);
  105. if($type=='gif' || $type=='png') {
  106. $background_color = imagecolorallocate($thumbimg, 0, 255, 0); // 指派一个绿色
  107. imagecolortransparent($thumbimg, $background_color); // 设置为透明色,若注释掉该行则输出绿色的图
  108. }
  109. if($type=='jpg' || $type=='jpeg') imageinterlace($thumbimg, $this->interlace);
  110. $imagefun = 'image'.($type=='jpg' ? 'jpeg' : $type);
  111. if(empty($filename)) $filename = substr($image, 0, strrpos($image, '.')).$suffix.'.'.$type;
  112. $imagefun($thumbimg, $filename);
  113. imagedestroy($thumbimg);
  114. imagedestroy($srcimg);
  115. if($ftp) {
  116. @unlink($image);
  117. }
  118. return $filename;
  119. }
  120. function watermark($source, $target = '', $w_pos = '', $w_img = '', $w_text = 'phpcms',$w_font = 8, $w_color = '#ff0000') {
  121. $w_pos = $w_pos ? $w_pos : $this->w_pos;
  122. $w_img = $w_img ? $w_img : $this->w_img;
  123. if(!$this->watermark_enable || !$this->check($source)) return false;
  124. if(!$target) $target = $source;
  125. $w_img = PHPCMS_PATH.$w_img;
  126. $source_info = getimagesize($source);
  127. $source_w = $source_info[0];
  128. $source_h = $source_info[1];
  129. if($source_w < $this->w_minwidth || $source_h < $this->w_minheight) return false;
  130. switch($source_info[2]) {
  131. case 1 :
  132. $source_img = imagecreatefromgif($source);
  133. break;
  134. case 2 :
  135. $source_img = imagecreatefromjpeg($source);
  136. break;
  137. case 3 :
  138. $source_img = imagecreatefrompng($source);
  139. break;
  140. default :
  141. return false;
  142. }
  143. if(!empty($w_img) && file_exists($w_img)) {
  144. $ifwaterimage = 1;
  145. $water_info = getimagesize($w_img);
  146. $width = $water_info[0];
  147. $height = $water_info[1];
  148. switch($water_info[2]) {
  149. case 1 :
  150. $water_img = imagecreatefromgif($w_img);
  151. break;
  152. case 2 :
  153. $water_img = imagecreatefromjpeg($w_img);
  154. break;
  155. case 3 :
  156. $water_img = imagecreatefrompng($w_img);
  157. break;
  158. default :
  159. return;
  160. }
  161. } else {
  162. $ifwaterimage = 0;
  163. $temp = imagettfbbox(ceil($w_font*2.5), 0, PC_PATH.'libs/data/font/elephant.ttf', $w_text);
  164. $width = $temp[2] - $temp[6];
  165. $height = $temp[3] - $temp[7];
  166. unset($temp);
  167. }
  168. switch($w_pos) {
  169. case 1:
  170. $wx = 5;
  171. $wy = 5;
  172. break;
  173. case 2:
  174. $wx = ($source_w - $width) / 2;
  175. $wy = 0;
  176. break;
  177. case 3:
  178. $wx = $source_w - $width;
  179. $wy = 0;
  180. break;
  181. case 4:
  182. $wx = 0;
  183. $wy = ($source_h - $height) / 2;
  184. break;
  185. case 5:
  186. $wx = ($source_w - $width) / 2;
  187. $wy = ($source_h - $height) / 2;
  188. break;
  189. case 6:
  190. $wx = $source_w - $width;
  191. $wy = ($source_h - $height) / 2;
  192. break;
  193. case 7:
  194. $wx = 0;
  195. $wy = $source_h - $height;
  196. break;
  197. case 8:
  198. $wx = ($source_w - $width) / 2;
  199. $wy = $source_h - $height;
  200. break;
  201. case 9:
  202. $wx = $source_w - $width;
  203. $wy = $source_h - $height;
  204. break;
  205. case 10:
  206. $wx = rand(0,($source_w - $width));
  207. $wy = rand(0,($source_h - $height));
  208. break;
  209. default:
  210. $wx = rand(0,($source_w - $width));
  211. $wy = rand(0,($source_h - $height));
  212. break;
  213. }
  214. if($ifwaterimage) {
  215. if($water_info[2] == 3) {
  216. imagecopy($source_img, $water_img, $wx, $wy, 0, 0, $width, $height);
  217. } else {
  218. imagecopymerge($source_img, $water_img, $wx, $wy, 0, 0, $width, $height, $this->w_pct);
  219. }
  220. } else {
  221. if(!empty($w_color) && (strlen($w_color)==7)) {
  222. $r = hexdec(substr($w_color,1,2));
  223. $g = hexdec(substr($w_color,3,2));
  224. $b = hexdec(substr($w_color,5));
  225. } else {
  226. return;
  227. }
  228. imagestring($source_img,$w_font,$wx,$wy,$w_text,imagecolorallocate($source_img,$r,$g,$b));
  229. }
  230. switch($source_info[2]) {
  231. case 1 :
  232. imagegif($source_img, $target);
  233. break;
  234. case 2 :
  235. imagejpeg($source_img, $target, $this->w_quality);
  236. break;
  237. case 3 :
  238. imagepng($source_img, $target);
  239. break;
  240. default :
  241. return;
  242. }
  243. if(isset($water_info)) {
  244. unset($water_info);
  245. }
  246. if(isset($water_img)) {
  247. imagedestroy($water_img);
  248. }
  249. unset($source_info);
  250. imagedestroy($source_img);
  251. return true;
  252. }
  253. function check($image) {
  254. return extension_loaded('gd') && preg_match("/\.(jpg|jpeg|gif|png)/i", $image, $m) && file_exists($image) && function_exists('imagecreatefrom'.($m[1] == 'jpg' ? 'jpeg' : $m[1]));
  255. }
  256. }
  257. ?>