phpqrcode library return image as a string

匿名 (未验证) 提交于 2019-12-03 08:54:24

问题:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!