Laravel框架下生成验证码的类 供大家参考

实例为大家分享了Laravel生成验证码的类,供大家参考,具体内容如下:

<?php
&nbsp;&nbsp;
namespace App\Tool\Validate;
&nbsp;&nbsp;
//验证码类
class ValidateCode {
&nbsp;&nbsp;private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';//随机因子
&nbsp;&nbsp;private $code;//验证码
&nbsp;&nbsp;private $codelen = 4;//验证码长度
&nbsp;&nbsp;private $width = 130;//宽度
&nbsp;&nbsp;private $height = 50;//高度
&nbsp;&nbsp;private $img;//图形资源句柄
&nbsp;&nbsp;private $font;//指定的字体
&nbsp;&nbsp;private $fontsize = 20;//指定字体大小
&nbsp;&nbsp;private $fontcolor;//指定字体颜色
&nbsp;&nbsp;
&nbsp;&nbsp;//构造方法初始化
&nbsp;&nbsp;public function __construct()
&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;$this->font = public_path() . '/fonts/Elephant.ttf';//注意字体路径要写对,否则显示不了图片
&nbsp;&nbsp;&nbsp;&nbsp;$this->createCode();
&nbsp;&nbsp;}
&nbsp;&nbsp;//生成随机码
&nbsp;&nbsp;private function createCode()
&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;$_len = strlen($this->charset) - 1;
&nbsp;&nbsp;&nbsp;&nbsp;for ($i = 0;$i < $this->codelen;++$i) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->code .= $this->charset[mt_rand(0, $_len)];
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;}
&nbsp;&nbsp;//生成背景
&nbsp;&nbsp;private function createBg()
&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;$this->img = imagecreatetruecolor($this->width, $this->height);
&nbsp;&nbsp;&nbsp;&nbsp;$color = imagecolorallocate($this->img, mt_rand(157, 255), mt_rand(157, 255), mt_rand(157, 255));
&nbsp;&nbsp;&nbsp;&nbsp;imagefilledrectangle($this->img, 0, $this->height, $this->width, 0, $color);
&nbsp;&nbsp;}
&nbsp;&nbsp;//生成文字
&nbsp;&nbsp;private function createFont()
&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;$_x = $this->width / $this->codelen;
&nbsp;&nbsp;&nbsp;&nbsp;for ($i = 0;$i < $this->codelen;++$i) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->fontcolor = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;imagettftext($this->img, $this->fontsize, mt_rand(-30, 30), $_x * $i + mt_rand(1, 5), $this->height / 1.4, $this->fontcolor, $this->font, $this->code[$i]);
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;}
&nbsp;&nbsp;//生成线条、雪花
&nbsp;&nbsp;private function createLine()
&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;//线条
&nbsp;&nbsp;&nbsp;&nbsp;for ($i = 0;$i < 6;++$i) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$color = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;imageline($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $color);
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;//雪花
&nbsp;&nbsp;&nbsp;&nbsp;for ($i = 0;$i < 100;++$i) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$color = imagecolorallocate($this->img, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;imagestring($this->img, mt_rand(1, 5), mt_rand(0, $this->width), mt_rand(0, $this->height), '*', $color);
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;}
&nbsp;&nbsp;//输出
&nbsp;&nbsp;private function outPut()
&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;header('Content-type:image/png');
&nbsp;&nbsp;&nbsp;&nbsp;imagepng($this->img);
&nbsp;&nbsp;&nbsp;&nbsp;imagedestroy($this->img);
&nbsp;&nbsp;}
&nbsp;&nbsp;//对外生成
&nbsp;&nbsp;public function doimg()
&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;$this->createBg();
&nbsp;&nbsp;&nbsp;&nbsp;$this->createLine();
&nbsp;&nbsp;&nbsp;&nbsp;$this->createFont();
&nbsp;&nbsp;&nbsp;&nbsp;$this->outPut();
&nbsp;&nbsp;}
&nbsp;&nbsp;//获取验证码
&nbsp;&nbsp;public function getCode()
&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;return strtolower($this->code);
&nbsp;&nbsp;}
}
© 版权声明
THE END

喜欢就支持一下吧

点赞0 分享