码农编程阁-全国最大的中文编程交流平台

 找回密码
 立即注册
查看: 124|回复: 0

[文字教程] PHP 生成验证码图片

[复制链接]
回帖奖励 1 金币 回复本帖可获得 1 金币奖励! 每人限 1 次
  • TA的每日心情
    擦汗
    2025-3-10 11:56
  • 签到天数: 95 天

    连续签到: 1 天

    [LV.6]常住居民II

    170

    主题

    902

    帖子

    2万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    26745
    发表于 2021-3-5 19:49:10 | 显示全部楼层 |阅读模式


    1. /**
    2. * 验证码类
    3. */
    4. class VeriCode
    5. {
    6.     // 获取验证码配置
    7.     private static function _getCodeConfig()
    8.     {
    9.         return [
    10.             // 验证码字符集
    11.             'codeStr' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
    12.             // 验证码个数
    13.             'codeCount' => 4,
    14.             // 字体大小
    15.             'fontsize' => 25,
    16.             // 验证码的宽度
    17.             'width' => 130,
    18.             // 验证码高度
    19.             'height' => 36,
    20.             // 是否有干扰点?true有,false没有
    21.             'disturbPoint' => false,
    22.             // 干扰点个数,disturbPoint开启后生效
    23.             'pointCount' => 1,
    24.             // 是否有干扰条?true有,false没有
    25.             'disturbLine' => false,
    26.             // 干扰条个数,disturbLine开启后生效
    27.             'lineCount' => 9
    28.         ];
    29.     }

    30.     // 创建图片验证码
    31.     public static function create()
    32.     {
    33.         // 配置
    34.         $config = self::_getCodeConfig();

    35.         //创建画布
    36.         $image = imagecreatetruecolor($config['width'], $config['height']);
    37.         //背景颜色
    38.         $bgcolor = imagecolorallocate($image, 255, 255, 255);
    39.         imagefill($image, 0, 0, $bgcolor);
    40.         $captch_code = '';//存储验证码
    41.         $captchCodeArr = str_split($config['codeStr']);

    42.         //随机选取4个候选字符
    43.         for ($i = 0; $i < $config['codeCount']; $i++) {
    44.             $fontsize = $config['fontsize'];
    45.             $fontcolor = imagecolorallocate($image, rand(0, 120), rand(0, 120), rand(0, 120));//随机颜色
    46.             $fontcontent = $captchCodeArr[rand(0, strlen($config['codeStr']) - 1)];
    47.             $captch_code .= $fontcontent;
    48.             $_x = $config['width'] / $config['codeCount'];
    49.             $x = ($i * (int)$_x) + rand(5, 10);   //随机坐标
    50.             $y = rand(5, 10);
    51.             imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);    // 水平地画一行字符串

    52.         }

    53.         session_start();
    54.         $_SESSION['code'] = $captch_code;
    55.         //增加干扰点
    56.         if ($config['disturbPoint']) {
    57.             for ($i = 0; $i < $config['pointCount']; $i++) {
    58.                 $pointcolor = imagecolorallocate($image, rand(50, 200), rand(50, 200), rand(50, 200));
    59.                 imagesetpixel($image, rand(1, 99), rand(1, 29), $pointcolor);

    60.             }
    61.         }

    62.         //增加干扰线
    63.         if ($config['disturbLine']) {
    64.             for ($i = 0; $i < $config['lineCount']; $i++) {
    65.                 $linecolor = imagecolorallocate($image, rand(80, 280), rand(80, 220), rand(80, 220));
    66.                 imageline($image, rand(1, 99), rand(1, 29), rand(1, 99), rand(1, 29), $linecolor);
    67.             }
    68.         }

    69.         //输出格式
    70.         header('content-type:image/png');
    71.         imagepng($image);

    72.         //销毁图片
    73.         imagedestroy($image);
    74.     }
    复制代码
    调用


    1. VeriCode::create();
    复制代码


    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|码农编程阁-全国最大的中文编程交流平台

    GMT+8, 2025-4-29 21:27 , Processed in 0.233278 second(s), 24 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2020, Tencent Cloud.

    快速回复 返回顶部 返回列表