当前位置:首页 > 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()画点。

php实现图片验证码

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()输出图像,最后销毁资源释放内存。

php实现图片验证码

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中的验证码是否一致。

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

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

相关文章

vue实现放大图片

vue实现放大图片

实现图片放大功能的方法 在Vue中实现图片放大功能,可以通过多种方式实现。以下是几种常见的方法: 使用CSS transform属性 通过CSS的transform: scale()属性实现图片放…

vue实现无缝图片

vue实现无缝图片

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

Vue实现图片 放大

Vue实现图片 放大

Vue实现图片放大功能 使用v-viewer插件 安装v-viewer插件: npm install v-viewer 在main.js中引入并配置: import Viewer from 'v-…

实现图片旋转js

实现图片旋转js

使用CSS transform属性旋转图片 通过CSS的transform属性可以轻松实现图片旋转。以下是一个简单的示例代码: const image = document.getElement…

vue实现图片合成

vue实现图片合成

Vue实现图片合成的方法 使用Vue实现图片合成可以通过HTML5的Canvas API或第三方库来完成。以下是几种常见的方法: 使用Canvas API 在Vue组件中创建一个Canvas元素,…

vue实现图片插入

vue实现图片插入

Vue 实现图片插入的方法 在 Vue 中插入图片可以通过多种方式实现,具体取决于图片的来源和用途。以下是几种常见的方法: 静态图片插入 对于项目中的静态图片,可以直接通过 src 属性引入。图片需…