Generate QR Code Dynamically using PHP

QR Code or Quick-Response code is machine-readable code consisting of squares arranged in square grid format. QR code is typically used to store information that can be readable by an imaging device (such as camera). QR codes may be used for various purposes, like displaying info to users, opening web page URLs, downloading mobile apps, adding contacts, etc. The user needs to scan QR code by device camera to view the information associated with the code.

QR code or 2-dimensional barcode can be created dynamically. You can generate QR code using PHP. In this tutorial, we will show you how to generate QR code in PHP and save images on the server. You can add text content, email, phone number, contact, URL, and other info to the QR code and generate QR barcode images with PHP.

PHP QR Code Generator Library

We will use a custom PHP library to handle the QR code generation operations. This PHP library has a dependency on the GD library. GD extension must be enabled in PHP to generate QR code images.

Before getting started, make sure the GD extension is enabled in the PHP server.

Note that: The PHP QR Code generator library files are included in the source code package.

Generate QR Code using PHP

In the following example, we will show you how to create, save, and display QR code with our custom PHP library.

Include PHP QR Code generator library:

include_once "qrcode-lib/qrlib.php";

Set the folder path where the QR images will be stored:

  • $IMG_TEMP_DIR – Folder path to save QR code image on the server.
  • $IMG_WEB_DIR – Directory location from where the QR image will be displayed on the web page.
  • $qr_file_path – Full file path to save QR image.
// Set it to a writable location, a place to store generated QR image files 
$IMG_TEMP_DIR dirname(__FILE__).DIRECTORY_SEPARATOR.'temp'.DIRECTORY_SEPARATOR;

// Create directory if not exists
if (!file_exists($IMG_TEMP_DIR)){
    
mkdir($IMG_TEMP_DIR);
}

// QR image directory
$IMG_WEB_DIR 'temp/';

// QR image file path
$qr_file_path $IMG_TEMP_DIR.'qrcode.png';

Specify configurations to set the Error Correction Level, Size, and Margin of the QR code image:

  • $errorCorrectionLevel – Specify the error correction level of the QR code.
    • Level L (Low) – 7% of data bytes can be restored.
    • Level M (Medium) – 15% of data bytes can be restored.
    • Level Q (Quartile) – 25% of data bytes can be restored.
    • Level H (High) – 30% of data bytes can be restored.
  • $matrixPointSize – Specify the size of the QR code matrix point.
  • $margin – Set the margin between the QR code and the background.
// Additional config 
$errorCorrectionLevel 'L'//'L','M','Q','H'
$matrixPointSize 10;
$margin 2;

Specify content to be stored in the QR code:

// QR code content 
$qrContent 'PHP QR Code Generated by CodexWorld :)';

Use the png() method of the QRcode class to generate a QR code with PHP:
The QR code will be generated and stored in the specified folder on the server.

QRcode::png($qrContent$qr_file_path$errorCorrectionLevel$matrixPointSize$margin);  

Display QR code in HTML:
Use <img> tag to display the QR code on the web page.

<img src="<?php echo $IMG_WEB_DIR.basename($qr_file_path); ?>" />

Create Different Types of QR Code with PHP

Here are some examples to generate different types of QR code images using PHP.

URL QR Code:
Include the protocol (HTTP or HTTPS) to recognize the QR code as a webpage URL.

$qrContent 'https://www.codexworld.com/';

App Download Link QR Code:
The Google Play Store app link should be in the following format.

$qrContent 'market://details?id=com.codexworld';

The Apple App Store app link should be in the following format.

$qrContent 'https://apps.apple.com/in/app/facebook/id284882215';

Email QR Code:
Use the following format to create a QR code with an email address.

$qrContent 'mailto:john.doe@gmail.com';

QR code for an email address with subject and body message:

  • The text used in the subject and body should be an encoded string. You can use urlencode() function to encode string in PHP.
$qrContent 'mailto:email@example.com?subject=Email%20Subject&body=Body%20goes%20here.';

Phone Number QR Code:
Use the phone number with the country code in the following format.

$qrContent 'tel:+15556667777';

SMS QR Code:
Use a phone number with a country code to send SMS/MMS to a number.

$qrContent 'sms:+18005556161';

You can pre-filled message text in SMS QR.

$qrContent 'sms:+18005556161:This%20is%20my%20text%20message.';

Contact QR Code:
Create a contact QR code with name, email, phone, and address.

$qrContent 'MECARD:N:Doe,John;ADR:76 9th Avenue, 4th Floor, New York, NY 10011;TEL:15556667777;EMAIL:john.doe@example.com;;';

Use the following format to create vCard QR code.

$qrContent ' 
    BEGIN:VCARD
    VERSION:3.0
    N:Doe;John;;;
    FN:John Doe
    TITLE:Software Engineer
    EMAIL;TYPE=INTERNET;TYPE=WORK;TYPE=PREF:john.doe@gmail.com
    URL;TYPE=Homepage:https://example.com
    END:VCARD
'
;

Conclusion

PHP QR Code generator script helps you to create QR code images dynamically using PHP. Use this custom library to generate QR code in PHP. In this example, we have created all the commonly used QR codes (URL, App, Email, Contact, MeCard, vCard, Phone, etc) with PHP. You can enhance and customize the code to integrate QR code creation functionality in the website using PHP.

Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request

Leave a reply

keyboard_double_arrow_up