phpqrcode 库以字符串形式返回图像


phpqrcode library return image as a string

>http://sourceforge.net/projects/phpqrcode/,是一个很棒的库,但我找不到如何将 PNG 图像作为字符串返回,基本示例是

QRcode::png('code data text', 'filename.png'); // creates file 
QRcode::png('some othertext 1234'); // creates code image and outputs it directly into browser

我检查了文档,什么都没有,帮助! :B

ob_start();
QRCode::png('text', null);
$imageString = base64_encode( ob_get_contents() );
ob_end_clean();
$qrTempDir = 'path/to/your/temp';
$filePath = $qrTempDir.'/'.uniqid();
QRcode::png('some text', $filePath);
$qrImage = file_get_contents($filePath);
unlink($filePath);

这应该是您要找的。您可以扩展它以显示这样的图像:

<img src="data:image/png;base64,<?php echo base64_encode($qrImage) ?>" />

不幸的是,该库目前不支持任何其他方法,因为在没有 file 参数的情况下调用 QRcode::p ng 函数不仅可以发送这些标头,还可以退出代码执行,因此不会收回或覆盖标头。

我遇到了与 @iim.hlk 相同的问题

这是我@Lusitanian他对此的回答稍作修改的内容

ob_start();
QRCode::png($string);
$imageString = base64_encode( ob_get_clean() );
header('Content-Type: text/html');

这通过简单地覆盖标头问题来解决标头问题。不干净或任何东西,但它适用于目的。

这对

我有用

include '../phpqrcode/qrlib.php';
$content = "any content";
ob_start();
QRcode::png($content);
$result_qr_content_in_png = ob_get_contents();
ob_end_clean();
// PHPQRCode change the content-type into image/png... we change it again into html
header("Content-type: text/html");
$result_qr_content_in_base64 =  base64_encode($result_qr_content_in_png);

然后在您的 HTML 文件中

<img src="data:image/jpeg;base64,$result_qr_content_in_base64'"/>