程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> php生成驗證碼類代碼

php生成驗證碼類代碼

編輯:關於PHP編程

php教程生成驗證碼類代碼
*/
class securecode
{
    private static $instance=null;
    private $code = '';
    private $fontfile;
    private $validate;
    private $image;
    private $specialadd = 'special string for securecode';
    private $codeexpire=86400;
    private $codecookiename='secure_code';

    /**
     * 構造方法
     */
    private function securecode()
    {
        $this->fontfile = dirname( __file__ ) . '/arial.ttf';
    }

    private function __construct()
    {
        $this->securecode();
    }
   
    public static function getinstance()
    {
        if (self::$instance==null)
            self::$instance=new self();
       
        return self::$instance;
    }

    /**
     * 指定字體文件所在路徑,默認為當前文件夾下arial.ttf文件
     * @param $fontfile 文件路徑
     * @return void
     */
    function loadfont($fontfile)
    {
        $this->fontfile = $fontfile;
    }

    /**
     * 圖片輸出方法,在執行本方法前程序不應該有任何形式的輸出
     * @return void;
     */
    function stroke()
    {
        $this->savecode();
        self::sendheader();
        imagegif( $this->validate );
        imagedestroy( $this->validate );
        imagedestroy( $this->image );
    }

    /**
     * 圖片保存方法
     * @param $filename 保存路徑
     * @return void
     */
    function save($filename)
    {
        $this->savecode();
        imagegif( $this->validate , $filename );
        imagedestroy( $this->validate );
        imagedestroy( $this->image );
    }
   
  /**
     * 驗證碼驗證方法
     * @param $input 要驗證的字符串,即用戶的輸入內容
     * @return boolean 驗證結果
     */
    function verify($input)
    {
        $input=strtolower($input);
        $targetcode=$this->authcode($input);
        $code=$this->getcookie();
        if (empty($code)||$code!=$targetcode)
            $result= false;
        else
            $result=true;
        $_cookie[$this->codecookiename]='';
        setcookie ( $this->codecookiename, '', - 1 );
        return $result;
    }

