php生成验证码

复制代码
<?php
    check_code(100, 50, 5);
    function check_code($width = 100, $height = 50, $num = 4, $type = 'jpeg') {
        $img = imagecreate($width, $height);
        $string = '';
        for ($i = 0; $i < $num; $i++) {
            $rand = mt_rand(0, 2);
            
            switch($rand) {
                case 0:
                    $ascii = mt_rand(48, 57);
                    break;
                case 1:
                    $ascii = mt_rand(65, 90);
                    break;
                case 2:
                    $ascii = mt_rand(97, 122);
                    break;
            }
            
            $string .= sprintf('%c', $ascii);
        }
        
        imagefilledrectangle($img, 0, 0, $width, $height, randBg($img));
        
        for ($i = 0; $i < 50; $i++) {
            imagesetpixel($img, mt_rand(0, $width), mt_rand(0, $height), randPix($img));
        }
        
        for ($i = 0; $i < $num; $i++) {
            $x = floor($width/$num) *$i + 2;
            $y = mt_rand(0, $height - 15);
            
            imagechar($img, 5, $x, $y, $string[$i], randPix($img));
        }
        
        $func = 'image' . $type;
        
        $header = 'content-type:image/' . $type;
        
        if (function_exists($func)) {
            header($header);
            $func($img);
        } else {
            exit('不支持');
        }
        
        imagedestroy($img);
        return $string;
    }
    
    function randBg($img) {
        return imagecolorallocate($img, mt_rand(130, 255), mt_rand(130, 255), mt_rand(130, 255));
    }
    
    function randPix($img) {
        return imagecolorallocate($img, mt_rand(0, 120), mt_rand(0, 120), mt_rand(0, 120));
    }
?>
复制代码

 

posted @   qiuxd  阅读(181)  评论(0编辑  收藏  举报
编辑推荐:
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ASP.NET Core 模型验证消息的本地化新姿势
阅读排行:
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
· Ai满嘴顺口溜,想考研?浪费我几个小时
· Browser-use 详细介绍&使用文档
· 软件产品开发中常见的10个问题及处理方法
点击右上角即可分享
微信分享提示