码迷,mamicode.com
首页 > Web开发 > 详细

php生成验证码

时间:2020-07-26 00:27:33      阅读:76      评论:0      收藏:0      [点我收藏+]

标签:rectangle   ora   width   cas   exit   tpi   image   idt   int   

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

 

php生成验证码

标签:rectangle   ora   width   cas   exit   tpi   image   idt   int   

原文地址:https://www.cnblogs.com/qiuxd/p/13376475.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!