    /**
     * 圖片創建方法
     * @return void;
     */
    function createimage()
    {
        $this->randcode();
       
        $size = 30;
        $width = 90;
        $height = 35;
        $degrees = array (
            rand( 0 , 30 ), rand( 0 , 30 ), rand( 0 , 30 ), rand( 0 , 30 )
        );
       

        for ($i = 0; $i < 4; ++$i)
        {
            if (rand() % 2);
            else $degrees[$i] = -$degrees[$i];
        }
       
        $this->image = imagecreatetruecolor( $size , $size );
        $this->validate = imagecreatetruecolor( $width , $height );
        $back = imagecolorallocate( $this->image , 255 , 255 , 255 );
        $border = imagecolorallocate( $this->image , 0 , 0 , 0 );
        imagefilledrectangle( $this->validate , 0 , 0 , $width , $height , $back );
       
        for ($i = 0; $i < 4; ++$i)
        {
            $temp = self::rgbtohsv( rand( 0 , 250 ) , rand( 0 , 150 ) , rand( 0 , 250 ) );
           
            if ($temp[2] > 60) $temp[2] = 60;
           
            $temp = self::hsvtorgb( $temp[0] , $temp[1] , $temp[2] );
            $textcolor[$i] = imagecolorallocate( $this->image , $temp[0] , $temp[1] , $temp[2] );
        }
       
        for ($i = 0; $i < 200; ++$i)
        {
            $randpixelcolor = imagecolorallocate( $this->validate , rand( 0 , 255 ) , rand( 0 , 255 ) , rand( 0 , 255 ) );
            imagesetpixel( $this->validate , rand( 1 , 87 ) , rand( 1 , 35 ) , $randpixelcolor );
        }
       
        $temp = self::rgbtohsv( rand( 220 , 255 ) , rand( 220 , 255 ) , rand( 220 , 255 ) );
       
        if ($temp[2] < 200) $temp[2] = 255;
       
        $temp = self::hsvtorgb( $temp[0] , $temp[1] , $temp[2] );
        $randlinecolor = imagecolorallocate( $this->image , $temp[0] , $temp[1] , $temp[2] );
       
        self::imagelinethick( $this->validate , $textcolor[rand( 0 , 3 )] );
       
        imagefilledrectangle( $this->image , 0 , 0 , $size , $size , $back );
        putenv( 'gdfontpath=' . realpath( '.' ) );
       
        // name the font to be used (note the lack of the .ttf extension
        imagettftext( $this->image , 15 , 0 , 8 , 20 , $textcolor[0] , $this->fontfile , $this->code[0] );
       
        $this->image = imagerotate( $this->image , $degrees[0] , $back );
        imagecolortransparent( $this->image , $back );
        imagecopymerge( $this->validate , $this->image , 1 , 4 , 4 , 5 , imagesx( $this->image ) - 10 , imagesy( $this->image ) - 10 , 100 );
       
        $this->image = imagecreatetruecolor( $size , $size );
        imagefilledrectangle( $this->image , 0 , 0 , $size , $size , $back );
        imagettftext( $this->image , 15 , 0 , 8 , 20 , $textcolor[1] , $this->fontfile , $this->code[1] );
        $this->image = imagerotate( $this->image , $degrees[1] , $back );
        imagecolortransparent( $this->image , $back );
        imagecopymerge( $this->validate , $this->image , 21 , 4 , 4 , 5 , imagesx( $this->image ) - 10 , imagesy( $this->image ) - 10 , 100 );
       
        $this->image = imagecreatetruecolor( $size , $size );
        imagefilledrectangle( $this->image , 0 , 0 , $size - 1 , $size - 1 , $back );
        imagettftext( $this->image , 15 , 0 , 8 , 20 , $textcolor[2] , $this->fontfile , $this->code[2] );
        $this->image = imagerotate( $this->image , $degrees[2] , $back );
        imagecolortransparent( $this->image , $back );
        imagecopymerge( $this->validate , $this->image , 41 , 4 , 4 , 5 , imagesx( $this->image ) - 10 , imagesy( $this->image ) - 10 , 100 );
       
        $this->image = imagecreatetruecolor( $size , $size );
        imagefilledrectangle( $this->image , 0 , 0 , $size - 1 , $size - 1 , $back );
        imagettftext( $this->image , 15 , 0 , 8 , 20 , $textcolor[3] , $this->fontfile , $this->code[3] );
        $this->image = imagerotate( $this->image , $degrees[3] , $back );
        imagecolortransparent( $this->image , $back );
        imagecopymerge( $this->validate , $this->image , 61 , 4 , 4 , 5 , imagesx( $this->image ) - 10 , imagesy( $this->image ) - 10 , 100 );
        imagerectangle( $this->validate , 0 , 0 , $width - 1 , $height - 1 , $border );
    }
   
    /**
     * 獲取隨機生成的驗證碼
     * @return string 隨機驗證碼,返回的驗證碼不進行任何處理
     */
    function getcode()
    {
        return $this->code;
    }

    /**
     * 生成隨機碼方法
     * @return void;
     */
    protected function randcode()
    {
        $alphastr = 'abcdefghijklmnpqrstuvwxyz123456789';
        $randstr = array (
            $alphastr{rand( 0 , 33 )}, $alphastr{rand( 0 , 33 )}, $alphastr{rand( 0 , 33 )}, $alphastr{rand( 0 , 33 )}
        );
        $this->code = strtolower( $randstr[0] . $randstr[1] . $randstr[2] . $randstr[3] );
    }

