前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Yii2 RESTful API 实现框架自带的 captcha 图形验证码

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

原创
作者头像
熬夜的花斑狗
发布2022-01-10 10:26:24
8400
发布2022-01-10 10:26:24
举报
文章被收录于专栏:开发+运维+架构

创建验证码生成类

CodeImgGenerate.php

代码语言:javascript
复制
<?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 中。

代码语言:javascript
复制
    /**
     * [ 验证码 ]
     * @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);
    }

最后生成的验证码

返回随机颜色

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

代码语言:javascript
复制
    /**
     * [ 返回随机颜色 ]
     * @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 实现图形验证码的方法,也请各位大神多多指教,希望对大家有所帮助。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 创建验证码生成类
  • 控制器调用
  • 最后生成的验证码
  • 返回随机颜色
相关产品与服务
验证码
腾讯云新一代行为验证码(Captcha),基于十道安全栅栏, 为网页、App、小程序开发者打造立体、全面的人机验证。最大程度保护注册登录、活动秒杀、点赞发帖、数据保护等各大场景下业务安全的同时,提供更精细化的用户体验。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档