chillerlan / php-qrcode

A PHP QR Code generator and reader with a user-friendly API.
https://smiley.codes/qrcode/
Apache License 2.0
2.02k stars 302 forks source link

Encoding binary QR codes is broken #182

Closed 9072997 closed 1 year ago

9072997 commented 1 year ago

Describe the bug In commits before 4da90c5c3bc2b7aa1d28a31eba0c9681a892deeb it was possible to create QR codes containing binary data, such as those used by Clever. This is no longer possible.

Steps to reproduce the behavior When I do

$renderer = new chillerlan\QRCode\QRCode(
    new chillerlan\QRCode\QROptions([
        'outputType'   => chillerlan\QRCode\QRCode::OUTPUT_MARKUP_SVG,
        'eccLevel'     => chillerlan\QRCode\Common\EccLevel::M,
        'connectPaths' => true,
        'addQuietzone' => false,
        'imageBase64'  => false
    ])
);
$svg = $renderer->render(hex2bin('01015989f47dff8e852122117e04c90b9f15defc1c36477b1fe1'));

header('Content-Type: image/svg+xml');
echo $svg;

I get the error message:

PHP Fatal error:  Uncaught chillerlan\QRCode\Data\QRCodeDataException: mb_detect_encoding error in C:\inetpub\wwwroot\photomgr\vendor\chillerlan\php-qrcode\src\Data\Kanji.php:62
Stack trace:
#0 C:\inetpub\wwwroot\photomgr\vendor\chillerlan\php-qrcode\src\Data\Kanji.php(82): chillerlan\QRCode\Data\Kanji::convertEncoding()
#1 C:\inetpub\wwwroot\photomgr\vendor\chillerlan\php-qrcode\src\QRCode.php(205): chillerlan\QRCode\Data\Kanji::validateString()
#2 C:\inetpub\wwwroot\photomgr\scratch.php(13): chillerlan\QRCode\QRCode->render()
#3 {main}
  thrown in C:\inetpub\wwwroot\photomgr\vendor\chillerlan\php-qrcode\src\Data\Kanji.php on line 62

Expected behavior a QR-code like this one is generated (like it was in previous versions): image

Environment (please complete the following information):

codemasher commented 1 year ago

Hmm, this is weird, i wonder how the auto detection passed as Kanji. I've made changes to the detection recently, so i guess i'll have to revisit this. However, you can circumvent the detection issue by forcing the renderer into a certain mode as demonstrated in the multi mode example.

codemasher commented 1 year ago

Wait no, the error is not because the binary string passed but because it produced an error during conversion to SJIS, which is technically the expected behaviour.

codemasher commented 1 year ago

Hey, i've pushed a little fix in https://github.com/chillerlan/php-qrcode/commit/cc58c403f8d24343276778713579e4798fa255fa - please let me know if it works for you or causes other issues! Thanks for helping to improve this library!

9072997 commented 1 year ago

cc58c403f8d24343276778713579e4798fa255fa resolves the issue. Thanks for the tip about forcing a mode and for this handy library :)

codemasher commented 1 year ago

Happy to help!