phpqrcode库将图像作为字符串返回

Age*_*nce 6 php

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
Run Code Online (Sandbox Code Playgroud)

我检查了文档,没有,帮助!:乙

Lus*_*ian 19

ob_start();
QRCode::png('text', null);
$imageString = base64_encode( ob_get_contents() );
ob_end_clean();
Run Code Online (Sandbox Code Playgroud)

  • @ iim.hlk:如果你只是将null传递给第二个参数(`$ filename`)到`png()`方法中,那么标题将不会被发送,答案应该有效. (5认同)
  • 这有效!,我不知道为什么类开发人员不带这样的基本使用功能......无论如何,非常感谢! (3认同)
  • 因此,删除该标题或修改代码,使其不发送. (2认同)

ari*_*rik 6

$qrTempDir = 'path/to/your/temp';
$filePath = $qrTempDir.'/'.uniqid();

QRcode::png('some text', $filePath);
$qrImage = file_get_contents($filePath);

unlink($filePath);
Run Code Online (Sandbox Code Playgroud)

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

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

不幸的是,该库目前不支持任何其他方法,因为在没有file参数的情况下调用QRcode :: png函数不仅使它发送了这些标头,而且还退出了代码执行,因此不会撤消或覆盖标头。