美文网首页
php验证码类

php验证码类

作者: php红薯 | 来源:发表于2017-05-31 11:07 被阅读0次

    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。

    相关文章

      网友评论

          本文标题:php验证码类

          本文链接:https://www.haomeiwen.com/subject/tfgdfxtx.html