How to generate QR Code using php

Manas Singh 29th Jul 2020

QR code is just a conventional two dimensional barcode or a matrix barcode which is using for a quick response or to retrieve small piece of data by scanning the QR image from your phone or any QR scanner.

These days, several  recent applications are using QR code to store minimal amount of data. It is generating using black square boxes which is organize in white box and can be read from any imaging device or any scanner application. you can also read our tutorial on autocomplete search in PHP.

QR code abbreviated as (Quick Response code) is first design for automotive industry in Japan in early 1994. It is a machine readable code that persist of small amount of data which can be scanned and read it easily.

In this article, I will explain you to easily create QR code in any PHP application. Please follow the below steps to integrate it. You can view the demo and download the complete working scripts from our repository.

Create a QR code in PHP

Here we are going to use PHP as a language to create QR code because PHP is widely use for creating web application and several online industry is preferring the QR code to store some data like product details etc. Here we are using PHP QR Code library to generate QR code.

This library is open source which is based on libqrencode C library which provide API for creating QR Code images in multiple image extension. You can also read to upload multiple images and resize in PHP.

We have written a well designed code and explain each code which is easy to understand. You can also download the working code to create QR code in PHP

Importance of QR code in Web application

We are using QR code in many of our web application or web app to store the piece of data for some purpose. These days, QR code is a common word which is familiar by everyone. It is widely prefer by online or offline market from e-commerce store, digital wallet, banking etc because it is very convenient to use by any person.

Prerequisite for creating QR code in PHP

  1. Basic understanding oh PHP
  2. Enable the GD library

Code explanation for creating QR code in PHP

1. Create index.php file and put the below code. Now I will explain the code step by step below:

include('library/php_qr_code/qrlib.php'); // Include a library for PHP QR code

if(isset($_REQUEST['submit']) and $_REQUEST['submit']!=""){

//its a location where generated QR code can be stored.
$qr_code_file_path = dirname(__FILE__).DIRECTORY_SEPARATOR.'qr_assets'.DIRECTORY_SEPARATOR;
$set_qr_code_path = 'qr_assets/';

// If directory is not created, the create a new directory

//Set a file name of each generated QR code
$filename = $qr_code_file_path.time().'.png';

/* All the user generated data must be sanitize before the processing */
if (isset($_REQUEST['level']) && $_REQUEST['level']!='')
$errorCorrectionLevel = $_REQUEST['level'];

if (isset($_REQUEST['size']) && $_REQUEST['size']!='')
$matrixPointSize = $_REQUEST['size'];

$frm_link = $_REQUEST['frm_qr'];
$framePointSize = 2;
// After getting all the data, now pass all the value to generate QR code.
QRcode::png($frm_link, $filename, $errorCorrectionLevel, $matrixPointSize, $framePointSize);

In the above code, we have first include the library of PHP QR Code. Here no need to do any changes in this folder.
Now create a directory where the generated QR code will get store for further use.

You can also read : How to create and run Cron job in PHP

Then we will set the name of that image (QR code).

Now we will get all the entered data by user from below HTML form and sanitize it. We should must sanitize all the users's input data before processing it.

The next step is to call the PHP QR Code library by passing all the input data and it will create a QR code based on the input data.

Passed Parameters or argument in QRcode::png() : The above function will take 5 parameters which is mentioned:

$frm_link : This is the input parameter for which QR code need to generate.

$filename: The path or location to store the generated QR code.

$errorCorrectionLevel : It specifies the capability of error correction in QR code.

$matrixPointSize : It is the pixel size of QR code.

$framePointSize: This parameter tells the complete size of QR code.

2. Create a HTML form inside index.php or any where else

<link rel="stylesheet" href="" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

<div class="container">
<div class="row justify-content-md-center">
<div class="ml-2 col-sm-6">
<?php if(isset($frm_link) and $frm_link!=""){?>
<div class="alert alert-success">QR created for <strong>[<?php echo $frm_link;?>]</strong></div>
<div class="text-center"><img src="<?php echo $set_qr_code_path.basename($filename); ?>" /></div>
<?php } ?>
<form method="post">
<div class="form-group">
<label>Enter QR parameter</label>
<input type="text" name="frm_qr" id="frm_qr" class="form-control" placeholder="Enter QR parameter" required>
<div class="form-group">
<label>QR Code Level</label>
<select name="level" class="form-control">
<option value="L">L - smallest</option>
<option value="M" selected>M</option>
<option value="Q">Q</option>
<option value="H">H - best</option>
<div class="form-group">
<label>QR Code Size</label>
<select name="size" class="form-control">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4" selected>4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<div class="form-group">
<input type="submit" name="submit" value="Upload" class="btn btn-danger">

<script src=""></script>
<script src=""></script>
<script src=""></script>

In this HTML form we need to enter the value to generate the QR code and select some other parameter.

Now the PHP code calls the function 'QRcode::png' which is available in PHP QR code library and passes the user's input parameter. After processing the data, it generates the QR code and save it into specified location.

Below are the list of features of PHP QR Code

  1. It supports QR Code versions (size) 1-40
  2. It can export QR code to PNG, JPEG images
  3. PHP language is use to implement this QR code.
  4. It uses PHP GD2 library
  5. It is very easy to configure

Now you can store the name of QR code in database like Mysql, MongoDB table and use to display it anywhere in your application once it will create the QR code image.

Authored By Manas Singh

He is a continuous blogger and has blogged on different topic. He loves to surf Internet and always trying to get new Idea about new Technology and Innovations and sharing these great information to all the technology lovers.

Also on DiscussDesk