Generate QR code using PHP

For any developer knowing how to generating dynamic QR code and allow user to download it as image file is an important task. QR codes are playing important role in many industries like online payments, eCommerce or ticket booking. Now days sharing QR code on vising card 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 serial no or address of website. QR codes are two dimensional barcode which can be scanned by mobile phones or scanning machines.

In this article, we will see how to create or generate QR code using PHP. There are many ready to use libraries are available to create QR code easily. In 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 browser. Let's create new file and add below code to create QR code which stores simple text in it.

    $data = "123456789";

So whenever user visit this webpage it will create QR code from given text and display it to user browser.

Here, we can also store generated QR code into 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 same task. You can use those libraries as per your requirements.

Share your thoughts

Ask anything about this examples