Generate QR Code in PHP
QR code (Quick Response code) is a pattern of different black shapes and squares drawn on a white background to store information for faster reading. Learn how to dynamically generate QR codes in PHP for URLs, emails and phone numbers.

QR codes were originally developed by Denso Wave to track manufacturing processes. Today they are widely used for payments, marketing and quick data sharing. In this post, we will generate QR code in PHP with multiple versions for URLs, emails, phone numbers and SMS. Files we are going to create for this post:
- index.php: Contains the HTML content with QR code images.
- qrcode.php: The PHP script to create QR codes held and maintained by Kreative Software.
- style.css: The CSS stylesheet.
PHP QR Code Generator Example (URL, Email, Phone & SMS)
The script we are using is a modified version of barcode.php created by Kreative Software. It can be used from a PHP script like below:
<?php
$generator = new QRCode('https://www.codestacked.info');
$image = $generator->render_image();
ob_start();
imagepng($image);
imagedestroy($image);
$image_content = ob_get_clean();
?>
<img src="data:image/png;base64, <?=base64_encode($image_content)?>" />
It can also be used with the GET method URL in image tags or a direct URL, which will generate a QR code image with image response headers:
qrcode.php?d=https://www.codestacked.info
Create QR Code for URL
The following URL will generate a URL QR code containing the website URL address and will launch the website on that URL in the browser.
qrcode.php?d=https://www.codestacked.info
Create QR Code for Email
The following URL will generate an Email QR code containing the email address and launch the mailing application on the device.
qrcode.php?d=mailto:[email protected]
Create QR Code for Phone Number
The following URL will generate a phone number QR code containing the phone number and will launch the caller application on the device.
qrcode.php?d=tel:+12223334444
Create QR Code for SMS
The following URL will generate an SMS QR code containing the phone number to send SMS and will launch the SMS compose application on the device.
qrcode.php?d=sms:+12223334444
How to Dynamically Generate QR Code in PHP (URL, Email, Phone & SMS)
QR Codes are more popular than barcodes as they are faster and easier to use. QR codes can be scanned by mobile phones, tablets or other devices with a camera and a QR code reading application. QR code is mostly used to make instant payments, product information scanning and for marketing and advertising, with storing the URL of a website, social media or a product page. The following is the code to create a QR code with PHP for website URL, email, phone number and SMS:
index.php
<?php include 'qrcode.php';?>
<!DOCTYPE html>
<html>
<head>
<title>Generate QR Code in PHP - Demo</title>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/>
<meta content="width=device-width, initial-scale=1, maximum-scale=1" name="viewport" />
<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<section class="section py-4">
<div class="container">
<div class="row">
<div class="col-6 md-col-auto">
<div class="mb-4">
<h3>URL QR Code</h3>
<img src="qrcode.php?d=https://www.codestacked.info" />
</div>
</div>
<div class="col-6 md-col-auto">
<div class="mb-4">
<h3>Email QR Code</h3>
<img src="qrcode.php?d=mailto:[email protected]" />
</div>
</div>
<div class="col-6 md-col-auto">
<div class="mb-4">
<h3>Phone Number QR Code</h3>
<img src="qrcode.php?d=tel:+12223334444" />
</div>
</div>
<div class="col-6 md-col-auto">
<div class="mb-4">
<h3>SMS QR Code</h3>
<img src="qrcode.php?d=sms:+12223334444" />
</div>
</div>
</div>
</div>
</section>
</body>
</html>
Add CSS Styles
Add CSS styles for the entire HTML page and QR code containers.
style.css
* {
box-sizing: border-box;
}
html,body {
margin: 0;
padding: 0;
}
body {
background-color: #f6f6f6;
font-family: "Segoe UI", "Roboto", "Helvetica", sans-serif;
font-size: 15px;
font-weight: normal;
font-style: normal;
}
.container {
max-width: 1024px;
margin: 0 auto;
padding-left: 15px;
padding-right: 15px;
}
.my-4, .mb-4 {
margin-bottom: 1rem;
}
.row {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
margin-right: -15px;
margin-left: -15px;
}
.col-6, .md-col-auto {
padding-right: 15px;
padding-left: 15px;
}
.col-6 {
-webkit-box-flex: 0;
-ms-flex: 0 0 50%;
flex: 0 0 50%;
max-width: 50%;
}
@media screen and (min-width: 768px) {
.md-col-auto {
-webkit-box-flex: 0;
-ms-flex: 0 0 auto;
flex: 0 0 auto;
width: auto;
max-width: 100%;
}
}We demonstrated how easy it is to implement QR code generation for URLs, email addresses, phone numbers and SMS in PHP applications. We used qrcode.php to generate QR codes without any complex integrations. The implementation can be further extended to meet the application's specific requirements.
Credit: Copyright for portions of qrcode.php is held by Kreative Software.