CI框架中,扩展验证码类。

使用CI框架的朋友,应该都知道CI框架的的验证码辅助函数,不太好用。它需要写入到数据库中,然后再进行比对。

大家在实际项目中,好像不会这样去使用,因为会对数据库造成一定的压力。

所以,我们还是利用session来临时存储验证码,比较的稳妥。

下面附上验证码类的代码。这个类是放在libraries这个库文件夹下。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
 
/**
 * 验证码类
 */
 
class Code{
    //资源
    private $img;
    //画布宽度
    public $width = 150;
    //画布高度
    public $height = 45;
    //背景颜色
    public $bgColor = "#ffffff";
    //验证码
    public $code;
    //验证码的随机种子
    public $codeStr = "123456789abcdefghijklmnpqrstuvwsyz";
    //验证码长度
    public $codeLen = 4;
    //验证码字体
    public $font = "";//具体环境具体需要更改路径
    //验证码字体大小
    public $fontSize = 22;
    //验证码字体颜色
    public $fontColor = "";
 
    /**
     * 构造函数
     */
    public function __construct($arr = array()) {
 
        $width = '';
        $height = '';
        $codeLen = '';
        $fontSize = '';
        $bgColor = '';
        $fontColor = '';
 
        if(!empty($arr)){
           extract($arr);
        }
        $this->font = BASEPATH . "fonts/font.ttf";      
        if (!is_file($this->font)) {
            error("验证码字体文件不存在");
        }
        $this->width = empty($width) ? $this->width : $width;
        $this->height = empty($height) ? $this->height : $height;
        $this->bgColor = empty($bgColor) ? $this->bgColor : $bgColor;
        $this->codeLen = empty($codeLen) ? $this->codeLen : $codeLen;
        $this->fontSize = empty($fontSize) ? $this->fontSize : $fontSize;
        $this->fontColor = empty($fontColor) ? $this->fontColor : $fontColor;
        $this->create();//生成验证码
    }
 
    /**
     * 生成验证码
     */
    private function createCode() {
        $code = '';
        for ($i = 0; $i < $this->codeLen; $i++) {
            $code .= $this->codeStr [mt_rand(0, strlen($this->codeStr) - 1)];
        }
        $this->code = strtoupper($code);
        if(!isset($_SESSION)){
            session_start();
        }
        $_SESSION ['code'] = $this->code;
    }
 
    /**
     * 返回验证码
     */
    public function getCode() {
        return $this->code;
    }
 
    /**
     * 建画布
     */
    public function create() {
        if (!$this->checkGD())
            return false;
        $w = $this->width;
        $h = $this->height;
        $bgColor = $this->bgColor;
        $img = imagecreatetruecolor($w, $h);
        $bgColor = imagecolorallocate($img, hexdec(substr($bgColor, 1, 2)), hexdec(substr($bgColor, 3, 2)), hexdec(substr($bgColor, 5, 2)));
        imagefill($img, 0, 0, $bgColor);
        $this->img = $img;
        $this->createLine();
        $this->createFont();
        $this->createPix();
        $this->createRec();
    }
    /**
    *  画线
    */
    private function createLine(){
        $w = $this->width;
        $h = $this->height;
        $line_height = $h/10;
        $line_color = "#D0D0D0";
        $color = imagecolorallocate($this->img, hexdec(substr($line_color, 1, 2)), hexdec(substr($line_color, 3, 2)), hexdec(substr($line_color, 5, 2)));
        for($i=0;$i<10;$i++){
            $step =$line_height*$i+2;
            imageline($this->img, 0, $step, $w,$step, $color);
        }
        $line_width = $w/10;
        for($i=0;$i<10;$i++){
            $step =$line_width*$i+2;
            imageline($this->img, $step-2, 0, $step+2,$h, $color);
        }
    }
    /**
     * 画矩形边框
     */
    private function createRec() {
        imagerectangle($this->img, 0, 0, $this->width - 1, $this->height - 1, $this->fontColor);
    }
 
    /**
     * 写入验证码文字
     */
    private function createFont() {
        $this->createCode();
        $color = $this->fontColor;
        if (!empty($color)) {
            $fontColor = imagecolorallocate($this->img, hexdec(substr($color, 1, 2)), hexdec(substr($color, 3, 2)), hexdec(substr($color, 5, 2)));
        }
        $x = ($this->width - 10) / $this->codeLen;
        for ($i = 0; $i < $this->codeLen; $i++) {
            if (empty($color)) {
                $fontColor = imagecolorallocate($this->img, mt_rand(50, 155), mt_rand(50, 155), mt_rand(50, 155));
            }
            imagettftext($this->img, $this->fontSize, mt_rand(- 30, 30), $x * $i + mt_rand(6, 10), mt_rand($this->height / 1.3, $this->height - 5), $fontColor, $this->font, $this->code [$i]);
        }
        $this->fontColor = $fontColor;
    }
 
    /**
     * 画线
     */
    private function createPix() {
        $pix_color = $this->fontColor;
        for ($i = 0; $i < 50; $i++) {
            imagesetpixel($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), $pix_color);
        }
 
        for ($i = 0; $i < 2; $i++) {
            imageline($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $pix_color);
        }
        //画圆弧
        for ($i = 0; $i < 1; $i++) {
            // 设置画线宽度
           // imagesetthickness($this->img, mt_rand(1, 3));
            imagearc($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height)
                    , mt_rand(0, 160), mt_rand(0, 200), $pix_color);
        }
        imagesetthickness($this->img, 1);
    }
 
    /**
     * 显示验证码
     */
    public function show() {
        header("Content-type:image/png");
        imagepng($this->img);
        imagedestroy($this->img);
        exit;
    }
 
    /**
     * 验证GD库是不否打开imagepng函数是否可用
     */
    private function checkGD() {
        return extension_loaded('gd') && function_exists("imagepng");
    }
}

  然后再控制器中调用就可以了。

最后,提醒大家记得开启在自动加载文件中session哦。

 

posted @   Yxh_blogs  阅读(267)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示