
A QR code is an arrangement of black and white squares – no, we are not talking about chess boards – conventionally used for storing URLs which requires scanning from mobile phone camera to read. Dotted below are the ways you can use to generate different types of QR codes in Laravel.
There aren’t many packages for generating QR codes in Laravel, but the ones there are, are really good. simple-qrcode is a Laravel package which is actually a wrapper around the BaconQrCode package for PHP.
Install the package:
- Get the simple-qrcode composer package into your Laravel project by running this in your project directory:
composer require simplesoftwareio/simple-qrcode
- NOTE: If your Laravel version is 5.4 or less
Add SimpleSoftwareIO\QrCode\QrCodeServiceProvider::class to the providers array in your config/app.php
Add the alias 'QrCode' => SimpleSoftwareIO\QrCode\Facades\QrCode::class to the aliases array in config/app.php
Reload composer auto-load cache:
composer dumpautoload
Basic usage:
- You can quickly build a QR code which contains your defined string using
QrCode::generate("string")
method.
- Add a QR-code route to your routes/web.php routes file.
Route::get('qr-code', function () {
return QrCode::size(500)->generate('Welcome to sparkouttech.com!');
});
- size() method can be used to define the size of the QR code. By default, the size is quite small so we will have to specify the size like 500 or more.
- If you visit the route, you will be able to see the QR code (Ex, localhost:8000/QR-code).
- Scan the QR code so that you can see the text.
- You can also display the QR code in Laravel’s Blade template:
{!! QrCode::generate('Welcome to sparkouttech.com!'); !!}
Encoding special data in your QR code:
- The primary function of the QR code is to hide data in plain sight. Data or information is encoded in the block and it has to be scanned to reveal the information.
- The QR codes can be encoded in different formats and most of the QR code readers can able to take action according to the encoding format.
- E-mail:
- Automatically fill-in email address, subject, and body of the email.
Syntax:
QrCode::email($to_address, $subject, $body);
Example:
//Specify email address, subject and the body
QrCode::email('sapnesh@sparkouttech.com', 'Thank you for the QR code tutorial.', 'This was awesome!.');
//Specify subject and the body, let user enter the to_address
QrCode::email(null, 'Hi there.', 'This is the message body.');
//Specify just email address
QrCode::email('sapnesh@sparkouttech.com');
Embedding the calling function in QR codes:
- The QR code is much more than just tools to hide data. For example, you have pasted your codes around an area, maybe a marketing campaign or a giveaway for 10. When scanned, the QR code automatically reverts back to the dialing function and calls the specified number. You can talk to the first 10 people who scanned for they will be the first 10 to call you.
Syntax:
QrCode::phoneNumber($number);
Example:
QrCode::phoneNumber('776-004-1698');
Embedding the messaging function in QR codes:
- Similarly, the scan automatically sends the specified message to the specified number.
Syntax:
QrCode::SMS($number, $message);
Example:
//Specify only the phone number to send the message
QrCode::SMS('555-555-5555');
//Specify phone number and message
QrCode::SMS('555-555-5555', 'Body of the message');
Embedding Geo co-ordinates in QR codes:
- When scanned, the QR code prompts the mobile device to open the map application and navigates to the given co-ordinate. It can be a good add-on in a marketing campaigns to spread the word and location of your new establishment.
Syntax:
QrCode::geo($latitude, $longitude);
QrCode::geo(13.3499, 74.798059);
Changing the Color of the QR Code:
- Even the colors of the QR code pattern and the background can be changed using color(red, green, blue)and backgroundColor(red, green, blue)methods. You can find the RGB color codes here.
Route::get('qr-code', function () {
return QrCode::backgroundColor(255, 255, 0)->color(255, 0, 127)
->size(500)->generate('Welcome to sparkouttech.com!');
});
NOTE:
- QR codes in different colors are all vibrant in theory but can look obnoxious when the color mismatch. So it is better to stick with the black and white. And the non-colored version is what works with most scanners too.
- You can also change the margin around the QR code by using the margin(5) method. Lower the value lesser the margin.
Placing an image inside the QR code:
- To place an image in the center of your QR code, use merge(‘filename.png’) Note that the function can only accept .png files.
Route::get('qr-code', function () {
$pngImage = QrCode::format('png')->merge('ss.png', 0.3, true)
->size(500)-
>errorCorrection('H')
->generate('Welcome to sparkouttech.com!');
return response($pngImage)->header('Content-type','image/png');
});
- The image can also be wrapped with a <img> tag in your blade template similar to this:
<img src="data:image/png;base64, {!! base64_encode(QrCode::format('png')->merge('ss.png', 0.3, true)
->size(500)->errorCorrection('H')
->generate('Welcome to sparkouttech.com!')) !!} ">
- By default, the merge() function will look for the file specified in public directory, so be place your .png image there
- QR code generated with Laravel is not just one of the best because of its right functions and efficiency, it also has a lot of customizing features which on deliberate action can summate to a better look.