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.
<?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();<?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.
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.