首页 > 其他分享 >ci添加任意图片验证码方式

ci添加任意图片验证码方式

时间:2023-04-10 14:05:54浏览次数:42  
标签:function ci word img 验证码 height captcha 添加 255


验证码类需要放到用户类库中

只是注意的一点就是记得要清空输出缓存,否则就会造成图片不能显示

D:\Program Files\Apache\htdocs\edm\application\libraries\captcha.php
------------
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class captcha
{
    
    /**
     * 背景图片所在目录
     *
     * @var string  $folder
     */
    var $folder     = 'images/captcha/';//用于存放背景图片的文件夹,

    /**
     * 图片的文件类型
     *
     * @var string  $img_type
     */
    var $img_type   = 'jpeg';

    /*------------------------------------------------------ */
    //-- 存在session中的名称
    /*------------------------------------------------------ */
    var $session_word = 'captcha_word';

    /**
     * 背景图片以及背景颜色
     *用于存放背景图片的文件夹中的图片文件,必须存在否则出错,主要是用于随机出现背景图片
     * 0 => 背景图片的文件名
     * 1 => Red, 2 => Green, 3 => Blue
     * @var array   $themes
     */
    var $themes_jpg = array(
        1 => array('captcha_bg1.jpg', 255, 255, 255),
        2 => array('captcha_bg2.jpg', 0, 0, 0),
        3 => array('captcha_bg3.jpg', 0, 0, 0),
        4 => array('captcha_bg4.jpg', 255, 255, 255),
        5 => array('captcha_bg5.jpg', 255, 255, 255),
    );

    var $themes_gif = array(
        1 => array('captcha_bg1.gif', 255, 255, 255),
        2 => array('captcha_bg2.gif', 0, 0, 0),
        3 => array('captcha_bg3.gif', 0, 0, 0),
        4 => array('captcha_bg4.gif', 255, 255, 255),
        5 => array('captcha_bg5.gif', 255, 255, 255),
    );

    /**
     * 图片的宽度
     *
     * @var integer $width
     */
    var $width      = 53;

    /**
     * 图片的高度
     *
     * @var integer $height
     */
    var $height     = 20;

    /**
     * 构造函数
     *
     * @access  public
     * @param   string  $folder     背景图片所在目录
     * @param   integer $width      图片宽度
     * @param   integer $height     图片高度
     * @return  bool
     */
    function captcha($folder = '', $width = 53, $height = 20)
    {
        session_start();
        $this->folder = str_replace('\\', '/', dirname(dirname(dirname(__FILE__)))). "/". $this->folder;
        
        if (!empty($folder))
        {
            $this->folder = $folder;
        }

        $this->width    = $width;
        $this->height   = $height;

        /* 检查是否支持 GD */
        if (PHP_VERSION >= '4.3')
        {

            return (function_exists('imagecreatetruecolor') || function_exists('imagecreate'));
        }
        else
        {

            return (((imagetypes() & IMG_GIF) > 0) || ((imagetypes() & IMG_JPG)) > 0 );
        }
    }

    /**
     * 构造函数
     *
     * @access  public
     * @param
     *
     * @return void
     */
    function __construct($folder = '', $width = 53, $height = 20)
    {
        $this->captcha($folder, $width, $height);
    }


    /**
     * 检查给出的验证码是否和session中的一致
     *
     * @access  public
     * @param   string  $word   验证码
     * @return  bool
     */
    function check_word($word)
    {
        $recorded = isset($_SESSION[$this->session_word]) ? base64_decode($_SESSION[$this->session_word]) : '';
        $given    = $this->encrypts_word(strtoupper($word));

        return (preg_match("/$given/", $recorded));
    }

    /**
     * 生成图片并输出到浏览器
     *
     * @access  public
     * @param   string  $word   验证码
     * @return  mix
     */
    function generate_image($word = false)
    {
        if (!$word)
        {
            $word = $this->generate_word();
        }

        /* 记录验证码到session */
        $this->record_word($word);

        /* 验证码长度 */
        $letters = strlen($word);

        /* 选择一个随机的方案 */
        mt_srand((double) microtime() * 1000000);

        if (function_exists('imagecreatefromjpeg') && ((imagetypes() & IMG_JPG) > 0))
        {
            $theme  = $this->themes_jpg[mt_rand(1, count($this->themes_jpg))];
        }
        else
        {
            $theme  = $this->themes_gif[mt_rand(1, count($this->themes_gif))];
        }

        if (!file_exists($this->folder . $theme[0]))
        {
            return false;
        }
        else
        {
            $img_bg    = (function_exists('imagecreatefromjpeg') && ((imagetypes() & IMG_JPG) > 0)) ?
                            imagecreatefromjpeg($this->folder . $theme[0]) : imagecreatefromgif($this->folder . $theme[0]);
            $bg_width  = imagesx($img_bg);
            $bg_height = imagesy($img_bg);

            $img_org   = ((function_exists('imagecreatetruecolor')) && PHP_VERSION >= '4.3') ?
                          imagecreatetruecolor($this->width, $this->height) : imagecreate($this->width, $this->height);

            /* 将背景图象复制原始图象并调整大小 */
            if (function_exists('imagecopyresampled') && PHP_VERSION >= '4.3') // GD 2.x
            {
                imagecopyresampled($img_org, $img_bg, 0, 0, 0, 0, $this->width, $this->height, $bg_width, $bg_height);
            }
            else // GD 1.x
            {
                imagecopyresized($img_org, $img_bg, 0, 0, 0, 0, $this->width, $this->height, $bg_width, $bg_height);
            }
            imagedestroy($img_bg);

            $clr = imagecolorallocate($img_org, $theme[1], $theme[2], $theme[3]);

            /* 绘制边框 */
            //imagerectangle($img_org, 0, 0, $this->width - 1, $this->height - 1, $clr);

            /* 获得验证码的高度和宽度 */
            $x = ($this->width - (imagefontwidth(5) * $letters)) / 2;
            $y = ($this->height - imagefontheight(5)) / 2;
            imagestring($img_org, 5, $x, $y, $word, $clr);
            @ob_end_clean();//必须清除,防止配合CI使用时,出现非图片内容.
            header('Expires: Thu, 01 Jan 1970 00:00:00 GMT');

            // HTTP/1.1
            header('Cache-Control: private, no-store, no-cache, must-revalidate');
            header('Cache-Control: post-check=0, pre-check=0, max-age=0', false);

            // HTTP/1.0
            header('Pragma: no-cache');
            if ($this->img_type == 'jpeg' && function_exists('imagecreatefromjpeg'))
            {
                header('Content-type: image/jpeg');
                imageinterlace($img_org, 1);
                imagejpeg($img_org, false, 95);
            }
            else
            {
                header('Content-type: image/png');
                imagepng($img_org);
            }
            imagedestroy($img_org);
            return true;
        }
    }

    /*------------------------------------------------------ */
    //-- PRIVATE METHODs
    /*------------------------------------------------------ */

    /**
     * 对需要记录的串进行加密
     *
     * @access  private
     * @param   string  $word   原始字符串
     * @return  string
     */
    function encrypts_word($word)
    {
        return substr(md5($word), 1, 10);
    }

    /**
     * 将验证码保存到session
     *
     * @access  private
     * @param   string  $word   原始字符串
     * @return  void
     */
    function record_word($word)
    {
        $_SESSION[$this->session_word] = base64_encode($this->encrypts_word($word));
    }

    /**
     * 生成随机的验证码
     *
     * @access  private
     * @param   integer $length     验证码长度
     * @return  string
     */
    function generate_word($length = 5)
    {
        $chars = '23456789ABCDEFGHJKLMNPQRSTUVWXYZ';

        for ($i = 0, $count = strlen($chars); $i < $count; $i++)
        {
            $arr[$i] = $chars[$i];
        }

        mt_srand((double) microtime() * 1000000);
        shuffle($arr);

        return substr(implode('', $arr), 5, $length);
    }
}--------------

