Generating dynamic QR codes in PHP can be done using various libraries.One of the most popular library is the PHP QR Code. Here’s a step-by-step guide on how to generate dynamic QR codes in PHP and Storing QR Code Images on the Server. You can add text content, Website URL( or any URL ), email, phone number, SMS, Business Card, and other info to the QR code and generate QR barcode images with PHP.

Php QR Code - Generating QR Codes using PHP and Storing QR Code Images on the Server

Table Of Content

1 Prerequisites

1.) PHP version of >7.4
2.) PHP GD Extension

2 Introduction

The QR code or Quick Response code is a kind of 2D barcode used to store information. You can generate QR code using PHP. In this guide, we will show you how to generate dynamic QR codes in PHP and Storing QR Code Images on the Server.

3 Create Project Folder "qrcode-app"

Create Project Folder "qrcode-app" in root directory store all project files

4 Install the PHP QR Code Library

Download PHP QR Code Library (phpqrcode) and place in "qrcode-app" project folder:
https://sourceforge.net/projects/phpqrcode/
Syntax:

QRcode::png($qrContent, $qr_image_path, $ecc, $size, $frame_Size);  

Parameters: This function accepts five parameters as mentioned above and described below:

$qrContent: This parameter gives the message which needs to be in QR code. It is mandatory parameter.
$qr_image_path: It specifies the place to store the generated QR Code.
$ecc: This parameter specifies the error correction capability of QR. Error correction level (L, M, Q, H).
$size: Matrix point size (1 - 10).
$frame_Size: This specifies the size of Qr. It is from level 1-10.

5 Examples to Create Various Types of QR Codes Using PHP

Here are some examples to generate Various types of QR code images 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 Number
Specify the Phone Number including Country Code to Generate QR Code.

$qrContent = 'tel:+16471234567';
4.SMS
Specify the Phone Number including Country Code and pre filled message to Generate QR Code.

$qrContent = 'sms:+16471234567:Samplemessage';
5.EMAIL
Specify the Email Address to Generate QR Code.

$qrContent = ''mailto:getsamplecode@gmail.com';';

6 Create Form (index.php)

In this section, we'll build the index.php file, which contains the form.
Go ahead and create the index.php file with the following codes.

<form method="post" class="pt-4" action="qr_code.php">
                  
                  <div class="input-box">
                    <label class="label-text">QR CODE Type:                    </label>
                    <div class="form-group">
                      <select class="form-control form--control" name="qrcode_type" onChange="choose_content(this.value)">
                        <option value="1">Text</option>
                        <option value="2">URL</option>
                        <option value="3">Phone Number</option>
                        <option value="4">SMS</option>
                        <option value="5">Email Simple</option>
                        <option value="6">Email Extended</option>
                        <option value="7">Skype</option>
                        <option value="8">Business Card</option>

                      </select>
                    </div>
                  </div>
                  <div id="text_content" class="qr_content" >
                  <div class="input-box">
                    <label class="label-text">Content</label>
                    <div class="form-group">
                      <input class="form-control form--control" type="text" name="qr_code_content" placeholder="Content" value="<?php echo $_REQUEST['qr_code_content'];?>" >
                     
                    </div>
                  </div>
                </div>
                <div id="phone" class="qr_content">
                  <div class="input-box">
                    <label class="label-text">Phone Number</label>
                    <div class="form-group">
                    <input class="form-control form--control" type="text" name="qr_phone_number" placeholder="Phone Number" value="<?php echo $_REQUEST['qr_phone_number'];?>">
                    </div>
                  </div>
                </div>
                

                <div id="sms"  class="qr_content">
                  <div class="input-box">
                    <label class="label-text">Phone Number</label>
                    <div class="form-group">
                    <input class="form-control form--control" type="text" name="qr_sms_phone_number" placeholder="Phone Number" value="<?php echo $_REQUEST['qr_sms_phone_number'];?>">
                    </div>
                  </div>
                 
                </div>
                <div id="email_simple"  class="qr_content">
                  <div class="input-box">
                    <label class="label-text">Email ID</label>
                    <div class="form-group">
                      <input class="form-control form--control" type="text" name="qr_email_email_id" placeholder="Email ID" value="<?php echo $_REQUEST['qr_email_email_id'];?>">
                     
                    </div>
                  </div>
                </div>
                <div id="email_extended"  class="qr_content">
                  <div class="input-box">
                    <label class="label-text">Email ID</label>
                    <div class="form-group">
                    <input class="form-control form--control" type="text" name="qr_email_extended_email_id" placeholder="Email ID" value="<?php echo $_REQUEST['qr_email_extended_email_id'];?>">
                    </div>
                  </div>
                  <div class="input-box">
                    <label class="label-text">Subject</label>
                    <div class="form-group">
                    <input class="form-control form--control" type="text" name="qr_email_extended_subject" placeholder="Subject" value="<?php echo $_REQUEST['qr_email_extended_subject'];?>">
                    </div>
                  </div>
                  <div class="input-box">
                    <label class="label-text">Message</label>
                    <div class="form-group">
                    <input class="form-control form--control" type="text" name="qr_email_extended_message" placeholder="Message" value="<?php echo $_REQUEST['qr_email_extended_message'];?>">
                    </div>
                  </div>
                </div>
                <div id="skype"  class="qr_content">
                  <div class="input-box">
                    <label class="label-text">Skype Username</label>
                    <div class="form-group">
                    <input class="form-control form--control" type="text" name="qr_skype_username" placeholder="Skype Username" value="<?php echo $_REQUEST['qr_email_extended_message'];?>">
                    </div>
                  </div>
                </div>
                <div id="business_card" <?php if($_REQUEST['qrcode_type']==8) echo 'style="display:block"';else echo 'style="display:none"';?> class="qr_content">
                  <div class="input-box">
                    <label class="label-text">First Name</label>
                    <div class="form-group">
                      <input class="form-control form--control" type="text" name="qr_business_card_first_name" placeholder="First name" value="<?php echo $_REQUEST['qr_business_card_first_name'];?>">
                    </div>
                  </div>
                  <div class="input-box">
                    <label class="label-text">Last Name</label>
                    <div class="form-group">
                      <input class="form-control form--control" type="text" name="qr_business_card_last_name" placeholder="Last name" value="<?php echo $_REQUEST['qr_business_card_last_name'];?>">
                    </div>
                  </div>
                  <div class="input-box">
                    <label class="label-text">Phone</label>
                    <div class="form-group">
                      <input class="form-control form--control" type="text" name="qr_business_card_phone" placeholder="Last name" value="<?php echo $_REQUEST['qr_business_card_phone'];?>">
                    </div>
                  </div>
                  <div class="input-box">
                    <label class="label-text">Email</label>
                    <div class="form-group">
                      <input class="form-control form--control" type="text" name="qr_business_card_email" placeholder="Email" value="<?php echo $_REQUEST['qr_business_card_email'];?>">
                    </div>
                  </div>
                  <div class="input-box">
                    <label class="label-text">Address</label>
                    <div class="form-group">
                      <input class="form-control form--control" type="text" name="qr_business_card_address" placeholder="Address" value="<?php echo $_REQUEST['qr_business_card_address'];?>">
                    </div>
                  </div>
                </div>
                  
                  <div class="btn-box" style="text-align:center">
                    <button class="btn theme-btn" type="submit" name="generate">Generate QR Code
                    </button>
                  </div>
                </form>
