Dynamically Generating a Qr Code with PHP

Dynamically generating a QR code with PHP

It's worth adding that, in addition to the QR codes library posted by @abaumg, Google provides a QR Codes API QR Codes APImany thanks to @Toukakoukan for the link update.

To use this , basically:

https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl=http%3A%2F%2Fwww.google.com%2F&choe=UTF-8
  • 300x300 is the size of the QR image you want to generate,
  • the chl is the url-encoded string you want to change into a QR code, and
  • the choe is the (optional) encoding.

The link, above, gives more detail, but to use it just have the src of an image point to the manipulated value, like so:

<img src="https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl=http%3A%2F%2Fwww.google.com%2F&choe=UTF-8" title="Link to Google.com" />

Demo:

How to generate QR CODE for dynamic generating link and logo using Simple QrCode in laravel?

You already use tag {!! and you don't need to use {{ it anymore

<img src="data:image/png;base64, {!! base64_encode(QrCode::format('png')->size(100)->generate(url('/qr_code/invoice?no_inv=' . $detail->waybill->encrypt_data)),
) !!}" style="margin-bottom: 10px;">```

Generating QR Code using PHP QrCode

The problem is here:

<html>
<?php
QRcode::png('barrda554');
?>
</html>

To understand what this is doing, imagine that you open a regular PNG file in a text editor, and just copy/paste the contents directly into your HTML file. It's not going to show an image - it'll just be garbage, like you're seeing.

To include an image in an HTML file, you need to use the <img> tag, and point to the URL of the image. In this case, the URL of the image would be a PHP script that outputs nothing except the PNG contents - like this:

<img src="qrcode.php">

And then in qrcode.php, generate the image:

<?php
include "phpqrcode/qrlib.php";
QRcode::png('barrda554');
?>

If you need some information from the HTML page in order to generate the image, you can include it as query parameters in the URL, like this:

<img src="qrcode.php?product=1&format=2">

And then get those values in your PHP like this:

<?php
include "phpqrcode/qrlib.php";
$product = $_GET['product'];
$format = $_GET['format']
// ...
// whatever you need to do to generate the proper code
// ...
QRcode::png('barrda554');
?>

And finally - there are ways to include the image data directly into the HTML page, but it's not supported by all browsers, and is not recommended because it makes the page size much larger and prevents the browser from being able to cache the image separately.

You can see more about base64 data URLs here and here.



Related Topics



Leave a reply



Submit