The complete Lavarel framework development tutorial

Powered by Blogger.

Friday 3 March 2017

Laravel- QR code generator example using Simple QrCode Package


QR codes are designed to carry a bunch of data. It has greater storage capacity and fast readability as compared to barcodes. QR codes are consists of black squares horizontally and vertically on a white background. This can be read by imaging devices like cameras. The QR codes can be used in applications where you need to track any product, to identify any item or for time tracking etc. There are a number of libraries you can use to generate your own custom QR code. Here I'm going to explain in brief how you embed a QR code generator in your laravel web application.

Laravel- QR code generator example- Configuration

We need a little configuration, to embed a QR code generator in our laravel web application. Follow the following steps-
  • Open composer.json file from your root directory and add the Simple QR package package in the require array.
    "require": {
        "simplesoftwareio/simple-qrcode": "~1"
    }

  • Run the command.
    composer update

  • Open app/config/app.php file and add the following class to the providers array.
    SimpleSoftwareIO\QrCode\QrCodeServiceProvider::class

  • Within the same file add the following line to aliases array.
    'QrCode' => SimpleSoftwareIO\QrCode\Facades\QrCode::class

Congrats, you have embedded a QR code generator to your laravel web application. Now we need to know about how we can generate custom QR codes using this QR code generator.

Laravel- QR code generator example- Usage

Generate

The generate method is used to generate a QR code. It accepts a string as a parameter.
QrCode::generate('My First QR code')

The will return a svg image string. To display a actual QR code to a blade template, you can use-
{!!QrCode::generate('My First QR code')!!}

You can also pass the php variables as an argument to the generate method.
{!!QrCode::generate($string)!!}

You can also save the QR code in your storage by passing a path to the generate method as a second parameter.
QrCode::generate('My First QR code', '../public/qrcode.svg')

It will generate a QR code of the given string and save the svg image in public folder.
Remember, if you are chaining the methods then call the generate method at the end

Format

By default generate method returns a svg image. You can change the format of the returned image of generate method using the format method.

QrCode::format('png');  //This returns a PNG image
QrCode::format('eps');  //This returns a EPS image
QrCode::format('svg');  //This returns a SVG image

The format should be called before any method, if you chaining the methods.

You an embed a png version of QR code in img tag of a laravel blade template like this-
<img src="data:image/png;base64, {!! base64_encode(QrCode::format('png')->generate('My First QR code'))!!} ">

Size

By default generate method returns a smallest pixel of image that is required to create the QR code. You can use size method to change the default size of the image using size method.

QrCode::size(200);

You can chain the above methods to display an bigger size of QR code.

{!!QrCode::size(200)->generate('My First QR code')!!}
//or
<img src="data:image/png;base64, {!! base64_encode(QrCode::format('png')->size(200)->generate('My First QR code'))!!} ">

Color Change

You can also change the color of QR code. It means the black squares can be any color. To change the color of the QR code use the color method and pass the RGB value of the color as a parameter.

QrCode::color(255,12,45)->generate('My First QR code');

Remember, some QR code readers may have difficulties to read the QR code of color that is not black.
You can also change the background color of a QR code using backgroundColor method.

QrCode::backgroundColor(255,255,0)->generate('My First QR code');

Margin

You do also have an option to change the margin around a QR code. Use the margin method to specify a margin.

QrCode::margin(50)->generate('My First QR code');

Encoding

You can change the character encoding that is being used to create the QR code. To know about the character encoding click here. By default generate method is using ISO-8859-1.

Use the encoding method to change the character encoder.

QrCode::encoding('UTF-8')->generate('This is a QR code with special symbols ♠♥!!');

 
Available Character Encoder
ISO-8859-1
ISO-8859-2
ISO-8859-3
ISO-8859-4
ISO-8859-5
ISO-8859-6
ISO-8859-7
ISO-8859-8
ISO-8859-9
ISO-8859-10
ISO-8859-11
ISO-8859-12
ISO-8859-13
ISO-8859-14
ISO-8859-15
ISO-8859-16
SHIFT-JIS
WINDOWS-1250
WINDOWS-1251
WINDOWS-1252
WINDOWS-1256
UTF-16BE
UTF-8
ASCII
GBK
EUC-KR
If you are getting an error that says Could not encode content to ISO-8859-1, then you are using the wrong character encoding type. If you are unsure about which encoder is to use then use UTF-8

Laravel- QR code generator example- Helpers

Helpers are also used to generate QR code, but it also causes the reader to perform some action whenever scanned. The Simple QrCode package supports a lot of helpers. Here I'm going to describe some of them.

E-Mail

The e-mail helper can be used to generate a QR code that fills the email fields like address, subject and body.

//pass php variables
QrCode::email($to, $subject, $body);

//pass the strings
QrCode::email('admin@w3laravel.com', 'Message Subject', 'Message body.');

Geo

It generates a QR code with latitude and longitude and opens Google Maps or any other map app when scanned.

QrCode::geo($lat, $lng);

QrCode::geo(47.8224714, 222.481769);

Phone Number

It accepts a phone number and dials the number whenever scanned.

QrCode::phoneNumber($phone);

QrCode::phoneNumber('91-0000000000');

SMS

It can be used to pre-fill the number of the receiver and the message body.

QrCode::SMS($phoneNumber, $message);

QrCode::SMS('0000000000', 'The body of the message goes here.');

Conclusion

So, in this tutorial you have learned to embed your own custom QR code generator using Simple QR code package in your Laravel web app. The simple QrCode package provide a simple and clean way to generate QR codes.

Do follow us to learn more and share to help others.

13 comments:

  1. Hello friends , here is the PHP laravel integration for QR-CODE Login , My article How to integrate on your project : http://www.rolandalla.com/laravel-login-qr-code/ My github full project: https://github.com/roladn/laravel-qr-code-login

    ReplyDelete
  2. This blog awesome and i learn a lot about programming from here.The best thing about this blog is that you doing from beginning to experts level.
    Laravel Development Services

    ReplyDelete
  3. Hello brother, we only ask for the information, we have been hours, we tried to escape in PNG format, and I could not, thanks for everything.

    ReplyDelete
  4. I am glad you take pride in what you write. This makes you stand way out from many other writers that push poorly written content. free eshop codes

    ReplyDelete
  5. Thanks for share laravel QR code generator coding it is very helpful for me. Thanks once again...
    And keep sharing more about laravel coding.

    Laravel Development Company

    ReplyDelete
  6. How to generate Qrcode and read Qrcode is the most important and the most difficult part.

    ReplyDelete
  7. Great post full of useful tips! My site is fairly new and I am also having a hard time getting my readers to leave comments. Analytics shows they are coming to the site but I have a feeling “nobody wants to be first”.
    eShop codes

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  9. How can I generate image using coordinates?

    ReplyDelete