一、问题描述

1、项目需求要求使用PHP8.1.*版本

2、运行程序发现验证码不生效报错如下:

二、错误描述

1、报错信息得出:从浮点(数字)到整数的隐式转换将失去精度

三、解决流程

1、找到报错文件位置

vendor\topthink\think-captcha\src\Captcha.php line 309

2、发现是第309行报错,将代码改成以下内容(也可直接替换)

    /**
* 画杂点
* 往图片上写不同颜色的字母或数字
*/
protected function writeNoise(): void
{
$codeSet = '2345678abcdefhijkmnpqrstuvwxyz';
for ($i = 0; $i < 10; $i++) {
//杂点颜色
$noiseColor = imagecolorallocate($this->im, mt_rand(150, 225), mt_rand(150, 225), mt_rand(150, 225));
for ($j = 0; $j < 5; $j++) {
// 绘杂点
imagestring($this->im, 5, mt_rand(-10, (int) $this->imageW), mt_rand(-10, (int)$this->imageH), $codeSet[mt_rand(0, 29)], $noiseColor);
}
}
}

3、此时刷新页面发现了新的报错信息(意思基本相同):

4、搜索(writeCurve)方法直接替换:

/**
* 画一条由两条连在一起构成的随机正弦函数曲线作干扰线(你可以改成更帅的曲线函数)
*
* 高中的数学公式咋都忘了涅,写出来
* 正弦型函数解析式:y=Asin(ωx+φ)+b
* 各常数值对函数图像的影响:
* A:决定峰值(即纵向拉伸压缩的倍数)
* b:表示波形在Y轴的位置关系或纵向移动距离(上加下减)
* φ:决定波形与X轴位置关系或横向移动距离(左加右减)
* ω:决定周期(最小正周期T=2π/∣ω∣)
*
*/
protected function writeCurve(): void
{
$px = $py = 0; // 曲线前部分
$A = mt_rand(1, (int) $this->imageH / 2); // 振幅
$b = mt_rand(-intval($this->imageH / 4), intval($this->imageH / 4)); // Y轴方向偏移量
$f = mt_rand(-intval($this->imageH / 4), intval($this->imageH / 4)); // X轴方向偏移量
$T = mt_rand((int) $this->imageH, intval($this->imageW * 2)); // 周期
$w = (2 * M_PI) / $T; $px1 = 0; // 曲线横坐标起始位置
$px2 = mt_rand($this->imageW / 2, $this->imageW * 0.8); // 曲线横坐标结束位置 for ($px = $px1; $px <= $px2; $px = $px + 1) {
if (0 != $w) {
$py = $A * sin($w * $px + $f) + $b + $this->imageH / 2; // y = Asin(ωx+φ) + b
$i = (int) ($this->fontSize / 5);
while ($i > 0) {
imagesetpixel($this->im, (int) $px + $i, (int) $py + $i, $this->color); // 这里(while)循环画像素点比imagettftext和imagestring用字体大小一次画出(不用这while循环)性能要好很多
$i--;
}
}
} // 曲线后部分
$A = mt_rand(1, intval($this->imageH / 2)); // 振幅
$f = mt_rand(-intval($this->imageH / 4), intval($this->imageH / 4)); // X轴方向偏移量
$T = mt_rand((int) $this->imageH, intval($this->imageW * 2)); // 周期
$w = (2 * M_PI) / $T;
$b = $py - $A * sin($w * $px + $f) - $this->imageH / 2;
$px1 = $px2;
$px2 = $this->imageW; for ($px = $px1; $px <= $px2; $px = $px + 1) {
if (0 != $w) {
$py = $A * sin($w * $px + $f) + $b + $this->imageH / 2; // y = Asin(ωx+φ) + b
$i = (int) ($this->fontSize / 5);
while ($i > 0) {
imagesetpixel($this->im, (int) $px + $i, (int) $py + $i, $this->color);
$i--;
}
}
}
}

