Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

phpqrcode library return image as a string

Tags:

php

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

like image 736
AgelessEssence Avatar asked Jul 05 '12 20:07

AgelessEssence


2 Answers

ob_start();
QRCode::png('text', null);
$imageString = base64_encode( ob_get_contents() );
ob_end_clean();
like image 149
Lusitanian Avatar answered Sep 20 '22 22:09

Lusitanian


$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.

like image 7
arik Avatar answered Sep 21 '22 22:09

arik