imagecreatetruecolor()<?php
session_start();
$width = 100; $height = 30;
$img = imagecreatetruecolor($width, $height);
// 背景白
$bg = imagecolorallocate($img, 255, 255, 255);
imagefill($img, 0, 0, $bg);
// 4 位随机数字
$code = '';
for ($i = 0; $i < 4; $i++) {
$code .= mt_rand(0, 9);
$color = imagecolorallocate($img, mt_rand(0, 150), mt_rand(0, 150), mt_rand(0, 150));
$x = $i * $width / 4 + mt_rand(5, 15);
$y = mt_rand(5, 15);
imagestring($img, 5, $x, $y, $code[$i], $color); // 内置字体 5
}
$_SESSION['captcha'] = $code;
// 干扰点
for ($i = 0; $i < 200; $i++) {
$color = imagecolorallocate($img, mt_rand(50, 200), mt_rand(50, 200), mt_rand(50, 200));
imagesetpixel($img, mt_rand(1, $width-1), mt_rand(1, $height-1), $color);
}
// 干扰线
for ($i = 0; $i < 3; $i++) {
$color = imagecolorallocate($img, mt_rand(100, 250), mt_rand(100, 250), mt_rand(100, 250));
imageline($img, mt_rand(0, $width/2), mt_rand(0, $height/2), mt_rand($width/2, $width), mt_rand($height/2, $height), $color);
}
header('Content-Type: image/png');
imagepng($img);
imagedestroy($img);
<?php
session_start();
// 字符库:常用汉字 + 十六进制字符
$str = '芸芸众生绿水青山名胜古迹敞开心胸便会云蒸霞蔚快乐将永远伴随着你';
$chars = [];
for ($i = 0, $len = mb_strlen($str, 'UTF-8'); $i < $len; $i++) {
$chars[] = mb_substr($str, $i, 1, 'UTF-8');
}
$code = '';
for ($i = 0; $i < 4; $i++) {
$code .= (mt_rand(0, 1) === 0) ? $chars[mt_rand(0, count($chars)-1)] : dechex(mt_rand(0, 15));
}
$_SESSION['captcha'] = $code;
$img = imagecreatetruecolor(130, 40);
$bg = imagecolorallocate($img, 240, 240, 240);
imagefill($img, 0, 0, $bg);
$textColor = imagecolorallocate($img, 0, 0, 0);
// 需确保服务器有 MSYH.TTF (微软雅黑) 或其他 TTF 字体文件
$fontFile = __DIR__ . '/fonts/MSYH.TTF';
if (!is_file($fontFile)) {
// 回退内置字体
imagestring($img, 5, 20, 10, $code, $textColor);
} else {
// TTF 绘制:大小、角度、x、y、颜色、字体文件、文本
imagettftext($img, 20, mt_rand(-10, 10), 15, 30, $textColor, $fontFile, $code);
}
// 干扰线
for ($i = 0; $i < 5; $i++) {
$color = imagecolorallocate($img, mt_rand(150, 255), mt_rand(150, 255), mt_rand(150, 255));
imageline($img, mt_rand(0, 130), mt_rand(0, 40), mt_rand(0, 130), mt_rand(0, 40), $color);
}
// 干扰点
for ($i = 0; $i < 100; $i++) {
$color = imagecolorallocate($img, mt_rand(100, 255), mt_rand(100, 255), mt_rand(100, 255));
imagesetpixel($img, mt_rand(0, 130), mt_rand(0, 40), $color);
}
header('Content-Type: image/jpeg');
imagejpeg($img, null, 90);
imagedestroy($img);
<img id="captcha" src="captcha.php" onclick="this.src='captcha.php?'+Date.now()" style="cursor:pointer">
<input name="captcha" placeholder="点击图片刷新">
<?php
session_start();
if (strtoupper($_POST['captcha']) !== strtoupper($_SESSION['captcha'] ?? '')) {
die('验证码错误');
}
// 通过,执行业务逻辑
unset($_SESSION['captcha']); // 用完即销毁,防重放
random_bytes() 或 Random\Randomizer 生成随机字符(PHP 8.2+)captcha_ 支持同页多验证码| 方案 | 优点 | 缺点 |
|---|---|---|
| GD 生成图片验证码 | 零依赖、完全可控、兼容性好 | 需维护字体、图片体积大、无障碍差 |
| Google reCAPTCHA v3 | 无感验证、风控强、免维护 | 需外网、隐私合规、国内访问慢 |
| 极验/网易易盾等国内验证码 | 行为分析、滑动拼图、国内加速 | 商业收费、SDK 集成成本 |
| Honeypot 隐藏表单 | 零用户感知、极简 | 仅防低级爬虫 |
对于中小项目,自建 GD 验证码依然是性价比最高的选择;关键业务建议接入成熟验证码服务。