PageRenderTime 2ms CodeModel.GetById 60ms app.highlight 38ms RepoModel.GetById 12ms app.codeStats 1ms

/code/addons/libs/Image.class.php

http://thinksns-2.googlecode.com/
PHP | 502 lines | 304 code | 20 blank | 178 comment | 59 complexity | 585f94d5a433cdc688aa38eb6b2567a5 MD5 | raw file
  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 +------------------------------------------------------------------------------
 17 * @category   ORG
 18 * @package  ORG
 19 * @subpackage  Util
 20 * @author    liu21st <liu21st@gmail.com>
 21 * @version   $Id$
 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            if( $maxHeight=='auto' ){
136           		$scale = $maxWidth/$srcWidth;
137            }else{
138            	$scale = min($maxWidth/$srcWidth, $maxHeight/$srcHeight); // ??????
139            }
140            if($scale>=1) {
141                // ??????????
142                $width   =  $srcWidth;
143                $height  =  $srcHeight;
144            }else{
145                // ?????
146                $width  = (int)($srcWidth*$scale);
147                $height = (int)($srcHeight*$scale);
148            }
149
150            // ????
151            $createFun = 'ImageCreateFrom'.($type=='jpg'?'jpeg':$type);
152            $srcImg     = $createFun($image);
153
154            //?????
155            if($type!='gif' && function_exists('imagecreatetruecolor'))
156                $thumbImg = imagecreatetruecolor($width, $height);
157            else
158                $thumbImg = imagecreate($width, $height);
159
160            // ????
161            if(function_exists("ImageCopyResampled"))
162                imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth,$srcHeight);
163            else
164                imagecopyresized($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height,  $srcWidth,$srcHeight);
165            if('gif'==$type || 'png'==$type) {
166                //imagealphablending($thumbImg, false);//?????????
167                //imagesavealpha($thumbImg,true);//??????? alpha ????
168                $background_color  =  imagecolorallocate($thumbImg,  0,255,0);  //  ??????
169				imagecolortransparent($thumbImg,$background_color);  //  ????????????????????
170            }
171
172            // ?jpeg????????
173            if('jpg'==$type || 'jpeg'==$type) 	imageinterlace($thumbImg,$interlace);
174
175            //$gray=ImageColorAllocate($thumbImg,255,0,0);
176            //ImageString($thumbImg,2,5,5,"ThinkPHP",$gray);
177            // ????
178            $imageFun = 'image'.($type=='jpg'?'jpeg':$type);
179            $imageFun($thumbImg,$thumbname);
180            imagedestroy($thumbImg);
181            imagedestroy($srcImg);
182            return $thumbname;
183         }
184         return false;
185    }
186
187    /**
188     +----------------------------------------------------------
189     * ????????????
190     +----------------------------------------------------------
191     * @static
192     * @access public
193     +----------------------------------------------------------
194     * @param string $string  ???
195     * @param string $size  ???? width,height ?? array(width,height)
196     * @param string $font  ???? fontface,fontsize ?? array(fontface,fontsize)
197     * @param string $type ???? ??PNG
198     * @param integer $disturb ???? 1 ??? 2 ??? 3 ???? 0 ???
199	 * @param bool $border  ????? array(color)
200     +----------------------------------------------------------
201     * @return string
202     +----------------------------------------------------------
203     */
204	static function buildString($string,$rgb=array(),$filename='',$type='png',$disturb=1,$border=true) {
205		if(is_string($size))		$size	=	explode(',',$size);
206		$width	=	$size[0];
207		$height	=	$size[1];
208		if(is_string($font))		$font	=	explode(',',$font);
209		$fontface	=	$font[0];
210		$fontsize	 	=	$font[1];
211		$length		=	strlen($string);
212        $width = ($length*9+10)>$width?$length*9+10:$width;
213		$height	=	22;
214        if ( $type!='gif' && function_exists('imagecreatetruecolor')) {
215            $im = @imagecreatetruecolor($width,$height);
216        }else {
217            $im = @imagecreate($width,$height);
218        }
219		if(empty($rgb)) {
220			$color = imagecolorallocate($im, 102, 104, 104);
221		}else{
222			$color = imagecolorallocate($im, $rgb[0], $rgb[1], $rgb[2]);
223		}
224        $backColor = imagecolorallocate($im, 255,255,255);    //???????
225		$borderColor = imagecolorallocate($im, 100, 100, 100);                    //???
226        $pointColor = imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));                 //???
227
228        @imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);
229        @imagerectangle($im, 0, 0, $width-1, $height-1, $borderColor);
230        @imagestring($im, 5, 5, 3, $string, $color);
231		if(!empty($disturb)) {
232			// ????
233			if($disturb=1 || $disturb=3) {
234				for($i=0;$i<25;$i++){
235					imagesetpixel($im,mt_rand(0,$width),mt_rand(0,$height),$pointColor);
236				}
237			}elseif($disturb=2 || $disturb=3){
238				for($i=0;$i<10;$i++){
239					imagearc($im,mt_rand(-10,$width),mt_rand(-10,$height),mt_rand(30,300),mt_rand(20,200),55,44,$pointColor);
240				}
241			}
242		}
243        Image::output($im,$type,$filename);
244	}
245
246    /**
247     +----------------------------------------------------------
248     * ???????
249     +----------------------------------------------------------
250     * @static
251     * @access public
252     +----------------------------------------------------------
253     * @param string $length  ??
254     * @param string $mode  ??
255     * @param string $type ????
256     * @param string $width  ??
257     * @param string $height  ??
258     +----------------------------------------------------------
259     * @return string
260     +----------------------------------------------------------
261     */
262    static function buildImageVerify($length=4,$mode=1,$type='png',$width=48,$height=22,$verifyName='verify')
263    {
264		import('ORG.Util.String');
265        $randval = String::rand_string($length,$mode);
266        $_SESSION[$verifyName]= md5($randval);
267        $width = ($length*10+10)>$width?$length*10+10:$width;
268        if ( $type!='gif' && function_exists('imagecreatetruecolor')) {
269            $im = @imagecreatetruecolor($width,$height);
270        }else {
271            $im = @imagecreate($width,$height);
272        }
273        $r = Array(225,255,255,223);
274        $g = Array(225,236,237,255);
275        $b = Array(225,236,166,125);
276        $key = mt_rand(0,3);
277
278        $backColor = imagecolorallocate($im, $r[$key],$g[$key],$b[$key]);    //???????
279		$borderColor = imagecolorallocate($im, 100, 100, 100);                    //???
280        $pointColor = imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));                 //???
281
282        @imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);
283        @imagerectangle($im, 0, 0, $width-1, $height-1, $borderColor);
284        $stringColor = imagecolorallocate($im,mt_rand(0,200),mt_rand(0,120),mt_rand(0,120));
285		// ??
286		for($i=0;$i<10;$i++){
287			$fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
288			imagearc($im,mt_rand(-10,$width),mt_rand(-10,$height),mt_rand(30,300),mt_rand(20,200),55,44,$fontcolor);
289		}
290		for($i=0;$i<25;$i++){
291			$fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
292			imagesetpixel($im,mt_rand(0,$width),mt_rand(0,$height),$pointColor);
293		}
294		for($i=0;$i<$length;$i++) {
295			imagestring($im,5,$i*10+5,mt_rand(1,8),$randval{$i}, $stringColor);
296		}
297//        @imagestring($im, 5, 5, 3, $randval, $stringColor);
298        Image::output($im,$type);
299    }
300
301	// ?????
302	static function GBVerify($length=4,$type='png',$width=180,$height=50,$fontface='simhei.ttf',$verifyName='verify') {
303		import('ORG.Util.String');
304        $code = String::rand_string($length,4);
305        $width = ($length*45)>$width?$length*45:$width;
306		$_SESSION[$verifyName]= md5($code);
307		$im=imagecreatetruecolor($width,$height);
308		$borderColor = imagecolorallocate($im, 100, 100, 100);                    //???
309		$bkcolor=imagecolorallocate($im,250,250,250);
310		imagefill($im,0,0,$bkcolor);
311        @imagerectangle($im, 0, 0, $width-1, $height-1, $borderColor);
312		// ??
313		for($i=0;$i<15;$i++){
314			$fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
315			imagearc($im,mt_rand(-10,$width),mt_rand(-10,$height),mt_rand(30,300),mt_rand(20,200),55,44,$fontcolor);
316		}
317		for($i=0;$i<255;$i++){
318			$fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
319			imagesetpixel($im,mt_rand(0,$width),mt_rand(0,$height),$fontcolor);
320		}
321		if(!is_file($fontface)) {
322			$fontface = dirname(__FILE__)."/".$fontface;
323		}
324		for($i=0;$i<$length;$i++){
325			$fontcolor=imagecolorallocate($im,mt_rand(0,120),mt_rand(0,120),mt_rand(0,120)); //??????????????
326			$codex= msubstr($code,$i,1);
327			imagettftext($im,mt_rand(16,20),mt_rand(-60,60),40*$i+20,mt_rand(30,35),$fontcolor,$fontface,$codex);
328		}
329		Image::output($im,$type);
330	}
331
332    /**
333     +----------------------------------------------------------
334     * ??????????
335     +----------------------------------------------------------
336     * @static
337     * @access public
338     +----------------------------------------------------------
339     * @param string $image  ??????
340     * @param string $type  ???????????
341     +----------------------------------------------------------
342     * @return string
343     +----------------------------------------------------------
344     */
345    static function showASCIIImg($image,$string='',$type='')
346    {
347        $info  = Image::getImageInfo($image);
348        if($info !== false) {
349            $type = empty($type)?$info['type']:$type;
350            unset($info);
351            // ????
352            $createFun = 'ImageCreateFrom'.($type=='jpg'?'jpeg':$type);
353            $im     = $createFun($image);
354            $dx = imagesx($im);
355            $dy = imagesy($im);
356			$i	=	0;
357            $out   =  '<span style="padding:0px;margin:0;line-height:100%;font-size:1px;">';
358			set_time_limit(0);
359            for($y = 0; $y < $dy; $y++) {
360              for($x=0; $x < $dx; $x++) {
361                  $col = imagecolorat($im, $x, $y);
362                  $rgb = imagecolorsforindex($im,$col);
363				  $str	 =	 empty($string)?'*':$string[$i++];
364                  $out .= sprintf('<span style="margin:0px;color:#%02x%02x%02x">'.$str.'</span>',$rgb['red'],$rgb['green'],$rgb['blue']);
365             }
366             $out .= "<br>\n";
367            }
368            $out .=  '</span>';
369            imagedestroy($im);
370            return $out;
371        }
372        return false;
373    }
374
375    /**
376     +----------------------------------------------------------
377     * ?????????
378     +----------------------------------------------------------
379     * @static
380     * @access public
381     +----------------------------------------------------------
382     * @param string $type ????
383     * @param string $width  ??
384     * @param string $height  ??
385     +----------------------------------------------------------
386     * @return string
387     +----------------------------------------------------------
388     */
389    static function showAdvVerify($type='png',$width=180,$height=40,$verifyName='verifyCode')
390    {
391		$rand	=	range('a','z');
392		shuffle($rand);
393		$verifyCode	=	array_slice($rand,0,10);
394        $letter = implode(" ",$verifyCode);
395        $_SESSION[$verifyName] = $verifyCode;
396        $im = imagecreate($width,$height);
397        $r = array(225,255,255,223);
398        $g = array(225,236,237,255);
399        $b = array(225,236,166,125);
400        $key = mt_rand(0,3);
401        $backColor = imagecolorallocate($im, $r[$key],$g[$key],$b[$key]);
402		$borderColor = imagecolorallocate($im, 100, 100, 100);                    //???
403        imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);
404        imagerectangle($im, 0, 0, $width-1, $height-1, $borderColor);
405        $numberColor = imagecolorallocate($im, 255,rand(0,100), rand(0,100));
406        $stringColor = imagecolorallocate($im, rand(0,100), rand(0,100), 255);
407		// ????
408		/*
409		for($i=0;$i<10;$i++){
410			$fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
411			imagearc($im,mt_rand(-10,$width),mt_rand(-10,$height),mt_rand(30,300),mt_rand(20,200),55,44,$fontcolor);
412		}
413		for($i=0;$i<255;$i++){
414			$fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
415			imagesetpixel($im,mt_rand(0,$width),mt_rand(0,$height),$fontcolor);
416		}*/
417        imagestring($im, 5, 5, 1, "0 1 2 3 4 5 6 7 8 9", $numberColor);
418        imagestring($im, 5, 5, 20, $letter, $stringColor);
419        Image::output($im,$type);
420    }
421
422    /**
423     +----------------------------------------------------------
424     * ??UPC-A???
425     +----------------------------------------------------------
426     * @static
427     +----------------------------------------------------------
428     * @param string $type ????
429     * @param string $type ????
430     * @param string $lw  ????
431     * @param string $hi   ????
432     +----------------------------------------------------------
433     * @return string
434     +----------------------------------------------------------
435     */
436    static function UPCA($code,$type='png',$lw=2,$hi=100) {
437        static $Lencode = array('0001101','0011001','0010011','0111101','0100011',
438                         '0110001','0101111','0111011','0110111','0001011');
439        static $Rencode = array('1110010','1100110','1101100','1000010','1011100',
440                         '1001110','1010000','1000100','1001000','1110100');
441        $ends = '101';
442        $center = '01010';
443        /* UPC-A Must be 11 digits, we compute the checksum. */
444        if ( strlen($code) != 11 ) { die("UPC-A Must be 11 digits."); }
445        /* Compute the EAN-13 Checksum digit */
446        $ncode = '0'.$code;
447        $even = 0; $odd = 0;
448        for ($x=0;$x<12;$x++) {
449          if ($x % 2) { $odd += $ncode[$x]; } else { $even += $ncode[$x]; }
450        }
451        $code.=(10 - (($odd * 3 + $even) % 10)) % 10;
452        /* Create the bar encoding using a binary string */
453        $bars=$ends;
454        $bars.=$Lencode[$code[0]];
455        for($x=1;$x<6;$x++) {
456          $bars.=$Lencode[$code[$x]];
457        }
458        $bars.=$center;
459        for($x=6;$x<12;$x++) {
460          $bars.=$Rencode[$code[$x]];
461        }
462        $bars.=$ends;
463        /* Generate the Barcode Image */
464        if ( $type!='gif' && function_exists('imagecreatetruecolor')) {
465            $im = imagecreatetruecolor($lw*95+30,$hi+30);
466        }else {
467            $im = imagecreate($lw*95+30,$hi+30);
468        }
469        $fg = ImageColorAllocate($im, 0, 0, 0);
470        $bg = ImageColorAllocate($im, 255, 255, 255);
471        ImageFilledRectangle($im, 0, 0, $lw*95+30, $hi+30, $bg);
472        $shift=10;
473        for ($x=0;$x<strlen($bars);$x++) {
474          if (($x<10) || ($x>=45 && $x<50) || ($x >=85)) { $sh=10; } else { $sh=0; }
475          if ($bars[$x] == '1') { $color = $fg; } else { $color = $bg; }
476          ImageFilledRectangle($im, ($x*$lw)+15,5,($x+1)*$lw+14,$hi+5+$sh,$color);
477        }
478        /* Add the Human Readable Label */
479        ImageString($im,4,5,$hi-5,$code[0],$fg);
480        for ($x=0;$x<5;$x++) {
481          ImageString($im,5,$lw*(13+$x*6)+15,$hi+5,$code[$x+1],$fg);
482          ImageString($im,5,$lw*(53+$x*6)+15,$hi+5,$code[$x+6],$fg);
483        }
484        ImageString($im,4,$lw*95+17,$hi-5,$code[11],$fg);
485        /* Output the Header and Content. */
486        Image::output($im,$type);
487    }
488
489    static function output($im,$type='png',$filename='')
490    {
491        header("Content-type: image/".$type);
492        $ImageFun='image'.$type;
493		if(empty($filename)) {
494	        $ImageFun($im);
495		}else{
496	        $ImageFun($im,$filename);
497		}
498        imagedestroy($im);
499    }
500
501}//?????
502?>