PageRenderTime 24ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/fwoq/core/libs/img_engine/image_gd.php

http://phpfor.googlecode.com/
PHP | 493 lines | 337 code | 30 blank | 126 comment | 62 complexity | 80f6ef5ebe5836b3add7aa3e7fa0c4e2 MD5 | raw file
  1. <?php
  2. /**
  3. * $Id: image_gd.php 228 2011-09-19 03:39:11Z lingter@gmail.com $
  4. *
  5. * @author : Lingter
  6. * @support : http://www.meiu.cn
  7. * @copyright : (c)2010 meiu.cn lingter@gmail.com
  8. */
  9. class image_gd {
  10. /**
  11. * ??????
  12. *
  13. * @var image
  14. */
  15. var $image;
  16. /**
  17. * ????
  18. *
  19. * @var imagetype
  20. */
  21. var $image_type;
  22. var $image_quality=90;
  23. var $true_color = false;
  24. /**
  25. * ????
  26. *
  27. * @param string $filename ??????
  28. * @return void
  29. */
  30. function load($filename) {
  31. $image_info = getimagesize($filename);
  32. $this->image_type = $image_info[2];
  33. if( $this->image_type == IMAGETYPE_JPEG ) {
  34. $this->image = imagecreatefromjpeg($filename);
  35. } elseif( $this->image_type == IMAGETYPE_GIF ) {
  36. $this->image = imagecreatefromgif($filename);
  37. } elseif( $this->image_type == IMAGETYPE_PNG ) {
  38. $this->image = imagecreatefrompng($filename);
  39. }else{
  40. return false;
  41. }
  42. if(function_exists("imagecopyresampled") && function_exists("imagecreatetruecolor") && $this->image_type != IMAGETYPE_GIF){
  43. $this->true_color = true;
  44. }
  45. return true;
  46. }
  47. function supportType(){
  48. return array('jpg','jpeg','gif','png');
  49. }
  50. function setQuality($q){
  51. if($q>0)
  52. $this->image_quality = $q;
  53. }
  54. /**
  55. * ?????
  56. *
  57. * @return string ???
  58. */
  59. function getExtension(){
  60. if( $this->image_type == IMAGETYPE_JPEG ) return 'jpg';
  61. elseif( $this->image_type == IMAGETYPE_GIF ) return 'gif';
  62. elseif( $this->image_type == IMAGETYPE_PNG ) return 'png';
  63. }
  64. /**
  65. * ??????????
  66. *
  67. * @param string $filename ???
  68. * @param int $image_type ????
  69. * return volid
  70. */
  71. function save($filename) {
  72. $image_type = $this->image_type;
  73. if( $image_type == IMAGETYPE_JPEG ) {
  74. imagejpeg($this->image,$filename,$this->image_quality);
  75. } elseif( $image_type == IMAGETYPE_GIF ) {
  76. imagegif($this->image,$filename);
  77. } elseif( $image_type == IMAGETYPE_PNG ) {
  78. imagepng($this->image,$filename);
  79. }
  80. }
  81. /**
  82. * ?????????
  83. *
  84. * @param int $image_type ????
  85. * @return void
  86. */
  87. function output() {
  88. $image_type = $this->image_type;
  89. if( $image_type == IMAGETYPE_JPEG ) {
  90. header('Content-Type: image/jpeg');
  91. imagejpeg($this->image,NULL,$this->image_quality);
  92. } elseif( $image_type == IMAGETYPE_GIF ) {
  93. header('Content-type: image/gif');
  94. imagegif($this->image);
  95. } elseif( $image_type == IMAGETYPE_PNG ) {
  96. header('Content-type: image/png');
  97. imagepng($this->image);
  98. }
  99. }
  100. /**
  101. * ??????
  102. *
  103. * @return int ????
  104. */
  105. function getWidth() {
  106. return imagesx($this->image);
  107. }
  108. /**
  109. * ??????
  110. *
  111. * @return int ????
  112. */
  113. function getHeight() {
  114. return imagesy($this->image);
  115. }
  116. /**
  117. * ??????????
  118. *
  119. * @param int $height ????
  120. */
  121. function resizeToHeight($height) {
  122. $ratio = $height / $this->getHeight();
  123. $width = $this->getWidth() * $ratio;
  124. $this->resize($width,$height);
  125. }
  126. /**
  127. * ???????
  128. *
  129. * @param int $w ????
  130. * @param int $h ????
  131. */
  132. function resizeTo($w=0, $h=0) {
  133. if($w>0 && $h>0) return $this->resize($w,$h);
  134. else if($w>0) return $this->resizeToWidth($w);
  135. else if($h>0) return $this->resizeToHeight($h);
  136. }
  137. /**
  138. * ???????????
  139. * @param int $w ????
  140. * @param int $h ????
  141. */
  142. function resizeScale($w=0,$h=0){
  143. if($w == 0 && $h>0){
  144. return $this->resizeToHeight($h);
  145. }
  146. if($h == 0 && $w>0){
  147. return $this->resizeToWidth($w);
  148. }
  149. if($w == 0 && $h==0){
  150. return false;
  151. }
  152. $maxwidth = $w;
  153. $maxheight = $h;
  154. $width = $this->getWidth();
  155. $height = $this->getHeight();
  156. $RESIZEWIDTH = $RESIZEHEIGHT = false;
  157. if($maxwidth && $width > $maxwidth){
  158. $widthratio = $maxwidth/$width;
  159. $RESIZEWIDTH=true;
  160. }
  161. if($maxheight && $height > $maxheight){
  162. $heightratio = $maxheight/$height;
  163. $RESIZEHEIGHT=true;
  164. }
  165. if($RESIZEWIDTH && $RESIZEHEIGHT){
  166. if($widthratio < $heightratio){
  167. return $this->resizeToWidth($w);
  168. }else{
  169. return $this->resizeToHeight($h);
  170. }
  171. }elseif($RESIZEWIDTH){
  172. return $this->resizeToWidth($w);
  173. }elseif($RESIZEHEIGHT){
  174. return $this->resizeToHeight($h);
  175. }
  176. }
  177. /**
  178. * ????????????????
  179. *
  180. * @param int $v ????/??
  181. */
  182. function square($v){
  183. $width = $this->getWidth();
  184. $height = $this->getHeight();
  185. $left = 0;
  186. $top = 0;
  187. if($width>$height){
  188. $this->resizeToHeight($v);
  189. $left = ceil(($v/$height * $width - $v)/2);
  190. }else{
  191. $this->resizeToWidth($v);
  192. $top = ceil(($v/$width * $height - $v)/2);
  193. }
  194. $this->cut($v,$v,$left,$top);
  195. }
  196. /**
  197. * ??????????
  198. *
  199. * @param int $width ????
  200. */
  201. function resizeToWidth($width) {
  202. if($width>=$this->getWidth()) return;
  203. $ratio = $width / $this->getWidth();
  204. $height = $this->getHeight() * $ratio;
  205. $this->resize($width,$height);
  206. }
  207. /**
  208. * ???????????
  209. *
  210. * @param int $scale ????
  211. */
  212. function scale($scale) {
  213. $width = $this->getWidth() * $scale/100;
  214. $height = $this->getHeight() * $scale/100;
  215. $this->resize($width,$height);
  216. }
  217. /**
  218. * ??????
  219. *
  220. * @param int $width ????
  221. * @param int $height ????
  222. */
  223. function resize($width,$height) {
  224. if($this->true_color){
  225. $newim = imagecreatetruecolor($width, $height);
  226. imagecopyresampled($newim, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
  227. }else{
  228. $newim = imagecreate($width, $height);
  229. imagecopyresized($newim, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
  230. }
  231. $this->image = $newim;
  232. }
  233. /**
  234. * ????
  235. *
  236. * @param int $width ????
  237. * @param int $height ????
  238. */
  239. function cut($width,$height,$left = 0,$top = 0){
  240. if($this->true_color){
  241. $new_image = imagecreatetruecolor($width, $height);
  242. }else{
  243. $new_image = imagecreate($width, $height);
  244. }
  245. imagecopy($new_image, $this->image, 0, 0, $left, $top, $width, $height);
  246. $this->image = $new_image;
  247. }
  248. /**
  249. * ?????????????????
  250. *
  251. * @param int $top ????
  252. * @param int $height ????
  253. */
  254. function vcut($top,$height){
  255. $width = $this->getWidth();
  256. $height = $this->getHeight()-$top+$height;
  257. if($height<200) return;
  258. if($this->true_color){
  259. $new_image = imagecreatetruecolor($width, $height);
  260. }else{
  261. $new_image = imagecreate($width, $height);
  262. }
  263. imagecopy($new_image, $this->image, 0, 0, 0, $top, $width, $height);
  264. $this->image = $new_image;
  265. }
  266. /*
  267. ????
  268. */
  269. function rotate($dgree){
  270. $tran = imagecolortransparent($this->image,NULL);
  271. $new_image = imagerotate($this->image, $dgree , $tran);
  272. $this->image = $new_image;
  273. }
  274. function waterMarkSetting($param){
  275. $this->param = $param;
  276. }
  277. function waterMarkImg(){
  278. if(empty($this->param['water_mark_image']) || !file_exists($this->param['water_mark_image'])){
  279. return false;
  280. }
  281. $water_info = getimagesize($this->param['water_mark_image']);
  282. $w = $water_info[0];//????????
  283. $h = $water_info[1];//????????
  284. switch($water_info[2])//?????????
  285. {
  286. case 1:$water_im = imagecreatefromgif($this->param['water_mark_image']);break;
  287. case 2:$water_im = imagecreatefromjpeg($this->param['water_mark_image']);break;
  288. case 3:$water_im = imagecreatefrompng($this->param['water_mark_image']);break;
  289. default:return false;
  290. }
  291. $ground_w = $this->getWidth();
  292. $ground_h = $this->getHeight();
  293. if( $ground_w<$w || $ground_h<$h ){
  294. return false;
  295. }
  296. switch($this->param['water_mark_pos'])
  297. {
  298. case 0://??
  299. $posX = rand(5,($ground_w - $w - 5));
  300. $posY = rand(5,($ground_h - $h - 5));
  301. break;
  302. case 1://1?????
  303. $posX = 5;
  304. $posY = 5;
  305. break;
  306. case 2://2?????
  307. $posX = ($ground_w - $w) / 2;
  308. $posY = 5;
  309. break;
  310. case 3://3?????
  311. $posX = $ground_w - $w -5;
  312. $posY = 5;
  313. break;
  314. case 4://4?????
  315. $posX = 5;
  316. $posY = ($ground_h - $h) / 2;
  317. break;
  318. case 5://5?????
  319. $posX = ($ground_w - $w) / 2;
  320. $posY = ($ground_h - $h) / 2;
  321. break;
  322. case 6://6?????
  323. $posX = $ground_w - $w - 5;
  324. $posY = ($ground_h - $h) / 2;
  325. break;
  326. case 7://7?????
  327. $posX = 5;
  328. $posY = $ground_h - $h - 5;
  329. break;
  330. case 8://8?????
  331. $posX = ($ground_w - $w) / 2;
  332. $posY = $ground_h - $h - 5;
  333. break;
  334. case 9://9?????
  335. $posX = $ground_w - $w - 5;
  336. $posY = $ground_h - $h - 5;
  337. break;
  338. default://??
  339. $posX = rand(5,($ground_w - $w - 5));
  340. $posY = rand(5,($ground_h - $h - 5));
  341. break;
  342. }
  343. //?????????
  344. imagealphablending($this->image, true);
  345. if(function_exists('imagecopymerge') && $this->param['water_mark_opacity'] != 0){
  346. @imagecopymerge($this->image, $water_im, $posX, $posY, 0, 0, $w,$h,$this->param['water_mark_opacity']);
  347. }else{
  348. imagecopy($this->image, $water_im, $posX, $posY, 0, 0, $w,$h);//?????????
  349. }
  350. imagedestroy($water_im);
  351. }
  352. function waterMarkFont(){
  353. if($this->param['water_mark_color']){
  354. $color = $this->param['water_mark_color'];
  355. }else{
  356. $color = '#000000';
  357. }
  358. $r = hexdec( substr( $color, 1, 2 ) );
  359. $g = hexdec( substr( $color, 3, 2 ) );
  360. $b = hexdec( substr( $color, 5, 2 ) );
  361. if($this->param['water_mark_opacity']>0 && $this->param['water_mark_opacity']<100){
  362. $fontcolor = imagecolorallocatealpha( $this->image, $r, $g, $b ,$this->param['water_mark_opacity']);
  363. }else{
  364. $fontcolor = imagecolorallocate( $this->image, $r, $g, $b );
  365. }
  366. $box = ImageTTFBBox(
  367. $this->param['water_mark_fontsize'],
  368. $this->param['water_mark_angle'],
  369. $this->param['water_mark_font'],
  370. $this->param['water_mark_string']);
  371. $ground_w = $this->getWidth();
  372. $ground_h = $this->getHeight();
  373. $h = max($box[1], $box[3]) - min($box[5], $box[7]);
  374. $w = max($box[2], $box[4]) - min($box[0], $box[6]);
  375. $ax = min($box[0], $box[6]) * -1;
  376. $ay = min($box[5], $box[7]) * -1;
  377. switch($this->param['water_mark_pos'])
  378. {
  379. case 0://??
  380. $posX = rand(5,($ground_w - $w - 5));
  381. $posY = rand(5,($ground_h - $h - 5));
  382. break;
  383. case 1://1?????
  384. $posX = 5;
  385. $posY = 5;
  386. break;
  387. case 2://2?????
  388. $posX = ($ground_w - $w) / 2;
  389. $posY = 5;
  390. break;
  391. case 3://3?????
  392. $posX = $ground_w - $w -5;
  393. $posY = 5;
  394. break;
  395. case 4://4?????
  396. $posX = 5;
  397. $posY = ($ground_h - $h) / 2;
  398. break;
  399. case 5://5?????
  400. $posX = ($ground_w - $w) / 2;
  401. $posY = ($ground_h - $h) / 2;
  402. break;
  403. case 6://6?????
  404. $posX = $ground_w - $w - 5;
  405. $posY = ($ground_h - $h) / 2;
  406. break;
  407. case 7://7?????
  408. $posX = 5;
  409. $posY = $ground_h - $h - 5;
  410. break;
  411. case 8://8?????
  412. $posX = ($ground_w - $w) / 2;
  413. $posY = $ground_h - $h - 5;
  414. break;
  415. case 9://9?????
  416. $posX = $ground_w - $w - 5;
  417. $posY = $ground_h - $h - 5;
  418. break;
  419. default://??
  420. $posX = rand(5,($ground_w - $w - 5));
  421. $posY = rand(5,($ground_h - $h - 5));
  422. break;
  423. }
  424. /*if($this->param['water_mark_shadow'] == 'gray'){
  425. $shadowcolor = imagecolorallocate( $this->image, 160, 160, 160 );
  426. imagettftext($this->image,
  427. $this->param['water_mark_fontsize'],
  428. $this->param['water_mark_angle'],
  429. $posX + $ax+1,
  430. $posY + $ay+1,
  431. $shadowcolor,
  432. $this->param['water_mark_font'],
  433. $this->param['water_mark_string']);
  434. }elseif($this->param['water_mark_shadow'] == 'white'){
  435. $shadowcolor = imagecolorallocate( $this->image, 255, 255, 255 );
  436. imagettftext($this->image,
  437. $this->param['water_mark_fontsize'],
  438. $this->param['water_mark_angle'],
  439. $posX + $ax + 1,
  440. $posY + $ay + 1,
  441. $shadowcolor,
  442. $this->param['water_mark_font'],
  443. $this->param['water_mark_string']);
  444. }*/
  445. imagettftext($this->image,
  446. $this->param['water_mark_fontsize'],
  447. $this->param['water_mark_angle'],
  448. $posX + $ax,
  449. $posY + $ay,
  450. $fontcolor,
  451. $this->param['water_mark_font'],
  452. $this->param['water_mark_string']);
  453. }
  454. function waterMark(){
  455. //??????
  456. if($this->param['water_mark_type'] == 'image'){
  457. $this->waterMarkImg();
  458. }elseif($this->param['water_mark_type'] == 'font'){
  459. $this->watermarkFont();
  460. }
  461. return false;
  462. }
  463. }