• php生成图片验证码


    验证码主要用来防止暴力破解的方式来进行登录,下面是php中的图片验证码生成方式,废话不多说,直接上代码

    /**
     * 生成验证码
     */
    function buildRandomString($type=3,$length=4){
        if ($type==1){
            //join()函数是implode()函数的别名,用来将数组变成字符串,第一个参数为数组元素之间放置的内容
            $chars=join( "",range(0,9));
        }elseif($type==2){    
            //range()函数创建一个包含指定范围的元素的数组
            //array_merge()函数把一个或多个数组合并为一个数组                 
            $chars=join("",array_merge(range("a","z"),range("A","Z")));
        }elseif($type==3){
            $chars=join("",array_merge(range("a","z"),range("A","Z"),range(0,9)));
            //产生的数组数据abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
        }
        if($length>strlen($chars)){
            exit("字符串长度不够");
        }    
        $chars=str_shuffle($chars);//str_shuffle()函数随机打乱字符串中的所有字符
        return substr($chars,0,$length);//substr()函数返回指定部分的字符串
    }
    
    /**
     *通过GD库绘制图片验证码
     */
    function verifyImage($type=1,$length=4,$pixel=5,$line=5,$sess_name = "verify"){
        session_start();
    
        //创建画布
        $width=80;
        $height=28;
        //imagecreatetruecolor()返回一个图像标识符,代表了一幅大小为 x_size 和 y_size 的黑色图像
        $image=imagecreatetruecolor($width,$height);
        //imagecolorallocate()返回一个标识符,代表了由给定的RGB成分组成的颜色
        $white=imagecolorallocate($image,255,255,255);
        $black=imagecolorallocate($image,0,0,0);        
        //将图片的封闭长方形区域着色,参数为画布资源、矩形对角线坐标,颜色
        imagefilledrectangle($image,1,1,$width-2,$height-2,$white);
    
        //创建验证码
        $chars=buildRandomString($type,$length);
        //将验证码记录在session中,判断正误
        $_SESSION[$sess_name]=$chars;
    
        //画出验证码的内容
        $fontfiles=array("SIMYOU.TTF");//字体文件            
        for($i=0;$i<$length;$i++){
            //mt_rand()返回范围内的随机数
            $size=mt_rand(14,18);//字型尺寸
            $angle=mt_rand(-15,15);//字型角度
            $x=5+$i*$size;
            $y=mt_rand(20,26);//x y为文字坐标值,原点为文字左上角
            $fontfile="./fonts/".$fontfiles[mt_rand(0,count($fontfiles)-1)];//字体:通过数组方式取文件,方便以后扩展
            $color=imagecolorallocate($image,mt_rand(50,90),mt_rand(80,200),mt_rand(90,180));//字体颜色
            $text=substr($chars,$i,1);//内容
            //imagettftext()将字型文字写入图片
            imagettftext($image,$size,$angle,$x,$y,$color,$fontfile,$text);
        }
        
        if($pixel){//绘制干扰点的数量,参数传递
            for($i=0;$i<50;$i++){
                //绘点
                imagesetpixel($image,mt_rand(0,$width-1),mt_rand(0,$height-1),$black);
            }
        }
        if($line){//绘制干扰线的数量,参数传递
            for($i=1;$i<$line;$i++){
                $color=imagecolorallocate($image,mt_rand(50,90),mt_rand(80,200),mt_rand(90,180));
                imageline($image,mt_rand(0,$width-1),mt_rand(0,$height-1),mt_rand(0,$width-1),mt_rand(0,$height-1),$color);
            }
        }
        header("content-type:image/gif");
        imagegif($image);//建立gif格式的图形
        imagedestroy($image);//销毁图像资源
    }
  • 相关阅读:
    利用无线网络数据包分析无线网络安全
    C++ basic
    几道题目
    Pythonunittestddt(应用到类,实际参数化)
    Pythonunittestddt(基本应用)
    【第二章】处理数据源信息(config、excel数据源处理
    Python操作excel003(封装读取excel类
    Python+selenium 【第一章】什么叫ui自动化以及环境搭建
    【第五章】接口关联 正则表达式jsonpath模块
    【第四章】封装request类
  • 原文地址:https://www.cnblogs.com/isuifeng/p/5180344.html
Copyright © 2020-2023  润新知