PHP implementation of the encapsulation Verification Code class details

Source: Internet
Author: User

Write a verification code class in PHP and encapsulate it.
Class Name: validationcode. class. php
The Code is as follows: Copy codeThe Code is as follows: <? 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 (){
// Here the random code is mainly generated, starting from 2 to distinguish between 1 and l
$ 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 );
}
}

Use:
Test and call Verification Code
Code. phpCopy codeThe Code is as follows: <? 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.

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.