Generate QR Codes with PHP
PHP has excellent QR code libraries like endroid/qr-code for modern web applications and chillerlan/php-qrcode for lightweight generation. Perfect for Laravel, Symfony, and WordPress.
Installation
Install PHP QR code libraries using Composer.
composer require endroid/qr-codecomposer require chillerlan/php-qrcodeGenerate QR Codes with PHP Libraries
Code examples using popular PHP QR code libraries.
Basic QR Code with endroid/qr-code
<?php
use Endroid\QrCode\QrCode;
use Endroid\QrCode\Writer\PngWriter;
$qrCode = new QrCode('https://qrcode.fun');
$qrCode->setSize(300);
$qrCode->setMargin(10);
$writer = new PngWriter();
$result = $writer->write($qrCode);
// Save to file
$result->saveToFile('qrcode.png');
// Or output directly
header('Content-Type: ' . $result->getMimeType());
echo $result->getString();QR Code with Logo (endroid)
<?php
use Endroid\QrCode\QrCode;
use Endroid\QrCode\Logo\Logo;
use Endroid\QrCode\Color\Color;
use Endroid\QrCode\Writer\PngWriter;
use Endroid\QrCode\ErrorCorrectionLevel;
$qrCode = new QrCode(
data: 'https://qrcode.fun',
size: 300,
margin: 10,
foregroundColor: new Color(26, 43, 60),
backgroundColor: new Color(255, 255, 255),
errorCorrectionLevel: ErrorCorrectionLevel::High
);
$logo = new Logo(
path: 'logo.png',
resizeToWidth: 80
);
$writer = new PngWriter();
$result = $writer->write($qrCode, $logo);
$result->saveToFile('qrcode_logo.png');Generate QR Codes via API in PHP
Call the QRCode.fun API from PHP using cURL or Guzzle.
<?php
$payload = json_encode([
'data' => 'https://qrcode.fun',
'width' => 300,
'height' => 300,
'type' => 'png',
'margin' => 10,
'dotsOptions' => ['color' => '#1A2B3C', 'type' => 'rounded'],
'cornersSquareOptions' => ['color' => '#8564C3', 'type' => 'extra-rounded'],
'backgroundOptions' => ['color' => '#FFFFFF'],
]);
$ch = curl_init('https://qrcode.fun/api/generate-qr-styled');
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $payload,
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
CURLOPT_RETURNTRANSFER => true,
]);
$response = curl_exec($ch);
curl_close($ch);
$result = json_decode($response, true);
// $result['data'] contains the base64 PNG data URL
echo substr($result['data'], 0, 50);Live QR Code Preview
Try generating a QR code with PHP right now.
Native Library vs API
Compare using a PHP QR code library versus the QRCode.fun API.
| Feature | Native Library | QRCode.fun API |
|---|---|---|
| Setup complexity | Composer install + GD/Imagick | Single HTTP request via cURL |
| Customization | Colors, logos, labels (endroid) | Full styling: colors, shapes, logos |
| Offline support | Yes | Requires internet |
| Maintenance | Composer update | Always up-to-date |
| Output formats | PNG, SVG, PDF (endroid) | PNG, SVG |
PHP QR Code Use Cases
Common scenarios for QR codes in PHP applications.
Laravel & Symfony
Generate QR codes in Laravel or Symfony controllers for user profiles, two-factor authentication, and payment links.
WordPress Plugins
Build WordPress plugins that generate QR codes for posts, products, and custom content types.
E-commerce
Create QR codes for product pages, payment gateways, order tracking, and digital receipts.
PDF Invoices
Embed QR codes into PDF invoices using TCPDF or FPDF for payment verification and document linking.
PHP QR Code Ecosystem Deep Dive
PHP powers the majority of the web, and its QR code libraries integrate naturally with popular CMS platforms and frameworks.
Laravel Integration & Service Providers
In Laravel, register endroid/qr-code as a singleton in a ServiceProvider for dependency injection throughout your application. Create a QrCodeService with methods for different output formats. Use Laravel's Response facade to return QR images with proper headers and cache-control. For queue-based generation, dispatch QR creation to Laravel Horizon workers and store results in S3 or local storage.
WordPress & WooCommerce Plugins
PHP QR code libraries power thousands of WordPress plugins. Build custom shortcodes like [qrcode data="https://..."] that render inline QR codes. In WooCommerce, generate QR codes on order confirmation for payment verification, pickup codes, and warranty tracking. WordPress's REST API can expose QR generation as a custom endpoint, enabling headless CMS architectures where the frontend requests QR codes via API.
Server Configuration & GD/Imagick Considerations
PHP QR code generation depends on the GD or Imagick extensions for raster output. Most hosting providers include GD by default, but Imagick offers better quality for print-resolution QR codes. For shared hosting with limited extensions, use chillerlan/php-qrcode which generates SVG output without any image extensions. In Docker environments, install php-gd via apt-get in your Dockerfile. For production, configure OPcache to cache compiled QR generation code for faster repeated execution.
Frequently Asked Questions
Common questions about generating QR codes with PHP.
Start generating QR codes with PHP
Use our free generator or integrate the API into your PHP applications.