A Practical php verification code class _ PHP Tutorial

Source: Internet
Author: User
A very practical php verification code class. A more practical php graphic verification code generation class, the call method is also very simple, you can refer to a friend in need. The code is as follows: Copy the code? PhpclassImageCode {private $ widt is a practical php graphic verification code generation class. The Calling method is also very simple. if you need it, you can refer to it.
The code is as follows:

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 _ construct ($ width = 60, $ height = 20, $ codeNum = 4 ){
$ This-> width = $ width;
$ This-> height = $ height;
$ This-> codeNum = $ codeNum;
$ This-> checkCode = $ this-> createCheckCode ();
}

Function getcreateImage (){
$ This-> getcreateImage ();
$ This-> outputText ();
$ This-> setDisturbColor ();
$ This-> outputImage ();
}
Function getCheckCode (){
Return $ this-> checkCode;
}

Private function getCreateImage (){
$ This-> image = imagecreatetruecolor ($ this-> width, $ this-> height );
$ Black = imagecolorallocate ($ this-> image, 255,255,255, 0 );
$ Border = imagecolorallocate ($ this-> image, 255,255,255,255 );
Imagefilledrectangle ($ 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); // number
Break;
Case 1:
$ Rand_number = rand (); // uppercase letters
Break;
Case 2:
$ Rand_number = rand (97,122 );
Break;
}
$ Asc = sprintf ("% c", $ rand_number );
$ Asc_number = $ asc_number. $ asc;
}
Return $ asc_number;
}

Private function setDisturbColor (){
For ($ I = 0; $ I <= 100; $ I ++ ){
$ Color = imagecolorallocate ($ this-> image, 255,255,255 );
Imagesetpixel ($ this-> image, rand (1, $ this-> width-2), rand (1, $ this-> height-2), $ color );
}
}

Private function outputImage (){
If (imagetypes () & IMG_GIF ){
Header ("Content_type: image/gif ");
Imagegif ($ this-> image );
} Elseif (imagetypes () & IMG_JGP ){
Header ("Content_type: image/jpeg ");
Imagejpeg ($ this-> image, "", 0.5 );
} Else {
Die ("PHP does not support Image creation ");
}
}

Function _ destruct (){
Imagedestroy ($ this-> image );
}
}

?>

Bytes. The code is as follows? Php classImageCode {private $ widt...

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.