ThinkPHP V6.0.12在php8.1下驗證碼出現問題
阿新 • • 發佈:2022-05-11
一、問題描述
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'); }
說明:以上是按照報錯資訊依次修改;如有大佬有更好的解決辦法歡迎評論留言