php

Generate QR code using PHP

The full meaning of the QR code is the Quick Response Code. It is a matrix barcode and machine-readable code, which is used to read a small amount of data by scanning QR image with the mobile phone or QR scanner. Many open-source PHP libraries exist to generate QR code using PHP script. The coder can download any better free QR Code generator PHP library from souceforge.net. phpqrcode library is used in this tutorial to generate the QR code. How phpqrcode library can be used to generate QR code using PHP script is shown in this tutorial.

Install phpqrcode library

You have to install the phpqrcode package in your system to generate QR code using PHP script. You also need to make your system updated before installing the package.

Run the following command to update the system:

$ sudo apt-get update -y

Run the following command to install the phpqrcode on Ubuntu:

$ sudo apt-get install -y phpqrcode

Run the following command to restart the apache server:

$ sudo service apache2 restart

QRcode is one of the classes of this library that contains the png() function, which is used in the examples of this tutorial to the QR image. The syntax of this function is given below.

Syntax:

QRcode::png($text, $file, $error_correction, $pixel_size, $frame_size);

png() function can take five arguments. The first argument, $text, sets the data that will be used for generating the QR code. The second argument, $file, sets the filename to store the QR image. The third argument, $error_correction, sets the error correction capability. Four characters (L, M, Q, and H) can be used to specify the correction level. The fourth argument, $pixel_size, sets the size of the pixel. Lastly, the fifth argument, $frame_size, sets the size of QR that can be from level 1-10.

Example 1: Generate QR code and display it in the browser

The following script will generate a QR image of simple data and display the image directly into the browser. Create a PHP script with the following script.

The required script from the phpqrcode library is included at the beginning of the script. “Welcome to LinuxHint” is used as the data in the script for generating a QR image. ‘if’ condition is used to check whether the QRcode class exists in the script or not. If the class exists, then a QR image will be created, otherwise, an error message will be printed. Next, the tag is used to print the generated QR image by this script.

<?php
    //Include the necessary library for Ubuntu
    include('/usr/share/phpqrcode/qrlib.php');
    //Set the data for QR
    $code = "Welcome to LinuxHint";
    //check the class is exist or not
    if(class_exists('QRcode'))
    {
        //Generate QR
        QRcode::png($code);
    }else{
        //Print error message
        echo 'class is not loaded properly';
    }
?>
<!-- display the QR image -->
<img src="q1.php" />

Output:

The following output will appear after running the script from the webserver. You can scan the data from the image using any smart mobile or QR scanner.

Example 2: Create and store the QR image into a file

In the previous example, the QR image is printed directly in the browser and not stored as an image file. The following script will store the QR image as a PNG file. Create a PHP file with the script.

png() function is used with two arguments here. ‘PHP QR code Generator’ is used in the first argument as data for generating the QR code, and ‘QRImage.png’ is used in the second argument to store the image in a file. Next, HTML code is embedded with <img> tag to display QRImage.png file in the browser.

<?php
    //Include the necessary library for Ubuntu
    include('/usr/share/phpqrcode/qrlib.php');
    //Set the data for QR
    $text = "PHP QR Code Generator";

    //check the class is exist or not
    if(class_exists('QRcode'))
    {
        //Generate QR
        QRcode::png($text, 'QRImage.png');
    }else{
        //Print error message
        echo 'class is not loaded properly';
    }

?>
<html>
    <head>
    <title>QR Code Generator</title>
    </head>
    <body>
        <!-- display the QR image -->
        <img src="QRImage.png" />
    </body>
</html>

Output:

The following output will appear after running the script from the webserver. You can scan the data from the image using any smart mobile or QR scanner.

Example 3: Create and store the QR image into a file with size

In the previous two examples, no frame size and pixel size are defined to generate the QR image. The following script will create a QR image with the particular pixel and frame size and store the image in a PNG file. All five arguments are used in the png() function of this script. ‘Learn PHP from Linuxhint’ is set in the first argument of the png() function that is used to generate the QR image. uniqid() function is used in the script to create a PNG filename with a unique ID. ‘L’ is used for the error correction level. 12 is set for the pixel size, and 8 is set for the frame size of the QR image. Next, the filename is used in the tag to display the generated QR image of the data.

<?php
    //Include the necessary library for Ubuntu
    include('/usr/share/phpqrcode/qrlib.php');
    //Set the data for QR
    $text = "Learn PHP from Linuxhint";
    //Set the filename with unique id
    $filename = uniqid().".png";
    //Set the error correction Level('L')
    $e_correction = 'L';
    //Set pixel size
    $pixel_size = 12;
    //Set the frame size
    $frame_size = 8;
    //Generates QR image
    QRcode::png($text, $filename, $e_correction, $pixel_size, $frame_size);
    //Display the QR image
    echo "<img src='".$filename."'>";
?>

Output:

The following output will appear after running the script from the webserver. You can scan the data from the image by using any smart mobile or QR scanner.

Conclusion

Many features exist in the phpqrcode library for QR code generation. The uses of the png() function of the QRcode class are explained in this tutorial by using various examples. The ways of creating a QR image directly, saving the QR image into a file, and creating the QR image with the custom size are shown in this tutorial to help the PHP coder to create QR code easily using PHP script.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.