Current Location: Home> Latest Articles> PHP QR Code Generator Tutorial: A Practical Guide for Developers

PHP QR Code Generator Tutorial: A Practical Guide for Developers

M66 2025-07-09

How to Generate QR Codes with PHP

QR codes are widely used in modern web development—for payments, URL redirections, marketing, and more. Learning how to generate them using PHP allows you to integrate powerful features into your projects. This guide walks you through the process, from setup to implementation.

Installing a PHP QR Code Library

Before writing any code, we need a library that can handle QR code generation. A great lightweight option is the PHP QR Code library, which is simple to use and reliable.

Installation steps:

// Create project directory and navigate into it
mkdir qrcode
cd qrcode

// Clone the library
git clone https://github.com/t0k4rt/phpqrcode.git

Once cloned, you'll find the main file qrlib.php inside the phpqrcode folder.

Generating a Basic QR Code

After setting up the library, you can generate a basic QR code using the following PHP snippet:

<?php
// Include the library
include "qrcode/qrlib.php";

// Define the content for the QR code
$text = "https://www.example.com";

// Define the output path
$path = "qrcode.png";

// Generate the QR code
QRcode::png($text, $path);
?>

This script generates a QR code that links to https://www.example.com and saves it as qrcode.png in your project directory.

Generating a QR Code with a Logo

If you'd like to embed a logo in the center of your QR code—for branding purposes—the following code demonstrates how:

<?php
include "qrcode/qrlib.php";

$text = "https://www.example.com";
$path = "qrcode_logo.png";
$logo = "logo.png";

// Generate the QR code
QRcode::png($text, $path, QR_ECLEVEL_H, 10, 2);

// Add the logo
addLogo($path, $logo);

function addLogo($path, $logo) {
    $QR = imagecreatefromstring(file_get_contents($path));
    $logo = imagecreatefromstring(file_get_contents($logo));

    $QR_width = imagesx($QR);
    $QR_height = imagesy($QR);
    $logo_width = imagesx($logo);
    $logo_height = imagesy($logo);

    $logo_qr_width = $QR_width / 5;
    $scale = $logo_width / $logo_qr_width;
    $logo_qr_height = $logo_height / $scale;
    $from_width = ($QR_width - $logo_qr_width) / 2;

    imagecopyresampled(
        $QR, $logo,
        $from_width, $from_width,
        0, 0,
        $logo_qr_width, $logo_qr_height,
        $logo_width, $logo_height
    );

    imagepng($QR, $path);
    imagedestroy($QR);
    imagedestroy($logo);
}
?>

This script embeds the logo.png image into the center of your QR code, which improves brand visibility while keeping the QR functional.

Conclusion

With this guide, you now know how to generate both standard and logo-enhanced QR codes using PHP. These capabilities can significantly enhance your web applications, whether you’re handling user interactions, tracking links, or simply delivering better UX. Use these examples as a foundation for more advanced implementations in your future projects.