Self-encapsulated ———— Captcha Tool class ————

Source: Internet
Author: User

The following is my own encapsulation of the Verification Code tool class, in peacetime projects will be more often used in the tool class, the current package of this tool class simple version, if the need for a partner can be used, of course, I recommend using the configuration file before adding some options information

    //Verification Code Width    Private $width; //Verification Code Height    Private $height; //Number of validations    Private $length; //number of interference points    Private $dots; //types of interference points    Private $type; //number of interference lines    Private $lines; //text    Private $font;

It is convenient to change the requirements of the verification code in the project to facilitate the requirements of the project logic, and the font chosen by the CAPTCHA needs to be placed in the same directory as the Captcha tool class, otherwise the path of the font in the configuration file will be changed in order to display the verification code.

Create Authenticode Tool class
Class captcha{
Various parameters of the verification code

Verification Code width
Private $width;
Verification Code Height
Private $height;
Number of validations
Private $length;
Number of interference points
Private $dots;
Types of interference points
Private $type;
Number of interference lines
Private $lines;
Text
Private $font;


How to construct verification code attributes
Public function __construct ($arr =array ()) {
Assigning a property to a value
$this->width =isset ($arr [' width '])? Trim ($arr [' width ']): ' 270 ';
$this->height=isset ($arr [' height '])? Trim ($arr [' Height ']): ' 30 ';
$this->length=isset ($arr [' length '])? Trim ($arr [' length ']): ' 4 ';
$this->dots =isset ($arr [' dots '])? Trim ($arr [' dots ']): ' 81 ';
$this->type =isset ($arr [' type '])? Trim ($arr [' type ']): ' * ';
$this->lines =isset ($arr [' lines '])? Trim ($arr [' Lines ']): ' 5 ';
$this->font =isset ($arr [' font '])? Trim ($arr [' Font ']): '/cambriab.ttf ';

}

How to create a verification code
Public Function Captcha () {

Create a canvas
$img = Imagecreatetruecolor ($this->width, $this->height);

Fill Color
Color Resources
$color =imagecolorallocate ($img, Mt_rand (200,255), Mt_rand (200,255), Mt_rand (200,255));
Fill background
Imagefill ($img, 0,0, $color);

Adding interference points
for ($i =0; $i < $this->dots; $i +) {
Color Resources
$dots _color=imagecolorallocate ($img, Mt_rand (150,200), Mt_rand (150,200), Mt_rand (150,200));
Inserting interference points
Imagestring ($img, Mt_rand (1,5), Mt_rand (0, $this->width), Mt_rand (0, $this->height), $this->type, $dots _ color);
}

Adding interference lines
for ($i =0; $i < $this->lines; $i +) {
Color Resources
$line _color=imagecolorallocate ($img, Mt_rand (150,200), Mt_rand (150,200), Mt_rand (150,200));
Inserting interference lines
Imageline ($img, Mt_rand (0, $this->width), Mt_rand (0, $this->height), Mt_rand (0, $this->width), Mt_rand (0,$ This->height), $line _color);
}

First get the verification code, then insert the verification text
Text height
$size =mt_rand (18,20);
Get Verification Code
$str = $this->captchastring ();

for ($i =0; $i <strlen ($STR); $i + +) {
Color Resources
$str _color=imagecolorallocate ($img, Mt_rand (50,150), Mt_rand (50,150), Mt_rand (50,150));
Insert Verification Code
Imagettftext ($img, $size, Mt_rand ( -45,45), $this->width/($this->length+2) * ($i + 1), (($this->height-$size)/ 2) + $size, $str _color, $this->font, $str [$i]);
}

The verification code will be saved to the session, so that it is easy to judge the verification code.
@session_start ();
$_session[' Captcha ']= $str;



Output picture
Header ("Content-type:image/png");
Imagepng ($IMG);
Clear Resources
Imagedestroy ($IMG);
}

Get random verification content: a-z,a-z,1-9
Private Function captchastring () {
Get four random strings
$str = "";
for ($i =0; $i < $this->length; $i +) {
Switch (Mt_rand (1,3)) {
Case 1: $str. = Chr (Mt_rand (49,57));
Break
Case 2: $str. = Chr (Mt_rand (97,122));
Break
Case 3: $str. = Chr (Mt_rand (65,90));
Break
}
}
return $str;
}

Judging the Verification code
public static function Checkcaptcha ($captcha) {
@session_start ();
Return Strtoupper ($captcha) = = = Strtoupper ($_session[' captcha ');
}
}

Self-encapsulated ———— Captcha Tool class ————

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.