PageRenderTime 198ms CodeModel.GetById 111ms app.highlight 47ms RepoModel.GetById 33ms app.codeStats 0ms

/startZine/src/main/admin/Lib/ORG/Image.class.php

http://ownerpress.googlecode.com/
PHP | 544 lines | 342 code | 22 blank | 180 comment | 64 complexity | 4d4b11e6287f48a097d70ac9e152458a 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$
 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            $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			echo $createFun;
150            $srcImg     = $createFun($image);
151
152            //?????
153            if($type!='gif' && function_exists('imagecreatetruecolor'))
154                $thumbImg = imagecreatetruecolor($width, $height);
155            else
156                $thumbImg = imagecreate($width, $height);
157
158            // ????
159            if(function_exists("ImageCopyResampled"))
160                imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth,$srcHeight);
161            else
162                imagecopyresized($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height,  $srcWidth,$srcHeight);
163            if('gif'==$type || 'png'==$type) {
164                //imagealphablending($thumbImg, false);//?????????
165                //imagesavealpha($thumbImg,true);//??????? alpha ????
166                $background_color  =  imagecolorallocate($thumbImg,  0,255,0);  //  ??????
167				imagecolortransparent($thumbImg,$background_color);  //  ????????????????????
168            }
169
170            // ?jpeg????????
171            if('jpg'==$type || 'jpeg'==$type) 	imageinterlace($thumbImg,$interlace);
172
173            //$gray=ImageColorAllocate($thumbImg,255,0,0);
174            //ImageString($thumbImg,2,5,5,"ThinkPHP",$gray);
175            // ????
176            $imageFun = 'image'.($type=='jpg'?'jpeg':$type);
177            if($imageFun == 'imagejpeg')
178                imagejpeg($thumbImg,$thumbname,100);
179            else
180                $imageFun($thumbImg,$thumbname);
181            imagedestroy($thumbImg);
182            imagedestroy($srcImg);
183            return $thumbname;
184         }
185         return false;
186    }
187
188    /**
189     +----------------------------------------------------------
190     * ????????????
191     +----------------------------------------------------------
192     * @static
193     * @access public
194     +----------------------------------------------------------
195     * @param string $string  ???
196     * @param string $size  ???? width,height ?? array(width,height)
197     * @param string $font  ???? fontface,fontsize ?? array(fontface,fontsize)
198     * @param string $type ???? ??PNG
199     * @param integer $disturb ???? 1 ??? 2 ??? 3 ???? 0 ???
200	 * @param bool $border  ????? array(color)
201     +----------------------------------------------------------
202     * @return string
203     +----------------------------------------------------------
204     */
205	static function buildString($string,$rgb=array(),$filename='',$type='png',$disturb=1,$border=true) {
206		if(is_string($size))		$size	=	explode(',',$size);
207		$width	=	$size[0];
208		$height	=	$size[1];
209		if(is_string($font))		$font	=	explode(',',$font);
210		$fontface	=	$font[0];
211		$fontsize	 	=	$font[1];
212		$length		=	strlen($string);
213        $width = ($length*9+10)>$width?$length*9+10:$width;
214		$height	=	22;
215        if ( $type!='gif' && function_exists('imagecreatetruecolor')) {
216            $im = @imagecreatetruecolor($width,$height);
217        }else {
218            $im = @imagecreate($width,$height);
219        }
220		if(empty($rgb)) {
221			$color = imagecolorallocate($im, 102, 104, 104);
222		}else{
223			$color = imagecolorallocate($im, $rgb[0], $rgb[1], $rgb[2]);
224		}
225        $backColor = imagecolorallocate($im, 255,255,255);    //???????
226		$borderColor = imagecolorallocate($im, 100, 100, 100);                    //???
227        $pointColor = imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));                 //???
228
229        @imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);
230        @imagerectangle($im, 0, 0, $width-1, $height-1, $borderColor);
231        @imagestring($im, 5, 5, 3, $string, $color);
232		if(!empty($disturb)) {
233			// ????
234			if($disturb=1 || $disturb=3) {
235				for($i=0;$i<25;$i++){
236					imagesetpixel($im,mt_rand(0,$width),mt_rand(0,$height),$pointColor);
237				}
238			}elseif($disturb=2 || $disturb=3){
239				for($i=0;$i<10;$i++){
240					imagearc($im,mt_rand(-10,$width),mt_rand(-10,$height),mt_rand(30,300),mt_rand(20,200),55,44,$pointColor);
241				}
242			}
243		}
244        Image::output($im,$type,$filename);
245	}
246
247    /**
248     +----------------------------------------------------------
249     * ???????
250     +----------------------------------------------------------
251     * @static
252     * @access public
253     +----------------------------------------------------------
254     * @param string $length  ??
255     * @param string $mode  ??
256     * @param string $type ????
257     * @param string $width  ??
258     * @param string $height  ??
259     +----------------------------------------------------------
260     * @return string
261     +----------------------------------------------------------
262     */
263    static function buildImageVerify($length=4,$mode=1,$type='png',$width=48,$height=22,$verifyName='verify')
264    {
265		import('@.ORG.String');
266        $randval = String::rand_string($length,$mode);
267        $_SESSION[$verifyName]= md5($randval);
268        $width = ($length*10+10)>$width?$length*10+10:$width;
269        if ( $type!='gif' && function_exists('imagecreatetruecolor')) {
270            $im = @imagecreatetruecolor($width,$height);
271        }else {
272            $im = @imagecreate($width,$height);
273        }
274        $r = Array(225,255,255,223);
275        $g = Array(225,236,237,255);
276        $b = Array(225,236,166,125);
277        $key = mt_rand(0,3);
278
279        $backColor = imagecolorallocate($im, $r[$key],$g[$key],$b[$key]);    //???????
280		$borderColor = imagecolorallocate($im, 100, 100, 100);                    //???
281        $pointColor = imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));                 //???
282
283        @imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);
284        @imagerectangle($im, 0, 0, $width-1, $height-1, $borderColor);
285        $stringColor = imagecolorallocate($im,mt_rand(0,200),mt_rand(0,120),mt_rand(0,120));
286		// ??
287		for($i=0;$i<10;$i++){
288			$fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
289			imagearc($im,mt_rand(-10,$width),mt_rand(-10,$height),mt_rand(30,300),mt_rand(20,200),55,44,$fontcolor);
290		}
291		for($i=0;$i<25;$i++){
292			$fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
293			imagesetpixel($im,mt_rand(0,$width),mt_rand(0,$height),$pointColor);
294		}
295		for($i=0;$i<$length;$i++) {
296			imagestring($im,5,$i*10+5,mt_rand(1,8),$randval{$i}, $stringColor);
297		}
298//        @imagestring($im, 5, 5, 3, $randval, $stringColor);
299        Image::output($im,$type);
300    }
301
302	// ?????
303	static function GBVerify($length=4,$type='png',$width=180,$height=50,$fontface='simhei.ttf',$verifyName='verify') {
304		$code	=	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)
390    {
391		$rand	=	range('a','z');
392		shuffle($rand);
393		$verifyCode	=	array_slice($rand,0,10);
394        $letter = implode(" ",$verifyCode);
395        $_SESSION['verifyCode'] = $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	// ??????
490	static public function buildPhone() {
491	}
492	// ??????
493	static public function buildEmail($email,$rgb=array(),$filename='',$type='png') {
494		$mail		=	explode('@',$email);
495		$user		=	trim($mail[0]);
496		$mail		=	strtolower(trim($mail[1]));
497		$path		=	dirname(__FILE__).'/Mail/';
498		if(is_file($path.$mail.'.png')) {
499			$im	= imagecreatefrompng($path.$mail.'.png');
500			$user_width = imagettfbbox(9, 0, dirname(__FILE__)."/Mail/tahoma.ttf", $user);
501			$x_value = (200 - ($user_width[2] + 113));
502			if(empty($rgb)) {
503				$color = imagecolorallocate($im, 102, 104, 104);
504			}else{
505				$color = imagecolorallocate($im, $rgb[0], $rgb[1], $rgb[2]);
506			}
507			imagettftext($im, 9, 0, $x_value, 16, $color, dirname(__FILE__)."/Mail/tahoma.ttf", $user);
508		}else{
509			$user_width = imagettfbbox(9, 0, dirname(__FILE__)."/Mail/tahoma.ttf", $email);
510			$width	=	$user_width[2]+15;
511			$height	=	20;
512			$im	=	imagecreate($width,20);
513			$backColor = imagecolorallocate($im, 255,255,255);    //???????
514			$borderColor = imagecolorallocate($im, 100, 100, 100);                    //???
515			$pointColor = imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));                 //???
516			imagefilledrectangle($im, 0, 0, $width - 1, $height - 1, $backColor);
517			imagerectangle($im, 0, 0, $width-1, $height-1, $borderColor);
518			if(empty($rgb)) {
519				$color = imagecolorallocate($im, 102, 104, 104);
520			}else{
521				$color = imagecolorallocate($im, $rgb[0], $rgb[1], $rgb[2]);
522			}
523			imagettftext($im, 9, 0, 5, 16, $color, dirname(__FILE__)."/Mail/tahoma.ttf", $email);
524			for($i=0;$i<25;$i++){
525				imagesetpixel($im,mt_rand(0,$width),mt_rand(0,$height),$pointColor);
526			}
527		}
528		Image::output($im,$type,$filename);
529	}
530
531    static function output($im,$type='png',$filename='')
532    {
533        header("Content-type: image/".$type);
534        $ImageFun='image'.$type;
535		if(empty($filename)) {
536	        $ImageFun($im);
537		}else{
538	        $ImageFun($im,$filename);
539		}
540        imagedestroy($im);
541    }
542
543}//?????
544?>