Why Generate QR Codes with PHP?
QR codes remain essential in 2026 for quickly sharing links, contact information, payment details, Wi-Fi credentials, event tickets, product info, and more. With the continued rise of contactless interactions, dynamic and customizable QR codes are more valuable than ever for websites, mobile apps, marketing campaigns, and business tools.
This step-by-step tutorial shows you how to generate dynamic QR codes in PHP, support multiple data types (URL, plain text, phone, SMS, email, vCard/business cards, and beyond), automatically save them as high-quality PNG files directly on your server, and display the resulting QR code image instantly to the user.
We will use the modern, actively maintained endroid/qr-code library — one of the most popular and flexible PHP QR code generators available today. It is Composer-installable, supports advanced features such as:
- Custom colors (foreground/background)
- Embedded logos/icons with transparency handling
- Multiple output formats including PNG, SVG, WebP, and EPS
- Adjustable size, margin, error correction levels (Low to High)
- Encoding options and label/text overlays
Latest version (as of early 2026): 6.1.0 — fully compatible with PHP 8.4+ and built on top of reliable dependencies like bacon/bacon-qr-code for core matrix generation.
Installation is simple with Composer:
composer require endroid/qr-code
Make sure the GD extension is enabled in your PHP setup (required for raster image output like PNG). Let's get started building your own PHP QR code generator!

