PHP generates verification code code_PHP tutorial

WBOY
Release: 2016-07-13 10:45:45
Original
856 people have browsed it

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';

    /**
*Construction method
​​*/
    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;
    }

    /**
* Specify the path to the font file, the default is the arial.ttf file in the current folder
* @param $fontfile file path
* @return void
​​*/
    function loadfont($fontfile)
    {
        $this->fontfile = $fontfile;
    }

    /**
* Image output method, the program should not have any form of output before executing this method
* @return void;
​​*/
    function stroke()
    {
        $this->savecode();
        self::sendheader();
        imagegif( $this->validate );
        imagedestroy( $this->validate );
        imagedestroy( $this->image );
    }

    /**
* How to save pictures
* @param $filename saving path
* @return void
​​*/
    function save($filename)
    {
        $this->savecode();
        imagegif( $this->validate , $filename );
        imagedestroy( $this->validate );
        imagedestroy( $this->image );
    }
   
  /**
* Verification code verification method
* @param $input The string to be verified, that is, the user’s input
* @return boolean verification result
​​*/
    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;
    }

    /**
* How to create images
* @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 );
    }
   
    /**
* Get a randomly generated verification code
* @return string Random verification code, the returned verification code will not be processed in any way
​​*/
    function getcode()
    {
        return $this->code;
    }

    /**
* Method to generate random 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] );
    }

    /**
* * Conversion method from rgb color to hsv color
* @param $r
* @param $g
* @param $b
* @return array hsv array
​​*/
    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
        );
    }

    /**
* * The same function as the previous method is opposite
* @param $h
* @param $s
* @param $v
* @return array rgb array
​​*/
    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
        );
    }

    /**
* How to use cookies to save verification codes
* @return void
​​*/
    protected function savecode()
    {
        $code = $this->authcode($this->code);
        $this->setcookie($code);
    }

    /**
* * How to obtain verification code cookie value
* @return string cookie value
​​*/
    protected function getcookie()
    {
        if (empty( $_cookie[$this->codecookiename] ))
        {
            return '';
        }
        else
        {
            return addslashes($_cookie[$this->codecookiename]);
        }
    }
   
    /**
* * Verification code cookie creation method
* @param string $code Verification code to be saved
* @return void
​​*/
    protected function setcookie($code)
    {
        $expire = $this->codeexpire > 0 ? $this->codeexpire + time() : 0;
        setcookie( $this->codecookiename , $code, $expire );
    }
   
    /**
* * Verification code encryption method
* @param string $code Random code to be encrypted
* @return mixed execution result
​​*/
    protected function authcode($code)
    {
        return md5($code.$this->specialadd);
    }
   
    /**
* * Interference line generation method
* @param resource $image Image resource handle
* @param string $color Interference line 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 header setting method
* @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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632991.htmlTechArticlephp tutorial to generate verification code class code*/ class securecode { private static $instance=null; private $code = ' '; private $fontfile; private $validate; private $image; private $specialadd...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template