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。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • class Verify { //宽度 protected $width; //高度 protected $hei...
    斯文小蚂蚁阅读 1,514评论 0 0
  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 14,349评论 0 33
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 11,711评论 0 17
  • 背景 验证码就是把一串随机产品的数字动态生成一幅图片,再加上干扰元素。此时用户可以通过肉眼能识别里面的数字或者字符...
    dy2903阅读 6,490评论 0 7
  • 人生只有一瞬间,而那一瞬间,却让我眺望良久。 一本唐诗,说不尽这韵味。一部宋词,唱不完这柔美。 有一刻,闪现那尘封...
    镜画者阅读 2,187评论 0 1