调用方法:
$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);
}
}