PHP QRCODE generator with logo
Categories:
Generating QR Codes with Logos in PHP

Learn how to create dynamic QR codes in PHP and embed a custom logo at their center, enhancing brand recognition and user engagement.
QR codes have become ubiquitous for quickly sharing information, from website links to contact details. While standard black-and-white QR codes are functional, embedding a logo can significantly improve brand recognition and make the code more visually appealing. This article will guide you through the process of generating QR codes with an integrated logo using PHP, leveraging popular libraries to achieve a professional result.
Understanding the Core Components
To generate QR codes with logos in PHP, we typically need two main components: a QR code generation library and image manipulation capabilities. The QR code library will handle the encoding of your data into a QR code matrix, while image functions will allow us to overlay a logo onto the generated QR code image. We'll primarily use the php-qrcode-generator
library (or similar) for QR code creation and PHP's built-in GD library for image processing.
flowchart TD A[Start] --> B{Define Data & Logo Path} B --> C[Generate QR Code (PNG/JPG)] C --> D[Load QR Code Image] D --> E[Load Logo Image] E --> F{Calculate Logo Position & Size} F --> G[Overlay Logo onto QR Code] G --> H[Save/Output Final Image] H --> I[End]
Workflow for generating a QR code with an embedded logo.
Setting Up Your Environment
Before diving into the code, ensure your PHP environment is set up correctly. You'll need Composer to install the QR code library and the GD extension enabled in your php.ini
for image manipulation. Most modern PHP installations have GD enabled by default, but it's worth checking if you encounter image-related errors.
composer require chillerlan/php-qrcode
Install the chillerlan/php-qrcode
library via Composer.
Generating a Basic QR Code
First, let's generate a simple QR code without a logo to ensure our setup is working. The chillerlan/php-qrcode
library provides a straightforward API for this. You'll need to specify the data to encode and the output format.
<?php
require_once __DIR__ . '/vendor/autoload.php';
use chillerlan\QRCode\QRCode;
use chillerlan\QRCode\QROptions;
$options = new QROptions([
'outputType' => QRCode::OUTPUT_IMAGE_PNG,
'eccLevel' => QRCode::ECC_H, // Error Correction Level H (highest)
'scale' => 10,
'imageBase64' => false,
]);
$qrcode = new QRCode($options);
// Data to encode
$data = 'https://www.example.com';
// Save the QR code to a file
$filename = 'qrcode_basic.png';
$qrcode->render($data, $filename);
echo "Basic QR code generated: {$filename}\n";
?>
PHP code to generate a basic QR code image.
eccLevel
(Error Correction Level) is crucial when embedding logos. A higher ECC level (like H
) allows for more damage or obstruction (like a logo) while still being scannable. However, it also results in a denser QR code.Embedding the Logo into the QR Code
Now for the main event: integrating the logo. This involves generating the QR code, loading both the QR code and the logo images into memory, resizing the logo appropriately, and then copying the logo onto the QR code image. The logo should be small enough not to obscure critical QR code patterns, typically around 10-30% of the QR code's total width.
<?php
require_once __DIR__ . '/vendor/autoload.php';
use chillerlan\QRCode\QRCode;
use chillerlan\QRCode\QROptions;
// 1. QR Code Generation Options
$options = new QROptions([
'outputType' => QRCode::OUTPUT_IMAGE_PNG,
'eccLevel' => QRCode::ECC_H, // High error correction for logo
'scale' => 10, // Size of each module (pixel)
'imageBase64' => false,
'imageTransparent' => false, // Ensure background is not transparent initially
]);
$qrcode = new QRCode($options);
// Data to encode
$data = 'https://www.yourcompany.com/product-page';
// Generate QR code as a string (binary data)
$qrCodeData = $qrcode->render($data);
// 2. Load QR Code Image
$qrImage = imagecreatefromstring($qrCodeData);
if (!$qrImage) {
die('Failed to create QR code image from string.');
}
$qrWidth = imagesx($qrImage);
$qrHeight = imagesy($qrImage);
// 3. Load Logo Image
$logoPath = __DIR__ . '/logo.png'; // Path to your logo image
if (!file_exists($logoPath)) {
die('Logo file not found: ' . $logoPath);
}
$logoImage = imagecreatefrompng($logoPath); // Use imagecreatefromjpeg/gif for other formats
if (!$logoImage) {
die('Failed to create logo image from file.');
}
$logoWidth = imagesx($logoImage);
$logoHeight = imagesy($logoImage);
// 4. Calculate Logo Position and Size
// Logo should be about 20-25% of the QR code size
$logoDesiredWidth = $qrWidth * 0.25;
$logoDesiredHeight = ($logoDesiredWidth / $logoWidth) * $logoHeight;
// Center the logo
$logoX = ($qrWidth - $logoDesiredWidth) / 2;
$logoY = ($qrHeight - $logoDesiredHeight) / 2;
// 5. Overlay Logo onto QR Code
imagecopyresampled(
$qrImage, // Destination image
$logoImage, // Source image
(int)$logoX, // Destination X coordinate
(int)$logoY, // Destination Y coordinate
0, // Source X coordinate
0, // Source Y coordinate
(int)$logoDesiredWidth, // Destination width
(int)$logoDesiredHeight, // Destination height
$logoWidth, // Source width
$logoHeight // Source height
);
// 6. Save/Output Final Image
$outputFilename = 'qrcode_with_logo.png';
imagepng($qrImage, $outputFilename);
// Clean up memory
imagedestroy($qrImage);
imagedestroy($logoImage);
echo "QR code with logo generated: {$outputFilename}\n";
?>
Complete PHP script to generate a QR code with an embedded logo.
Best Practices for Logo Integration
When embedding logos, consider these best practices to ensure scannability and visual appeal:
- Error Correction Level: Always use a high ECC level (H or Q) to provide enough redundancy for the logo area.
- Logo Size: Keep the logo relatively small, typically occupying no more than 20-30% of the QR code's area. Larger logos increase the risk of unscannable codes.
- Logo Design: Simple, high-contrast logos work best. Avoid intricate details that might become indistinguishable when scaled down.
- Padding: Consider adding a small white border around your logo to create a clear separation from the QR code patterns, further aiding scannability.
- Testing: Always test your generated QR codes with various scanning apps and devices to confirm they are easily scannable.

A well-designed QR code with a logo, demonstrating good contrast and appropriate sizing.
By following these steps and best practices, you can create professional-looking QR codes that not only convey information but also reinforce your brand identity. This method is highly flexible and can be integrated into various PHP applications, from e-commerce platforms to event management systems.