Blogger Information
Blog 15
fans 2
comment 0
visits 15772
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
验证码的封装
LiuBo的博客
Original
799 people have browsed it

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

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!