5、最后一步,搜索(create)方法直接替换:

 /**
* 输出验证码并把验证码的值保存的session中
* @access public
* @param null|string $config
* @param bool $api
* @return Response
*/
public function create(string $config = null, bool $api = false): Response
{
$this->configure($config); $generator = $this->generate(); // 图片宽(px)
$this->imageW || $this->imageW = $this->length * $this->fontSize * 1.5 + $this->length * $this->fontSize / 2;
// 图片高(px)
$this->imageH || $this->imageH = $this->fontSize * 2.5;
// 建立一幅 $this->imageW x $this->imageH 的图像
$this->im = imagecreate((int) $this->imageW, (int) $this->imageH);
// 设置背景
imagecolorallocate($this->im, $this->bg[0], $this->bg[1], $this->bg[2]); // 验证码字体随机颜色
$this->color = imagecolorallocate($this->im, mt_rand(1, 150), mt_rand(1, 150), mt_rand(1, 150)); // 验证码使用随机字体
$ttfPath = __DIR__ . '/../assets/' . ($this->useZh ? 'zhttfs' : 'ttfs') . '/'; if (empty($this->fontttf)) {
$dir = dir($ttfPath);
$ttfs = [];
while (false !== ($file = $dir->read())) {
if ('.' != $file[0] && substr($file, -4) == '.ttf') {
$ttfs[] = $file;
}
}
$dir->close();
$this->fontttf = $ttfs[array_rand($ttfs)];
} $fontttf = $ttfPath . $this->fontttf; if ($this->useImgBg) {
$this->background();
} if ($this->useNoise) {
// 绘杂点
$this->writeNoise();
}
if ($this->useCurve) {
// 绘干扰线
$this->writeCurve();
} // 绘验证码
$text = $this->useZh ? preg_split('/(?<!^)(?!$)/u', $generator['value']) : str_split($generator['value']); // 验证码 foreach ($text as $index => $char) { $x = $this->fontSize * ($index + 1) * mt_rand((int) 1.2, (int) 1.6) * ($this->math ? 1 : 1.5);
$y = $this->fontSize + mt_rand(10, 20);
$angle = $this->math ? 0 : mt_rand(-40, 40); imagettftext($this->im, $this->fontSize, $angle, (int) $x, (int) $y, $this->color, $fontttf, $char);
} ob_start();
// 输出图像
imagepng($this->im);
$content = ob_get_clean();
imagedestroy($this->im); return response($content, 200, ['Content-Length' => strlen($content)])->contentType('image/png');
}

说明:以上是按照报错信息依次修改;如有大佬有更好的解决办法欢迎评论留言

