Php verification code class .? Php *** verification code class * chaojie2008@126.com * 2012-02-09 *** classVailimg {private $ width; verification code picture width private $ height; verification code picture height private $ codeN /**
* Verification code
* Chaojie2008@126.com
* 2012-02-09
**/
Class Vailimg {
Private $ width; // the image width of the verification code.
Private $ height; // height of the verification code Image
Private $ codeNum; // Number of characters in the verification code
Private $ checkCode; // verification code character
Private $ image; // verification code canvas
/* The constructor is used to instantiate the verification code object and initialize some member attributes */
/* Parameter width: set the image width of the verification code. the default width is 60 pixels */
/* Parameter height: sets the height of the verification code Image. the default height is 20 pixels */
/* CodeNum: set the number of letters and numbers in the verification code. the default number is 4 */
Function _ construct ($ width = 60, $ height = 20, $ codeNum = 4 ){
$ This-> width = $ width; // is the initial attribute width of the member.
$ This-> height = $ height; // is the initial modification of the height attribute of the member.
$ This-> codeNum = $ codeNum; // The initial deployment of the member attribute codeNum
$ This-> checkCode = $ this-> createCheckCode (); // The initial implementation of the member attribute checkCode
}
Function showImage () {// access this method to output the image to the browser
$ This-> getCreateImage (); // call an internal method to create a canvas and initialize it.
$ This-> outputText (); // output random strings to the image
$ This-> setDisturbColor (); // sets interference pixels to the image.
$ This-> outputImage (); // generate an image of the corresponding format and output it
}
Function getCheckCode () {// access this method to obtain the randomly created verification code string
Return $ this-> checkCode; // returns the string saved by the member attribute $ checkCode.
}
Private function getCreateImage () {// used to create image resources and visualize the background
$ This-> image = imageCreate ($ this-> width, $ this-> height );
$ Back = imageColorAllocate ($ this-> image, 255,255,255 );
$ Border = imageColorAllocate ($ this-> image, 0, 0, 0 );
ImageRectangle ($ this-> image, 0, 0, $ this-> width-1, $ this-> height-1, $ border );
}
Private function createCheckCode () {// randomly generates a specified number of strings.
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
}
$ Ascii = sprintf ("% c", $ rand_number );
$ Ascii_number = $ ascii_number. $ ascii;
}
Return $ ascii_number;
}
Private function setDisturbColor () {// sets the interference pixel and outputs 100 points of different colors to the image.
For ($ I = 0; $ I <= 50; $ 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 );
}
}
Private function outputText () {// random color, random placement, 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 () {// automatically detects the image types supported by GD and outputs the image
If (imagetypes () & IMG_GIF) {// determines whether the function for generating GIF images exists
Header ("Content-type: image/gif"); // Set the MIME type to image/gif.
Imagegif ($ this-> image); // output the image to the browser in GIF format
} Elseif (imagetypes () & IMG_JPG) {// determines whether the function for generating JPG images exists
Header ("Content-type: image/jpeg"); // Set the MIME type to image/jpeg.
Imagejpeg ($ this-> image, "", 0.5); // output the image to the browser in JPEN format
} Elseif (imagetypes () & IMG_PNG) {// determines whether the function for generating a PNG image exists
Header ("Content-type: image/png"); // you can specify the MIME type as image/png.
Imagepng ($ this-> image); // output the image to the browser in PNG format
} Elseif (imagetypes () & IMG_WBMP) {// determines whether the function for generating a WBMP image exists
Header ("Content-type: image/vnd. wap. wbmp"); // The sending header is image/wbmp.
Imagewbmp ($ this-> image); // output the image to the browser in WBMP format
} Else {// if no supported Image type is available
Die ("PHP does not support Image creation! "); // No image output, an error message is output, and the program exits.
}
}
Function _ destruct () {// destroy Image resources before the end of the object to release memory
Imagedestroy ($ this-> image); // call the method in the GD library to destroy image resources
}
}
?>
From chaojie2009
Http://www.bkjia.com/PHPjc/478390.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/478390.htmlTechArticle? Php/*** verification code class * chaojie2008@126.com * 2012-02-09 ***/class Vailimg {private $ width; // verification code picture width private $ height; // the height of the verification code image is private $ codeN...