让ChatGPT3.5写的一个PHP图形验证码,大家觉得怎么样?

闲来无事让ChatGPT3.5写的一个PHP图形验证码,并运行了一下,感觉验证码有点丑。。。

 

下面是ChatGPT3.5写的代码和使用说明,各位大佬感受一下!

<?php
// 创建图像
$imageWidth = 150;
$imageHeight = 50;
$image = imagecreatetruecolor($imageWidth, $imageHeight);

// 定义颜色
$backgroundColor = imagecolorallocate($image, 240, 240, 240);
$textColor = imagecolorallocate($image, 0, 0, 0);
$lineColor = imagecolorallocate($image, 180, 180, 180);

// 填充背景色
imagefilledrectangle($image, 0, 0, $imageWidth, $imageHeight, $backgroundColor);

// 生成验证码
$characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
$codeLength = 6;
$code = '';
for ($i = 0; $i < $codeLength; $i++) {
    $code .= $characters[rand(0, strlen($characters) - 1)];
}

// 将验证码存储到会话中,以供验证时使用
session_start();
$_SESSION['captcha_code'] = $code;

// 绘制验证码文本
$fontSize = 20;
$angle = 0;
$x = 20;
$y = 35;
imagettftext($image, $fontSize, $angle, $x, $y, $textColor, 'path/to/your/font.ttf', $code);

// 绘制干扰线条
$lineCount = 5;
for ($i = 0; $i < $lineCount; $i++) {
    $x1 = rand(0, $imageWidth / 2);
    $y1 = rand(0, $imageHeight);
    $x2 = rand($imageWidth / 2, $imageWidth);
    $y2 = rand(0, $imageHeight);
    imageline($image, $x1, $y1, $x2, $y2, $lineColor);
}

// 发送图像头部
header('Content-type: image/png');

// 输出图像
imagepng($image);

// 释放资源
imagedestroy($image);
?>

请注意,上述代码中的 path/to/your/font.ttf 应该替换为您自己的 TrueType 字体文件的路径。确保您在服务器上具有相应字体文件,并将路径正确指向它。

这段代码将生成一个 150x50 像素的图像,具有浅灰色的背景、黑色的文本和灰色的干扰线条。验证码文本是随机生成的包含大写字母、小写字母和数字的 6 位字符串。

您可以将上述代码保存为 captcha.php 文件,并在 HTML 表单中使用以下代码来显示图像验证码:

<!DOCTYPE html>
<html>
<head>
    <title>验证码示例</title>
    <style>
        .captcha-image {
            display: block;
            margin-bottom: 10px;
        }
    </style>
</head>
<body>
    <h1>验证码示例</h1>

    <form action="" method="POST">
        <label for="captcha">请输入验证码:</label>
        <input type="text" id="captcha" name="captcha" required>
        <img class="captcha-image" src="captcha.php" alt="验证码">
        <button type="submit">提交</button>
    </form>
</body>
</html>

这将在页面上显示一个包含验证码的图像。用户可以根据需要输入验证码并提交表单进行验证。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值