thinkphp5.0如何使用框架以及自定义封装验证码

今天因为这个问题郁闷了很久,好在最后的最后谜团终于被我解开了。

我的开发环境是wamp2.4,php版本是5.4,tp框架采用的是5.0,但这都不是重点,重点是我在安装think-captcha时一直出错,上网查了很多,排除了不少原因,可依然无效,悲催的我一直以为是开发环境的问题,结果果断卸载了wamp,安装phpStudy,第一次使用phpStudy,不得不说phpStudy真的太好用啦!无需手动添加配置,一步到位啊。
安装好了phpStudy后,我又安装了最新的composer,tp5.1,再然后composer require topthink/think-captcha果断成功了,走出了第一步,验证码的问题随之而解,可新的问题又来了,原来的项目放到tp5.1上后,出现了许多新的问题,那就是框架版本改动后导致我新多地方的功能实现不了了,这样一来,需要改动的地方又多了,无奈之下,我只有自己手动写一个验证码了。代码如下:

class Captcha {
    private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';//随机因子
    private $code;//验证码
    private $codelen = 4;//验证码长度
    private $width = 130;//宽度
    private $height = 50;//高度
    private $img;//图形资源句柄
    private $font;//指定的字体
    private $fontsize = 25;//指定字体大小
    private $fontcolor;//指定字体颜色
    //构造方法初始化
    public function __construct() {
        $this->font = ADMIN_PATH.'assets/ttfs/1.ttf';//注意字体路径要写对,否则显示不了图片
    }
    //生成随机码
    private function createCode() {
        $_len = strlen($this->charset)-1;
        for ($i=0;$i<$this->codelen;$i++) {
            $this->code .= $this->charset[mt_rand(0,$_len)];
        }
    }
    //生成背景
    private function createBg() {
        $this->img = imagecreatetruecolor($this->width, $this->height);
        $color = imagecolorallocate($this->img, mt_rand(157,255), mt_rand(157,255), mt_rand(157,255));
        imagefilledrectangle($this->img,0,$this->height,$this->width,0,$color);
    }
    //生成文字
    private function createFont() {
        $_x = $this->width / $this->codelen;
        for ($i=0;$i<$this->codelen;$i++) {
            $this->fontcolor = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
            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]);
        }
    }
    //生成线条、雪花
    private function createLine() {
        //线条
        for ($i=0;$i<6;$i++) {
            $color = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
            imageline($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$color);
        }
        //雪花
        for ($i=0;$i<100;$i++) {
            $color = imagecolorallocate($this->img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
            imagestring($this->img,mt_rand(1,5),mt_rand(0,$this->width),mt_rand(0,$this->height),'*',$color);
        }
    }
    //输出
    private function outPut() {
        ob_start();
        imagepng($this->img);
        $content = ob_get_clean();
        imagedestroy($this->img);
        return $content;
    }
    //对外生成
    public function doimg() {
        $this->createBg();
        $this->createCode();
        $this->createLine();
        $this->createFont();
        Session::set('verify_code',strtolower($this->code));
        return $this->outPut();
    }
}

其实生成验证码的过程很简单,创建画布----添加背景颜色----限定宽高----干扰项----随机验证码—输出;
需要注意的是在框架里面不能直接return outPut()因为这样得到的是图片流,是无法正常显示的。

public function captcha()
    {
        $captcha = new Captcha();
        $content = $captcha->doimg();
        return response($content, 200, ['Content-Length' => strlen($content)])->contentType('image/png');
    }
`验证码终于成功嵌到框架里面去了。

解决了这个问题,我还是有点不甘心,明明tp框架自带的验证码,却要这样来写,总感觉哪里怪怪的。
抱着试一试的心态我把原tp5.0框架根目录下面的composer.json文件删掉替换成tp5.1新生成的composer.json,再次composer require topthink/think-captcha 终于成功了,问题的本身就出在框架上,在网上拷贝的小伙伴注意了,千万不要随便down别人的资源,否则后果真的很严重哦

久违的小黑框,em~
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值