Blog Detail

13

Dec
A PHP QrCode Generator with first-party support for Laravel cover image

arrow_back A PHP QrCode Generator with first-party support for Laravel

Simple QrCode is an easy-to-use wrapper for the popular Laravel framework based on the great work provided by Bacon/BaconQrCode. This package has a lot of advanced customization options and different features. You can see different examples here.

Installation

First, You’ve to install the imagick PHP extension if you plan on using the png image format.

Next, You’ve to run this command for the installation via composer.

composer require simplesoftwareio/simple-qrcode "~4"

Laravel will automatically pick up and install the package.

Usage

Basic Usage

// All examples below assume you are pulling in the QrCode facade with the following line of code. The Facade is auto-loaded for Laravel users.

use SimpleSoftwareIO\QrCode\Facades\QrCode;

Using the QrCode Generator is very easy. The most basic syntax is:

use SimpleSoftwareIO\QrCode\Facades\QrCode;

QrCode::generate('Make me into a QrCode!');

This will make a QrCode that says “Make me into a QrCode!”

Generate (string $data, string $filename = null)

Generate is used to make the QrCode.

QrCode::generate('Make me into a QrCode!');

Generate by default will return an SVG image string. You can print this directly into a modern browser within Laravel’s Blade system with the following:

{!! QrCode::generate('Make me into a QrCode!'); !!}

The generate method has a second parameter that will accept a filename and path to save the QrCode.

QrCode::generate('Make me into a QrCode!', '../public/qrcodes/qrcode.svg');

Format (string $format)
Three formats are currently supported; png, eps, and svg. To change the format use the following code:

QrCode::format('png');  //Will return a png image
QrCode::format('eps');  //Will return a eps image
QrCode::format('svg');  //Will return a svg image

imagick is required in order to generate a png image.

Size (int $size)

You can change the size of a QrCode by using the size method. Simply specify the size desired in pixels using the following syntax:

QrCode::size(100);

Color (int $red, int $green, int $blue, int $alpha = null)

Be careful when changing the color of a QrCode, as some readers have a very difficult time reading QrCodes in color.

All colors must be expressed in RGBA (Red Green Blue Alpha). You can change the color of a QrCode by using the following:

QrCode::color(255, 0, 0); // Red QrCode
QrCode::color(255, 0, 0, 25); //Red QrCode with 25% transparency 

Style (string $style, float $size = 0.5)

The style can be easily swapped out with square, dot, or round. This will change the blocks within the QrCode. The second parameter will affect the size of the dots or roundness.

QrCode::style('dot'); // Uses the `dot` style.

Margin (int $margin)

The ability to change the margin around a QrCode is also supported. Simply specify the desired margin using the following syntax:

QrCode::margin(100);

Error Correction (string $errorCorrection)

Changing the level of error correction is easy. Just use the following syntax:

QrCode::errorCorrection('H');

Encoding (string $encoding)
Change the character encoding that is used to build a QrCode. By default, ISO-8859-1 is selected as the encoder. Read more about character encoding.

You can change this to any of the following:

QrCode::encoding('UTF-8')->generate('Make me a QrCode with special symbols ♠♥!!');

Usage Outside of Laravel

You may use this package outside of Laravel by instantiating a new Generator class.

use SimpleSoftwareIO\QrCode\Generator;

$qrcode = new Generator;
$qrcode->size(500)->generate('Make a qrcode without Laravel!');

There are a lot more awesome features & options in this package. If you wanted to explore more about this package, you can visit its documentation & source code on Github.

Published at : 13-12-2021

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project