<script type= text/javascript>
    function choose_content(id)
    {
        $('.qr_content').hide();
        if(id==1)
        $('#text_content').show();
        else if(id==3)
        $('#phone').show();
        else if(id==4)
        $('#sms').show();
        else if(id==5)
        $('#email_simple').show();
        else if(id==6)
        $('#email_extended').show();
        else if(id==7)
        $('#skype').show();
        else if(id==8)
        $('#business_card').show()

    }
</script>                

7 Generate QR Code(qr_code.php)

In this section, we'll build the qr_code.php file, which contains the code to Generate Qr Code.
Go ahead and create the qr_code.php file with the following codes.

<?php
error_reporting(0);
include_once "phpqrcode/qrlib.php";
$show_qr_code=0;
$qr_file_path='';
if(isset($_REQUEST['generate']))
{
  if($_REQUEST['qrcode_type']==1)
  $qrContent=$_REQUEST['qr_code_content'];
  else if($_REQUEST['qrcode_type']==3)
  $qrContent='tel:+'.$_REQUEST['qr_phone_number'];
  else if($_REQUEST['qrcode_type']==4)
  $qrContent='sms:'.$_REQUEST['qr_sms_phone_number'];
  else if($_REQUEST['qrcode_type']==5)
  $qrContent='mailto:'.$_REQUEST['qr_email_email_id'];
  else if($_REQUEST['qrcode_type']==6)
  {
  $qrContent='mailto:'.$_REQUEST['qr_email_extended_email_id'].'?subject='.urlencode($_REQUEST['qr_email_extended_subject']).'&body='.urlencode($_REQUEST['qr_email_extended_message']);
  }else if($_REQUEST['qrcode_type']==7)
  $qrContent='skype:'.urlencode($_REQUEST['qr_skype_username']).'?call';
  else if($_REQUEST['qrcode_type']==8)
  {
    $qrContent  = 'BEGIN:VCARD'."\n";
    $qrContent .= 'VERSION:3.0'."\n";
    $qrContent .= 'N:'.$_REQUEST['qr_business_card_first_name']."\n";
    $qrContent .= 'FN:'.$_REQUEST['qr_business_card_last_name']."\n";  
    $qrContent .= 'TEL;TYPE=cell:'.$_REQUEST['qr_business_card_phone']."\n";
    $qrContent .= 'EMAIL:'.$_REQUEST['qr_business_card_email']."\n";
    $qrContent .= 'ADR;TYPE=HOME;'.'LABEL="'.$_REQUEST['qr_business_card_address']."\n";
    $qrContent .= 'END:VCARD';
  
  }
  
  $qr_file_path = 'qrcodepath/qrcode'.'_'.time().'.png';
  $errorCorrectionLevel = 'L'; //'L','M','Q','H' 
  $matrixPointSize = 10; 
  $margin = 2;
  QRcode::png($qrContent, $qr_file_path, $errorCorrectionLevel, $matrixPointSize, $margin);  
  $show_qr_code=1;

}
?>

8 Folder Structure

9 Run Web Server to Test the App

Visit the URL http://localhost/qrcode-app/index.php

10 Conclusion

The QR code or Quick Response code is a kind of 2D barcode used to store information. You can generate QR code using PHP. In this guide, we will show you, how to generate dynamic QR codes in PHP and Storing QR Code Images on the Server.
View Demo

Tags