A machine-readable code consisting of an array of black and white squares, typically used for storing URLs or other information for reading by the camera on a smartphone. QR code is used to take a piece of information.
In this article, we will generate QR code in our laravel application. We will use a simplesoftwareio/simple-qrcode
package. It is a wrapper for the laravel framework based on the work of Bacon/BaconQrCode
.
Install Package
We will use composer to install the simple-qrcode
package. We are using laravel 5.7 version. If you are installing in laravel 5.4 and below, you need to add service provider and alias.
$ composer require simplesoftwareio/simple-qrcode
Basic Usage
Now, we are ready to use simple-qrcode in our application. We will start with the basic usage. Using this is dead simple. It’s basic syntax is:
QrCode::size(300)->generate('A basic example of Simple QR code!');
To test it, we will add a route and return QR code. Simply add the following code in your web.php.
Route::get('qrcode', function () { return QrCode::size(300)->generate('A basic example of QR code!'); });
size()
function is used to specify the size of QR.
When we navigate to the route /qrcode
, we get the QR code as below:
We can also directly generate the QR code in the blade file as below:
{!! QrCode::generate('A basic example of QR code!'); !!}
Format
By default, generate
method returns a svg image. We can change the format of returned image by following way:
QrCode::format('png'); //This returns a PNG image QrCode::format('eps'); //This returns a EPS image QrCode::format('svg'); //This returns a SVG image
Please note that, format should be called before any method while chaining:
{!! base64_encode(QrCode::format('png')->generate('Example of QR code'))!!}
Size
We can change the size of QR code according to our needs.
{!!QrCode::size(200)->generate('A simple example of QR code')!!} //or![]()
Color
Similar to size, we can change the color of QR code. To change the color of QR code other than black, we use color
method and pass RBG value of color as parameter.
QrCode::color(25,12,45)->generate('A simple example of QR code');
Similarly, we can change the background of QR code as like below:
Route::get('qrcode-with-color', function () { return QrCode::size(300) ->backgroundColor(255,55,0) ->generate('A simple example of QR code'); });
The above code snippet out a QR code as screenshot below:
Note: Most of the scanners find it difficult to read the colored QR code. So, it is best to stick with the Black and White color.
Margin
This package also provides an option to change the margin around the QR code. For this, we should margin
method with value as a parameter.
QrCode::margin(20)->generate('A simple example of QR code');
Encoding
It is also possible to provide encoding of the character. The encoding
method is used to specify it.
QrCode::encoding('UTF-8')->generate('This is a QR code with special symbols ♠♥!!');
Placing Image
We can also place an image inside the QR code. The merge
method is used to place an image. A note to remember is that it only accepts png
and also need to format the response back to png.
Route::get('qrcode-with-image', function () { $image = QrCode::format('png') ->merge('images/laravel.png', 0.5, true) ->size(500)->errorCorrection('H') ->generate('A simple example of QR code!'); return response($image)->header('Content-type','image/png'); });
The above code snippet out a QR code as screenshot below:
Encoding sepcial data
With the helper of helpers
, we can cause a reader to perform certain action when scanned.
Bitcoin
We can send payment when scanned with the help of this helper function.
QrCode::BTC($address, $amount); //Sends a 0.334BTC payment to the address QrCode::BTC('bitcoin address', 0.334); //Sends a 0.334BTC payment to the address with some optional arguments QrCode::size(500)->BTC('address', 0.0034, [ 'label' => 'my label', 'message' => 'my message', 'returnAddress' => 'https://www.returnaddress.com' ]);
We can also automatically fill email
, subject
and body
when scanned.
Route::get('qrcode-with-special-data', function() { return QrCode::size(500) ->email('info@vijayrana.com.np/blog', 'Welcome to Kodementor!', 'This is !.'); });
Geo Location
We can provide longitude and latitude through the QR code and open the location in google map.
QrCode::geo(37.822214, -122.481769);
Phone Number
This package also helps to dial a number when QR code is scanned.
QrCode::phoneNumber('555-555-5555');
Text Message
We can prefilled phone number and text messages when scanned the QR code.
QrCode::SMS('555-555-5555', 'Body of the message');
Connect Wifi
This helpers makes scannable QrCodes that can connect a phone to a WiFI network.
QrCode::wiFi([ 'encryption' => 'WPA/WEP', 'ssid' => 'SSID of the network', 'password' => 'Password of the network', 'hidden' => 'Whether the network is a hidden SSID or not.' ]);
Conclustion
It’s easy to generate QR code with the help of this package. We see that we can do a lot with this package from generating a simple text QR code to Sending emails. This is a nice easy to use package. You can read it’s documentation on its official website also.
If you love this article, you may also like to read Laravel 5.7 CRUD example from scratch.
If you find this article interesting, please share. If you have any questions or feedback, please drop in the comment section below.