zoukankan      html  css  js  c++  java
  • 验证码的封装

    <?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));

        }

  • 相关阅读:
    Angular的执行顺序
    小程序地理位置授权,以及无法打开授权弹框的解决办法
    当需要对一个集合遍历删除元素的时候,都应该倒着删
    .net core部署在CentOS上时关于使用GDI报错的问题
    FactoryMethod(工厂方法模式)
    SimpleFactory(简单工厂模式)
    .net core3.1中swagger的使用
    使用HtmlAgilityPack开发爬虫筛选HTML时,关于xpath的坑
    在centos7.x环境中SQL Server附加数据库
    centos7.x中安装SQL Server
  • 原文地址:https://www.cnblogs.com/cqlb/p/9106231.html
Copyright © 2011-2022 走看看