    /**
     * rgb色到hsv色轉變方法
     * @param $r
     * @param $g
     * @param $b
     * @return array hsv數組
     */
    protected static function rgbtohsv($r, $g, $b)
    {
        $tmp = min( $r , $g );
        $min = min( $tmp , $b );
        $tmp = max( $r , $g );
        $max = max( $tmp , $b );
        $v = $max;
        $delta = $max - $min;
       
        if ($max != 0) $s = $delta / $max; // s
        else
        {
            $s = 0;
            //$h = undefinedcolor;
            return;
        }
        if ($r == $max) $h = ($g - $b) / $delta; // between yellow & magenta
        else if ($g == $max) $h = 2 + ($b - $r) / $delta; // between cyan & yellow
        else $h = 4 + ($r - $g) / $delta; // between magenta & cyan
       


        $h *= 60; // degrees
        if ($h < 0) $h += 360;
        return array (
            $h, $s, $v
        );
    }

    /**
     * 同上一方法功能相反
     * @param $h
     * @param $s
     * @param $v
     * @return array rgb數組
     */
    protected static function hsvtorgb($h, $s, $v)
    {
        if ($s == 0)
        {
            // achromatic (grey)
            $r = $g = $b = $v;
            return;
        }
       
        $h /= 60; // sector 0 to 5
        $i = floor( $h );
        $f = $h - $i; // factorial part of h
        $p = $v * (1 - $s);
        $q = $v * (1 - $s * $f);
        $t = $v * (1 - $s * (1 - $f));
       
        switch ($i)
        {
            case 0 :
                $r = $v;
                $g = $t;
                $b = $p;
                break;
            case 1 :
                $r = $q;
                $g = $v;
                $b = $p;
                break;
            case 2 :
                $r = $p;
                $g = $v;
                $b = $t;
                break;
            case 3 :
                $r = $p;
                $g = $q;
                $b = $v;
                break;
            case 4 :
                $r = $t;
                $g = $p;
                $b = $v;
                break;
            default : // case 5:
                $r = $v;
                $g = $p;
                $b = $q;
                break;
        }
        return array (
            $r, $g, $b
        );
    }

    /**
     * 使用cookie保存驗證碼的方法
     * @return void
     */
    protected function savecode()
    {
        $code = $this->authcode($this->code);
        $this->setcookie($code);
    }

    /**
     * 驗證碼cookie值獲取方法
     * @return string cookie值
     */
    protected function getcookie()
    {
        if (empty( $_cookie[$this->codecookiename] ))
        {
            return '';
        }
        else
        {
            return addslashes($_cookie[$this->codecookiename]);
        }
    }
   
    /**
     * 驗證碼cookie創建方法
     * @param string $code 要保存的驗證碼
     * @return void
     */
    protected function setcookie($code)
    {
        $expire = $this->codeexpire > 0 ? $this->codeexpire + time() : 0;
        setcookie( $this->codecookiename , $code, $expire );
    }
   
    /**
     * 驗證碼加密方法
     * @param string $code 要加密的隨機碼
     * @return mixed 執行結果
     */
    protected function authcode($code)
    {
        return md5($code.$this->specialadd);
    }
   
    /**
     * 干擾線生成方法
     * @param resource $image 圖片資源句柄
     * @param string $color 干擾線顏色
     */
    protected static function imagelinethick($image, $color)
    {
        $k = rand( 5 , 20 );
        for ($px = 0; $px < 400; $px = $px + 1)
        {
            $y = $k * sin( 0.1 * ($px) ); //$y=200+10*sin(0.1*($px-200));
            for ($i = 0; $i < 2; $i++)
            {
                imagesetpixel( $image , $px , $y + 10 + $i , $color );
            }
       
        }
    }

    /**
     * http標頭設置方法
     * @return void
     */
    protected static function sendheader()
    {
        header( "pragma: no-cache" );
        header( "cache-control: max-age=1, s-maxage=1, no-cache, must-revalidate" );
        header( 'content-type: image/gif' );
    }
}
http://down.bKjia.c0m/down/code/php/qitayuanma/2010/1220/22330.html

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved