PageRenderTime 43ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/sop/rc1/ThinkPHP/Lib/ORG/Util/Image.class.php

http://iiccms.googlecode.com/
PHP | 793 lines | 512 code | 44 blank | 237 comment | 75 complexity | 4ffea04d5ce6f386225e5e9ed90e7d2a MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2009 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. // $Id$
  12. /**
  13. +------------------------------------------------------------------------------
  14. * ??????
  15. +------------------------------------------------------------------------------
  16. * @category ORG
  17. * @package ORG
  18. * @subpackage Util
  19. * @author liu21st <liu21st@gmail.com>
  20. * @version $Id$
  21. +------------------------------------------------------------------------------
  22. */
  23. class Image extends Think
  24. {//?????
  25. /**
  26. +----------------------------------------------------------
  27. * ??????
  28. *
  29. +----------------------------------------------------------
  30. * @static
  31. * @access public
  32. +----------------------------------------------------------
  33. * @param string $image ?????
  34. +----------------------------------------------------------
  35. * @return mixed
  36. +----------------------------------------------------------
  37. */
  38. static function getImageInfo($img) {
  39. $imageInfo = getimagesize($img);
  40. if( $imageInfo!== false) {
  41. $imageType = strtolower(substr(image_type_to_extension($imageInfo[2]),1));
  42. $imageSize = filesize($img);
  43. $info = array(
  44. "width"=>$imageInfo[0],
  45. "height"=>$imageInfo[1],
  46. "type"=>$imageType,
  47. "size"=>$imageSize,
  48. "mime"=>$imageInfo['mime']
  49. );
  50. return $info;
  51. }else {
  52. return false;
  53. }
  54. }
  55. /**
  56. +----------------------------------------------------------
  57. * ?????????
  58. * ??URL??
  59. +----------------------------------------------------------
  60. * @static
  61. * @access public
  62. +----------------------------------------------------------
  63. * @param string $imgFile ?????
  64. * @param string $text ?????
  65. * @param string $width ????
  66. * @param string $height ????
  67. +----------------------------------------------------------
  68. * @return void
  69. +----------------------------------------------------------
  70. */
  71. static function showImg($imgFile,$text='',$width=80,$height=30) {
  72. //????????
  73. $info = Image::getImageInfo($imgFile);
  74. if($info !== false) {
  75. $createFun = str_replace('/','createfrom',$info['mime']);
  76. $im = $createFun($imgFile);
  77. if($im) {
  78. $ImageFun= str_replace('/','',$info['mime']);
  79. if(!empty($text)) {
  80. $tc = imagecolorallocate($im, 0, 0, 0);
  81. imagestring($im, 3, 5, 5, $text, $tc);
  82. }
  83. if($info['type']=='png' || $info['type']=='gif') {
  84. imagealphablending($im, false);//?????????
  85. imagesavealpha($im,true);//??????? alpha ????
  86. }
  87. header("Content-type: ".$info['mime']);
  88. $ImageFun($im);
  89. imagedestroy($im);
  90. return ;
  91. }
  92. }
  93. //?????????????????PNG??
  94. $im = imagecreatetruecolor($width, $height);
  95. $bgc = imagecolorallocate($im, 255, 255, 255);
  96. $tc = imagecolorallocate($im, 0, 0, 0);
  97. imagefilledrectangle($im, 0, 0, 150, 30, $bgc);
  98. imagestring($im, 4, 5, 5, "NO PIC", $tc);
  99. Image::output($im);
  100. return ;
  101. }
  102. /**
  103. +----------------------------------------------------------
  104. * ?????
  105. +----------------------------------------------------------
  106. * @static
  107. * @access public
  108. +----------------------------------------------------------
  109. * @param string $image ??
  110. * @param string $type ????
  111. * @param string $thumbname ??????
  112. * @param string $maxWidth ??
  113. * @param string $maxHeight ??
  114. * @param string $position ???????
  115. * @param boolean $interlace ??????
  116. +----------------------------------------------------------
  117. * @return void
  118. +----------------------------------------------------------
  119. */
  120. static function thumb($image,$thumbname='',$type='',$maxWidth=200,$maxHeight=200,$interlace=true,$suffix='_thumb')
  121. {
  122. // ??????
  123. $info = Image::getImageInfo($image);
  124. if($info !== false) {
  125. $srcWidth = $info['width'];
  126. $srcHeight = $info['height'];
  127. $type = empty($type)?$info['type']:$type;
  128. $type = strtolower($type);
  129. $interlace = $interlace? 1:0;
  130. unset($info);
  131. $scale = min($maxWidth/$srcWidth, $maxHeight/$srcHeight); // ??????
  132. if($scale>=1) {
  133. // ??????????
  134. $width = $srcWidth;
  135. $height = $srcHeight;
  136. }else{
  137. // ?????
  138. $width = (int)($srcWidth*$scale);
  139. $height = (int)($srcHeight*$scale);
  140. }
  141. // ????
  142. $createFun = 'ImageCreateFrom'.($type=='jpg'?'jpeg':$type);
  143. $srcImg = $createFun($image);
  144. //?????
  145. if($type!='gif' && function_exists('imagecreatetruecolor'))
  146. $thumbImg = imagecreatetruecolor($width, $height);
  147. else
  148. $thumbImg = imagecreate($width, $height);
  149. // ????
  150. if(function_exists("ImageCopyResampled"))
  151. imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth,$srcHeight);
  152. else
  153. imagecopyresized($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth,$srcHeight);
  154. if('gif'==$type || 'png'==$type) {
  155. //imagealphablending($thumbImg, false);//?????????
  156. //imagesavealpha($thumbImg,true);//??????? alpha ????
  157. $background_color = imagecolorallocate($thumbImg, 0,255,0); // ??????
  158. imagecolortransparent($thumbImg,$background_color); // ????????????????????
  159. }
  160. // ?jpeg????????
  161. if('jpg'==$type || 'jpeg'==$type) imageinterlace($thumbImg,$interlace);
  162. //$gray=ImageColorAllocate($thumbImg,255,0,0);
  163. //ImageString($thumbImg,2,5,5,"ThinkPHP",$gray);
  164. // ????
  165. $imageFun = 'image'.($type=='jpg'?'jpeg':$type);
  166. $thumbname = empty($thumbname)? substr($image,0,strrpos($image, '.')).$suffix.'.'.$type : $thumbname;
  167. $imageFun($thumbImg,$thumbname);
  168. imagedestroy($thumbImg);
  169. imagedestroy($srcImg);
  170. return $thumbname;
  171. }
  172. return false;
  173. }
  174. /**
  175. +----------------------------------------------------------
  176. * ????????????
  177. +----------------------------------------------------------
  178. * @static
  179. * @access public
  180. +----------------------------------------------------------
  181. * @param string $string ???
  182. * @param string $size ???? width,height ?? array(width,height)
  183. * @param string $font ???? fontface,fontsize ?? array(fontface,fontsize)
  184. * @param string $type ???? ??PNG
  185. * @param integer $disturb ???? 1 ??? 2 ??? 3 ???? 0 ???
  186. * @param bool $border ????? array(color)
  187. +----------------------------------------------------------
  188. * @return string
  189. +----------------------------------------------------------
  190. */
  191. static function buildString($string,$rgb=array(),$filename='',$type='png',$disturb=1,$border=true) {
  192. if(is_string($size)) $size = explode(',',$size);
  193. $width = $size[0];
  194. $height = $size[1];
  195. if(is_string($font)) $font = explode(',',$font);
  196. $fontface = $font[0];
  197. $fontsize = $font[1];
  198. $length = strlen($string);
  199. $width = ($length*9+10)>$width?$length*9+10:$width;
  200. $height = 22;
  201. if ( $type!='gif' && function_exists('imagecreatetruecolor')) {
  202. $im = @imagecreatetruecolor($width,$height);
  203. }else {
  204. $im = @imagecreate($width,$height);
  205. }
  206. if(empty($rgb)) {
  207. $color = imagecolorallocate($im, 102, 104, 104);
  208. }else{
  209. $color = imagecolorallocate($im, $rgb[0], $rgb[1], $rgb[2]);
  210. }
  211. $backColor = imagecolorallocate($im, 255,255,255); //???????
  212. $borderColor = imagecolorallocate($im, 100, 100, 100); //???
  213. $pointColor = imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)); //???
  214. @imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);
  215. @imagerectangle($im, 0, 0, $width-1, $height-1, $borderColor);
  216. @imagestring($im, 5, 5, 3, $string, $color);
  217. if(!empty($disturb)) {
  218. // ????
  219. if($disturb=1 || $disturb=3) {
  220. for($i=0;$i<25;$i++){
  221. imagesetpixel($im,mt_rand(0,$width),mt_rand(0,$height),$pointColor);
  222. }
  223. }elseif($disturb=2 || $disturb=3){
  224. for($i=0;$i<10;$i++){
  225. imagearc($im,mt_rand(-10,$width),mt_rand(-10,$height),mt_rand(30,300),mt_rand(20,200),55,44,$pointColor);
  226. }
  227. }
  228. }
  229. Image::output($im,$type,$filename);
  230. }
  231. /**
  232. +----------------------------------------------------------
  233. * ???????
  234. +----------------------------------------------------------
  235. * @static
  236. * @access public
  237. +----------------------------------------------------------
  238. * @param string $length ??
  239. * @param string $mode ??
  240. * @param string $type ????
  241. * @param string $width ??
  242. * @param string $height ??
  243. +----------------------------------------------------------
  244. * @return string
  245. +----------------------------------------------------------
  246. */
  247. static function buildImageVerify($length=4,$mode=1,$type='png',$width=48,$height=22,$verifyName='verify')
  248. {
  249. import('ORG.Util.String');
  250. $randval = String::rand_string($length,$mode);
  251. $_SESSION[$verifyName]= md5($randval);
  252. $width = ($length*10+10)>$width?$length*10+10:$width;
  253. if ( $type!='gif' && function_exists('imagecreatetruecolor')) {
  254. $im = @imagecreatetruecolor($width,$height);
  255. }else {
  256. $im = @imagecreate($width,$height);
  257. }
  258. $r = Array(225,255,255,223);
  259. $g = Array(225,236,237,255);
  260. $b = Array(225,236,166,125);
  261. $key = mt_rand(0,3);
  262. $backColor = imagecolorallocate($im, $r[$key],$g[$key],$b[$key]); //???????
  263. $borderColor = imagecolorallocate($im, 100, 100, 100); //???
  264. $pointColor = imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)); //???
  265. @imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);
  266. @imagerectangle($im, 0, 0, $width-1, $height-1, $borderColor);
  267. $stringColor = imagecolorallocate($im,mt_rand(0,200),mt_rand(0,120),mt_rand(0,120));
  268. // ??
  269. for($i=0;$i<10;$i++){
  270. $fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
  271. imagearc($im,mt_rand(-10,$width),mt_rand(-10,$height),mt_rand(30,300),mt_rand(20,200),55,44,$fontcolor);
  272. }
  273. for($i=0;$i<25;$i++){
  274. $fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
  275. imagesetpixel($im,mt_rand(0,$width),mt_rand(0,$height),$pointColor);
  276. }
  277. for($i=0;$i<$length;$i++) {
  278. imagestring($im,5,$i*10+5,mt_rand(1,8),$randval{$i}, $stringColor);
  279. }
  280. // @imagestring($im, 5, 5, 3, $randval, $stringColor);
  281. Image::output($im,$type);
  282. }
  283. // ?????
  284. static function GBVerify($length=4,$type='png',$width=180,$height=50,$fontface='simhei.ttf',$verifyName='verify') {
  285. import('ORG.Util.String');
  286. $code = String::rand_string($length,4);
  287. $width = ($length*45)>$width?$length*45:$width;
  288. $_SESSION[$verifyName]= md5($code);
  289. $im=imagecreatetruecolor($width,$height);
  290. $borderColor = imagecolorallocate($im, 100, 100, 100); //???
  291. $bkcolor=imagecolorallocate($im,250,250,250);
  292. imagefill($im,0,0,$bkcolor);
  293. @imagerectangle($im, 0, 0, $width-1, $height-1, $borderColor);
  294. // ??
  295. for($i=0;$i<15;$i++){
  296. $fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
  297. imagearc($im,mt_rand(-10,$width),mt_rand(-10,$height),mt_rand(30,300),mt_rand(20,200),55,44,$fontcolor);
  298. }
  299. for($i=0;$i<255;$i++){
  300. $fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
  301. imagesetpixel($im,mt_rand(0,$width),mt_rand(0,$height),$fontcolor);
  302. }
  303. if(!is_file($fontface)) {
  304. $fontface = dirname(__FILE__)."/".$fontface;
  305. }
  306. for($i=0;$i<$length;$i++){
  307. $fontcolor=imagecolorallocate($im,mt_rand(0,120),mt_rand(0,120),mt_rand(0,120)); //??????????????
  308. $codex= msubstr($code,$i,1);
  309. imagettftext($im,mt_rand(16,20),mt_rand(-60,60),40*$i+20,mt_rand(30,35),$fontcolor,$fontface,$codex);
  310. }
  311. Image::output($im,$type);
  312. }
  313. /**
  314. +----------------------------------------------------------
  315. * ??????????
  316. +----------------------------------------------------------
  317. * @static
  318. * @access public
  319. +----------------------------------------------------------
  320. * @param string $image ??????
  321. * @param string $type ???????????
  322. +----------------------------------------------------------
  323. * @return string
  324. +----------------------------------------------------------
  325. */
  326. static function showASCIIImg($image,$string='',$type='')
  327. {
  328. $info = Image::getImageInfo($image);
  329. if($info !== false) {
  330. $type = empty($type)?$info['type']:$type;
  331. unset($info);
  332. // ????
  333. $createFun = 'ImageCreateFrom'.($type=='jpg'?'jpeg':$type);
  334. $im = $createFun($image);
  335. $dx = imagesx($im);
  336. $dy = imagesy($im);
  337. $i = 0;
  338. $out = '<span style="padding:0px;margin:0;line-height:100%;font-size:1px;">';
  339. set_time_limit(0);
  340. for($y = 0; $y < $dy; $y++) {
  341. for($x=0; $x < $dx; $x++) {
  342. $col = imagecolorat($im, $x, $y);
  343. $rgb = imagecolorsforindex($im,$col);
  344. $str = empty($string)?'*':$string[$i++];
  345. $out .= sprintf('<span style="margin:0px;color:#%02x%02x%02x">'.$str.'</span>',$rgb['red'],$rgb['green'],$rgb['blue']);
  346. }
  347. $out .= "<br>\n";
  348. }
  349. $out .= '</span>';
  350. imagedestroy($im);
  351. return $out;
  352. }
  353. return false;
  354. }
  355. /**
  356. +----------------------------------------------------------
  357. * ?????????
  358. +----------------------------------------------------------
  359. * @static
  360. * @access public
  361. +----------------------------------------------------------
  362. * @param string $type ????
  363. * @param string $width ??
  364. * @param string $height ??
  365. +----------------------------------------------------------
  366. * @return string
  367. +----------------------------------------------------------
  368. */
  369. static function showAdvVerify($type='png',$width=180,$height=40,$verifyName='verifyCode')
  370. {
  371. $rand = range('a','z');
  372. shuffle($rand);
  373. $verifyCode = array_slice($rand,0,10);
  374. $letter = implode(" ",$verifyCode);
  375. $_SESSION[$verifyName] = $verifyCode;
  376. $im = imagecreate($width,$height);
  377. $r = array(225,255,255,223);
  378. $g = array(225,236,237,255);
  379. $b = array(225,236,166,125);
  380. $key = mt_rand(0,3);
  381. $backColor = imagecolorallocate($im, $r[$key],$g[$key],$b[$key]);
  382. $borderColor = imagecolorallocate($im, 100, 100, 100); //???
  383. imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);
  384. imagerectangle($im, 0, 0, $width-1, $height-1, $borderColor);
  385. $numberColor = imagecolorallocate($im, 255,rand(0,100), rand(0,100));
  386. $stringColor = imagecolorallocate($im, rand(0,100), rand(0,100), 255);
  387. // ????
  388. /*
  389. for($i=0;$i<10;$i++){
  390. $fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
  391. imagearc($im,mt_rand(-10,$width),mt_rand(-10,$height),mt_rand(30,300),mt_rand(20,200),55,44,$fontcolor);
  392. }
  393. for($i=0;$i<255;$i++){
  394. $fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
  395. imagesetpixel($im,mt_rand(0,$width),mt_rand(0,$height),$fontcolor);
  396. }*/
  397. imagestring($im, 5, 5, 1, "0 1 2 3 4 5 6 7 8 9", $numberColor);
  398. imagestring($im, 5, 5, 20, $letter, $stringColor);
  399. Image::output($im,$type);
  400. }
  401. /**
  402. +----------------------------------------------------------
  403. * ??UPC-A???
  404. +----------------------------------------------------------
  405. * @static
  406. +----------------------------------------------------------
  407. * @param string $type ????
  408. * @param string $type ????
  409. * @param string $lw ????
  410. * @param string $hi ????
  411. +----------------------------------------------------------
  412. * @return string
  413. +----------------------------------------------------------
  414. */
  415. static function UPCA($code,$type='png',$lw=2,$hi=100) {
  416. static $Lencode = array('0001101','0011001','0010011','0111101','0100011',
  417. '0110001','0101111','0111011','0110111','0001011');
  418. static $Rencode = array('1110010','1100110','1101100','1000010','1011100',
  419. '1001110','1010000','1000100','1001000','1110100');
  420. $ends = '101';
  421. $center = '01010';
  422. /* UPC-A Must be 11 digits, we compute the checksum. */
  423. if ( strlen($code) != 11 ) { die("UPC-A Must be 11 digits."); }
  424. /* Compute the EAN-13 Checksum digit */
  425. $ncode = '0'.$code;
  426. $even = 0; $odd = 0;
  427. for ($x=0;$x<12;$x++) {
  428. if ($x % 2) { $odd += $ncode[$x]; } else { $even += $ncode[$x]; }
  429. }
  430. $code.=(10 - (($odd * 3 + $even) % 10)) % 10;
  431. /* Create the bar encoding using a binary string */
  432. $bars=$ends;
  433. $bars.=$Lencode[$code[0]];
  434. for($x=1;$x<6;$x++) {
  435. $bars.=$Lencode[$code[$x]];
  436. }
  437. $bars.=$center;
  438. for($x=6;$x<12;$x++) {
  439. $bars.=$Rencode[$code[$x]];
  440. }
  441. $bars.=$ends;
  442. /* Generate the Barcode Image */
  443. if ( $type!='gif' && function_exists('imagecreatetruecolor')) {
  444. $im = imagecreatetruecolor($lw*95+30,$hi+30);
  445. }else {
  446. $im = imagecreate($lw*95+30,$hi+30);
  447. }
  448. $fg = ImageColorAllocate($im, 0, 0, 0);
  449. $bg = ImageColorAllocate($im, 255, 255, 255);
  450. ImageFilledRectangle($im, 0, 0, $lw*95+30, $hi+30, $bg);
  451. $shift=10;
  452. for ($x=0;$x<strlen($bars);$x++) {
  453. if (($x<10) || ($x>=45 && $x<50) || ($x >=85)) { $sh=10; } else { $sh=0; }
  454. if ($bars[$x] == '1') { $color = $fg; } else { $color = $bg; }
  455. ImageFilledRectangle($im, ($x*$lw)+15,5,($x+1)*$lw+14,$hi+5+$sh,$color);
  456. }
  457. /* Add the Human Readable Label */
  458. ImageString($im,4,5,$hi-5,$code[0],$fg);
  459. for ($x=0;$x<5;$x++) {
  460. ImageString($im,5,$lw*(13+$x*6)+15,$hi+5,$code[$x+1],$fg);
  461. ImageString($im,5,$lw*(53+$x*6)+15,$hi+5,$code[$x+6],$fg);
  462. }
  463. ImageString($im,4,$lw*95+17,$hi-5,$code[11],$fg);
  464. /* Output the Header and Content. */
  465. Image::output($im,$type);
  466. }
  467. static function output($im,$type='png',$filename='')
  468. {
  469. header("Content-type: image/".$type);
  470. $ImageFun='image'.$type;
  471. if(empty($filename)) {
  472. $ImageFun($im);
  473. }else{
  474. $ImageFun($im,$filename);
  475. }
  476. imagedestroy($im);
  477. }
  478. /**
  479. +----------------------------------------------------------
  480. * ???????
  481. +----------------------------------------------------------
  482. * @static public
  483. +----------------------------------------------------------
  484. * @param string $source ????
  485. * @param string $water ????
  486. * @param string $$savename ?????????
  487. * @param string $alpha ??????
  488. +----------------------------------------------------------
  489. * @return string
  490. +----------------------------------------------------------
  491. * @throws ThinkExecption
  492. +----------------------------------------------------------
  493. */
  494. static public function water($source,$water,$waterPos=9,$savename=null,$alpha=80)
  495. {
  496. //????????
  497. if(!file_exists($source)){
  498. //echo "source::file_exists!";
  499. return false;
  500. }
  501. if(!file_exists($water)){
  502. //echo "water::file_exists!";
  503. return false;
  504. }
  505. //????
  506. $sInfo=self::getImageInfo($source);
  507. $wInfo=self::getImageInfo($water);
  508. //????????????????
  509. if($sInfo["width"]<$wInfo["width"] || $sInfo['height']<$wInfo['height']){
  510. //echo "error::pic small!";
  511. return false;
  512. }
  513. //????
  514. $sCreateFun="imagecreatefrom".$sInfo['type'];
  515. $sImage=$sCreateFun($source);
  516. $wCreateFun="imagecreatefrom".$wInfo['type'];
  517. $wImage=$wCreateFun($water);
  518. //?????????
  519. imagealphablending($wImage, true);
  520. //????,?????????
  521. //$posY=$sInfo["height"]-$wInfo["height"];
  522. //$posX=$sInfo["width"]-$wInfo["width"];
  523. $ground_w=$sInfo["width"];
  524. $ground_h=$sInfo["height"];
  525. $w=$wInfo["width"];
  526. $h=$wInfo["height"];
  527. ///////////////////////////////
  528. switch($waterPos)
  529. {
  530. case 0://??
  531. $posX = rand(0,($ground_w - $w));
  532. $posY = rand(0,($ground_h - $h));
  533. break;
  534. case 1://1?????
  535. $posX = 0;
  536. $posY = 0;
  537. break;
  538. case 2://2?????
  539. $posX = ($ground_w - $w) / 2;
  540. $posY = 0;
  541. break;
  542. case 3://3?????
  543. $posX = $ground_w - $w;
  544. $posY = 0;
  545. break;
  546. case 4://4?????
  547. $posX = 0;
  548. $posY = ($ground_h - $h) / 2;
  549. break;
  550. case 5://5?????
  551. $posX = ($ground_w - $w) / 2;
  552. $posY = ($ground_h - $h) / 2;
  553. break;
  554. case 6://6?????
  555. $posX = $ground_w - $w;
  556. $posY = ($ground_h - $h) / 2;
  557. break;
  558. case 7://7?????
  559. $posX = 0;
  560. $posY = $ground_h - $h;
  561. break;
  562. case 8://8?????
  563. $posX = ($ground_w - $w) / 2;
  564. $posY = $ground_h - $h;
  565. break;
  566. case 9://9?????
  567. $posX = $ground_w - $w;
  568. $posY = $ground_h - $h;
  569. break;
  570. default://??
  571. $posX = rand(0,($ground_w - $w));
  572. $posY = rand(0,($ground_h - $h));
  573. break;
  574. }
  575. /////////////////////////////////////
  576. //??????
  577. imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo['width'],$wInfo['height'],$alpha);
  578. //????
  579. $ImageFun='Image'.$sInfo['type'];
  580. //???????????????????
  581. if(!$savename){
  582. $savename=$source;
  583. @unlink($source);
  584. }
  585. //????
  586. $ImageFun($sImage,$savename);
  587. imagedestroy($sImage);
  588. }
  589. /*
  590. * ???PHP???? (?????????)
  591. * ???
  592. * $groundImage ???????????????????GIF,JPG,PNG???
  593. * $waterPos ??????10????0??????
  594. * 1??????2??????3??????
  595. * 4??????5??????6??????
  596. * 7??????8??????9??????
  597. * $waterImage ??????????????????GIF,JPG,PNG???
  598. * $waterText ?????????????????ASCII????????
  599. * $textFont ???????1?2?3?4?5????5?
  600. * $textColor ??????????????????#FF0000(??)?
  601. *
  602. * ???Support GD 2.0?Support FreeType?GIF Read?GIF Create?JPG ?PNG
  603. * $waterImage ? $waterText ????????????????????? $waterImage?
  604. * ?$waterImage??????$waterString?$stringFont?$stringColor?????
  605. * ???????????? $groundImage ???
  606. * ???longware @ 2004-11-3 14:15:13
  607. */
  608. function imageWaterMark($groundImage,$waterPos=0,$waterImage="",$waterText="",$textFont=9,$textColor="#FF0000")
  609. {
  610. $isWaterImage = FALSE;
  611. $formatMsg = "????????????????????????GIF?JPG?PNG???";
  612. //??????
  613. if(!empty($waterImage) && file_exists($waterImage))
  614. {
  615. $isWaterImage = TRUE;
  616. $water_info = getimagesize($waterImage);
  617. $water_w = $water_info[0];//????????
  618. $water_h = $water_info[1];//????????
  619. switch($water_info[2])//?????????
  620. {
  621. case 1:$water_im = imagecreatefromgif($waterImage);break;
  622. case 2:$water_im = imagecreatefromjpeg($waterImage);break;
  623. case 3:$water_im = imagecreatefrompng($waterImage);break;
  624. default:die($formatMsg);
  625. }
  626. }
  627. //??????
  628. if(!empty($groundImage) && file_exists($groundImage))
  629. {
  630. $ground_info = getimagesize($groundImage);
  631. $ground_w = $ground_info[0];//????????
  632. $ground_h = $ground_info[1];//????????
  633. switch($ground_info[2])//?????????
  634. {
  635. case 1:$ground_im = imagecreatefromgif($groundImage);break;
  636. case 2:$ground_im = imagecreatefromjpeg($groundImage);break;
  637. case 3:$ground_im = imagecreatefrompng($groundImage);break;
  638. default:die($formatMsg);
  639. }
  640. }
  641. else
  642. {
  643. die("????????????");
  644. }
  645. //????
  646. if($isWaterImage)//????
  647. {
  648. $w = $water_w;
  649. $h = $water_h;
  650. $label = "???";
  651. }
  652. else//????
  653. {
  654. $temp = imagettfbbox(ceil($textFont*2),0,"./cour.ttf",$waterText);//???? TrueType ????????
  655. $w = $temp[2] - $temp[6];
  656. $h = $temp[3] - $temp[7];
  657. unset($temp);
  658. $label = "????";
  659. }
  660. if( ($ground_w<$w) || ($ground_h<$h) )
  661. {
  662. echo "?????????????????".$label."??????????";
  663. return;
  664. }
  665. switch($waterPos)
  666. {
  667. case 0://??
  668. $posX = rand(0,($ground_w - $w));
  669. $posY = rand(0,($ground_h - $h));
  670. break;
  671. case 1://1?????
  672. $posX = 0;
  673. $posY = 0;
  674. break;
  675. case 2://2?????
  676. $posX = ($ground_w - $w) / 2;
  677. $posY = 0;
  678. break;
  679. case 3://3?????
  680. $posX = $ground_w - $w;
  681. $posY = 0;
  682. break;
  683. case 4://4?????
  684. $posX = 0;
  685. $posY = ($ground_h - $h) / 2;
  686. break;
  687. case 5://5?????
  688. $posX = ($ground_w - $w) / 2;
  689. $posY = ($ground_h - $h) / 2;
  690. break;
  691. case 6://6?????
  692. $posX = $ground_w - $w;
  693. $posY = ($ground_h - $h) / 2;
  694. break;
  695. case 7://7?????
  696. $posX = 0;
  697. $posY = $ground_h - $h;
  698. break;
  699. case 8://8?????
  700. $posX = ($ground_w - $w) / 2;
  701. $posY = $ground_h - $h;
  702. break;
  703. case 9://9?????
  704. $posX = $ground_w - $w;
  705. $posY = $ground_h - $h;
  706. break;
  707. default://??
  708. $posX = rand(0,($ground_w - $w));
  709. $posY = rand(0,($ground_h - $h));
  710. break;
  711. }
  712. //?????????
  713. imagealphablending($ground_im, true);
  714. if($isWaterImage)//????
  715. {
  716. imagecopy($ground_im, $water_im, $posX, $posY, 0, 0, $water_w,$water_h);//?????????
  717. }
  718. else//????
  719. {
  720. if( !empty($textColor) && (strlen($textColor)==7) )
  721. {
  722. $R = hexdec(substr($textColor,1,2));
  723. $G = hexdec(substr($textColor,3,2));
  724. $B = hexdec(substr($textColor,5));
  725. }
  726. else
  727. {
  728. die("????????????");
  729. }
  730. //imagestring ( $ground_im, $textFont, $posX, $posY, $waterText, imagecolorallocate($ground_im, $R, $G, $B));
  731. imagestring($ground_im, 12,$posX, $posY, $waterText, imagecolorallocate($ground_im, $R, $G, $B));
  732. }
  733. //????????
  734. @unlink($groundImage);
  735. switch($ground_info[2])//?????????
  736. {
  737. case 1:imagegif($ground_im,$groundImage);break;
  738. case 2:imagejpeg($ground_im,$groundImage);break;
  739. case 3:imagepng($ground_im,$groundImage);break;
  740. default:die($errorMsg);
  741. }
  742. //????
  743. if(isset($water_info)) unset($water_info);
  744. if(isset($water_im)) imagedestroy($water_im);
  745. unset($ground_info);
  746. imagedestroy($ground_im);
  747. }
  748. }//?????
  749. ?>