PHP verification code class code of the latest modification fully customized!

  • 2020-03-31 21:25:40
  • OfStack

Authnum.class.php (link: http://xiazai.jb51.net/201012/yuanma/Authnum_class.rar)
 
<?php 
session_start(); 
class Authnum { 
//Picture object, width, height, captcha 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 color is light gray
public $red=238; 
public $green=238; 
public $blue=238; 
 
//The default is a mixture of uppercase and lowercase Numbers, with 1, 2, and 3 representing lowercase, uppercase, and numeric types, respectively
public $ext_num_type=''; 
public $ext_pixel = false; //Noise points
public $ext_line = false; //Interference lines
public $ext_rand_y= true; //Y random
function __construct ($len=4,$im_width='',$im_height=25) { 
//Captcha length, image width, and height are the data 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 the background color of the image. The default is a light gray background
function set_bgcolor () { 
imagecolorallocate($this->im,$this->red,$this->green,$this->blue); 
} 
//Obtain a random code for any number of digits
function get_randnum () { 
$an1 = 'abcdefghijklmnopqrstuvwxyz'; 
$an2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; 
$an3 = '0123456789'; 
if ($this->ext_num_type == '') $str = $an1.$an2.$an3; 
if ($this->ext_num_type == 1) $str = $an1; 
if ($this->ext_num_type == 2) $str = $an2; 
if ($this->ext_num_type == 3) $str = $an3; 
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->im_height/5); 
else $this->y = $this->im_height / 4 ; 
} 
//Get random color
function get_randcolor () { 
$this->randcolor = imagecolorallocate($this->im,rand(0,100),rand(0,150),rand(0,200)); 
} 
//  add Noise points
function set_ext_pixel () { 
if ($this->ext_pixel) { 
for($i = 0; $i < 100; $i++){ 
$this->get_randcolor(); 
imagesetpixel($this->im, rand()%100, rand()%100, $this->randcolor); 
} 
} 
} 
//  add Interference lines
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 captcha image:  
*  Create canvas ( __construct Function)  
*  Set the canvas background ( $this->set_bgcolor(); )  
*  Get a random string ( $this->get_randnum (); )  
*  Writing on the picture ( imagestring Function)  
*  Add interference point / Line ( $this->set_ext_line(); $this->set_ext_pixel(); )  
*  The output image  
**/ 
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); //Release image resources
} 
}//end class 
 
$an = new Authnum(); 
$an->ext_num_type=''; 
$an->ext_pixel = true; //Noise points
$an->ext_line = false; //Interference lines
$an->ext_rand_y= true; //Y random
$an->green = 238; 
$an->create(); 
?> 

Related articles: