Php generates verification code. Php Tutorial generation verification code * classsecurecode {privatestatic $ instancenull; private $ code; private $ fontfile; private $ validate; private $ image; private $ specialadd php Tutorial generation verification 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 );
}
/**
* Obtain the randomly generated verification code
* @ Return string: random verification code. the returned verification code is not processed.
*/
Function getcode ()
{
Return $ this-> code;
}
/**
* Method for generating random codes
* @ 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 to hsv color conversion method
* @ 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 PLUS = 360;
Return array (
$ H, $ s, $ v
);
}
/**
* Opposite to the previous method
* @ Param $ h
* @ Param $ s
* @ Param $ v
* @ Return array rgb array
*/
Protected static function HSV torgb ($ h, $ s, $ v)
{
If ($ s = 0)
{
// Achromatic (gray)
$ 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 save the verification code using cookies
* @ Return void
*/
Protected function savecode ()
{
$ Code = $ this-> authcode ($ this-> code );
$ This-> setcookie ($ code );
}
/**
* How to obtain the cookie value of the verification code
* @ Return string cookie value
*/
Protected function getcookie ()
{
If (empty ($ _ cookie [$ this-> codecookiename])
{
Return '';
}
Else
{
Return addslashes ($ _ cookie [$ this-> codecookiename]);
}
}
/**
* How to create a verification code cookie
* @ Param string $ code the verification code to save
* @ 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 $ the 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 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
Optional */class securecode {private static $ instance = null; private $ code = ''; private $ fontfile; private $ validate; private $ image; private $ specialadd...