How to generate QR Code using PHP, QR Code generator

Image for post
Image for post

QR code is just a conventional two-dimensional barcode or a matrix barcode that is using for a quick response or to retrieve a 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 a minimal amount of data. It is generating using black square boxes which are organized in the 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 the first design for the automotive industry in Japan in early 1994. It is a machine-readable code that persists of a small amount of data that can be scanned and read it easily.

In this article, I will explain to 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 used for creating web applications and several online industries is preferring the QR code to store some data like product details etc. Here we are using the PHP QR Code library to generate QR code.

This library is open-source which is based on the libqrencode C library which provides API for creating QR Code images in multiple image extensions. You can also read to upload multiple images and resize them 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 a 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 preferred by online or offline markets from e-commerce stores, digital wallets, 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:

In the above code, we have first included 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 a 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 the user from below HTML form and sanitize it. We should sanitize all the users’ input data before processing it.

The next step is to call the PHP QR Code library bypassing 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 are mentioned:

$frm_link: This is the input parameter for which QR code needs 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 the QR code.

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

2. Create an HTML form inside index.php or anywhere else

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 the PHP QR code library and passes the user's input parameter. After processing the data, it generates the QR code and saves it into the specified location.

Below is 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 used to implement this QR code.
  4. It uses the PHP GD2 library
  5. It is very easy to configure

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

Originally published at https://www.discussdesk.com

Written by

Discuss desk (www.DiscussDesk.com) is a blogging website with new technology content. Here, Users can read and comment on the latest blog.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store