生成图片的时候页面显示一堆乱码,其实就是图片的传输格式.
只要定义好图片头就可以正常显示了
// 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}' />";