封装captcha类 -- 画图四 - LiuYier

zsongs 2021-08-04 原文


封装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();

 

发表于
2016-11-26 22:27 
LiuYier 
阅读(452
评论(0
编辑 
收藏 
举报

 

版权声明:本文为zsongs原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/zsongs/p/6105502.html

封装captcha类 -- 画图四 - LiuYier的更多相关文章

  1. Vue项目中使用Vuex + axios发送请求

      本文是受多篇类似博文的影响写成的,内容也大致相同。无意抄袭,只是为了总结出一份自己的经验。      一直 […]...

  2. 科来网络分析系统 V6.0 – 2006 Jackie Qin

    软件介绍 集网络检测、错误诊断、性能优化、安全分析为一体的综合网络分析系统。它可以帮助网络管理员进行网络监测、 […]...

  3. 在Linux上挂载Windows共享文件夹,如何开机自动挂载(mount) – 季枫

    使用mount命令在centos上挂接Windows的共享文件夹 mount //192.168.0.1/li […]...

  4. axure – monster7

    axure 1.页面结构 添加子或者同级页面 双击打开页面 可以进行重命名 2.添加元件库或者创建原件库以及下 […]...

  5. 从零开始一起学习SLAM | 为什么要学SLAM? – 计算机视觉life

    从零开始一起学习SLAM | 为什么要学SLAM? 2018-11-07 09:13  计算机视觉life  […]...

  6. 打分排序系统漫谈3 – 贝叶斯更新/平均

    想要从本质上解决小样本估计不置信的问题,一个更符合思维逻辑的方法是我们先基于经验给出一个预期估计,然后不断用收 […]...

  7. 别被你的双眼所欺骗 100张神奇的视觉欺骗图 – jack船长大哥

    别被你的双眼所欺骗 100张神奇的视觉欺骗图  人类的大脑分工不同,所导致的眼睛所见到的图像在左右脑中处理时产 […]...

  8. seq2seq通俗理解—-编码器和解码器(TensorFlow实现) – mantch

    seq2seq通俗理解—-编码器和解码器(TensorFlow实现) 文章目录1. 什么是seq2 […]...

随机推荐

  1. eclipse查看.class文件

    要使用jd-gui.exe反编译程序 步骤:window-preferences-general-editor […]...

  2. API测试之Postman使用完全指南(Postman教程,这篇文章就够了)

    Postman Postman是一个可扩展的API开发和测试协同平台工具,可以快速集成到CI/CD管道中。旨在 […]...

  3. 猪懂傻改之《powershell 代码规范》

    猪懂傻改之《powershell 代码规范》 猪懂傻改之《powershell 代码规范》 脚本程序员或许都经 […]...

  4. Java Web/Eclipse/Maven/Tomcat

    Java Web/Eclipse/Maven/Tomcat 最近有个新项目是java web项目,记录一下,可 […]...

  5. 在线HTML编辑器原理

    为什么能实现在线编辑呢?  首先需要ie 的支持,在 ie 5.5以后就有一个编辑状态,就是利用这个编辑状态, […]...

  6. pycharm+gitee环境搭建(超详细)

    背景:本地开发代码在没有云托管的时候代码很容易丢掉,如果是小团队,这时候可以使用公司团队注册一个账号共同使用。 […]...

  7. ElementUI级联选择器动态加载Demo

      嗯,今天项目遇到,弄了一会,这里分享一下,不足之处请小伙伴指出来, 官网Demo: <el-casc […]...

  8. 百度知道1000指数的关键词留链接排名到第一的实战案例

    网络推广中一个很重要的部分就是问答营销,可能有些人说问答营销并不难,但是要真正做好问答营销是需要用心的,不是仅 […]...

展开目录

目录导航