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

        }

  • 相关阅读:
    七牛大数据平台的演进与大数据分析实践--转
    Re:从0开始的微服务架构:(一)重识微服务架构--转
    Re:从 0 开始的微服务架构--(三)微服务架构 API 的开发与治理--转
    Java7里try-with-resources分析--转
    线上服务CPU100%问题快速定位实战--转
    Windows下本机简易监控系统搭建(Telegraf+Influxdb+Grafana)--转
    Scalable, Distributed Systems Using Akka, Spring Boot, DDD, and Java--转
    ES Segment Memory——本质上就是segment中加到内存的FST数据,因此segment越多,该内存越大
    Self Organizing Maps (SOM): 一种基于神经网络的聚类算法
    RBF网络——核心思想:把向量从低维m映射到高维P,低维线性不可分的情况到高维就线性可分了
  • 原文地址:https://www.cnblogs.com/cqlb/p/9106231.html
Copyright © 2011-2022 走看看