This tutorial will create an example for generating a QR code using PHP. This example uses the Chillerlan
QR code library. It is a PHP library with advanced features for creating QR codes, bar codes, and more.
There are two examples in this project. The first is a basic use-case scenario, and the second is an advanced example.
Both will help familiarize this library to send data for QR code rendering.
<?php
require_once '../vendor/autoload.php';
use chillerlan\QRCode\QRCode;
// Core class for generating the QR code
$qrCode = new QRCode();
// data for which the QR code will be generated
$data = 'www.phppot.com';
// QR code image generation using render function
// it returns the an image resource.
$qrCodeImage = $qrCode->render($data);
// Show the generated QR code image on screen
// following header is necessary to show image output
// in the browser
header('Content-Type: image/png');
imagepng($qrCodeImage);
imagedestroy($qrCodeImage);
The above code is a quick example of generating a Chillerlan
QR code. You should use Composer to download all dependencies.
This example imports the library class and gives the data to generate the QR code.
The render()
function passes the data to the library with which it will output the QR code image. This output can be returned to a browser or can be saved as a file.
In a previous article, we learned how to render the generated QR code to the browser.
Run the following command in your terminal to install this Chillerlan PHP library.
composer require chillerlan/php-qrcode
More configurations help to adjust the QR code quality without affecting readability. The below parameters are used, which override the default configurations.
This library has the QROptions class to set the configurations explicitly. When initiating this class, the code below prepares an array of {version, eccLeverl ...}
options.
This QROptions instance generates the QRCode object to call the render() action handler. As in the above example, the render() uses the data and bundles it into the QR code binary.
<?php
require_once '../vendor/autoload.php';
use chillerlan\QRCode\QRCode;
use chillerlan\QRCode\QROptions;
// data to embed in the QR code image
$data = 'www.phppot.com';
// configuration options for QR code generation
// eccLevel - Error correction level (L, M, Q, H)
// scale - QR code pixe size
// imageBase64 - output as image resrouce or not
$options = new QROptions([
'version' => 5,
'eccLevel' => QRCode::ECC_H,
'scale' => 5,
'imageBase64' => true,
'imageTransparent' => false,
'foregroundColor' => '#000000',
'backgroundColor' => '#ffffff'
]);
// Instantiating the code QR code class
$qrCode = new QRCode($options);
// generating the QR code image happens here
$qrCodeImage = $qrCode->render($data);
header('Content-Type: image/png');
imagepng($qrCodeImage);
imagedestroy($qrCodeImage);
This is one of the popular QR Code generators in PHP. It has clean and easily understandable code with proper modularity.
Some of its features are listed below. This feature list represents the capability of being a component of a PHP application.
Hereafter we will see more about the QR code and its evolution, advantages, and usage scenarios.
The QR code, or the quick response code, is a two-dimensional (2D) bar code. The linked article has the code to generate a barcode using PHP.
The QR code is a Japanese invention for the automotive industry. Later it spreads to more domains. Some of the commonly used places are,
It provides easy access to online information through digital scanners. The QR code contains encoded data that can be decoded with digital scanning. It shares the information, links the service provider or prompts for the payment initiation after scanning.