ThinkPHP V6.0.12在php8.1下验证码出现问题的更多相关文章

  1. VM virtualBox中文版 v6.0.12.133076官方版(64/-虚拟机

    virtualBox中文版 v6.0.12.133076官方版(64/-虚拟机 http://www.onlinedown.net/soft/82464.htm http://8dx.pc6.com/ ...

  2. ThinkPHP v6.0.x 反序列化漏洞利用

    前言: 上次做了成信大的安询杯第二届CTF比赛,遇到一个tp6的题,给了源码,目的是让通过pop链审计出反序列化漏洞. 这里总结一下tp6的反序列化漏洞的利用. 0x01环境搭建 现在tp新版本的官网 ...

  3. 详解Linux下swig 3.0.12的手动安装过程

    详解Linux下swig 3.0.12的手动安装过程 首先 从http://www.linuxfromscratch.org/blfs/view/cvs/general/swig.html上下载swi ...

  4. thinkphp 5.0 lnmp环境下 无法访问,报错500(public目录)

    两种方法: 1.修改fastcgi的配置文件 /usr/local/nginx/conf/fastcgi.conf fastcgi_param PHP_ADMIN_VALUE "open_b ...

  5. centos7下安装mysql8.0.12及设置权限

    一.mysql版本介绍 mysql的官网为:https://www.mysql.com/ 在官网上可以看到多个版本,主要版本如下, 1.MySQL Community Server 社区版本,开源免费 ...

  6. windows下mysql 8.0.12安装步骤及基本使用教程

    本文实例为大家分享了windows下mysql 8.0.12安装步骤及使用教程,供大家参考,具体内容如下 补充:mysql 已经更新到了 8.0.19,大致步骤和这个差不多,照着来就完事了. 我下载的 ...

  7. Linux下Redis4.0.12安装、配置、优化

    一.安装 1.检查gcc环境 执行命令,如果Linux系统没有安装gcc编译器,会提示“Command not found” # gcc -v 安装gcc # yum -y install gcc 以 ...

  8. Windows 下 Mysql8.0.12 的安装方法

    1. 之前在windows 上面安装了 mysql 5.6 还有 mysql 5.7 遇到了几个坑 , 最近想直接安装最新版的 mysql 8.0.12(较新) 发现还是有坑 跟之前的版本不一样 这里 ...

  9. 《ThinkPHP 5.0快速入门》 数据库、查询语言

    1.数据库配置 return [ 'type' => 'mysql',// 数据库类型 'hostname' => '127.0.0.1',// 服务器地址 'database' => ...

随机推荐

  1. django-debug-toolbar 开发利器的使用教程

    django-debug-toolbar介绍 django-debug-toolbar 是一组可配置的面板,可显示有关当前请求/响应的各种调试信息,并在单击时显示有关面板内容的更多详细信息. 下载安装 ...

  2. 五款轻量型bug管理工具横向测评

    五款轻量型bug管理工具横向测评 最近正在使用的本地bug管理软件又出问题了,已经记不清这是第几次了,每次出现问题都要耗费大量的时间精力去网上寻找解决方案,劳心劳力.为了避免再次出现这样的情况,我决定 ...

  3. ios audio不能够正常播放

    ios中audio不能直接通过audio.play()播放,需要用户在click事件或者touch事件中执行audio.play()才能播放. ajax回调中audio.play()音乐不能正常播放. ...

  4. 通读Python官方文档之wsgiref(未完成)

    wsgirf-WSGI功能及参考实现 源码:Lib/wsgiref Web服务器网关接口(Web Server Gateway Interface, WSGI),是用Python写的一个服务器软件和w ...

  5. 认识 Function.prototype.bind()

    欢迎前端爱好者加入QQ群:112916679 答疑解惑,且可获取更多前端资料! bind()方法创建一个新的函数, 当被调用时,将其this关键字设置为提供的值,在调用新函数时,在任何提供之前提供一个 ...

  6. BootstrapBlazor-ValidateForm 表单验证组件

    原文链接:https://www.cnblogs.com/ysmc/p/16082279.html 故名思意,这个组件的作用我就不再多说了,配合 AutoGenerateColumnAttribute ...

  7. c++对c的拓展_命名空间_简单使用

    名字的控制:c可使用static关键字使该关键字在本单元内可见,c++则使用命名空间对名字的可见性及产生进行控制 命名空间:控制标识符的作用域(本质上就是一个作用域) 使用特点:1.必须定义在全局范围 ...

  8. springboot静态资源无法访问

    前言 今天使用springboot+layui+shiro实现一个前后端分离的商城后台系统,一个小小静态资源(image)问题搞了一下午:还好坚持了下来,否者崩溃.吐血都是小事 这是引入的路径 这是图 ...

  9. Spring-级联赋值

    一.级联赋值第一种方法 1.创建Emp类 package com.bean; public class Emp { private String EName; private String gende ...

  10. SpringMVC-获得Restful风格的参数

    使用@PathVariable注解:接收请求路径中占位符的值 @RequestMapping("/report18/{username}") @ResponseBody publi ...