PHP QR Code Generator with phpqrcode Library

by Vincy. Last modified on March 30th, 2023.

QR code (Quick Response code) is a machine-readable pictorial format containing black and white squares. It is used for storing information like URLs, product ID, etc. It is a type of matrix barcode or a two-dimensional barcode.

It is a convenient form of storing and retrieving simple data and has become even more popular with the advent of smartphones. The camera in the smartphone can act as a reader and read the QR code and help to decipher the data stored in it.

This article gives many examples if you want a solution to generate QR codes in PHP. There are a lot of PHP libraries available to generate QR codes. This article uses the PHP QR code library.

1. Quick example

This quick example returns the QR code to the browser in one line. It produces the output as a PNG stream.

require_once __DIR__ . '/vendor/phpqrcode/qrlib.php';

// Displays QR Code image to the browser
QRcode::png('PHP QR Code :)');

Installing PHP QR code library

Download the latest library version and extract it into your application vendor folder. The source code downloadable in this article has the library.

All the examples in this article have the code to include the suitable library file to use its feature.

In a previous article, we have seen code for a PHP QR code generator using the tc-lib-barcode library.

qr code data

2. Display the QR code using an HTML image

To display the QR code using an HTML image tag, link the HTML image source to the PHP file that returns the QR code PNG data.

The generate.php file returns the QR code in the below code using the PHP QrCode library. The HTML image refers to this file to show the QR code in the browser.


require_once __DIR__ . '/vendor/phpqrcode/qrlib.php';

// Displays QR Code image to the browser
QRcode::png('PHP QR Code :)');
require_once __DIR__ . '/../vendor/phpqrcode/qrlib.php';

// outputs image as PNG that can be refered to a HTML image 'src'
QRcode::png('PHP QR Code :)');


<img src="generate.php" />

2. Passing arguments to the QR code generator

Passing parameters to a QR code generator helps to perform dynamic processing about the parameter.

For example, we can pass a contact id to retrieve contact information to bundle it with the QR code output.

This example shows how to send parameters and process them in the QR generator.

The view.php file prepares the QR code parameter in PHP. Then, it sends it to the generate.php in query. This URL with the QR code parameter is specified to an HTML image source.


// initialize PHP parameter to send to the QR code generator
$QRParameter = 1234;
<img src="generate.php?id=<?php echo $QRParameter; ?>" />

In this generate.php file, it does simple manipulation to the passed argument. It prefixes a string with the GET parameter received and bundles the manipulated line to the QR code PNG.


