PHP dynamic and random generation of verification code class code _ PHP Tutorial

Source: Internet
Author: User
PHP dynamically generates a random verification code. Below is, this is the interference code that is not enabled. below is the code for copying the class code :? Php *************************************** * ******** FILE: the following figure shows that the interference code is not enabled.

The following is the class code

The code is as follows:


/*************************************** *********
// 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 ();
**************************************** ***************************/
?>

The following code of the category is as follows :? Php /************************************** * ******** // FILE :...

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.