PHP verification code generation class complete code

Source: Internet
Author: User

This article provides a flexible and easy-to-use php verification code generation class, users can define each member including width, height, canvas, word count, Type, and painting Type. At the same time, we only need to modify $ Type to define the generated numbers and lowercase letters, A mix of uppercase and lowercase numbers. For more information, see.

The Code is as follows: Copy code

<? Php
Class Code {

// 1. Define the width, height, canvas, word count, type, and painting type of each member.

Private $ width; // width
Private $ height; // height
Private $ num; // number of characters in the verification code
Private $ imgType; // type of the generated image
Private $ Type; // string Type: 1, 2, 3, three options: 1, numbers, 2, lowercase letters, 3, uppercase, lowercase letters, and numbers
Private $ hb; // canvas
Public $ codestr; // verification code string

Public function _ construct ($ height = 20, $ num = 4, $ imgType = "jpeg", $ Type = 1 ){
$ This-> width = $ num * 20;
$ This-> height = $ height;
$ This-> num = $ num;
$ This-> imgType = $ imgType;
$ This-> Type = $ Type;
$ This-> codestr = $ this-> codestr ();
$ This-> zuhe ();
}

// 2. Define a random string Function
Private function codestr (){
Switch ($ this-> Type ){

Case 1: // type 1 to obtain a random number from 1 to 9
$ Str = implode ("", array_rand (range (0, 9), $ this-> num ));
Break;
Case 2: // type 2 get a-z random lowercase letter
$ Str = implode ("", array_rand (array_flip (range (a, z), $ this-> num ));
Break;
Case 3: // Type 3: Numbers, lowercase letters, and uppercase letters
For ($ I = 0; $ I <$ this-> num; $ I ++ ){
$ M = rand (0, 2 );
Switch ($ m ){
Case 0:
$ O = rand (48, 57 );
Break;
Case 1:
$ O = rand (65,90 );
Break;
Case 2:
$ O = revert (97,122 );
Break;
}
$ Str. = sprintf ("% c", $ o );
}
Break;
}


Return $ str;
}


// 3. initialize canvas image resources
Private function Hb (){
$ This-> hb = imagecreatetruecolor ($ this-> width, $ this-> height );
}

// 4. Generate the background color
Private function Bg (){
Return imagecolorallocate ($ this-> hb, rand (130,250), rand (130,250), rand (130,250 ));
}

// 5. Generate the font color
Private function Font (){
Return imagecolorallocate ($ this-> hb, rand (0,100), rand (0,100), rand (0,100 ));
}

// 6. Fill in the background color
Private function BgColor (){
Imagefilledrectangle ($ this-> hb, 0, $ this-> width, $ this-> height, $ this-> Bg ());
}

// 7. Interference point
Private function ganrao (){
$ Sum = floor ($ this-> width) * ($ this-> height)/3 );
For ($ I = 0; $ I <$ sum; $ I ++ ){
Imagesetpixel ($ this-> hb, rand (0, $ this-> width), rand (0, $ this-> height), $ this-> Bg ());
}
}

// 8. Random line arc
Private function huxian (){
For ($ I = 0; $ I <$ this-> num; $ I ++ ){
ImageArc ($ this-> hb, rand (0, $ this-> width), rand (0, $ this-> height), rand (0, $ this-> width), rand (0, $ this-> height), rand (0,360), rand (0,360), $ this-> Bg ());
}
}

// 9. Write
Private function xiezi (){
For ($ I = 0; $ I <$ this-> num; $ I ++ ){
$ X = ceil ($ this-> width/$ this-> num) * $ I;
$ Y = rand (1, $ this-> height-15 );
Imagechar ($ this-> hb, 5, $ x + 4, $ y, $ this-> codestr [$ I], $ this-> Font ());
}
}

// 10. Output
Private function OutImg (){
$ Shuchu = "image". $ this-> imgType;
$ Header = "Content-type: image/". $ this-> imgType;
If (function_exists ($ shuchu )){
Header ($ header );
$ Shuchu ($ this-> hb );
} Else {
Exit ("GD library does not have such images ");
}
}

// 11. Assemble
Private function zuhe (){
$ This-> Hb ();
$ This-> BgColor ();
$ This-> ganrao ();
$ This-> huxian ();
$ This-> xiezi ();
$ This-> OutImg ();
}

Public function getCodeStr (){
Return $ this-> codestr;
}
}
?>


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.