endroid / qr-code

QR Code Generator
https://endroid.nl
MIT License
4.46k stars 728 forks source link

How to encode raw binary data? #227

Closed Synchro closed 4 years ago

Synchro commented 4 years ago

I'm looking to embed non-text raw binary data into a QR, but I can't see any way to do that. I see that data is always passed through:

$bytes = @iconv('utf-8', $encoding, $content);

but here I don't want to do any charset-related conversion, and iconv dies complaining that it can't encode the content:

Fatal error: Uncaught BaconQrCode\Exception\WriterException: Could not encode content to in vendor/bacon/bacon-qr-code/src/Encoder/Encoder.php on line 598

As a simple example, I might want to encode a raw timestamp as 4 bytes, rather than the 10 a string representation would incur:

use Endroid\QrCode\QrCode;

$qrCode = new QrCode(pack('L', time()));

header('Content-Type: '.$qrCode->getContentType());
echo $qrCode->writeString();

I assume that there should be some value to pass in as the encoding to setEncoding, however, I can't find any indication of what values are supported. I've seen both ISO-8859-1 and UTF-8 used, but that's all - and they sound like charsets rather than encodings. Is there a 'raw binary' encoding or something?

Synchro commented 4 years ago

I solved this by using chillerlan/php-qrcode instead.