调用方法:
$captcha = new Captcha();
$captcha->createCaptcha();
代码:
<?php
/**
* Created by PhpStorm.
* User: ming
* Date: 2018/7/15
* Time: 下午4:34
*/
class Captcha
{
//背景宽度
protected $width;
//背景高度
protected $height;
//背景色调
protected $backTone;
//验证码类型
protected $codeType;
//验证码个数
protected $charSum;
//验证码大小
protected $codeSize;
//验证码色调
protected $codeTone;
//干扰点个数
protected $disturbSum;
//干扰点色调
protected $disturbTone;
//验证码
protected $code;
//图片资源
protected $image;
/**
* Captcha constructor.
* @param int $width
* @param int $height
* @param int $backTone 0:lightcolor, 1:darkcolor
* @param int $codeType 0:number, 1:letter, 2:number and letter
* @param int $charSum
* @param int $codeSize
* @param int $codeTone
* @param int $disturbSum
* @param int $disturbTone
*/
public function __construct($width=100, $height=40, $backTone=0, $codeType=2, $charSum=4, $codeSize=5, $codeTone=1, $disturbSum=150, $disturbTone=1)
{
$this->width = $width;
$this->height = $height;
$this->backTone = $backTone;
$this->codeType = $codeType;
$this->charSum = $charSum;
$this->codeSize = $codeSize;
$this->codeTone = $codeTone;
$this->disturbSum = $disturbSum;
$this->disturbTone = $disturbTone;
//获取验证码
$this->code = $this->createCode();
}
/**
* createCode
* @return string:生成的验证码字符串
*/
protected function createCode()
{
switch ($this->codeType) {
case 0:
return $this->numberCode();
break;
case 1:
return $this->letterCode();
break;
case 2:
return $this->numLetCode();
break;
default:
die('验证码格式不支持!');
}
}
public function __get($name)
{
if ($name == 'code') {
return $this->code;
}
}
public function __destruct()
{
imagedestroy($this->image);
}
/**
* typeStr 返回对用类型的字符串
* @param $type 0:number, 1:lowercase, 2:toupper
* @return string
*/
protected function typeStr($type)
{
if ($type == 0)
return join('', range(0, 9));
if ($type == 1)
return join('', range('a', 'z'));
if ($type == 2)
return join('', range('A', 'Z'));
}
/**
* randStr 返回打乱后的字符串
* @param $str
* @return string
*/
protected function randStr($str)
{
return substr(str_shuffle($str), 1, $this->charSum);
}
/**
* numberCode
* @return string:数字验证码
*/
protected function numberCode()
{
$str = $this->typeStr('0');
return $this->randStr($str);
}
/**
* letterCode
* @return string:字母验证码
*/
protected function letterCode()
{
$str = $this->typeStr('1').$this->typeStr('2');
return $this->randStr($str);
}
/**
* @return string:数字字母混合验证码
*/
protected function numLetCode()
{
$str = $this->typeStr('0').$this->typeStr('1').$this->typeStr('2');
return $this->randStr($str);
}
/**
* 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<$this->charSum; $i++) {
$aveWidth = ceil($this->width / $this->charSum);
$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);
}
}