可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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
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
回答1:
ob_start(); QRCode::png('text', null); $imageString = base64_encode( ob_get_contents() ); ob_end_clean();
回答2:
$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.
回答3:
I ran into the same problem as @iim.hlk
This is what i did slightly modified @Lusitanian his answer to this
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.