php验证码 类,php验证码类

class Verify

{

//宽度

protected $width;

//高度

protected $height;

//类型

protected $type;

//长度

protected $length = 4;

//验证码

protected $code;

//图片资源

protected $image;

public static function yzm($width=100,$height=40,$length=4,$type=0)

{

$v = new Verify($width,$height,$length,$type);

$v->output();

return $v->getLastCode();

}

public function __get($propertyName)

{

if ($propertyName == 'code') {

return $this->code;

}

return false;

}

public function getLastCode()

{

return $this->code;

}

public function __construct($width=200,$height=50,$length=4,$type=0)

{

$this->width = $width;

$this->height = $height;

if ($length >= 3 || $length <= 10) {

$this->length = $length;

}

$this->type = $type;

}

public function output()

{

$this->createImage();

$this->setVerifyCode();

$this->setDisturb();

$this->sendImage();

}

//创建画布并设置浅色背景

protected function createImage()

{

$this->image = imagecreatetruecolor($this->width, $this->height);

$lightColor = $this->getColor(true);

imagefill($this->image, 0, 0, $lightColor);

}

protected function setVerifyCode()

{

//产生验证码字符串

$this->code = $this->randString();

//将验证码画到画布上

$fontSize = $this->height / 2;

$perWidth = $this->width / $this->length;

$delta = ($perWidth - $fontSize) / 2;

$offsetY = ($this->height + $fontSize) / 2;

for ($i=0; $i < $this->length; $i++) {

//提取一个字符

$ch = mb_substr($this->code, $i, 1);

$color = $this->getColor();

$angle = mt_rand(-30,30);

$offsetX = $i * $perWidth + $delta;

//画到画布上

imagettftext($this->image, $fontSize, $angle, $offsetX, $offsetY, $color, '../../../../public/fonts/lxkmht.ttf', $ch);

}

}

protected function setDisturb()

{

$total = $this->width * $this->height / 50;

for ($i=0; $i < $total; $i++) {

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

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

$color = $this->getColor();

imagesetpixel($this->image, $x, $y, $color);

}

}

protected function sendImage()

{

header('Content-Type:image/png');

imagepng($this->image);

imagedestroy($this->image);

}

protected function getColor($isLight=false)

{

$start = (int)$isLight * 128;

$end = $start + 127;

$red = mt_rand($start,$end);

$green = mt_rand($start,$end);

$blue = mt_rand($start,$end);

return imagecolorallocate($this->image, $red, $green, $blue);

}

protected function randString()

{

switch ($this->type) {

case 0://纯数字

$str = $this->randNumber();

break;

case 1://纯字母

$str = $this->randAlpha();

break;

case 2://数字字母混合

$str = $this->randMixed();

break;

case 3://中文

$str = $this->randChinese();

break;

default://未知

$str = $this->randUnknow();

break;

}

return $str;

}

protected function randNumber()

{

$str = '1234567890';

return substr(str_shuffle($str), 0, $this->length);

}

protected function randAlpha()

{

$arr = range('a', 'z');

$str = join('', $arr);

$str .= strtoupper($str);

return substr(str_shuffle($str), 0, $this->length);

}

protected function randMixed()

{

$str = '';

for ($i=0; $i < $this->length; $i++) {

$type = mt_rand(0, 2);

switch ($type) {

case 0://数字

$str .= chr(mt_rand(ord('0'), ord('9')));

break;

case 1://小写字母

$str .= chr(mt_rand(ord('a'), ord('z')));

break;

case 2://大写字母

$str .= chr(mt_rand(ord('A'), ord('Z')));

break;

}

}

return $str;

}

protected function randChinese()

{

$str = '';

for ($i=0; $i < $this->length; $i++) {

$ch1 = mt_rand(176,214);

$ch2 = mt_rand(161,254);

$str .= chr($ch1) . chr($ch2);

}

return iconv('gbk', 'utf-8', $str);

}

protected function randUnknow()

{

$ch = rand(0,9);

$arr = array_fill(0, $this->length, $ch);

return join('', $arr);

}

}

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。互联网+时代,时刻要保持学习,携手千锋PHP,Dream It Possible。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值