Current Location: Home> Latest Articles> How to Generate Custom QR Codes with Logo in PHP

How to Generate Custom QR Codes with Logo in PHP

M66 2025-06-12

How to Generate Custom QR Codes with Logo in PHP

QR codes have become a widely used method of information transmission in modern society, with applications in business promotions, product marketing, and personal information sharing. To make QR codes more personalized, many users prefer to embed their own logos in the codes. In this article, we will explain how to generate QR codes with logos in PHP, and provide a complete code example.

Using Zebra_QRCode Library for QR Code Generation

To generate QR codes with logos in PHP, we can use the Zebra_QRCode library. This is an open-source PHP tool for generating QR codes, which supports adding logos directly to the QR codes. It is simple to use and highly functional.

Step 1: Download and Include Zebra_QRCode Library

First, we need to download and include the Zebra_QRCode library. You can find the latest version of the library on [Zebra_QRCode GitHub Repository](https://github.com/stefanhaustein/zxing/tree/master/android-integration/src/com/google/zxing). After downloading, extract the files and place them in your project directory.

Step 2: Create PHP File and Write Code

Next, we need to write the PHP code to generate the QR code. Below is a simple code example that demonstrates how to generate a QR code with a logo:

<?php
// Include Zebra_QRCode class
require('path/to/Zebra_QRCode/Zebra_QRCode.php');

// Set the QR code content
$data = 'http://example.com';

// Create a Zebra_QRCode object
$qrCode = new Zebra_QRCode();

// Set QR code options
$qrCode->data($data);
$qrCode->imageType(Zebra_QRCode::IMAGE_TYPE_PNG);
$qrCode->matrixPointSize(10);

// Generate QR code image
$qrCode->make();

// Get the generated QR code image path
$imagePath = $qrCode->getPNG();

// Set logo image path
$logoPath = 'path/to/your/logo.png';  // Logo image path

// Add logo to QR code image
$qrCode->addLogo($imagePath, $logoPath);

// Output QR code image
header('Content-Type: image/png');
header('Content-Disposition: inline; filename="qrcode.png"');
readfile($imagePath);
?>

Code Explanation

In the above code, we first include the Zebra_QRCode class and set the QR code content ($data). Then, we create a Zebra_QRCode object and configure the QR code options, such as image type (PNG) and matrix point size. By calling the make() method, the QR code is generated. Afterward, we use the getPNG() method to retrieve the path to the generated QR code image.

Next, we set the path for the logo image and use the addLogo() method to add the logo to the QR code. Finally, we set the content type and filename using the header() function and output the QR code image to the browser with readfile().

Conclusion

With the Zebra_QRCode library, generating QR codes with personalized logos is a straightforward task. By writing just a few lines of code, you can easily combine QR codes with your brand's logo, making your QR codes stand out and reflect your personal style. We hope this article helps you get started with custom QR code generation in PHP!