easyswoole 验证码使用
温馨提示:
本文最后更新于 2019年07月10日,已超过 1,918 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我。
easyswoole验证码组件,安装:
easyswoole/verifycode
调用方法:
$config = new Conf();
$code = new \EasySwoole\VerifyCode\VerifyCode($config);
$code->DrawCode();//生成验证码,返回一个Result对象
在easyswoole控制器中使用:
use EasySwoole\Utility\Random;
use EasySwoole\VerifyCode\Conf;
use EasySwoole\VerifyCode\VerifyCode as VerifyCodeModel;
function verifyCode()
{
$ttl = 120;
$conf = new Conf();
$VCode = new VerifyCodeModel($conf);
// 随机生成验证码
$random = Random::character(4, '1234567890abcdefghijklmnopqrstuvwxyz');
$code = $VCode->DrawCode($random);
$result = [
'verifyCode' => $code->getImageBase64(),
'verifyCodeTime' => time(),
];
$this->writeJson(200, $result, 'success');
}
注意,需要自己保存验证码,这样用户才能验证成功
可通过session组件,或者使用另一种验证方式进行验证:关于验证码存储机制的转变
关于详细的配置:
<?php// +----------------------------------------------------------------------// | easySwoole [ use swoole easily just like echo "hello world" ]// +----------------------------------------------------------------------// | WebSite: https://www.easyswoole.com// +----------------------------------------------------------------------// | Welcome Join QQGroup 633921431// +----------------------------------------------------------------------namespace EasySwoole\VerifyCode;use EasySwoole\Spl\SplBean;/**
* 验证码配置文件
* Class VerifyCodeConf
* @author : evalor <master@evalor.cn>
* @package Vendor\VerifyCode
*/class Conf extends SplBean{
public $charset = '1234567890AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'; // 字母表
public $useCurve = false; // 混淆曲线
public $useNoise = false; // 随机噪点
public $useFont = null; // 指定字体
public $fontColor = null; // 字体颜色
public $backColor = null; // 背景颜色
public $imageL = null; // 图片宽度
public $imageH = null; // 图片高度
public $fonts = []; // 额外字体
public $fontSize = 25; // 字体大小
public $length = 4; // 生成位数
public $mime = MIME::PNG; // 设置类型
public $temp = '/tmp'; // 设置缓存目录
public function setTemp($temp){
if (!is_dir($temp)) mkdir($temp,0755) && chmod($temp,0755);
$this->temp = $temp;
}
/**
* 设置图片格式
* @param $MimeType
* @author : evalor <master@evalor.cn>
* @return Conf
*/
public function setMimeType($MimeType)
{
$allowMime = [ MIME::PNG, MIME::GIF, MIME::JPG ];
if (in_array($MimeType, $allowMime)) $this->mime = $MimeType;
return $this;
}
/**
* 设置字符集
* @param string $charset
* @return Conf
*/
public function setCharset($charset)
{
is_string($charset) && $this->charset = $charset;
return $this;
}
/**
* 开启混淆曲线
* @param bool $useCurve
* @return Conf
*/
public function setUseCurve($useCurve = true)
{
is_bool($useCurve) && $this->useCurve = $useCurve;
return $this;
}
/**
* 开启噪点生成
* @param bool $useNoise
* @return Conf
*/
public function setUseNoise($useNoise = true)
{
is_bool($useNoise) && $this->useNoise = $useNoise;
return $this;
}
/**
* 使用自定义字体
* @param string $useFont
* @return Conf
*/
public function setUseFont($useFont)
{
is_string($useFont) && $this->useFont = $useFont;
return $this;
}
/**
* 设置文字颜色
* @param array|string $fontColor
* @return Conf
*/
public function setFontColor($fontColor)
{
if (is_string($fontColor)) $this->fontColor = $this->HEXToRGB($fontColor);
if (is_array($fontColor)) $this->fontColor = $fontColor;
return $this;
}
/**
* 设置背景颜色
* @param null $backColor
* @return Conf
*/
public function setBackColor($backColor)
{
if (is_string($backColor)) $this->backColor = $this->HEXToRGB($backColor);
if (is_array($backColor)) $this->backColor = $backColor;
return $this;
}
/**
* 设置图片宽度
* @param int|string $imageL
* @return Conf
*/
public function setImageWidth($imageL)
{
$this->imageL = intval($imageL);
return $this;
}
/**
* 设置图片高度
* @param null $imageH
* @return Conf
*/
public function setImageHeight($imageH)
{
$this->imageH = intval($imageH);
return $this;
}
/**
* 设置字体集
* @param array|string $fonts
* @return Conf
*/
public function setFonts($fonts)
{
if (is_string($fonts)) array_push($this->fonts, $fonts);
if (is_array($fonts) && !empty($fonts)) {
if (empty($this->fonts)) {
$this->fonts = $fonts;
} else {
array_merge($this->fonts, $fonts);
}
}
return $this;
}
/**
* 设置字体尺寸
* @param int $fontSize
* @return Conf
*/
public function setFontSize($fontSize)
{
$this->fontSize = intval($fontSize);
return $this;
}
/**
* 设置验证码长度
* @param int $length
* @return Conf
*/
public function setLength($length)
{
$this->length = intval($length);
return $this;
}
/**
* 获取配置值
* @param $name
* @author : evalor <master@evalor.cn>
* @return mixed
*/
public function __get($name)
{
return $this->$name;
}
/**
* 十六进制转RGB
* @param $hexColor
* @author : evalor <master@evalor.cn>
* @return array
*/
function HEXToRGB($hexColor)
{
$color = str_replace('#', '', $hexColor);
if (strlen($color) > 3) {
$rgb = array(
hexdec(substr($color, 0, 2)),
hexdec(substr($color, 2, 2)),
hexdec(substr($color, 4, 2))
);
} else {
$color = $hexColor;
$r = substr($color, 0, 1) . substr($color, 0, 1);
$g = substr($color, 1, 1) . substr($color, 1, 1);
$b = substr($color, 2, 1) . substr($color, 2, 1);
$rgb = array(
hexdec($r),
hexdec($g),
hexdec($b)
);
}
return $rgb;
}}
关于result输出结果
/**
* 获取验证码图片
* @author : evalor <master@evalor.cn>
* @return mixed
*/
function getImageByte()
{
return $this->CaptchaByte;
}
/**
* 返回图片Base64字符串
* @author : evalor <master@evalor.cn>
* @return string
*/
function getImageBase64()
{
$base64Data = base64_encode($this->CaptchaByte);
$Mime = $this->CaptchaMime;
return "data:{$Mime};base64,{$base64Data}";
}
/**
* 获取验证码内容
* @author : evalor <master@evalor.cn>
* @return mixed
*/
function getImageCode()
{
return $this->CaptchaCode;
}
/**
* 获取Mime信息
* @author : evalor <master@evalor.cn>
*/
function getImageMime()
{
return $this->CaptchaMime;
}
/**
* 获取验证码文件路径
* @author: eValor < master@evalor.cn >
*/
function getImageFile()
{
return $this->CaptchaFile;
}
本文转载自 www.easyswoole.com 官方文档
正文到此结束