if (empty($_GET['id'])) {
    echo "<b>ERROR:</b> Bad request. Required parameters are missing.";
} else {
    require_once __DIR__ . '/../vendor/phpqrcode/qrlib.php';
    $inputString = $_GET['id'];

    // Do not return anything to the browser
    // Process the input string
    $codeText = 'DEMO - ' . $inputString;
    // end of processing
    $debugLog = ob_get_contents();
    // outputs QR code as a PNG data

3. Save the QR code on the server

This example saves the generated QR code on the server.

It defines the QR code data and the png file name suffix in an array. Then, it uses them while creating the target to save the QR code.


require_once __DIR__ . '/vendor/phpqrcode/qrlib.php';

// how to configure silent zone (frame) size
$qrContent = array('content' => 'Contact information', 'slug' => 'contact-info');

$target = "uploads/qr-code-dir/";
$filePath = $target . 'ouput-qr-code-' . $qrContent['slug'] . '.png';

if (!file_exists($filePath)) {
    QRcode::png($qrContent['content'], $filePath);
<img src="<?php echo $filePath; ?>" />

4. Configure QR code ECC Level, Zoom factor, and Frame size

The PHP QRCode Library defines constants for different ECC levels, Zoom factor, and Frame size. These factors are used for the following purposes when generating a QR code.

This program creates QR codes in L, M, Q, and H levels with appropriate QR constants.

  • ECC level is the allowed percentage of damage without affecting reading data.
  • Zoom factor is the allowed resolution that can be changed based on the use cases.
  • Silent zone Frame size – The silent or quiet zone frame size varies based on different matrices. Setting the QR code above or equal to 4 blocks is recommended.


require_once __DIR__ . '/vendor/phpqrcode/qrlib.php';
require_once __DIR__ . '/vendor/phpqrcode/qrconst.php';

$qrContent = 'Demo data to bundle into a QR code';

$target = "uploads/ecc-level/";

// generating QR code in the 4 ECC level
QRcode::png($qrContent, $target . 'l.png', QR_ECLEVEL_L);
QRcode::png($qrContent, $target . 'm.png', QR_ECLEVEL_M);
QRcode::png($qrContent, $target . 'q.png', QR_ECLEVEL_Q);
QRcode::png($qrContent, $target . 'h.png', QR_ECLEVEL_H);

<img src="<?php echo $target; ?>l.png" />
<img src="<?php echo $target; ?>m.png" />
<img src="<?php echo $target; ?>q.png" />
<img src="<?php echo $target; ?>h.png" />

This program adds the zoom factor 1 to 4 with the QR_ECLEVEL_L ECC constant.


require_once __DIR__ . '/vendor/phpqrcode/qrlib.php';

$qrContent = 'Demo data to bundle into a QR code with zoom factor';

$target = "uploads/pixel-zoom-qr-code/";

// generating QR code with 4 ECC level and zoom factor
QRcode::png($qrContent, $target . 'l_1.png', QR_ECLEVEL_L, 1);
QRcode::png($qrContent, $target . 'l_2.png', QR_ECLEVEL_L, 2);
QRcode::png($qrContent, $target . 'l_3.png', QR_ECLEVEL_L, 3);
QRcode::png($qrContent, $target . 'l_4.png', QR_ECLEVEL_L, 4);

<img src="<?php echo $target; ?>l_1.png" />
<img src="<?php echo $target; ?>l_2.png" />
<img src="<?php echo $target; ?>l_3.png" />
<img src="<?php echo $target; ?>l_4.png" />

This program creates QR codes with all these factors ECC, Zoom, and Frame constants of the library.


require_once __DIR__ . '/vendor/phpqrcode/qrlib.php';

$qrContent = 'Demo data to bundle into a QR code with frame size';

$target = "uploads/pixel-zoom-qr-code/";

// generating

// frame config values below 4 are not recommended !!!
QRcode::png($qrContent, $target . 'l_3_4.png', QR_ECLEVEL_L, 3, 4);
QRcode::png($qrContent, $target . 'l_3_6.png', QR_ECLEVEL_L, 3, 6);
QRcode::png($qrContent, $target . 'l_3_10.png', QR_ECLEVEL_L, 3, 10);

// displaying
<img src="<?php echo $target; ?>l_3_4.png" />
<img src="<?php echo $target; ?>l_3_6.png" />
<img src="<?php echo $target; ?>l_3_10.png" />

5. Add phone, email, and contact info to a QR code

This section has PHP examples to create QR codes to attach contact information.  It prepares the QR code content with the “tel:”. “sms:” and “mail:” links and attaches it to the QR code.


require_once __DIR__ . '/vendor/phpqrcode/qrlib.php';
$target = "uploads/qr-code-phone/";
$phone = '(091)700-001-710';
// attache phone to call
$qrContent = 'tel:' . $phone;
QRcode::png($qrContent, $target . 'phone-to-call.png', QR_ECLEVEL_L, 3);
<img src="<?php echo $target; ?>phone-to-call.png" />   


require_once __DIR__ . '/vendor/phpqrcode/qrlib.php';
$target = "uploads/qr-code-phone/";
$phone = '(091)700-001-710';
// Attach the phone to text
$qrContent = 'sms:' . $phone;
// generating
QRcode::png($qrContent, $target. 'phone-to-text.png', QR_ECLEVEL_L, 3);
<img src="<?php echo $target; ?>phone-to-text.png" />

Attaching an email adds the mail subject and body as part of the mail recipient data. But the subject and the body parameters are optional.


require_once __DIR__ . '/vendor/phpqrcode/qrlib.php';
$target = "uploads/qr-code-phone/";
$recipient = '';
$mailSubject = 'Enquiry';
$mailBody = 'Post enquiry content';
// Prepare QR content with email recipient, subject and body
$qrContent = 'mailto:' . $recipient . '?subject=' . urlencode($mailSubject) . '&body=' . urlencode($mailBody);
// Attach maileto link to the QRCode
QRcode::png($qrContent, $target. 'mail.png', QR_ECLEVEL_L, 3);
<img src="<?php echo $target; ?>mail.png" /> 

This QR code example creates a v-card to bundle with the QR code. It includes basic details with the v-card.

This library allows adding more details to the QR code. For example, it can attach a contact avatar to a QR code.

In a previous article, we have seen how to generate and download a v-card for contact information.


require_once __DIR__ . '/vendor/phpqrcode/qrlib.php';
$target = "uploads/qr-code-phone/";
// Contact details
$name = 'Example1';
$phone = '(091)700-001-711';
// QR code content with VACARD
$qrCode = 'BEGIN:VCARD' . "\n";
$qrCode .= 'FN:' . $name . "\n";
$qrCode .= 'TEL;WORK;VOICE:' . $phone . "\n";
$qrCode .= 'END:VCARD';
// Attaching VCARD to QR code
QRcode::png($qrCode, $target. 'vcard-qr-code.png', QR_ECLEVEL_L, 3);
<img src="<?php echo $target; ?>vcard-qr-code.png" /> 


Leave a Reply

Your email address will not be published. Required fields are marked *

↑ Back to Top

Share this page