Php Chinese character verification code generation program (1/3)

Source: Internet
Author: User
Tags rand
The code is as follows: Copy code

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

/**
* Constructor
*/
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;
    }

/**
* Specifies the path of the font file. The default value 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 output any form before executing this method.
* @ Return void;
*/
Function stroke ()
    {
$ This-> savecode ();
Self: sendheader ();
Imagegif ($ this-> validate );
Imagedestroy ($ this-> validate );
Imagedestroy ($ this-> image );
    }

/**
* Image storage method
* @ Param $ filename save 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 content
* @ 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;
    }

/**
* Image creation method
* @ 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: HSV torgb ($ 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: HSV torgb ($ 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 );
    }

 

Homepage 1 2 3 Last page

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.