Php image verification code

Source: Internet
Author: User
[Php] & lt ;? Php/*** image verification code class * generates an image verification code. the verification code contains numbers and uppercase letters. the session stores the md5-encrypted verification code. ** usage: * $ captcha = newCatpcha (); * $ captcha-& gt; buildAndExportImage (); ** author... "/> <scr

[Php]
/**
* Image verification code
* An image verification code is generated. the verification code contains numbers and uppercase letters. the md5 encrypted verification code is stored in the session.
*
* Usage:
* $ Captcha = new Catpcha ();
* $ Captcha-> buildAndExportImage ();
*
* Author: luojing
* Creation Time: 11:42:12
*/
Class Captcha {

Private $ width; // width
Private $ height; // height
Private $ codeNum; // Number of characters in the verification code
Private $ image; // verification code image resource
Private $ sessionKey; // name saved in the session
Private $ captcha; // verification code string
Const charWidth = 10; // The width of a single character, which varies according to the size of the output character

/**
* Create a verification code class and initialize relevant parameters
* @ Param $ width: Image width
* @ Param $ height: Image height
* @ Param $ codeNum verification code character count
* @ Param $ Name saved in sessionKey session
*/
Function _ construct ($ width = 50, $ height = 20, $ codeNum = 4, $ sessionKey = 'captcha '){
$ This-> width = $ width;
$ This-> height = $ height;
$ This-> codeNum = $ codeNum;
$ This-> sessionKey = $ sessionKey;

// Minimum height and width
If ($ height <20 ){
$ This-> height = 20;
}
If ($ width <($ codeNum * self: charWidth + 10) {// Each left and right retain 5 pixel gaps
$ This-> width = $ codeNum * self: charWidth + 10;
}
}

/**
* Construct and output the verification code Image
*/
Public function buildAndExportImage (){
$ This-> createImage ();
$ This-> setDisturb ();
$ This-> setCaptcha ();
$ This-> exportImage ();
}

/**
* Construct an image and set the background color
*/
Private function createImage (){
// Create an image
$ This-> image = imagecreatetruecolor ($ this-> width, $ this-> height );
// Create a background color
$ Bg = imagecolorallocate ($ this-> image, mt_rand (220,255), mt_rand (220,255), mt_rand (220,255 ));
// Fill in the background color
Imagefilledrectangle ($ this-> image, 0, 0, $ this-> width-1, $ this-> height-1, $ bg );
}

/**
* Set interference elements
*/
Private function setDisturb (){

// Set interference points
For ($ I = 0; I I <150; $ I ++ ){
$ Color = imagecolorallocate ($ this-> image, mt_rand (150,200), mt_rand (150,200), mt_rand (150,200 ));
Imagesetpixel ($ this-> image, mt_rand (5, $ this-> width-10), mt_rand (5, $ this-> height-3), $ color );
}

// Set the interference line
For ($ I = 0; $ I <10; $ I ++ ){
$ Color = imagecolorallocate ($ this-> image, mt_rand (150,220), mt_rand (150,220), mt_rand (150,220 ));
Imagearc ($ this-> image, mt_rand (-10, $ this-> width), mt_rand (-10, $ this-> height), mt_rand (30,300 ), mt_rand (20,200), 55, 44, $ color );
}

// Create a border color
$ Border = imagecolorallocate ($ this-> image, mt_rand (0, 50), mt_rand (0, 50), mt_rand (0, 50 ));
// Draw a border
Imagerectangle ($ this-> image, 0, 0, $ this-> width-1, $ this-> height-1, $ border );
}

/**
* Generate and draw a verification code
*/
Private function setCaptcha (){
$ Str = '23456789abcdefghjklmnpqrstuvwxyz ';
// Generate verification code characters
For ($ I = 0; $ I <$ this-> codeNum; $ I ++ ){
$ This-> captcha. =$ str {mt_rand (0, strlen ($ str)-1 )};
}
// Draw the verification code
For ($ I = 0; $ I <strlen ($ this-> captcha); $ I ++ ){
$ Color = imagecolorallocate ($ this-> image, mt_rand (0,200), mt_rand (0,200), mt_rand (0,200 ));
$ X = floor ($ this-> width-10)/$ this-> codeNum );
$ X = $ x * $ I + floor ($ x-self: charWidth)/2) + 5;
$ Y = mt_rand (2, $ this-> height-20 );
Imagechar ($ this-> image, 5, $ x, $ y, $ this-> captcha {$ I}, $ color );
}
}

/*
* Output the image and save the verification code to the session.
*/
Private function exportImage (){
If (imagetypes () & IMG_GIF ){
Header ('content-type: image/gif ');
Imagegif ($ this-> image );
} Else if (imagetypes () & IMG_PNG ){
Header ('content-type: image/png ');
Imagepng ($ this-> iamge );
} Else if (imagetypes () & IMG_JPEG ){
Header ('content-type: image/jpeg ');
Imagepng ($ this-> iamge );
} Else {
Imagedestroy ($ this-> image );
Die ("Don't support image type! ");
}
// Save the verification code information to the session and encrypt it with md5.
If (! Isset ($ _ SESSION )){
Session_start ();
}
$ _ SESSION [$ this-> sessionKey] = md5 ($ this-> captcha );

Imagedestroy ($ this-> image );
}

