PageRenderTime 112ms CodeModel.GetById 53ms app.highlight 52ms RepoModel.GetById 1ms app.codeStats 0ms

/Admin/Lib/ORG/Image.class.php

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