Generate QR code using PHP

For any developer knowing how to generate dynamic QR codes and allow users to download them as the image file is an important task. QR codes are playing important role in many industries like online payments, eCommerce or ticket booking. Nowadays sharing QR code on vising cards is also very common.

The QR code stores some information in it and whenever someone scan it then they can use those data. Those data can be the serial no or addresses of website. QR codes are two-dimensional barcodes that can be scanned by mobile phones or scanning machines.

In this article, we will see how to create or generate a QR code using PHP. There are many ready-to-use libraries are available to create QR codes easily. In a further example, we will use PHP QR Code library to create QR codes.

Before starting, you need to download and extract library files. Later on we will use those files. You can use This link to download library.

Generate QR Code

Here, we simply generate different QR codes which will store some data and display it to user's browser. Let's create a new file and add below code to create a QR code that stores simple text in it.

    $data = "123456789";

So whenever user visits this webpage it will create a QR code from a given text and display it to user's browser.

Here, we can also store generated QR codes into an image file and available it user for downloading. Let's take another example where we'll create .png file of QR code.

    include 'phpqrcode/qrlib.php';
    $text = "John Wick";

    $file = 'images/'.uniqid().".png";

    $ecc = 'L';
    $pixel_Size = 10;
    $frame_Size = 10;

    // Generates QR Code and Stores it in directory given
    QRcode::png($text, $file, $ecc, $pixel_Size, $frame_size);

    // Displaying the stored QR code from directory
    echo "<center><img src='".$file."'></center>";

In this example, first of all we will create the filename and content for the QR code. We have also set pixel size and frame size for the QR code. At last, we have created a QR code and displayed it as an image to the user. Whenever a user runs this script it will create a new png file to our server and display it to the user.


In this article, we have taken some examples which demonstrate you to create QR codes using PHP with the PHP QR code library. There are many libraries are available to perform the same task. You can use those libraries as per your requirements.