Function _ destruct (){
Unset ($ this-> width, $ this-> height, $ this-> codeNum, $ this-> captcha );
}
}

/**
* Image verification code
* An image verification code is generated. the verification code contains numbers and uppercase letters. the md5 encrypted verification code is stored in the session.
*
* Usage:
* $ Captcha = new Catpcha ();
* $ Captcha-> buildAndExportImage ();
*
* Author: luojing
* Creation Time: 11:42:12
*/
Class Captcha {
 
Private $ width; // width
Private $ height; // height
Private $ codeNum; // Number of characters in the verification code
Private $ image; // verification code image resource
Private $ sessionKey; // name saved in the session
Private $ captcha; // verification code string
Const charWidth = 10; // The width of a single character, which varies according to the size of the output character
 
/**
* Create a verification code class and initialize relevant parameters
* @ Param $ width: Image width
* @ Param $ height: Image height
* @ Param $ codeNum verification code character count
* @ Param $ Name saved in sessionKey session
*/
Function _ construct ($ width = 50, $ height = 20, $ codeNum = 4, $ sessionKey = 'captcha '){
$ This-> width = $ width;
$ This-> height = $ height;
$ This-> codeNum = $ codeNum;
$ This-> sessionKey = $ sessionKey;

// Minimum height and width
If ($ height <20 ){
$ This-> height = 20;
}
If ($ width <($ codeNum * self: charWidth + 10) {// Each left and right retain 5 pixel gaps
$ This-> width = $ codeNum * self: charWidth + 10;
}
}
 
/**
* Construct and output the verification code Image
*/
Public function buildAndExportImage (){
$ This-> createImage ();
$ This-> setDisturb ();
$ This-> setCaptcha ();
$ This-> exportImage ();
}
 
/**
* Construct an image and set the background color
*/
Private function createImage (){
// Create an image
$ This-> image = imagecreatetruecolor ($ this-> width, $ this-> height );
// Create a background color
$ Bg = imagecolorallocate ($ this-> image, mt_rand (220,255), mt_rand (220,255), mt_rand (220,255 ));
// Fill in the background color
Imagefilledrectangle ($ this-> image, 0, 0, $ this-> width-1, $ this-> height-1, $ bg );
}
 
/**
* Set interference elements
*/
Private function setDisturb (){

// Set interference points
For ($ I = 0; I I <150; $ I ++ ){
$ Color = imagecolorallocate ($ this-> image, mt_rand (150,200), mt_rand (150,200), mt_rand (150,200 ));
Imagesetpixel ($ this-> image, mt_rand (5, $ this-> width-10), mt_rand (5, $ this-> height-3), $ color );
}

// Set the interference line
For ($ I = 0; $ I <10; $ I ++ ){
$ Color = imagecolorallocate ($ this-> image, mt_rand (150,220), mt_rand (150,220), mt_rand (150,220 ));
Imagearc ($ this-> image, mt_rand (-10, $ this-> width), mt_rand (-10, $ this-> height), mt_rand (30,300 ), mt_rand (20,200), 55, 44, $ color );
}

// Create a border color
$ Border = imagecolorallocate ($ this-> image, mt_rand (0, 50), mt_rand (0, 50), mt_rand (0, 50 ));
// Draw a border
Imagerectangle ($ this-> image, 0, 0, $ this-> width-1, $ this-> height-1, $ border );
}
 
/**
* Generate and draw a verification code
*/
Private function setCaptcha (){
$ Str = '23456789abcdefghjklmnpqrstuvwxyz ';
// Generate verification code characters
For ($ I = 0; $ I <$ this-> codeNum; $ I ++ ){
$ This-> captcha. =$ str {mt_rand (0, strlen ($ str)-1 )};
}
// Draw the verification code
For ($ I = 0; $ I <strlen ($ this-> captcha); $ I ++ ){
$ Color = imagecolorallocate ($ this-> image, mt_rand (0,200), mt_rand (0,200), mt_rand (0,200 ));
$ X = floor ($ this-> width-10)/$ this-> codeNum );
$ X = $ x * $ I + floor ($ x-self: charWidth)/2) + 5;
$ Y = mt_rand (2, $ this-> height-20 );
Imagechar ($ this-> image, 5, $ x, $ y, $ this-> captcha {$ I}, $ color );
}
}
 
/*
* Output the image and save the verification code to the session.
*/
Private function exportImage (){
If (imagetypes () & IMG_GIF ){
Header ('content-type: image/gif ');
Imagegif ($ this-> image );
} Else if (imagetypes () & IMG_PNG ){
Header ('content-type: image/png ');
Imagepng ($ this-> iamge );
} Else if (imagetypes () & IMG_JPEG ){
Header ('content-type: image/jpeg ');
Imagepng ($ this-> iamge );
} Else {
Imagedestroy ($ this-> image );
Die ("Don't support image type! ");
}
// Save the verification code information to the session and encrypt it with md5.
If (! Isset ($ _ SESSION )){
Session_start ();
}
$ _ SESSION [$ this-> sessionKey] = md5 ($ this-> captcha );

Imagedestroy ($ this-> image );
}
 
Function _ destruct (){
Unset ($ this-> width, $ this-> height, $ this-> codeNum, $ this-> captcha );
}
}

 

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.