生成图片的时候页面显示一堆乱码,其实就是图片的传输格式.
只要定义好图片头就可以正常显示了
// Create a basic QR code
$qrCode = new QrCode('http://www.baidu.com');
$qrCode->setSize(300);
// Set advanced options
$qrCode
->setWriterByName('png')
->setMargin(10)
->setEncoding('UTF-8')
->setErrorCorrectionLevel(ErrorCorrectionLevel::HIGH)
->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0])
->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255])
//->setLabel('Scan the code', 16, __DIR__.'/../assets/noto_sans.otf', LabelAlignment::CENTER)
//->setLogoPath(__DIR__.'/../assets/symfony.png')
->setLogoWidth(150)
->setValidateResult(false)
;
// 这里开始就是显示图片了
ob_start();//开启缓冲区
//定义图片格式头
header('Content-Type: '.$qrCode->getContentType());
echo $qrCode->writeString();
//将图片存储
$img =ob_get_contents();
ob_end_clean();
$imginfo = chunk_split(base64_encode($img));
ob_flush();
return "<img src='data:image/png;base64,{$imginfo}' />";
这篇博客介绍了在生成二维码图片时遇到的乱码问题,通过定义正确的图片头来确保图片正常显示。主要步骤包括设置图片大小、选项、编码及格式,并通过输出缓冲区和图片内容类型头来呈现图片。
5731

被折叠的 条评论
为什么被折叠?



