Yii2 RESTful API 实现框架自带的 captcha 图形验证码

创建验证码生成类

CodeImgGenerate.php

<?php
namespace common\helpers;

use yii\captcha\CaptchaAction;

class CodeImgGenerate extends CaptchaAction
{
    private $verifycode;

    public function __construct()
    {
        $this->init();
        // 更多api请访问yii\captcha\CaptchaAction类文档
        // 这里可以初始化默认样式
        $this->maxLength = 4;            // 最大显示个数
        $this->minLength = 4;            // 最少显示个数
        $this->backColor = 0x000000;     // 背景颜色
        $this->foreColor = 0x00ff00;     // 字体颜色
        $this->width = 80;               // 宽度
        $this->height = 45;              // 高度
    }

    /**
     * [返回图片二进制]
     * @return [type] [description]
     */
    public function inline()
    {
        return $this->renderImage($this->getPhrase());
    }

    /**
     * [返回图片验证码]
     * @return [type] [description]
     */
    public function getPhrase()
    {
        if($this->verifycode){
            return $this->verifycode;
        }else{
            return $this->verifycode = $this->generateVerifyCode();
        }
    }
}
?>

控制器调用

引用验证码生成类

use common\helpers\CodeImgGenerate;

验证码随机数根据业务需求自行存储验证,由于Api取消了SESSION 所以就存在框架自带的 Cache 中。

    /**
     * [ 验证码 ]
     * @return [type] [description]
     */
    public function actionCaptcha()
    {
        $CodeImgGenerate = new CodeImgGenerate();
        $CodeImgGenerate->fixedVerifyCode = YII_ENV_TEST ? 'testme' : null;
        // 更多api请访问yii\captcha\CaptchaAction类文档

        $CodeImgGenerate->maxLength = 4;                         // 最大显示个数
        $CodeImgGenerate->minLength = 4;                         // 最少显示个数
        $CodeImgGenerate->padding  = 0;                          // 间距
        $CodeImgGenerate->height = 58;                           // 高度
        $CodeImgGenerate->width  = 156;                          // 宽度
        $CodeImgGenerate->backColor = Util::captcha_color(1);    // 背景颜色
        $CodeImgGenerate->foreColor = Util::captcha_color(2);    // 字体颜色
        $CodeImgGenerate->offset = 4;                            // 字符之间的偏移量
        $codeInfo = $CodeImgGenerate->inline();                  // 验证码二进制流
        $code = $CodeImgGenerate->getPhrase();                   // 验证码随机数
        Cache::saveCaptchaCache(strtolower($code));              // 验证码加存储
        header("Content-type: image/png");                       // 输出图片
        exit($codeInfo);
    }

最后生成的验证码

返回随机颜色

这个方法用于生成随机的颜色,每次刷新都会展示不同的颜色

    /**
     * [ 返回随机颜色 ]
     * @param  integer $type [description]
     * @return [type]        [description]
     */
    public static function captcha_color($type=1) 
    {
        if(!in_array($type, array(1,2))) $type=1;
        if($type==1) {
            // 背景颜色
            $bg_color_arr=array('15595519','16316664');
            $bg=$bg_color_arr[array_rand($bg_color_arr)];
            return (int) '0x'.$bg;
        } else {
            // 字体颜色
            $text_color_arr=array('12326852','2185586');
            $tc=$text_color_arr[array_rand($text_color_arr)];
            return (int) '0x'.$tc;
        }
    }

 

以上就是本人根据网上搜到的一些信息自己做的 Yii2 RESTful API 实现图形验证码的方法,也请各位大神多多指教,希望对大家有所帮助。

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值