• 验证码的封装


    <?php

    //验证码的封装(宽 高 数字 字母 数字和字母混合 干扰线 干扰点 背景色 字体颜色)

      verify();

        function verify($width = 100,$height = 40,$num = 5,$type = 3)

        {

            //1.装备画布

            $image = imagecreatetruecolor($width,$height);

            //2.生成颜色

            

            //3.设置字符串

            $string = '';

            switch($type)

            {

                case 1:

                    //纯数字

                    $str = '0123456789';

                    //随机打乱字符串里的所有字符并截取字符串

                    $string = substr(str_shuffle($str),0,$num);

                break;

                

                case 2:

                    //纯字母

                    //创建一个a-z的数组

                    $arr = range('a','z');

                    //把数组里的元素重新排序

                    shuffle($arr);

                    //截取元素形成一个新的数组

                    $tmp = array_slice($arr,0,$num);

                    //将数组转换成一个字符串

                    $string = join('',$tmp);

                break;

                

                case 3:

                    //数组和字母混合

                    $str = '123456789abcdefghgklmnopqrstvuwxyzABCDEFGHIJKLMNOPQRSTVUWXYZ';

                    //随机打乱字符串里的所有字符并截取字符串

                    $string = substr(str_shuffle($str),0,$num);

                break;

            }

            

            //给背景颜色填充浅色

            imagefilledrectangle ($image,0,0,$width,$height,lightColor($image));

            

            //4.写字

            for($i=0;$i<$num;$i++)

            {

                $x = ($width/$num)*$i;

                $y = mt_rand(10,$height-20);

                imagechar($image,5,$x,$y,$string[$i],deepColor($image));

            }

            //5.干扰线(点)

            //线

            for($i=0;$i<$num;$i++)

            {

                imagearc($image,mt_rand(10,$width),mt_rand(10,$height),mt_rand(10,$width),mt_rand(10,$height),mt_rand(0,10),mt_rand(0,270),deepColor($image));

            }

            //点

            for($i=0;$i<50;$i++)

            {

                imagesetpixel($image,mt_rand(0,$width),mt_rand(0,$height),deepColor($image));

            }

            

            //6.指定输出的类型

            header('Content-type:image/png');

            //7.输出图片

            imagepng($image);

            //8.销毁资源

            imagedestroy($image);

            

            return $string;

        }

        

        //浅的颜色封装函数

        function lightColor($image)

        {

            return imagecolorallocate ($image,mt_rand(130,255),mt_rand(130,255),mt_rand(130,255));//0-255值越小颜色越深

        }

        

        //深的颜色封装函数

        function deepColor($image)

        {

            return imagecolorallocate($image,mt_rand(0,120),mt_rand(0,120),mt_rand(0,120));

        }

  • 相关阅读:
    swoole学习(四):websocket
    LeetCode--SQL 查询:体育馆的人流量
    LeetCode--SQL 查询:有趣的电影
    centos7下mysql5.7忘记密码
    LeetCode--SQL 查询:删除重复的电子邮箱。
    swoole学习(三):HTTP服务
    swoole学习(二):TCP/UDP服务器-客户端
    swoole学习(一):初识swoole
    LeetCode--SQL 查询:查找部门工资前三高的职员
    报文、报文段、分组、包、数据报、帧、数据流的概念区别
  • 原文地址:https://www.cnblogs.com/cqlb/p/9106231.html
Copyright © 2020-2023  润新知