Laravel 9 QR Code Generator Basic To Advanced Tutorial Example

  • 10-08-2022
  • 1559
  • Laravel 9
  • Haresh Chauhan

laravel 9 Qr Generate Code Tutorial Example; In this laravel simple QR generate tutorial we will learn from scratch how to generate QR codes in laravel application from simple QR to advance level. It is very simple to integrate into your application you just need to install a simple QR code composer package in your application and the service provider name define in the app.php file.

To generate a QR code image we must need to install the Imagick extension in your apache server else it will not able to generate a QR image from the local server. To install the Imagick extension in your system I will suggest you also.

In this Qr code generate tutorial I have taken some examples. It is basic level to advance level QR generate example given. Also, you can add your company logo inside the QR using the merge method in this QR code generate tutorial.

So let's start integrating Qr code generated in the laravel application with the example.

Step 1: Create Project

If you don't have a laravel project, You can create one using the given command in your terminal.

composer create-project --prefer-dist laravel/laravel laravelqr

Goto project directory :

cd laravelqr

Step 2: Add QR Package

Now, We will install composer simple-QRcode in our laravel application, Use the below command in your terminal it will install the package in your application.

composer require simplesoftwareio/simple-qrcode

Step 3: Providers & Aliases

After installing the Qr package, We just need to define the service provider and aliases name in your app.php file. This file you will find in the config folder on your root directory.

config/app.php
'providers' => [
    SimpleSoftwareIO\QrCode\QrCodeServiceProvider::class
],

'aliases' => [
    'QrCode' => SimpleSoftwareIO\QrCode\Facades\QrCode::class
],

Step 4: Basic To Advance Usage

Preview :
image
image
image

Simple usage, Just import your Qr class in your coding file like "use QrCode", Add generate method in this class also with simple string params base on that which you want to create Qr code image.

use QrCode;

QrCode::generate("webappfix");

Generate your Qr with simple adding size, It will take the same width and height which you will pass in the size method and then generate. The QR will generate with a given size only.

If you are getting an Imagick extension issue while generating QR code, To solve it, Read this article also to fix it.

Error : You need to install the imagick extension to use this back end

use QrCode;

Route::get('qr-code', function () {
    return QrCode::size(200)->generate('Webappfix.com');
});

Can use in blade file something like :

{!! QrCode::size(200)->generate('Webappfix.com'); !!}

Generate your Qr with the background color, You just pass the RGB color code which you want.

Route::get('color-qr-code', function () {
    return QrCode::size(200)->backgroundColor(255,55,0)->generate('Webappfix.com');
});
Qr With Image/Logo :

If you want to add a company logo or any of your brand logos to the QR code, You can place also place the image in the QR code using the "merge('path')" method, You must need to know that this method will accept only .png extension file only and that one you also need to format the response back from the png.

You can pass the image URL or path inside the merge method. set header and return.

Route::get('qr-code-with-image', function () {
    $image = QrCode::format('png')
        ->merge('https://w3adda.com/wp-content/uploads/2019/07/laravel.png', 0.3, true)
        ->size(200)
        ->errorCorrection('H')
        ->generate('Webappfix Qr Laravel Tutorial Example');

    return response($image)->header('Content-type','image/png');
});

Show image based on base64 code in blade file. You can very simply display a QR code with an image in the blade file using the image tag <img> follow the example.

<img src="data:image/png;base64, {!! base64_encode(QrCode::format('png')->merge('https://w3adda.com/wp-content/uploads/2019/07/laravel.png', 0.3, true)
->size(200)->errorCorrection('H')
->generate('Webappfix Qr Laravel Tutorial Example')) !!} ">
Qr With Email Send:

You can make an email QR code, The QR code will automatically fill the email address, subject of the email, and body of your email. You just need to scan it not need to write the whole email address or anything else.

use QrCode;

$to_address = 'Your Email Address';
$subject = 'Email Subject';
$body = 'You Email Body';

QrCode::email($to_address, $subject, $body);

You can also make it statically and dynamic, You if wish to add an email address your self you can NULL instead of the email address.

//YOU CAN JUST ADD EMAIL ADDRESS
QrCode::email('john@w3adda.com');

// PREFIX SUBJECT AND BODY EMAIL WILL ENTERED
QrCode::email(null, 'Test message subject', 'This is message body.');

//SPECIFY EMAIL,SUBJECT,BODY
QrCode::email('john@w3adda.com', 'Thank you for visiting w3adda.com', 'Laravel Tutorial'); 

You can also encode Qr with a phone number, That will automatically open your mobile phone dialer and dial the phone number which you added in your QR code.

QR Code With Phone Number :
use QrCode;

QrCode::phoneNumber('Your Mobile Number');

Also, you can write a message with the mobile number, Pass two arguments, First your mobile number the second for the message. The QR code that opens automatically on your mobile sends SMS with the given message text.

QR with Send SMS :
use QrCode;

QrCode::SMS(Your Mobile Numbre, Your Message);

The encoded Qr with the Geo coordinates will automatically open up your map application with the given latitude and longitude location mark.

QR Code Base On Geo co-ordinates :
use QrCode;

QrCode::geo('latitude', 'longitude');
Conclusion

In this Qr code generate tutorial from scratch we have learned all that things about Qr code generated in the laravel application. I have added the install package composer name, Also added how to define service provider config in the application. Given all the examples from basic understanding to advance laravel encoded Qr code generation in today's technological world. I hope you learn lots from this tutorial.


We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :