中文验证码

这是一个PHP类,用于生成中文验证码。类中包含了加载字体、设置噪点数量、生成验证码字符串等方法,通过图形处理实现验证码的显示。验证码的字体、大小、颜色、角度等属性可配置,有助于网站的安全验证。
<?php
/*
* @Author:zx
* @Time:2006年4月11日
* @File:vCode.php
* @version:1.0
*/

include_once( "graphics.php" );

class vCode
{
    private $info;
    /*
    * 存放系统的相关信息
    *
    * @var array
    * @access private
    */
   
    private $font;
    /*
    * 存放文字相关信息
    *
    * @var array
    * @access private
    */
   
    private $graph;
    /*
    * 存储图形对象
    *
    * @var array
    * @access private
    */
   
    public function __construct( $encoding="gb2312" )
    {
        /*
        * 验证码类初始化
        *
        * @param int $how
        * @access public
        */
       
        $this->graph = &new graphics( $encoding );
        $this->info['gpId'] = "vcode";
        $this->font = array();
        $this->info['text'] = "";
        $this->info['seed'] = 0;
    }
   
    public function loadFont( $path , $size )
    {
        /*
        * 加载字体
        *
        * @param string $path
        * @param int $size
        * @access public
        */
       
        $font['size'] = $size;
        $font['path'] = $path;
        $this->font[] = $font;
        return( true );
    }
   
    public function pixelNum( $number )
    {
        /*
        * 控制噪音数量
        *
        * @param int $number
        * @access public
        */
       
        $this->info['pixel'] = $number;
        return( $number );
    }
   
    public function rndCode( $len , $portrait )
    {
        /*
        * 取得并输出中文验证码
        *
        * @param int $len
        * @param int $portrait
        * @access public
        */
       
        $fonts = self::signet( $len , $portrait );
        self::backGround(  );
        self::drawPixel( $this->info['pixel'] );
        $s = "";
       
        for( $i=0 ; $i<$len ; $i++ )
        {
            $id = "D5C5D0F1".$fonts[$i]['id'];
            $this->graph->addFont( $id , $fonts[$i]['path'] );
            $this->graph->setText( $id , $fonts[$i]['size'] , $fonts[$i]['angle']*10 , "000000" );
            $s .= $fonts[$i]['ft'];
            $this->graph->writeText( $id , $fonts[$i]['x'] , $fonts[$i]['y'] , $fonts[$i]['ft'] );
        }

        return( $s );
    }
   
    public function showError(  )
    {
        $this->graph->showError(  );
        return( $this->error->pross( ) );
    }
   
    public function __destruct(  )
    {
        /*
        * 验证码销毁并释放资源
        *
        * @access public
        */
       
        $this->graph->show( $this->info['gpId'] , "gif" );
        $this->graph = NULL;
        $this->info = NULL;
    }
   
    private function backGround(  )
    {
        $this->graph->create( $this->info['gpId'] , $this->info['width'] , $this->info['height'] , false , false );
        $s[] = "FFFFFF";
        $s[] = "cceeFF";
        $s[] = "FFFFCC";
        $s[] = "FFEEFF";
        $s[] = "CCFFFF";
        $s[] = "CCCCFF";
        $s[] = "FFCCCC";
        $s[] = "FFFCCC";
        $s[] = "CCCFFF";
        $s[] = "EEEEEE";
        $s[] = "FFCC99";
        $this->graph->getColor( $s[ self::rands( 0 , count( $s )-1 ) ] );
        return( true );
    }
   
    private function drawPixel( $number )
    {
        $sqrt = ceil( sqrt( $number ) );
        $pW = floor( $this->info['width']/$sqrt );
        $pH = floor( $this->info['height']/$sqrt );
       
        for( $i=0 ; $i<$sqrt; $i++ )
        {
            $c = $pW*$i;
            $x = self::rands( $c , $c+$pW );
           
            for( $j=0 ; $j<$sqrt ; $j++ )
            {
                $s = $pH*$j;
                $y = self::rands( $s , $s+$pH );
                $this->graph->drawLine( $x , $y , self::rands( $c , $c+$pW ) , self::rands( $s , $s+$pH ) , "000000" );
            }
        }
       
        return( true );
    }
   
    private function signet( $len , $portrait )
    {
        $w = 0;
        $h = 0;
        $fonts = array();

        for( $i=0 ; $i<$len ; $i++ )
        {
            $font['id'] = self::rands( 0 , count( $this->font )-1 );
            $font['ft'] = self::gb(  );
            $font['angle'] = self::rands( -1 , 1 );
            $font['size'] = $this->font[ $font['id'] ]['size'];
            $font['path'] = $this->font[ $font['id'] ]['path'];
            $c = floor( $this->font[ $font['id'] ]['size']/2 );
            $s = $this->font[ $font['id'] ]['size']*2;
           
            if( $portrait )
            {
                $w = $s>$w ? $s : $w;
                $font['x'] = self::rands( $this->font[ $font['id'] ]['size']-2 , 2 );
                $font['y'] = $h+$this->font[ $font['id'] ]['size']+$c;
                $h += $this->font[ $font['id'] ]['size']*2;
            }
            else
            {
                $h = $s>$h ? $s : $h;
                $font['x'] = $w+$c;
                $font['y'] = self::rands( $h , $font['size'] );
                $w += $this->font[ $font['id'] ]['size']*2;
            }

            $fonts[] = $font;
        }
       
        $this->info['width']  = $w;
        $this->info['height'] = $h;
        return( $fonts );
    }
   
    private function getSeed(  )
    {
        $sec = date( "s" , time() );
        $sec += $this->info['seed'];
        $this->info['seed']++;
        return ( $sec );
    }
   
    private function rands( $s , $e )
    {
        mt_srand( self::getSeed(  ) );
        return( mt_rand( $s , $e ) );
    }
   
    private function gb(  )
    {
        return( chr( self::rands( 176 , 206 ) ).chr( self::rands( 207 , 252 ) ) );
    }
}
?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值