PHP Verification Code Class code (latest modification, fully customized!)

Source: Internet
Author: User
Tags numeric lowercase php download rand
Authnum.class.php Download
Copy CodeThe code is as follows:
<?php
Session_Start ();
Class Authnum {
Picture object, width, height, verification code length
Private $im;
Private $im _width;
Private $im _height;
Private $len;
Random string, y coordinate value, random color
Private $randnum;
Private $y;
Private $randcolor;
The background color is red, green and blue, the default is light gray
Public $red = 238;
Public $green = 238;
Public $blue = 238;
/**
* Optional Settings: Authentication code type, interference point, interference line, y-axis random
* Set to False to not enable
**/
The default is a combination of uppercase and lowercase numbers, 1 2 3 for small, upper-case, and numeric
Public $ext _num_type= ';
Public $ext _pixel = false; Jamming Point
Public $ext _line = false; Jamming line
Public $ext _rand_y= true; Y-Axis random
function __construct ($len =4, $im _width= ', $im _height=25) {
Verification code length, picture width, height is the data that is required to instantiate the class
$this->len = $len; $im _width = $len * 15;
$this->im_width = $im _width;
$this->im_height= $im _height;
$this->im = imagecreate ($im _width, $im _height);
}
Set picture background color, default is light gray background
function Set_bgcolor () {
Imagecolorallocate ($this->im, $this->red, $this->green, $this->blue);
}
Random code to obtain arbitrary digit number
function Get_randnum () {
$an 1 = ' abcdefghijklmnopqrstuvwxyz ';
$an 2 = ' abcdefghijklmnopqrstuvwxyz ';
$an 3 = ' 0123456789 ';
if ($this->ext_num_type = = ") $str = $an 1. $an 2. $an 3;
if ($this->ext_num_type = = 1) $str = $an 1;
if ($this->ext_num_type = = 2) $str = $an 2;
if ($this->ext_num_type = = 3) $str = $an 3;
for ($i = 0; $i < $this->len; $i + +) {
$start = rand (1,strlen ($STR)-1);
$randnum. = substr ($str, $start, 1);
}
$this->randnum = $randnum;
$_session[an] = $this->randnum;
}
Get Verification code Picture Y axis
function get_y () {
if ($this->ext_rand_y) $this->y = rand (5, $this-&GT;IM_HEIGHT/5);
else $this->y = $this-&GT;IM_HEIGHT/4;
}
Get random Color
function Get_randcolor () {
$this->randcolor = imagecolorallocate ($this->im,rand (0,100), Rand (0,150), Rand (0,200));
}
Add interference points
function Set_ext_pixel () {
if ($this->ext_pixel) {
for ($i = 0; $i < $i + +) {
$this->get_randcolor ();
Imagesetpixel ($this->im, rand ()%100, Rand ()%100, $this->randcolor);
}
}
}
Add interference Line
function Set_ext_line () {
if ($this->ext_line) {
for ($j = 0; $j < 2; $j + +) {
$rand _x = rand (2, $this->im_width);
$rand _y = rand (2, $this->im_height);
$rand _x2 = rand (2, $this->im_width);
$rand _y2 = rand (2, $this->im_height);
$this->get_randcolor ();
Imageline ($this->im, $rand _x, $rand _y, $rand _x2, $rand _y2, $this->randcolor);
}
}
}
/** Create a CAPTCHA Image:
* Create canvas (__construct function)
* Set Canvas background ($this->set_bgcolor ();)
* Get a random string ($this->get_randnum ();)
* Writing to the picture (imagestring function)
* Add interference point/Line ($this->set_ext_line (); $this->set_ext_pixel ();
* Output Picture
**/
function Create () {
$this->set_bgcolor ();
$this->get_randnum ();
for ($i = 0; $i < $this->len; $i + +) {
$font = rand (4,6);
$x = $i/$this->len * $this->im_width + rand (1, $this->len);
$this->get_y ();
$this->get_randcolor ();
Imagestring ($this->im, $font, $x, $this->y, substr ($this->randnum, $i, 1), $this->randcolor);
}
$this->set_ext_line ();
$this->set_ext_pixel ();
Header ("Content-type:image/png");
Imagepng ($this->im);
Imagedestroy ($this->im); Releasing image resources
}
}//end class
/** methods for using the Authentication code class:
* $an = new Authnum (Verification code length, picture width, picture height);
* Instantiated with no parameters, the default is four-bit 60*25 size of the general verification Code picture
* Form page Detection Verification code method, contrast $_session[an] is equal to $_post[captcha text box ID]
* Optional Configuration:
* 1 Authentication code type: $an->ext_num_type=1; A value of 1 is a lowercase type, 2 is an uppercase type, and 3 is a numeric type
* 2. Jamming point: $an->ext_pixel = false; A value of false indicates no interference point added
* 3. Interference line: $an->ext_line = false; A value of false indicates no interference lines added
* 4.Y Axis random: $an->ext_rand_y = false; A value of false indicates that the picture y-axis is not supported randomly
* 5. Picture background: Change $red $green $blue The value of three member variables
**/
$an = new Authnum ();
$an->ext_num_type= ';
$an->ext_pixel = true; Jamming Point
$an->ext_line = false; Jamming line
$an->ext_rand_y= true; Y-Axis random
$an->green = 238;
$an->create ();
?>


Related Article

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.