Php Chinese character verification code generation program (13 ). The php Chinese character verification code generation program is actually very simple about the php Chinese character verification code generation program. it requires a font to generate a Chinese character verification code, next we use the ar php Chinese character verification code generation program, which is actually very simple about the php Chinese character verification code generation program. to generate a Chinese character verification code, we need a font to support it. below we use arial. ttf to support generation, otherwise the Chinese image verification code will be garbled.
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 );
}
Http://www.bkjia.com/PHPjc/633019.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/633019.htmlTechArticlephp Chinese character verification code generation program this section on php Chinese character verification code generation program is actually very simple, generate Chinese character verification code requires font to support, below we use ar...