验证码类(包含两套皮肤)

<?php
    
    /*
        系统验证码类, 可直接在模板标签中调用
        提供了两套验证码(普通验证码和图片验证码)
    */
    
    //defined('IN_SHOP') or die();

    class Captcha {
        
        protected $img;                                        //验证码图片资源
        protected $width;                                    //图片宽度
        protected $height;                                    //图片高度
        protected $img_jpg = array(                            //以图片为背景
            1 =>'captcha_bg1.jpg', 
            2 =>'captcha_bg2.jpg',
            3 =>'captcha_bg3.jpg',
            4 =>'captcha_bg4.jpg', 
            5 =>'captcha_bg5.jpg',                     
        );
        protected $img_gif = array(
            1 =>'captcha_bg1.jpg', 
            2 =>'captcha_bg2.jpg',
            3 =>'captcha_bg3.jpg',
            4 =>'captcha_bg4.jpg', 
            5 =>'captcha_bg5.jpg',                 
        );
        protected $path = 'tmp/captcha_img/';                //背景图片路径
        protected $code_num = 5;                            //验证码个数
        protected $distrub_num = 100;                        //干扰点个数
        protected $code;                                    //验证码
        protected $type;                                    //验证码类型

        function __construct($type = '', $width = 145, $height = 20) {
            $this->width = $width;
            $this->height = $height;
            $this->type = $type;
            $this->code = $this->set_char();
            $this->record_code();
            //生成验证码
            $this->captcha();
        }

        protected function captcha() {
            
            //画图
            $this->get_img();
            //画干扰点
            if ($this->type == '')
                $this->get_point();
            //形成验证码
            $this->get_char();
            //输出图片
            $this->show_img();
        }
        
        //形成图像资源
        protected function get_img() {

            if ($this->type == '') 
            {
                $this->img = imagecreatetruecolor($this->width, $this->height);
                $bgcolor = imagecolorallocate($this->img, mt_rand(128, 255), mt_rand(128, 255), mt_rand(128, 255));
                imagefill($this->img, 0, 0, $bgcolor);
            }
            elseif ($this->type == 'img')
            {    
                if (!file_exists(ROOT_PATH . $this->path . $this->img_jpg[1]))
                {
                    return false;
                }
                else 
                {    
                    if ((function_exists('imagecreatefromjpeg') && (imagetypes() & IMG_JPEG)))
                    {    
                        $theme = $this->img_jpg[mt_rand(1, count($this->img_jpg))];
                        $bgcolor = imagecreatefromjpeg(ROOT_PATH . $this->path . $theme);
                    }
                    else
                    {    $theme = $this->img_gif[mt_rand(1, count($this->img_jpg))];
                        $bgcolor = imagecreatefromgif(ROOT_PATH . $this->path . $theme);
                    }
                }
                
                $x = imagesx($bgcolor);
                $y = imagesy($bgcolor);
                $this->img = imagecreatetruecolor($x, $y);
                
                (function_exists('imagecopyresampled')) ? imagecopyresampled($this->img, $bgcolor, 0, 0, 0, 0, $this->width, $this->height, $x, $y) : imagecopyresized($this->img, $bgcolor, 0, 0, 0, 0, $this->width, $this->height, $x, $y);

                imagecolorallocate($this->img, 255, 255, 255);

                imagedestroy($bgcolor);
            }

        }
        
        //画干扰点
        protected function get_point() {
            for ($i = 0; $i < $this->distrub_num; $i++)
            {
                $bg_color = imagecolorallocate($this->img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
                imagesetpixel($this->img, mt_rand(1, $this->width - 1), mt_rand(1, $this->height - 1), $bg_color);
            }
        }

        //生成验证码字符串
        protected function set_char() {
            
            $char = '1234567890qwertyuiopasdfghjklmnbvcxzQWERTYUIOPLKJHGFDSAZXCVBNM';

            $str = '';
            for ($i = 0; $i < $this->code_num; $i++)
            {
                $str .= substr($char, rand(0, strlen($char) - 1), 1);
            }

            return $str;
        }
        
        //将验证码输出到图布
        protected function get_char() {
            
            $bgcolor = imagecolorallocate($this->img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
            
            for ($i = 0; $i < $this->code_num; $i++)
            {    
                imagechar($this->img, mt_rand(4, 6), ($this->width / $this->code_num) * $i + 7, mt_rand(0, ($this->height - 15)), substr($this->code, $i, 1), $bgcolor);
            }
        }

        //输出图片
        protected function show_img() {
            header("Cache-Control: no-cache,no-store,must-revalidate");
            header("pragma: no-cache");

            if(imagetypes() & IMG_PNG)
            {
                header("Content-Type: image/png");
                imagepng($this->img);
            }
            elseif (imagetypes() & IMG_GIF)
            {
                header("Content-Type: image/gif");
                imagegif($this->img);
            }
            else
            {    
                header("Content-Type: image/jpeg");
                imagejpeg($this->img);            
            }
        }
        
        //记录验证码
        protected function record_code() {
            session_start();
            $_SESSION['__CAPTCHA__'] = base64_encode($this->code);
        }

        function __destruct() {
            imagedestroy($this->img);
        }
    }
?>

 

转载于:https://www.cnblogs.com/bao-c-x/articles/3253391.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值