首页 > 代码库 > 封装captcha类 -- 画图四

封装captcha类 -- 画图四

<?php
    
    // 验证码类
    class Captcha{
        //属性
        private $width;
        private $height;
        private $length;
        private $lines;
        private $pixels;
        private $color;
        private $font;
        private $string;
        /*
         *构造方法
         *@param1 array $arr, 一个数组, 里面几乎包含了所有的属性
         *
         */
        public function __construct($arr = array()) {
            $this->width = isset($arr[‘width‘]) ? $arr[‘width‘] : 146;
            $this->height = isset($arr[‘height‘]) ? $arr[‘height‘] : 20;
            $this->length = isset($arr[‘length‘]) ? $arr[‘length‘] : 4;
            $this->lines = isset($arr[‘lines‘]) ? $arr[‘lines‘] : 5;
            $this->pixels = isset($arr[‘pixels‘]) ? $arr[‘pixels‘] : 200;
            $this->font = isset($arr[‘font‘]) ? $arr[‘font‘] : 5;
            // 背景色
            $this->color[‘bg_min‘] = isset($arr[‘bg_min‘]) ? $arr[‘bg_min‘] : 200;
            $this->color[‘bg_max‘] = isset($arr[‘bg_max‘]) ? $arr[‘bg_max‘] : 255;
            // 字体颜色
            $this->color[‘font_min‘] = isset($arr[‘font_min‘]) ? $arr[‘font_min‘] : 0;
            $this->color[‘font_max‘] = isset($arr[‘font_max‘]) ? $arr[‘font_max‘] : 100;
            // 线颜色
            $this->color[‘line_min‘] = isset($arr[‘line_min‘]) ? $arr[‘line_min‘] : 100;
            $this->color[‘line_max‘] = isset($arr[‘line_max‘]) ? $arr[‘line_max‘] : 150;
            // 像素颜色
            $this->color[‘pixels_min‘] = isset($arr[‘pixels_min‘]) ? $arr[‘pixels_min‘] : 150;
            $this->color[‘pixels_max‘] = isset($arr[‘pixels_max‘]) ? $arr[‘pixels_max‘] : 200;
            // 字符串
            $this->string = isset($arr[‘string‘]) ? $arr[‘string‘] : ‘abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789‘;

        }

        /*
         *获得验证码图片
         *
         */
        public function generate() {

            //1. 创建画布
            $im = imagecreatetruecolor($this->width, $this->height);    

            //2. 背景顏色
            //2.1分配顏色
            $bg_color = imagecolorallocate($im, mt_rand($this->color[‘bg_min‘], $this->color[‘bg_max‘]), mt_rand($this->color[‘bg_min‘], $this->color[‘bg_max‘]), mt_rand($this->color[‘bg_min‘], $this->color[‘bg_max‘])); 

            //2.2背景填充
            imagefill($im, 0, 0, $bg_color);

            // 3.获取验证码
            $captcha = $this->getCaptchaStr();

            // var_dump($captcha); exit;

            // 4.分配颜色
            $str_color = imagecolorallocate($im, mt_rand($this->color[‘font_min‘], $this->color[‘font_max‘]), mt_rand($this->color[‘font_min‘], $this->color[‘font_max‘]), mt_rand($this->color[‘font_min‘], $this->color[‘font_max‘]));

            //5. 将验证码写入到图片
            imagestring($im, $this->font, ceil($this->width / 2) - 20, ceil($this->height / 2) - 10, $captcha, $str_color);

            // 6. 增加干扰线
            for( $i = 0; $i < $this->lines; $i++ ){
                // 分配颜色
                $line_color = imagecolorallocate($im, mt_rand($this->color[‘line_min‘], $this->color[‘line_max‘]), mt_rand($this->color[‘line_min‘], $this->color[‘line_max‘]), mt_rand($this->color[‘line_min‘], $this->color[‘line_max‘]));
                // 写入线段
                imageline($im, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $line_color);
            }

           // 7. 增加干扰点
           for( $i = 0; $i < $this->pixels; $i++ ){
                $pixels_color = imagecolorallocate($im, mt_rand($this->color[‘pixels_min‘], $this->color[‘pixels_max‘]), mt_rand($this->color[‘pixels_min‘], $this->color[‘pixels_max‘]), mt_rand($this->color[‘pixels_min‘], $this->color[‘pixels_max‘]));
                // 写入线段
                imagesetpixel($im, mt_rand(0, $this->width), mt_rand(0, $this->height), $pixels_color);

            }

            // 8. 保存输出
            imagepng($im);
            // imagepng($im, ‘captcha.png‘);
            
            // 9. 释放资源
            imagedestroy($im);

        }

        /*
         *获得验证码字符串
         *
        */
        private function getCaptchaStr() {
             // 定義變量保存字符串
             $captchaStr = ‘‘;
             // for( $i = 0; $i < $this.length; $i++ ){ //傻逼写法
             for( $i = 0; $i < $this->length; $i++ ){
                 // 获取随机字符串
                 $captchaStr .= $this->string[mt_rand(0, strlen($this->string) - 1)];
             }

             // 将随机字符串存放在session中
             $_SESSION[‘captcha‘] = $captchaStr;

             return $captchaStr;
        }

    }

    $captcha  = new Captcha();

    // header(‘content-type: image/php‘);
    header(‘content-type: image/png‘);
    $captcha->generate();

 

封装captcha类 -- 画图四