是非题

CodeIgniter生成验证码类
CodeIgniter自带验证码类太繁杂的,还要用到数据库,完全背离了CI简洁至上的初衷,鉴于此,自写验证码类。l...
扫描右侧二维码阅读全文
08
2018/10

CodeIgniter生成验证码类

CodeIgniter自带验证码类太繁杂的,还要用到数据库,完全背离了CI简洁至上的初衷,鉴于此,自写验证码类。

libraries/Captcha.php

<?php
class Captcha
{
    private $numbers = '0123456789';
    private $lowercase_letters = 'abcdefghijklmnopqrstuvwxyz';
    private $capital_letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    private $all_codes;
    private $code;
    public function generate_code($length, $type = ['numbers'])
    {
        foreach ($type as $value) {
            $this->all_codes .= $this->{$value};
        }
        for ($i = 0; $i <= $length - 1; $i++) {
            $this->code .= $this->all_codes[mt_rand(0, mb_strlen($this->all_codes) - 1)];
        }
    }
/**
 *$length int 必须 验证码长度
 *$type array 必须 验证码类型 可选lowercase_letters,capital_letters,numbers
 *$width int 必须 图像宽度
 *$height int 必须 图像高度
 *$size int 必须 字体大小
 *$line int 非必须 干扰线段数
 *$dot int 非必须 干扰点数
 */
    public function generate_image($length, $type = ['numbers'], $width, $height, $size, $line = false, $dot = false)
    {
        $this->generate_code($length, $type);
        $image = imagecreatetruecolor($width, $height);
        $color_bg = imagecolorallocate($image, mt_rand(233, 255), mt_rand(233, 255), mt_rand(233, 255));
        imagefill($image, 0, 0, $color_bg);
        $spacing = floor($width / $length);
        for ($i = 0; $i <= mb_strlen($this->code) - 1; $i++) {
            $color_code = imagecolorallocate($image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
            imagestring($image, $size, 2 + $i * $spacing, 0, $this->code[$i], $color_code);
        }
        if ($line) {
            for ($i = 1; $i <= $line; $i++) {
                $x1 = mt_rand(0, $width - 1);
                $y1 = mt_rand(0, $height - 1);
                $x2 = mt_rand(0, $width - 1);
                $y2 = mt_rand(0, $height - 1);
                $color_line = imagecolorallocate($image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
                imageline($image, $x1, $y1, $x2, $y2, $color_line);
            }
        }
        if ($dot) {
            for ($i = 1; $i <= $dot; $i++) {
                $x = mt_rand(0, $width - 1);
                $y = mt_rand(0, $height - 1);
                $color_dot = imagecolorallocate($image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
                imagesetpixel($image, $x, $y, $color_dot);
            }
        }
        ob_start();
        imagepng($image);
        $imagestring = ob_get_contents();
        ob_end_clean();
        imagedestroy($image);
        $images['base64'] = base64_encode($imagestring);
        $images['code'] = $this->code;
        return $images;
    }
}

使用

//加载生成验证码类
$this->load->library('captcha');

//生成一个60*15的四位大写字母与数字混合验证码,带有三条干扰线与十个干扰点,2号字体
$image = $this->captcha->generate_image(4, ['numbers', 'capital_letters'], 60, 15, 2, 3, 10);

//输出图片
echo '<img src="data:image/png;base64,'. $image['base64']
 .'" />';

!> 本文为博主原创,转载请注明作者与文章URL,否则视为同意按照每千字一万元的价格支付稿费!

最后修改:2018 年 10 月 09 日 10 : 34 PM
如果觉得我的文章对你有用,请随意赞赏

1 條評論

  1. 飞炫的火花

    技术文,支持个

發表評論