php简单中文随机验证码

php 基础简单案例 点击随机切换验证码

效果图
在这里插入图片描述
html代码如下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Title</title>
</head>
<body>
    <div style="width: 400px; height: 200px; margin: 300px auto">
        <img src="01-CAPTCHA_image.php" onclick="this.src='01-CAPTCHA_image.php?src=' + Math.random()">
    </div>
</body>
</html>

php代码如下

<?php
header('Content-type:image/png');

//创建图片资源
$img = imagecreatetruecolor(200, 50);
//添加背景色
$bg_color = imagecolorallocate($img, 220, 220, 220);
imagefill($img, 0, 0, $bg_color);

$font = 'simhei.ttf';
//获取随机颜色
function getRandomColor($img)
{
    return ImageColorAllocate($img, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255));
}

//定义随机文字
$str = '安徽法律界艾斯芬尼率拉上覅来挖矿饭窘境发哦舒服就好金卡文件发哦';
$len = strlen($str) / 3; //获取字符长度

//随机汉字数
$char = substr($str, mt_rand(0, $len - 1) * 3, 3);
$char1 = substr($str, mt_rand(0, $len - 1) * 3, 3);


//写入文字
$str_color = imagecolorallocate($img, mt_rand(0, 100), mt_rand(0, 200), mt_rand(0, 250));
$str_color1 = imagecolorallocate($img, mt_rand(0, 100), mt_rand(0, 200), mt_rand(0, 250));

imagettftext($img, mt_rand(15, 30), mt_rand(-15, 15), 60, mt_rand(25, 35), $str_color, $font, $char);
imagettftext($img, mt_rand(15, 30), mt_rand(-15, 25), 120, mt_rand(25, 35), $str_color1, $font, $char1);


//生成干扰点
for ($i = 0; $i < 50; $i++) {
    imagestring($img, mt_rand(1, 5), mt_rand(0, 200), mt_rand(0, 50), '&', getRandomColor($img));
}

//生成干扰线
for ($i = 0; $i < 10; $i++) {
    imageline($img, mt_rand(0, 200), mt_rand(0, 50), mt_rand(0, 200), mt_rand(0, 50), getRandomColor($img));
}

//输出资源
imagepng($img);
//销毁资源
imagedestroy($img);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

恋风.

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

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

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

打赏作者

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

抵扣说明:

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

余额充值