当前位置:首页 > PHP

php实现图片验证码

2026-02-27 20:55:42PHP

实现图片验证码的步骤

生成随机字符串

使用rand()mt_rand()函数生成随机数,再转换为字符。也可以定义包含数字和字母的字符串,随机选取字符组合成验证码。

$chars = 'ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjklmnpqrstuvwxyz23456789';
$code = substr(str_shuffle($chars), 0, 4);

创建图像资源

使用imagecreatetruecolor()函数创建指定宽高的图像资源。背景色可通过imagecolorallocate()设置。

$width = 100;
$height = 30;
$image = imagecreatetruecolor($width, $height);
$bgColor = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $bgColor);

添加干扰元素

增加干扰线或干扰点提高安全性。使用imageline()画线或imagesetpixel()画点。

for ($i = 0; $i < 5; $i++) {
    $color = 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), $color);
}

绘制验证码文本

使用imagettftext()imagestring()将验证码文本绘制到图像上。可设置字体、颜色和位置。

$font = 'path/to/font.ttf';
for ($i = 0; $i < strlen($code); $i++) {
    $color = imagecolorallocate($image, rand(0, 100), rand(0, 100), rand(0, 100));
    imagettftext($image, 20, rand(-30, 30), 10 + $i * 20, 25, $color, $font, $code[$i]);
}

输出图像并销毁资源

设置响应头为图像类型,使用imagepng()输出图像,最后销毁资源释放内存。

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

存储验证码

将生成的验证码存入$_SESSION,便于后续验证。

session_start();
$_SESSION['captcha'] = $code;

完整示例代码

session_start();
$chars = 'ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjklmnpqrstuvwxyz23456789';
$code = substr(str_shuffle($chars), 0, 4);
$_SESSION['captcha'] = $code;

$width = 100;
$height = 30;
$image = imagecreatetruecolor($width, $height);
$bgColor = imagecolorallocate($image, 255, 255, 255);
imagefill($image, 0, 0, $bgColor);

for ($i = 0; $i < 5; $i++) {
    $color = 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), $color);
}

$font = 'arial.ttf';
for ($i = 0; $i < strlen($code); $i++) {
    $color = imagecolorallocate($image, rand(0, 100), rand(0, 100), rand(0, 100));
    imagettftext($image, 20, rand(-30, 30), 10 + $i * 20, 25, $color, $font, $code[$i]);
}

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

验证用户输入

在表单提交时,比较用户输入与$_SESSION中的验证码是否一致。

php实现图片验证码

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

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

相关文章

vue 实现图片

vue 实现图片

Vue 实现图片加载与展示的方法 在 Vue 中实现图片加载和展示可以通过多种方式完成,包括静态资源引用、动态绑定、懒加载等。以下是几种常见的方法: 静态资源引用 将图片放在项目的 public 或…

react如何渲染图片

react如何渲染图片

在React中渲染图片的方法 使用<img>标签直接引入本地图片 将图片文件放入项目目录(如src/assets),通过相对路径引入。需注意Webpack可能需要配置file-loader…

vue实现无缝图片

vue实现无缝图片

实现无缝图片轮播的方法 在Vue中实现无缝图片轮播,可以通过以下步骤完成。这种方法利用Vue的响应式特性和CSS动画,确保图片切换平滑且无缝。 使用vue-awesome-swiper库 安装vue…

vue 实现图片单选

vue 实现图片单选

Vue 实现图片单选功能 在 Vue 中实现图片单选功能,可以通过结合 v-model 和自定义事件来实现。以下是具体实现方法: 方法一:使用 v-model 和计算属性 创建一个 Vue 组件…

js 实现图片轮播

js 实现图片轮播

基础实现方案 使用HTML、CSS和JavaScript创建一个简单的图片轮播。HTML部分定义轮播容器和图片,CSS负责样式布局,JavaScript处理轮播逻辑。 <div class="…

vue 实现 图片预览

vue 实现 图片预览

实现图片预览功能 在Vue中实现图片预览功能可以通过多种方式完成,以下是几种常见的方法: 使用HTML5的FileReader API 通过FileReader读取用户上传的图片文件并显示预览:…