一、问题描述

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');
}

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

最新文章

  1. ABP理论学习之日志记录
  2. ios基础篇(二十五)—— Animation动画(UIView、CoreAnimation)
  3. How to take partial screenshot with Selenium WebDriver in python
  4. 转:ASP.NET MVC3 Model验证总结
  5. lower power的IP设计
  6. [讲座]【项目收集】&ldquo;清流资本&rdquo;互联网金融沙龙&mdash;&mdash;颠覆者的创新与机会
  7. OpenJudge 2757 最长上升子序列 / Poj 2533 Longest Ordered Subsequence
  8. Spring IOC的配置使用(转)
  9. [ Talk is Cheap Show me the CODE ] : jQuery Mobile工具栏
  10. vue项目使用echarts按需引入实现地图动态显示效果时,报错:TypeError: Cannot read property &#39;dataToPoint&#39; of undefined
  11. 前端bug记录
  12. 【XSY2744】信仰圣光 分治FFT 多项式exp 容斥原理
  13. linux下json工具jq
  14. OpenStack云桌面系列【2】—OpenStack和Spice
  15. 【Java】异常类处理层次
  16. Win32 HTTP Download
  17. JS的事件冒泡和事件捕获
  18. [Java多线程]-学习多线程需要来了解哪些东西?(concurrent并发包的数据结构和线程池,Locks锁,Atomic原子类)
  19. hive组件和执行过程
  20. 第二百四十四节,Bootstrap下拉菜单和滚动监听插件

热门文章

  1. java-与文件相关
  2. javascript入门教程(二):变量
  3. C# 语法糖测试--未完待续
  4. Value注解获取值一直为Null
  5. js select 删除某一项下拉列表的值
  6. sring框架的jdbc应用
  7. Qt 实现配置 OpenCV 环境,并实现打开图片与调用摄像头
  8. 在Wireshrak中使用过滤器——捕获过滤器
  9. Blazor组件提交全记录: FullScreen 全屏按钮/全屏服务 (BootstrapBlazor - Bootstrap 风格的 Blazor UI 组件库)
  10. 12-factors