Generate simple Qrs codes with Laravel

QR (Quick Response) codes are an evolution of the usual two-dimensional barcodes that we see on any product we buy; these codes are capable of storing information of different types, such as numbers, texts, URLs, among others; and it is because of this versatility of having the information there but without exposing it directly that they have become the quintessential medium for sharing information; we see QRs in all kinds of products that we buy as well as on websites to easily share a resource.

To be able to translate a QR code you can do it from a smartphone with the camera application.

A QR code looks like:

Laravel QR generado

You can see the promotional video of this publication at:

Laravel, being the quintessential framework for web development in PHP, as we have talked about before, we have a huge number of third-party plugins that we can install and configure to do everything in our application, and this has been one of them; with this plugin, we can easily generate QR code in Laravel and store the QR image in any folder of the project.

In this post we are going to see how we can create a QR code generator using Laravel; for that we are going to use the following package that we can use in Laravel, since it is a package developed exclusively for this PHP framework:

Install the Simple QrCode package

To install it we have to execute the following command in our terminal, (you can go to the official page):

composer require simplesoftwareio/simple-qrcode "~4"

Remember to have your composer and the whole Laravel environment installed for all this to work.

And with this we have everything ready to start generating our QR.

Generate simple Qrs codes with Laravel

Now, from our controller, we need to create a function with the following code:

use SimpleSoftwareIO\QrCode\Facades\QrCode;
 
 
    public function qr_qenerate()
    {
        //QrCode::format('svg')->size(700)->color(255,0,0)->generate('Desarrollo libre Andres', '../public/qrcodes/qrcode.svg');
 
        QrCode::format('png')->size(700)->color(255, 0, 0)->merge('https://www.desarrollolibre.net/assets/img/logo.png', .3, true)->generate('Desarrollo libre Andres', '../public/qrcodes/qrcode.png');
    }

Explanation of the above code

We are simply using the Facade called QrCode and with this class we can create a QR, for that we specify the format of the image, which depending on our settings we can even generate in SVG or PNG whose format supports any of the settings we make.

With the color function we specify the color of the QR in hexadecimal format and of course with the generate function we send the code that we want to convert to QR.

Custom QR code

We can customize the QR a lot, through the size, format, color, among other aspects; for example, to generate a QR that we want to save in the public folder; It is worth specifying several parameters through the format function, which we can use thanks to the Facades QrCode; we have to specify:

  • The format
  • Size
  • Color
  • And the text that we want to convert to QR

And voila, with this we have the following:

Laravel QR generado

- Andrés Cruz

En español
Andrés Cruz

Develop with Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter

Andrés Cruz In Udemy

I agree to receive announcements of interest about this Blog.