Home >Backend Development >PHP Tutorial >PHP image verification code generation program code_PHP tutorial

PHP image verification code generation program code_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-13 10:45:49856browse

There are two verification code generation programs here. The first one is better, and the second one is also good. At the same time, the second generation program uses a complete example to illustrate the verification code calling and generation.

There are two verification code generation programs here. The first version of the verification code generation program is better, and the second one is also good. At the same time, the second generation program uses a complete example to illustrate the calling and generation of the verification code. */

session_start();
class authnum {
//Image object, width, height, verification code length
private $im;
private $im_width;
private $im_height;
private $len;
//Random string, y-axis coordinate value, random color
private $randnum;
private $y;
private $randcolor;
//Background color red, green and blue, default is light gray
public $red=238;
public $green=238;
public $blue=238;
/**
* Optional settings: verification code type, interference point, interference line, y-axis random
* Set to false to disable
**/
//The default is a mixture of uppercase and lowercase numbers, 1 2 3 respectively represents lowercase, uppercase, and numbers
public $ext_num_type='';
public $ext_pixel = false; //interference point
public $ext_line = false; //interference line
public $ext_rand_y= true; //y-axis random
function __construct ($len=4,$im_width='',$im_height=25) {
// Verification code length, image width, and height are required data when instantiating 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 image background color, the default is light gray background
function set_bgcolor () {
imagecolorallocate($this->im,$this->red,$this->green,$this->blue);
}
// Get a random code of any number
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 len; $i++) {
$start = rand(1,strlen($str) - 1);
$randnum .= substr($str,$start,1);
}
$this->randnum = $randnum;
$_session[an] = $this->randnum;
}
// Get the y-axis of the verification code image
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 interference points
function set_ext_pixel () {
if ($this->ext_pixel) {
for($i = 0; $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 $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 verification code image:
* Create canvas (__construct function)
* Set canvas background ($this->set_bgcolor();)
* Get a random string ($this->get_randnum ();)
* Write text to the image (imagestring function)
* Add interference points/lines ($this->set_ext_line(); $this->set_ext_pixel();)
* Output image
**/
function create () {
$this->set_bgcolor();
$this->get_randnum ();
for($i = 0; $i 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);     //释放图像资源
 }

}//end class
/**使用验证码类的方法:
 * $an = new authnum(验证码长度,图片宽度,图片高度);
 * 实例化时不带参数则默认是四位的60*25尺寸的常规验证码图片
 * 表单页面检测验证码的方法,对比 $_session[an] 是否等于 $_post[验证码文本框id]
 * 可选配置:
 *  1.验证码类型:$an->ext_num_type=1;  值为1是小写类型,2是大写类型,3是数字类型
 *  2.干扰点:$an->ext_pixel = false;   值为false表示不添加干扰点
 *  3.干扰线:$an->ext_line = false;    值为false表示不添加干扰线
 *  4.y轴随机:$an->ext_rand_y = false; 值为false表示不支持图片y轴随机
 *  5.图片背景:改变 $red $green $blue 三个成员变量的值即可
**/
$an = new authnum();
$an->ext_num_type='';
$an->ext_pixel = true; //干扰点
$an->ext_line  = false; //干扰线
$an->ext_rand_y= true; //y轴随机
$an->green = 238;
$an->create();
?>

好下面来看一款验证码调用实例

例子demo:

以下为引用的内容:




hi.baidu.com/ji_haiyang


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn