简体   繁体   English

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

[英]phpqrcode library return image as a string

http://sourceforge.net/projects/phpqrcode/ , is a great library but i can't find how to return the png image as a string , the basic examples are 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

i checked the documentation and nothing, help! 我检查了文档,没有,帮助! :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);

This should be what you're looking for. 这应该是你正在寻找的。 You can extend it to show an image like that: 您可以扩展它以显示如下图像:

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

Unfortunately, the library does not support any other method at the moment, because calling the QRcode::png function without the file parameter not only makes it send those headers, but it also exits the code execution, so there is no retracting or overwriting the headers. 遗憾的是,该库目前不支持任何其他方法,因为在没有file参数的情况下调用QRcode :: png函数不仅会使它发送这些头文件,而且还会退出代码执行,因此不会缩回或覆盖头。

I ran into the same problem as @iim.hlk 我遇到了与@ iim.hlk相同的问题

This is what i did slightly modified @Lusitanian his answer to this 这就是我对@Lusitanian略有修改的答案

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

This fixes the header issue by simply overwriting it. 这通过简单地覆盖它来修复标题问题。 Not clean or anything but it works for the purpose. 不干净或任何东西,但它的工作原理。

this works for me 这对我有用

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);

then in your html file 然后在你的html文件中

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM