thinkphp8無(wú)法顯示驗(yàn)證碼的原因之一
安裝了thinkphp8后,在使用驗(yàn)證碼時(shí),死活不顯示,字體換了幾個(gè),路徑也正確,就是不顯示。
最后,終于發(fā)現(xiàn)一個(gè)問(wèn)題,那就是獲取的路徑的編碼有問(wèn)題,通過(guò)轉(zhuǎn)碼后,最終驗(yàn)證碼顯示出來(lái)了
給有同樣問(wèn)題的友友們一個(gè)經(jīng)驗(yàn),碰到不顯示的,也可以用我的方法試試
$fontttf = $_SERVER['DOCUMENT_ROOT'].'/static/fonts/zkcryyt.ttf';
// 轉(zhuǎn)為 GBK(Windows)
$fontGbk = iconv('UTF-8', 'GBK//IGNORE', $fontttf);
if (@file_exists($fontGbk) && @is_readable($fontGbk)) {
$fontttf = $fontGbk;
} else {
$fontttf = realpath($fontttf);
if (!$fontttf) {
$fontttf = $fontttf;
}
}
將上面的添加到\vendor\topthink\think-captcha\src\Captcha.php中的215行,并注釋掉下面的代碼
// 驗(yàn)證碼使用隨機(jī)字體
// $ttfPath = __DIR__ . '/../assets/' . ($this->useZh ? 'zhttfs' : 'ttfs') . '/';
//
// if (empty($this->fontttf)) {
// $dir = dir($ttfPath);
// $ttfs = [];
// while (false !== ($file = $dir->read())) {
// if (substr($file, -4) === '.ttf' || substr($file, -4) === '.otf') {
// $ttfs[] = $file;
// }
// }
// $dir->close();
// $this->fontttf = $ttfs[array_rand($ttfs)];
// }
//
// $fontttf = $ttfPath . $this->fontttf;
最后要注意一下你字體的路徑即可。

浙公網(wǎng)安備 33010602011771號(hào)