随便在一个控制器中放入调用验证码的方法

我是放在home/init中

-----
    
    /**
     *显示认证图片
     */
    public function getCaptcha()
    {
        $this->load->library('captcha');
        $this->captcha->generate_image();
        exit;
    }------
-----
    
    /**
     *显示认证图片
     */
    public function getCaptcha()
    {
        $this->load->library('captcha');
        $this->captcha->generate_image();
        exit;
    }------

验证码调用

-----

<img align="验证码" οnclick="change_verify(this)" title="点击更换图片/输入时不区分大小写"
        src="<?php echo site_url("home/init/getCaptcha");?>">

-----

显示

ci添加任意图片验证码方式_背景图片

----

使用回调form验证方式验证输入码

---
    
    /**
     *回调认证码验证
     */
    function verifyCheck($str){
        
        $this->load->library('captcha');

        if ($this->captcha->check_word($str)){
            return true;
        }else{
            $this->form_validation->set_message('verifyCheck', '验证码有误');
            return false;
        }
    }------

设置回调规则

$this->form_validation->set_rules('verify', '认证码', 'callback_verifyCheck');

标签:function,ci,word,img,验证码,height,captcha,添加,255
From: https://blog.51cto.com/u_252283/6180642

相关文章

  • ubuntu22.04 添加开机启动脚本
    在目录/etc/init.d/目录新建脚本sudovim/etc/init.d/startup.sh#!/bin/bash#Onlyfortesttouch/root/1.txt添加执行权限sudochmod+x/etc/init.d/startup.sh添加启动脚本sudoupdate-rc.dstartup.shdefaults90查看服务列表sudoservice--status-all测试是......
  • python+playwright 学习-51 登录-验证码识别
    简单的登录验证码,数字和英文组合的,可以轻松识别登录验证码如下图登录验证码验证码是一个图片链接,每次打开页面它会自动刷新![[Pastedimage20230410084603.png]]解决思路是先获取到验证码图片,获取验证码图片的方式,可以直接定位到img元素,对元素截图即可#保存验证码page......
  • ubuntu安装python环境scikit-learn低版本
    Ubuntu默认使用的是python3.8,要安装插件需要先安装几个依赖包      安装uwsgi需要安装gccpython3.8-dev python-dev      安装scikit-learn旧版本需要安装python3-sklearnpython3-sklearn-lib这两个包,python3.8支持最早的版本是scikit-learn==0.24.2    ......
  • SCI 论文投稿的一些注意事项
    ......
  • jenkins+gitlab+harbor部署CICD
    jenkins相关操作docker部署jenkinsdockerrun--namejenkins-d-p8080:8080-p50000:50000--privileged=true--restart=always-v/jenkins/home:/var/jenkins_home-v/var/run/docker.sock:/var/run/docker.sock-v/usr/bin/docker:/usr/bin/dockerjenkins/jenkins:2......
  • 在.net项目中添加Husky.Net提交验证
    参考:C#项目添加husky-jesn-博客园(cnblogs.com)官方文档:GettingStarted|Husky.Net(alirezanet.github.io)什么是Husky.net?Husky是一款githook(钩子)工具,让我们在gitcommit之前可以做一些操作,例如,代码格式化,重生生成,提交规范检查等,而Husky.net便是适用于.net平台......
  • Win10虚拟网卡怎么安装|Win10如何添加虚拟网卡
    http://xitong86.com/article/win11jc/2404.html 虚拟网卡,又称虚拟网络适配器,即用软件模拟网络环境,模拟网络适配器,这篇文章将以Win10系统为例,给大家带来的虚拟网卡安装方法。1、首先,按键盘上的【Win+X】组合键,或右键点击任务栏左下角的【Windows开始徽标】; 2、在打开......
  • 源码共读 | 为 vite 项目自动添加 eslint 和 prettier
    前言Vite是一个用于现代JavaScript应用程序的快速、轻量级的构建工具,其设计目的是易于使用和适用于大型项目。Vite-pretty-lint是一个插件,可以在基于Vite的项目中安装和配置,以便在编写代码时能够自动对代码进行格式化和检查代码。这可以帮助开发人员在开发过程的早期捕获格......
  • 为博客添加看板娘
    博客原文博客优化内容基于hexo-helper-live2d插件给自己的博客添加一个看板娘,最后的成果图如下添加看板娘首先安装插件npminstallhexo-helper-live2d--save如果你的npm出现依赖问题vulnerabilities错误,那么可以通过降低npm版本,但是如果你不想降低又无法解决依赖问......
  • CSCI561 算法解析
    CSCI561CSCI561FirstOrderLogicResolutioGuidelinesThisisaprogrammingassignment.Youwillbeprovidedwithsampleinputsandoutputs(seebelow).Pleaseunderstandthatthegoalofthesamplesistocheckthatyoucancorrectlyparsetheproblemdefi......