simple-qrcode icon indicating copy to clipboard operation
simple-qrcode copied to clipboard

Can't put logo on the qrcode when using merge or mergeString png image

Open 9khaledSayed opened this issue 2 years ago • 2 comments
trafficstars

Hello t still not working it generate an random charcters like that:- image $qrCode = QrCode::gradient(28, 181, 224, 0, 8, 81, 'horizontal') ->format('png')->merge(asset('beta-logos/small-logo.png'), .3, true) ->style('dot', 0.9) ->size(95) ->eyeColor(0, 28, 181, 224, 0, 8, 81) ->eyeColor(1, 28, 181, 224, 0, 8, 81) ->eyeColor(2, 28, 181, 224, 0, 8, 81) ->generate(route('generate_pdf', $invoice->id)); and the path of my photo is this https://beta-plus.jevara.fun/public/beta-logos/small-logo.png

9khaledSayed avatar Apr 24 '23 20:04 9khaledSayed

same issue if i use public_path instead of asset() method

code-snapshot

9khaledSayed avatar Apr 24 '23 22:04 9khaledSayed

This happens because you're using format('png') - by default this library outputs an SVG, which is just a string of XML that the browser can render natively.

But a PNG is a binary format and so you'll need an <img /> tag to render it.

But the browser can't render the binary data directly as the encoding could break your HTML, so the safest way to load the binary directly into the page is a base64-encoded string:

- $qrCode = QrCode::gradient(28, 181, 224, 0, 8, 81, 'horizontal')
+ $qrCode = base64_encode(QrCode::gradient(28, 181, 224, 0, 8, 81, 'horizontal')
    ->format('png')
    ->merge(asset('beta-logos/small-logo.png'), .3, true)
    ->style('dot', 0.9)
    ->size(95)
    ->eyeColor(0, 28, 181, 224, 0, 8, 81)
    ->eyeColor(1, 28, 181, 224, 0, 8, 81)
    ->eyeColor(2, 28, 181, 224, 0, 8, 81)
-    ->generate(route('generate_pdf', $invoice->id));
+    ->generate(route('generate_pdf', $invoice->id)));

Then you can render the image tag using an inline data flag like this

<img src="data:image/png;base64,{{ $qrCode }}" />

simonhamp avatar Aug 05 '24 22:08 simonhamp