Php Verification Code

Source: Internet
Author: User

<? Php
/**
* 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.