Encapsulate a verification code class
Validationcode. class. php
<? Php
Class ValidationCode {
Private $ width;
Private $ height;
Private $ codeNum;
Private $ image; // image resources
Private $ disturbColorNum;
Private $ checkCode;
Function _ construct ($ width = 80, $ height = 20, $ codeNum = 4 ){
$ This-> width = $ width;
$ This-> height = $ height;
$ This-> codeNum = $ codeNum;
$ This-> checkCode = $ this-> createCheckCode ();
$ Number = floor ($ width * $ height/15 );
If ($ number> 240-$ codeNum ){
$ This-> disturbcolornum= 240-$ codeNum;
} Else {
$ This-> disturbColorNum = $ number;
}
}
// Output the image to the browser by accessing this method
Function showImage ($ fontFace = ""){
// Step 1: create an image background
$ This-> createImage ();
// Step 2: Set interference elements
$ This-> setDisturbColor ();
// Step 3: randomly draw text from the image
$ This-> outputText ($ fontFace );
// Step 4: output the image
$ This-> outputImage ();
}
// Obtain the randomly created verification code string by calling this method
Function getCheckCode (){
Return $ this-> checkCode;
}
Private function createImage (){
// Create image resources
$ This-> image = imagecreatetruecolor ($ this-> width, $ this-> height );
// Random background color
$ BackColor = imagecolorallocate ($ this-> image, rand (225,255), rand (225,255), rand (225,255 ));
// Add color to the background
Imagefill ($ this-> image, 0, 0, $ backColor );
// Set the border color
$ Border = imagecolorallocate ($ this-> image, 0, 0, 0 );
// Draw a rectangular border
Imagerectangle ($ this-> image, 0, 0, $ this-> width-1, $ this-> height-1, $ border );
}
Private function setDisturbColor (){
For ($ I = 0; $ I <$ this-> disturbColorNum; $ 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 );
}
For ($ I = 0; $ I <10; $ I ++ ){
$ Color = imagecolorallocate ($ this-> image, rand (200,255), rand (200,255), rand (200,255 ));
Imagearc ($ this-> image, rand (-10, $ this-> width), rand (-10, $ this-> height), rand (30,300 ), rand (20,200), 55, 44, $ color );
}
}
Private function createCheckCode (){
$ Code = "23456789 abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ ";
$ String = '';
For ($ I = 0; $ I <$ this-> codeNum; $ I ++ ){
$ Char = $ code {rand (0, strlen ($ code)-1 )};
$ String. = $ char;
}
Return $ string;
}
Private function outputText ($ fontFace = ""){
For ($ I = 0; $ I <$ this-> codeNum; $ I ++ ){
$ Fontcolor = imagecolorallocate ($ this-> image, rand (0,128), rand (0,128), rand (0,128 ));
If ($ fontFace = ""){
$ Fontsize = rand (3, 5 );
$ X = floor ($ this-> width/$ this-> codeNum) * $ I + 3;
$ Y = rand (0, $ this-> height-15 );
Imagechar ($ this-> image, $ fontsize, $ x, $ y, $ this-> checkCode {$ I}, $ fontcolor );
} Else {
$ Fontsize = rand (12, 16 );
$ X = floor ($ this-> width-8)/$ this-> codeNum) * $ I + 8;
$ Y = rand ($ fontSize + 5, $ this-> height );
Imagettftext ($ this-> image, $ fontsize, rand (-30, 30), $ x, $ y, $ fontcolor, $ fontFace, $ this-> checkCode {$ I });
}
}
}
Private function outputImage (){
If (imagetypes () & IMG_GIF ){
Header ("Content-Type: image/gif ");
Imagepng ($ this-> image );
} Else if (imagetypes () & IMG_JPG ){
Header ("Content-Type: image/jpeg ");
Imagepng ($ this-> image );
} Else if (imagetypes () & IMG_PNG ){
Header ("Content-Type: image/png ");
Imagepng ($ this-> image );
} Else if (imagetypes () & IMG_WBMP ){
Header ("Content-Type: image/vnd. wap. wbmp ");
Imagepng ($ this-> image );
} Else {
Die ("PHP does not support image creation ");
}
}
Function _ destruct (){
Imagedestroy ($ this-> image );
}
}
Call Verification Code
Code. php
<? Php
Session_start ();
Include "validationcode. class. php ";
$ Code = new ValidationCode (80, 20, 4 );
$ Code-> showImage (); // output to the page for registration or login
$ _ SESSION ["code"] = $ code-> getCheckCode (); // Save the verification code to the server.