php面向对象写验证码,PHP中面向对象的验证码类

/**

* 验证码类

*/

class Verify

{

//成员属性

private $width;//宽

private $height;//高

private $verify_code;//验证码字符串

private $verify_nums;//验证码个数

private $verify_type;//验证码字符类型

private $p_w_picpath_type;//图片类型(JPG/PNG)

private $res;//图片资源

//构造函数来执行需要初始化的成员属性

public function __construct($width = 120,$height = 50,$verify_nums = 4,$verify_type = 3,$p_w_picpath_type = 'png')

{

$this->width = $width;

$this->height = $height;

$this->verify_nums = $verify_nums;

$this->verify_type = $verify_type;

$this->p_w_picpath_type = $p_w_picpath_type;

$this->verify_code = $this->setVerifyCode();

$this->res = $this->createRes();

}

//唯一供别人调用的输出方法

public function show()

{

$this->createRes();

$this->fillBackground();

$this->fillPix();

$this->fillArc();

$this->fillVerifyCode();

$this->outPutImg();

}

//准备画布

private function createRes()

{

return p_w_picpathcreatetruecolor($this->width, $this->height);

}

//创建颜色(深色、亮色)

private function createDarkColor()

{

/*$res = $this->res;

$color1 = mt_rand(0,120);//red

$color2 = mt_rand(0,120);//green

$color3 = mt_rand(0,120);//blue

return p_w_picpathcolorallocate($res,$color1,$color2,$color3);*/

return p_w_picpathcolorallocate($this->res,mt_rand(0,120),mt_rand(0,120),mt_rand(0,120));

}

//创建亮色给字符串用

private function createLightColor()

{

return p_w_picpathcolorallocate($this->res,mt_rand(130,255),mt_rand(130,255),mt_rand(130,255));

}

//填充背景色

private function fillBackground()

{

/*$res = $this->res;

$x = 0;

$y = 0;

$color = $this->createDarkColor();

returnp_w_picpathfill($res,$x,$y,$color);*/

p_w_picpathfill($this->res,0,0,$this->createDarkColor());

}

//添加干扰元素

private function fillPix()

{

$nums = ($this->width * $this->height) / 20;

for ($i=0; $i 

$x = mt_rand(0,$this->width);

$y = mt_rand(0,$this->height);

p_w_picpathsetpixel($this->res, $x, $y, $this->createDarkColor());

}

}

//添加干扰弧线

private function fillArc()

{

for ($i=0; $i 

$cx = mt_rand(0,$this->width);//中心点x坐标

$cy = mt_rand(0,$this->height);//中心点y坐标

$w = mt_rand(10,$this->width/2);//弧线宽度

$h = mt_rand(5,$this->height/2);//弧线高度

$s = mt_rand(0,180);//起点角度

$e = mt_rand(181,360);//终点角度

p_w_picpatharc($this->res,$cx,$cy,$w,$h,$s,$e,$this->createDarkColor());

}

}

//产生验证码字符

private function setVerifyCode()

{

$verify_code = '';

switch ($this->verify_type) {

case 1:

//纯数字的验证码取4个

$str = join('',array_flip(range(0,9),$this->verify_nums));

break;

case 2:

$str = join('',array_rand(array_flip(range('a','z')),$this->verify_nums));

break;

case 3:

$str = 'qwertyuiplkjhgfdsaxcvbnmQWERTYUPLKJHGFDSAXCVBNM23456789';

$str = substr(str_shuffle($str), 0,$this->verify_nums);

break;

default:

die('请输入指定验证码的字符类型
');

}

$verify_code = $str;

return $verify_code;

}

//把字符串弄到画布上 $this->fillVerifyCode = 'abda'

//弱类型语言就比较随意

private function fillVerifyCode()

{

for ($i=0; $i verify_nums; $i++) {

$every_width = $this->width /$this->verify_nums;

/*mt_rand($every_width * 0,$every_width);

mt_rand($every_width * 1,$every_width);

mt_rand($every_width * 2,$every_width);

mt_rand($every_width * 3,$every_width);

$x = mt_rand((($this-width / $this->verify_nums) * $i) + 5,($this-width / $this->verify_nums) * ($i+1) - 5) ;*/

$x = mt_rand($every_width * $i + 10 ,$every_width * ($i + 1) -10);

$y = mt_rand(10,$this->height - 20);

p_w_picpathchar($this->res, 5, $x, $y, $this->verify_code[$i], $this->createLightColor());

}

}

//输出图片

private function outPutImg()

{

header('Content-type:p_w_picpath/'.$this->p_w_picpath_type);

$func = 'p_w_picpath' . $this->p_w_picpath_type;

$func($this->res);

}

public function __get($name)

{

if ($name == 'verify_code') {

return $this->verify_code;

}

}

//销毁资源

public function __destruct()

{

p_w_picpathdestroy($this->res);

}

}

$v = new Verify();

$v->show();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值