21行PHP代码实现数字验证码功能带干扰线条和噪点

<?php
session_start();
$w=44; $h=22;
$code = rand(1000,9999);// 随机生成4位数字验证码
$_SESSION['gif_captcha'] = $code;// 保存验证码到 session 中
header('Content-Type: image/gif');// 设置 MIME 类型为 image/gif
$im = imagecreate($w, $h);// 创建一张100x30像素的新图像
imagecolorallocate($im, 255, 255, 255);// 设置图像的背景色为白色
$text_color = imagecolorallocate($im, 0, 0, 0);// 设置图像的字体颜色为黑色
imagestring($im, 8, 4, 4, $code, $text_color);// 在图像中绘制验证码
for ($i = 0; $i < 3; $i++) { // 添加干扰线 3:线条数
    $line_color = imagecolorallocate($im, rand(0, 255), rand(0, 255), rand(0, 255));
    imageline($im, rand(0, $w), rand(0, $h), rand(0, $w), rand(0, $h), $line_color);
}
for ($i = 0; $i < 30; $i++) { // 添加干扰点 30:点数
    $pixel_color = imagecolorallocate($im, rand(0, 255), rand(0, 255), rand(0, 255));
    imagesetpixel($im, rand(0, $w), rand(0, $h), $pixel_color);
}
imagegif($im);// 输出图像
imagedestroy($im);// 销毁图像
?>

为什么图片验证码使用纯数字,因为全球通用甚至文盲也会。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YUJIANYUE

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值