This article mainly shares with you a piece of PHP code for creating a graphic verification code, which is very practical and can be used at will.
This article mainly shares with you a piece of PHP code for creating a graphic verification code, which is very practical and can be used at will.
Effect:
Myvcode. class. php: class that encapsulates the verification code
<? Php
/*
* File: myvcode. class. php
* Verification Code Class, Class Name Vcode
*/
Class Vcode
{
Private $ width;/* Verification Code width */
Private $ height;/* Verification Code height */
Private $ codeNum;/* number of characters in the Verification Code */
Private $ checkCode;/* Verification Code character */
Private $ image;/* Verification Code resource */
Private $ pixNum;/* Number of interference points to be drawn */
Private $ lineNum;/* number of lines that draw interference lines *//*
* Constructor instantiates the verification code object and initializes data.
* @ Param int $ width: Set the default width.
* @ Param int $ height: sets the default height.
* @ Param int $ codeNum: set the number of characters in the verification code.
* @ Param int $ pixNum: sets the number of interference points.
* @ Param int $ lineNum: sets the number of interference lines.
*/
Function _ construct ($ width = 80, $ height = 40, $ codeNum = 4, $ pixNum = 40, $ lineNum = 5)
{
$ This-> width = $ width;
$ This-> height = $ height;
$ This-> codeNum = $ codeNum;
$ This-> pixNum = $ pixNum;
$ This-> lineNum = $ lineNum;
}
/* Internal private method to create image resources */
Private function getCreateImage ()
{
$ This-> image = imagecreatetruecolor ($ this-> width, $ this-> height );
$ White = imagecolorallocate ($ this-> image, 0xff, 0xff, 0xff );
Imagefill ($ this-> image, 0, 0, $ white );
$ Black = imagecolorallocate ($ this-> image, 0, 0 );
Imagerectangle ($ this-> image, 0, 0, $ this-> width-1, $ this-> height-1, $ black );
}
/* Internal private method, draw characters, remove o0Llz and 012 */
Private function createCheckCode ()
{
$ Code = '3456789abcdefghijkmnpqrstuvwxyabcdefghijkmnpqrstuvwxy ';
$ This-> checkCode = "";
For ($ I = 0; $ I <$ this-> codeNum; $ I ++)
{
$ Char = $ code {rand (0, strlen ($ code)-1 )};
$ This-> checkCode. = $ char;
$ FontColor = imagecolorallocate ($ this-> image, rand (0,128), rand (0,128), rand (0,128 ));
$ FontSize = rand (3, 5 );
$ X = rand (0, $ this-> width-imagefontwidth ($ fontSize ));
$ Y = rand (0, $ this-> height-imagefontheight ($ fontSize ));
Imagechar ($ this-> image, $ fontSize, $ x, $ y, $ char, $ fontColor );
}
}
/* Set interference elements in internal private methods */
Private function setDisturbColor ()
{
/* Draw interference points */
For ($ I = 0; $ I <$ this-> pixNum; $ I ++)
{
$ Color = imagecolorallocate ($ this-> image, rand (0,255), rand (0,255), rand (0,255 ));
Imagesetpixel ($ this-> image, rand (1, $ this-> width-2), rand (1, $ this-> height-2), $ color );
}
/* Draw interference lines */
For ($ I = 0; $ I <$ this-> lineNum; $ I ++)
{
$ Color = imagecolorallocate ($ this-> image, rand (0,255), rand (0,255), rand (0,255 ));
Imageline ($ this-> image, rand (1, $ this-> width/2), rand (1, $ this-> height/2 ),
Rand ($ this-> width/2, $ this-> width-2), rand ($ this-> height/2, $ this-> height-2 ), $ color );}
}
/* Enable session saving and use echo to output the image */
Function _ toString ()
{
$ _ SESSION ['code'] = strtoupper ($ this-> checkCode );
$ This-> getCreateImage ();
$ This-> createCheckCode ();
$ This-> setDisturbColor ();
$ This-> outputImg ();
}
/* Internal private method output image */
Private function outputImg ()
{
Header ("content-type: image/png ");
Imagepng ($ this-> image );
}
/* Destructor, release object */
Function _ destruct ()
{
Imagedestroy ($ this-> image );
}
}
?>
Imgcode. php output image
<? Phpsession_start (); require_once ('myvcode. class. php'); echo new Vcode ();?>
Test.html: Same as img Tag reference
You can add a tag and use js to achieve another effect:
/* Partial refresh for verification code */
Function changeCode ()
{
Var imgcode = document. getElementById ('code ');
Var change = document. getElementById ('change ');
Change. onclick = function ()
{
/* You must add the following parameters to refresh */
Imgcode. src = 'Code. php? Tm '+ Math. random ();
}
}
Code and change are the IDs of img and a respectively.