验证码点击立马更新,php中验证码函数

64 篇文章 4 订阅
<img class="yzm" src="../../public/yzm.php" onclick="this.src='../../public/yzm.php?rand='+Math.random()">

../../public/yzm.php这个是验证码函数的文件,把生成的验证码图片加载进来。验证码函数如下:

<?php 
session_start();
// 1.创建画布资源
$img=imagecreatetruecolor(105,50);
// 2.准备颜色
$black=imagecolorallocate($img,0,0,0);
$white=imagecolorallocate($img,255,255,255);
$red=imagecolorallocate($img,255,0,0);
$green=imagecolorallocate($img,0,255,0);
$blue=imagecolorallocate($img,0,0,255);
$gray=imagecolorallocate($img,0,125,125);
$color=imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
// 3.填充画布
imagefill($img,0,0,$green);
$arr=array_merge(range(0,9),range('a','z'),range('A','Z'));
shuffle($arr);
$str=join(array_slice($arr,0,4));
//4.干扰素
for($i=0;$i<30;$i++){
    $color=imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
    imagearc($img,mt_rand(0,150),mt_rand(0,50),mt_rand(0,150),mt_rand(0,50),mt_rand(0,360),mt_rand(0,360),$color);
}
//5.画字
$color=imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imagettftext($img,20,0,20,35,$color,'font/comicbd.ttf',$str);
$_SESSION['yzm'] = !empty($_SESSION['yzm']) ? $_SESSION['yzm'] : null;
$_SESSION['yzm']=$str;
// 6.输出最终图像或保存最终图像
header('content-type:image/png');// 图片从浏览器上输出
imagepng($img);// 把图片保存到本地
// 7.释放画布资源
imagedestroy($img); 
 ?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

SHUIPING_YANG

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

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

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

打赏作者

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

抵扣说明:

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

余额充值