PHP应用:算数验证码

调用方法:

$a = new CountCaptcha();
$a->createCaptcha();

代码:

<?php
/**
 * Created by PhpStorm.
 * User: ming
 * Date: 2018/7/24
 * Time: 下午3:29
 */

class CountCaptcha
{
    //背景宽度
    protected $width;
    //背景高度
    protected $height;
    //背景色调
    protected $backTone;
    //算数类型
    protected $countType;
    //验证码大小
    protected $codeSize;
    //验证码色调
    protected $codeTone;
    //干扰点个数
    protected $disturbSum;
    //干扰点色调
    protected $disturbTone;
    //验证码
    protected $code;
    //图片资源
    protected $image;

    //算数结果
    protected $countKey;

    /**
     * Captcha constructor.
     * @param int $width
     * @param int $height
     * @param int $backTone   0:lightcolor, 1:darkcolor
     * @param int $countType   0:rand, 1:'+', 2:'-', 3:'*', 4:'/'
     * @param int $codeSize
     * @param int $codeTone
     * @param int $disturbSum
     * @param int $disturbTone
     */
    public function __construct($width=100, $height=40, $backTone=0, $countType=0, $codeSize=5, $codeTone=1, $disturbSum=150, $disturbTone=1)
    {
        $this->width = $width;
        $this->height = $height;
        $this->backTone = $backTone;
        $this->countType = $countType;
        $this->codeSize = $codeSize;
        $this->codeTone = $codeTone;
        $this->disturbSum = $disturbSum;
        $this->disturbTone = $disturbTone;
        //获取验证码
        $this->code = $this->createCode();
    }

    /**
     * createCode
     * @return string:生成的验证码字符串
     */
    protected function createCode()
    {
        switch ($this->countType) {
            case 0:
                $this->countType = rand(1, 4);
                return $this->createCode();
                break;
            case 1:
                return $this->addCode();
                break;
            case 2:
                return $this->subCode();
                break;
            case 3:
                return $this->mulCode();
                break;
            case 4:
                return $this->divCode();
                break;
            default:
                die('算数类型不支持!');
        }
    }

    public function addCode()
    {
        $oneNum = rand(0, 9);
        $twoNum = rand(0, 9);
        $addCode = $oneNum.'+'.$twoNum.'=?';
        $this->countKey = $oneNum + $twoNum;
        return $addCode;
    }
    public function subCode()
    {
        $oneNum = rand(0, 9);
        $twoNum = rand(0, 9);
        $addCode = $oneNum.'-'.$twoNum.'=?';
        $this->countKey = $oneNum - $twoNum;
        return $addCode;
    }
    public function mulCode()
    {
        $oneNum = rand(0, 9);
        $twoNum = rand(0, 9);
        $addCode = $oneNum.'*'.$twoNum.'=?';
        $this->countKey = $oneNum * $twoNum;
        return $addCode;
    }
    public function divCode()
    {
        $oneNum = rand(0, 9);
        $twoNum = rand(1, 9);
        $addCode = $oneNum.'/'.$twoNum.'=?';
        $this->countKey = $oneNum / $twoNum;
        return $addCode;
    }

    public function __get($name)
    {
        if ($name == 'countKey') {
            return $this->countKey;
        }
    }

    public function __destruct()
    {
        imagedestroy($this->image);
    }

    /**
     * createCaptcha:生成验证码图片
     */
    public function createCaptcha()
    {
        //生成画布
        $this->createImage();
        //填充背景色
        $this->fillColor();
        //画入验证码
        $this->fillCode();
        //添加干扰点
        $this->fillDisturb();
        //展示图片
        $this->showImage();
    }

    protected function createImage()
    {
        $this->image = imagecreatetruecolor($this->width, $this->height);
    }
    protected function fillColor()
    {
        imagefill($this->image, 0, 0, $this->tone($this->backTone));
    }
    protected function tone($rank)
    {
        if ($rank == 0)
            return $this->lightColor();
        if ($rank == 1)
            return $this->darkColor();
    }
    protected function lightColor()
    {
        return imagecolorallocate($this->image, mt_rand(130, 255), mt_rand(130, 255), mt_rand(130, 255));
    }
    protected function darkColor()
    {
        return imagecolorallocate($this->image, mt_rand(0, 120), mt_rand(0, 120), mt_rand(0, 120));
    }
    protected function fillCode()
    {
        for ($i=0; $i<5; $i++) {
            $aveWidth = ceil($this->width / 5);
            $x = mt_rand($aveWidth * $i, $aveWidth * ($i+1)-15);
            $y = mt_rand(0, $this->height - 15);
            imagechar($this->image, 5, $x, $y, $this->code[$i], $this->tone($this->codeTone));
        }
    }
    protected function fillDisturb()
    {
        for ($i=0; $i<$this->disturbSum; $i++) {
            $x = mt_rand(0, $this->width);
            $y = mt_rand(0, $this->height);
            imagesetpixel($this->image, $x, $y, $this->tone($this->disturbTone));
        }
    }
    protected function showImage()
    {
        header('Content-Type:image/png');
        imagepng($this->image);
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值