The following figure shows that the interference code is not enabled.
The following is the class code
CopyCode The Code is as follows: <? PHP
/*************************************** *********
// File: imagecode
// Done: dynamic verification code generation class
// Date "2010-3-31
// Author: www.5dkx.com 5d happy blog
**************************************** ********************************/
Class imagecode {
Private $ width; // The image width of the verification code.
Private $ height; // The Image Height of the Verification Code
Private $ codenum; // number of characters in the verification code
Private $ checkcode; // Verification Code character
Private $ image; // Verification Code canvas
/*************************************** *********************************
// Function: Constructor
// Done: Initialize the member attributes.
// Author: www.5dkx.com 5d happy blog
**************************************** ********************************/
Function _ construct ($ width = 60, $ Height = 20, $ codenum = 4)
{
$ This-> width = $ width;
$ This-> Height = $ height;
$ This-> codenum = $ codenum;
$ This-> checkcode = $ this-> createcheckcode ();
}
Function showimage ()
{
$ This-> getcreateimage ();
$ This-> outputtext ();
$ This-> setdisturbcolor ();
$ This-> outputimage ();
}
Function getcheckcode ()
{
Return $ this-> chekcode;
}
Private function getcreateimage ()
{
$ This-> image = imagecreatetruecolor ($ this-> width, $ this-> height );
$ Back = imagecolorallocate ($ this-> image, 255,255,255 );
$ Border = imagecolorallocate ($ this-> image, 255,255,255 );
Imagefilledrectangle ($ this-> image, 0, 0, $ this-> width-1, $ this-> height-1, $ border );
// Fill the rectangular frame with pure white color. If this box is used, the interference code is invalid.
/* If you want to use the interference code, use the following */
// Imagerectangle ($ this-> image, 0, 0, $ this-> width-1, $ this-> height-1, $ border );
}
Private function createcheckcode ()
{
For ($ I = 0; $ I <$ this-> codenum; $ I ++)
{
$ Number = rand (0, 2 );
Switch ($ number)
{
Case 0: $ rand_number = rand (48, 57); break; // number
Case 1: $ rand_number = rand (); break; // uppercase letter
Case 2: $ rand_number = rand (97,122); break; // lowercase letter
}
$ ASC = sprintf ("% C", $ rand_number );
$ Asc_number = $ asc_number. $ ASC;
}
Return $ asc_number;
}
Private function setdisturbcolor () // do you want to set interference?
{
For ($ I = 0; $ I <= 100; $ I ++)
{
// $ Color = imagecolorallocate ($ this-> image, Rand (0,255), Rand (0,255), Rand (0,255 ));
$ Color = imagecolorallocate ($ this-> image, 255,255,255 );
Imagesetpixel ($ this-> image, Rand (1, $ this-> width-2), Rand (1, $ this-> height-2), $ color );
}
// $ Color = imagecolorallocate ($ this-> image, 0, 0 );
// Imagesetpixel ($ this-> image, Rand (1, $ this-> width-2), Rand (1, $ this-> height-2), $ color );
}
Private function outputtext ()
{
// Random color, random placement, and random string output to the image
For ($ I = 0; $ I <= $ this-> codenum; $ I ++)
{
$ Bg_color = imagecolorallocate ($ this-> image, Rand (0,255), Rand (0,128), Rand (0,255 ));
$ X = floor ($ this-> width/$ this-> codenum) * $ I + 3;
$ Y = rand (0, $ this-> height-15 );
Imagechar ($ this-> image, 5, $ X, $ y, $ this-> checkcode [$ I], $ bg_color );
}
}
Private function outputimage ()
{
If (imagetypes () & img_gif)
{
Header ("content_type: image/GIF ");
Imagegif ($ this-> image );
}
Elseif (imagetypes () & img_jpg)
{
Header ("Content-Type: image/JPEG ");
Imagejpeg ($ this-> image, "", 0.5 );
}
Elseif (imagetypes () & img_png)
{
Header ("Content-Type: image/PNG ");
Imagejpeg ($ this-> image );
}
Elseif (imagetypes () & img_wbmp)
{
Header ("Content-Type: image/vnd. WAP. wbmp ");
Imagejpeg ($ this-> image );
}
Else
{
Die ("php does not support image creation ");
}
}
Function _ destruct ()
{
Imagedestroy ($ this-> image );
}
}
/* Display */
/*************************************** ****************************
Session_start ();
$ Image = new imagecode (60, 20, 4 );
$ Image-> showimage ();
$ _ Session ['imagecode'] = $ image-> getcheckcode ();
**************************************** ***************************/
?>