当前位置:首页 > 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);

生成随机验证码字符串

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

php实现图片验证码

$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);

添加干扰元素

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

php实现图片验证码

// 干扰线
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);
?>

验证码验证

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

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

注意事项

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

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

相关文章

vue实现图片取色

vue实现图片取色

Vue 实现图片取色功能 使用 Canvas API 提取颜色 在 Vue 项目中,可以通过 Canvas API 实现图片取色功能。创建一个组件,利用 ref 获取图片元素并绘制到 Canvas 上…

vue实现拖动图片验证

vue实现拖动图片验证

Vue 实现拖动图片验证功能 核心思路 拖动图片验证通常需要用户将图片拖动到指定区域或按正确顺序排列。实现时需要处理拖拽事件、验证逻辑和视觉反馈。 实现步骤 1. 基础组件结构 <templ…

vue实现图片查看

vue实现图片查看

实现图片查看功能 在Vue中实现图片查看功能可以通过多种方式完成,常见的有使用原生HTML5的<dialog>元素、第三方组件库(如Element UI、Vant等)或专用插件(如view…

vue实现展示图片

vue实现展示图片

在 Vue 中展示图片的方法 静态图片展示 静态图片通常存放在项目的 public 或 assets 目录下。使用 img 标签直接引用。 <template> <img sr…

js 实现图片 放大

js 实现图片 放大

使用 CSS transform 实现图片放大 通过 CSS 的 transform: scale() 属性可以实现图片的平滑放大效果。结合 JavaScript 监听鼠标事件控制放大状态: con…

vue实现图片缓存

vue实现图片缓存

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