当前位置:首页 > PHP

php实现图片验证码

2026-04-03 05:39:05PHP

使用PHP生成图片验证码

PHP可以通过GD库生成图片验证码,以下是具体实现方法。

创建验证码图片

使用imagecreatetruecolor函数创建一个空白图片,并填充背景色:

$width = 120;
$height = 40;
$image = imagecreatetruecolor($width, $height);
$bgColor = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $bgColor);

生成随机验证码字符串

创建包含数字和字母的随机字符串:

$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$length = 6;
$code = '';
for ($i = 0; $i < $length; $i++) {
    $code .= $chars[rand(0, strlen($chars) - 1)];
}

将验证码写入图片

使用imagestringimagettftext函数将验证码文本写入图片:

$textColor = imagecolorallocate($image, 0, 0, 0);
imagestring($image, 5, 30, 12, $code, $textColor);

// 使用TrueType字体(需指定字体文件路径)
// $font = 'arial.ttf';
// imagettftext($image, 20, 0, 10, 30, $textColor, $font, $code);

添加干扰元素

增加干扰线和干扰点提高安全性:

// 干扰线
for ($i = 0; $i < 5; $i++) {
    $lineColor = imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255));
    imageline($image, rand(0, $width), rand(0, $height), rand(0, $width), rand(0, $height), $lineColor);
}

// 干扰点
for ($i = 0; $i < 100; $i++) {
    $pixelColor = imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255));
    imagesetpixel($image, rand(0, $width), rand(0, $height), $pixelColor);
}

输出图片并保存验证码

输出图片到浏览器并将验证码存入session:

header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);

// 保存验证码到session
session_start();
$_SESSION['captcha'] = $code;

完整示例代码

<?php
session_start();
$width = 120;
$height = 40;
$image = imagecreatetruecolor($width, $height);
$bgColor = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $bgColor);

$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$length = 6;
$code = '';
for ($i = 0; $i < $length; $i++) {
    $code .= $chars[rand(0, strlen($chars) - 1)];
}

$textColor = imagecolorallocate($image, 0, 0, 0);
imagestring($image, 5, 30, 12, $code, $textColor);

for ($i = 0; $i < 5; $i++) {
    $lineColor = imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255));
    imageline($image, rand(0, $width), rand(0, $height), rand(0, $width), rand(0, $height), $lineColor);
}

for ($i = 0; $i < 100; $i++) {
    $pixelColor = imagecolorallocate($image, rand(0, 255), rand(0, 255), rand(0, 255));
    imagesetpixel($image, rand(0, $width), rand(0, $height), $pixelColor);
}

$_SESSION['captcha'] = $code;
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
?>

验证码验证

在表单提交后验证用户输入:

php实现图片验证码

session_start();
if ($_POST['captcha'] === $_SESSION['captcha']) {
    echo "验证码正确";
} else {
    echo "验证码错误";
}

注意事项

确保服务器已安装GD库扩展,可通过phpinfo()查看。TrueType字体需要指定字体文件路径。为提高安全性,可考虑使用更复杂的验证码生成算法或第三方验证码库。

标签: 验证码图片
分享给朋友:

相关文章

uniapp图形验证码

uniapp图形验证码

uniapp 图形验证码实现方法 在 uniapp 中实现图形验证码功能可以通过多种方式完成,以下是几种常见的实现方案: 使用第三方验证码服务 接入第三方验证码服务如极验、腾讯云验证码等,这些服务通…

h5实现图片压缩

h5实现图片压缩

使用Canvas API压缩图片 HTML5的Canvas API可以通过调整画布尺寸和图像质量来实现图片压缩。创建一个Canvas元素,将图片绘制到Canvas上,再通过toDataURL或toBl…

H5图片实现地图

H5图片实现地图

实现H5图片地图的方法 在H5中实现图片地图功能,可以通过以下方式完成。图片地图允许用户在图片的特定区域点击,触发不同的交互效果。 使用HTML的<map>和<area>标签…

vue实现图片缓存

vue实现图片缓存

实现图片缓存的方案 在Vue项目中实现图片缓存可以通过多种方式,以下列举几种常见且有效的方法: 使用Service Worker缓存图片 通过注册Service Worker实现离线缓存,利用Cac…

vue实现图片剪切

vue实现图片剪切

Vue 实现图片裁剪的方法 在 Vue 中实现图片裁剪功能,可以通过以下两种常见方式完成: 使用第三方库 cropperjs 安装 cropperjs 库: npm install cro…

vue实现图片列表

vue实现图片列表

实现图片列表的基本方法 在Vue中实现图片列表可以通过v-for指令遍历图片数据数组,结合<img>标签动态绑定src属性。以下是基本实现步骤: <template> &…