Table Of Content
1 Prerequisites
- PHP 8.1+ with GD extension.
- Composer installed.
- Create a project folder, run composer require endroid/qr-code.
- Create a qrcodes/ subfolder with write permissions.
2 Introduction
3 Create Project Folder "qr-generator"
4 Install the PHP QR Code Library
composer require endroid/qr-code
5 Examples to Create Various Types of QR Codes Using PHP
1.URL
Specify the website URL including the protocol (HTTP or HTTPS) to recognize the QR code as a URL.
$qrContent = 'https://getsamplecode.com/';
2.TEXT:Specify the Text to Generate QR Code.
$qrContent = 'QR Code Generated by GetSampleCode';
3.Phone NumberSpecify the Phone Number including Country Code to Generate QR Code.
$qrContent = 'tel:+16471234567';
4.SMSSpecify the Phone Number including Country Code and pre filled message to Generate QR Code.
$qrContent = 'sms:+16471234567:Samplemessage';
5.EMAILSpecify the Email Address to Generate QR Code.
$qrContent = ''mailto:getsamplecode@gmail.com';';
6 Create Form (index.php)
Go ahead and create the index.php file with the following codes.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>PHP QR Code Generator</title>
<style>
body { font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; }
.form-group { margin-bottom: 15px; }
label { display: block; margin-bottom: 5px; }
select, input, textarea { width: 100%; padding: 8px; }
button { padding: 10px 20px; background: #007bff; color: white; border: none; cursor: pointer; }
.qr-result { margin-top: 30px; text-align: center; }
</style>
</head>
<body>
<h1>Generate QR Code in PHP</h1>
<form method="POST" action="generate.php">
<div class="form-group">
<label for="type">QR Code Type:</label>
<select name="type" id="type" onchange="toggleFields(this.value)">
<option value="text">Text</option>
<option value="url">URL</option>
<option value="phone">Phone Number</option>
<option value="sms">SMS</option>
<option value="email">Email</option>
<option value="vcard">vCard (Business Card)</option>
</select>
</div>
<div class="form-group" id="text_field">
<label>Content:</label>
<textarea name="text" rows="4"></textarea>
</div>
<div class="form-group" id="url_field" style="display:none;">
<label>URL:</label>
<input type="url" name="url">
</div>
<!-- Add more fields for phone, sms, email, vcard similarly -->
<button type="submit" name="generate">Generate & Save QR Code</button>
</form>
<script>
function toggleFields(val) {
document.querySelectorAll('.form-group:not(:first-child)').forEach(el => el.style.display = 'none');
if (val === 'text') document.getElementById('text_field').style.display = 'block';
if (val === 'url') document.getElementById('url_field').style.display = 'block';
// Add JS for other types
}
toggleFields(document.getElementById('type').value);
</script>
</body>
</html>
7 Generate QR Code(generate.php)
Go ahead and create the qr_code.php file with the following codes.
<?php
require 'vendor/autoload.php';
use Endroid\QrCode\QrCode;
use Endroid\QrCode\Writer\PngWriter;
use Endroid\QrCode\Color\Color;
use Endroid\QrCode\Label\Label;
use Endroid\QrCode\Logo\Logo; // optional for logo
if (isset($_POST['generate'])) {
$type = $_POST['type'] ?? 'text';
$data = '';
// Build content based on type (sanitize inputs!)
if ($type === 'text') {
$data = trim($_POST['text'] ?? '');
} elseif ($type === 'url') {
$data = filter_var($_POST['url'] ?? '', FILTER_VALIDATE_URL) ?: '';
} // ... add logic for phone ('tel:'), sms ('sms:phone?body='), email ('mailto:'), vcard (full spec)
if (empty($data)) {
die('No valid data provided.');
}
$qrCode = QrCode::create($data)
->setSize(400)
->setMargin(10)
->setForegroundColor(new Color(0, 0, 0))
->setBackgroundColor(new Color(255, 255, 255));
// Optional: add label or logo
// $label = Label::create('Scan me!')->setTextColor(new Color(255, 0, 0));
// $qrCode->setLabel($label);
$writer = new PngWriter();
$result = $writer->write($qrCode);
// Save to server
$filename = 'qrcode_' . time() . '.png';
$path = 'qrcodes/' . $filename;
$result->saveToFile($path);
// Show result
echo '<h3>QR Code Generated!</h3>';
echo '<p>Data: ' . htmlspecialchars($data) . '</p>';
echo '<img src="' . $path . '" alt="Generated QR Code">';
echo '<p><a href="' . $path . '" download>Download Image</a></p>';
echo '<p>Saved at: ' . $path . '</p>';
} else {
header('Location: index.php');
}
?>
8 Folder Structure
qr-generator/
├── composer.json
├── vendor/ (auto-generated)
├── qrcodes/ (create this; writable)
├── index.php (form page)
└── generate.php (processing script)
9 How to Test
- Place both files in your project root.
- Install the library with Composer.
- Access index.php in your browser (e.g., http://localhost/qr-generator/index.php).
- Select a type, fill in fields, submit.
- The QR will generate, save, and display.
10 Conclusion
Written by Revathi M
PHP Developer & Technical Writer · 10+ years building web applications with CodeIgniter and Laravel
Revathi specializes in PHP backend development, authentication systems, and REST API design. She writes practical, production-tested tutorials at Get Sample Code to help developers build secure applications faster.
Frequently Asked Questions
You need PHP version greater than 7.4 and the PHP GD extension enabled.
The open-source PHP QR Code library (phpqrcode) downloaded from SourceForge.
Download it from https://sourceforge.net/projects/phpqrcode/, then place the extracted files in your project folder (e.g., as `phpqrcode/`).
Use include_once `phpqrcode/qrlib.php`; in your PHP file (e.g., `qr_code.php`).
`QRcode::png($qrContent, $qr_image_path, $ecc, $size, $margin);` where `$qr_image_path` is the server file path to save the image.
In the `qrcodepath/` directory, with filenames like `qrcode_{timestamp}.png`.
Text, URL (with http/https), Phone (`tel:`), SMS (`sms:`), Email Simple (`mailto:`), Email Extended (with subject/body), Skype (`skype:?call`), and Business Card (vCard format).
Using a dropdown and JavaScript function `choose_content(id)` that hides/shows relevant divs with class `.qr_content`.
Ensure the `qrcodepath/` directory exists and has write permissions. Also check for errors (the tutorial uses `error_reporting(0)` which hides them).
Remove or set `error_reporting(E_ALL);` to see errors. Common causes: missing GD extension, incorrect library path, or directory permissions.
Adjust parameters in `QRcode::png()`: `$ecc` (L/M/Q/H), `$matrixPointSize` (1-10), and `$margin` (frame size).
No, it uses `$_REQUEST` without sanitization. For production, add validation and use `$_POST`.
The tutorial generates the file but doesn't show it. Add <img src=`<?php echo $qr_file_path; ?>` /> in the response if `$show_qr_code == 1`.
Place files in your web server root (e.g., `qrcode-app/`), create `qrcodepath/` folder, and visit `http://localhost/qrcode-app/index.php`.
