Generate QR Codes using PHP
QR code (Quick Response code) is the trademark for a type of matrix barcode. PHP QR Code is an open-source (LGPL) library for generating QR Code, 2-dimensional barcode. Based on libqrencode C library, provides API for creating QR Code barcode images (PNG, JPEG thanks to GD2). Implemented purely in PHP, with no external dependencies (except GD2 if needed).
In this tutorial we will explain how to Generate QR Codes using PHP. I am using
phpqrcode
library to generate QR code to store data. This is a very simple example, you can just copy paste and change according to your requirement.Features:
- Supports QR Code versions (size) 1-40
- Numeric, Alphanumeric, 8-bit and Kanji encoding. (Kanji encoding was not fully tested, if you are japan-encoding enabled you can contribute by verifing it 🙂 )
- Implemented purely in PHP, no external dependencies except GD2
- Exports to PNG, JPEG images, also exports as bit-table
- TCPDF 2-D barcode API integration
- Easy to configure
- Data cache for calculation speed-up
- Provided merge tool helps deploy library as a one big dependency-less file, simple to “include and do not wory”
- Debug data dump, error logging, time benchmarking
- 100% Open Source, LGPL Licensed
- for more detail click here
- download and Extract PHP QR Code Library
Before started to implement the Generate QR Codes using PHP, look files structure:
- generate-qr-codes-using-php
- library/phpqrcode
- css
- style.css
- images
- templates
- header.php
- footer.php
- index.php
- function.php
Create HTML Form Date input. file named index.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
<?php require_once "function.php"; include('templates/header.php'); if (!empty($_POST['level'])) { $errorCorrectionLevel = $_POST['level']; } else { $errorCorrectionLevel = 'L'; } if (!empty($_POST['size'])) { $matrixPointSize = (int)$_POST['size']; } else { $matrixPointSize = 4; } if (!empty($_POST['str_data'])){ $strData = $_POST['str_data']; } else { $strData = 'https://techarise.com/generate-qr-codes-using-php'; } ?> <form name="datetime" method="post"> <section class="showcase"> <div class="container"> <div class="pb-2 mt-4 mb-2 border-bottom"> <h2>Generate QR Codes using PHP</h2> </div> <div class="row align-items-center"> <div class="form-group col-md-8"> <label for="inputEmail4">Data</label> <input type="text" class="form-control" id="str-data" name="str_data" value="<?php if(!empty($_POST["str_data"])) { print $_POST["str_data"]; } else { print $strData; } ?>"> </div> </div> <div class="row align-items-center"> <div class="form-group col-md-4"> <label for="inputEmail4">ECC</label> <select name="level" class="form-control" id="qr-level"> <option value="L" <?php if($errorCorrectionLevel=='L') { echo 'selected';} ?> >L - smallest</option> <option value="M" <?php if($errorCorrectionLevel=='M') { echo 'selected';} ?> >M</option> <option value="Q" <?php if($errorCorrectionLevel=='Q') { echo 'selected';} ?> >Q</option> <option value="H" <?php if($errorCorrectionLevel=='H') { echo 'selected';} ?> >H - best</option> </select> </div> <div class="form-group col-md-2"> <label for="inputEmail4">Size</label> <select class="form-control" name="size" id="qr-size"> <?php for($i=1;$i<=40;$i++) { echo '<option value="'.$i.'"'.(($matrixPointSize==$i)?' selected':'').'>'.$i.'</option>'; } ?> </select> </div> <div class="col"> <button type="submit" class="btn btn-primary mt-3 float-left">Generate QR</button> </div> </div> <div class="row align-items-center"> <div class="form-group col-md-6"> <label for="inputEmail4"> <?php // generate QR Codes $qrcode = generateQRCodes($strData, $errorCorrectionLevel, $matrixPointSize, 'tmp'); echo '<img src="tmp/'.basename($qrcode).'" />'; ?> </label> </div> </div> </div> </section> </form> <?php include('templates/footer.php');?> |
Generate QR Codes Function. file named function.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
<?php function generateQRCodes($strData, $level, $size, $folder) { // include QR Codes Lib include('library/phpqrcode/qrlib.php'); //set it to writable location, a place for temp generated PNG files $PNG_TEMP_DIR = dirname(__FILE__).DIRECTORY_SEPARATOR.$folder.DIRECTORY_SEPARATOR; //html PNG location prefix $PNG_WEB_DIR = $folder.'/'; //ofcourse we need rights to create temp dir if (!file_exists($PNG_TEMP_DIR)) mkdir($PNG_TEMP_DIR); $filename = $PNG_TEMP_DIR.'test.png'; $errorCorrectionLevel = $level; $matrixPointSize = $size; if (isset($strData)) { //it's very important! if (trim($strData) == '') die('data cannot be empty! <a href="?">back</a>'); // user data $filename = $PNG_TEMP_DIR.'test'.md5($strData.'|'.$errorCorrectionLevel.'|'.$matrixPointSize).'.png'; QRcode::png($strData, $filename, $errorCorrectionLevel, $matrixPointSize, 2); } else { //default data QRcode::png($string, $filename, $errorCorrectionLevel, $matrixPointSize, 2); } // benchmark //QRtools::timeBenchmark(); return $filename; } ?> |
Create files named (header.php and footer.php)
This file contains the header and footer section of the webpage. The Bootstrap library is used to provide a better UI, so, include it in the header and footer section.
header.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <meta name="description" content=""> <meta name="author" content=""> <title>Integrate CCAvenue Payment Gateway using PHP | Tech Arise</title> <link rel="icon" type="image/ico" href="<?php print HTTP_IMAGE_PATH; ?>favicon.ico"> <!-- Bootstrap core CSS --> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css" /> <!-- Custom fonts for this template --> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.7.2/css/all.min.css" /> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/simple-line-icons/2.4.1/css/simple-line-icons.css" /> <link href="https://fonts.googleapis.com/css?family=Lato:300,400,700,300italic,400italic,700italic" rel="stylesheet" type="text/css"> <!-- Custom styles for this template --> <link href="css/style.css" rel="stylesheet"> </head> <body> <!-- Navigation --> <nav class="navbar navbar-expand-lg navbar-dark bg-dark static-top header-bg-dark" style="background: ##FFFFFF!;"> <div class="container"> <a class="navbar-brand font-weight-bold" href="https://techarise.com"><h1>Tech Arise</h1></a> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarResponsive"> <ul class="navbar-nav ml-auto"> <li class="nav-item active"> <a class="nav-link" href="https://techarise.com">Home <span class="sr-only">(current)</span> </a> </li> <li class="nav-item"> <a class="nav-link" href="https://techarise.com/php-free-script-demos/">Live Demo</a> </li> </ul> </div> </div> </nav> |
footer.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
<!-- Footer --> <footer class="footer bg-light footer-bg-dark"> <div class="container"> <div class="row"> <div class="col-lg-6 h-100 text-center text-lg-left my-auto"> <ul class="list-inline mb-2"> <li class="list-inline-item"> <a href="#">About</a> </li> <li class="list-inline-item">⋅</li> <li class="list-inline-item"> <a href="#">Contact</a> </li> <li class="list-inline-item">⋅</li> <li class="list-inline-item"> <a href="#">Terms of Use</a> </li> <li class="list-inline-item">⋅</li> <li class="list-inline-item"> <a href="#">Privacy Policy</a> </li> </ul> <p class="text-muted small mb-4 mb-lg-0">Copyright © 2011 - <?php print date('Y', time());?> <a href="https://techarise.com/">TECHARISE.COM</a> All rights reserved.</p> </div> <div class="col-lg-6 h-100 text-center text-lg-right my-auto"> <ul class="list-inline mb-0"> <li class="list-inline-item mr-3"> <a href="#"> <i class="fab fa-facebook fa-2x fa-fw"></i> </a> </li> <li class="list-inline-item mr-3"> <a href="#"> <i class="fab fa-twitter-square fa-2x fa-fw"></i> </a> </li> <li class="list-inline-item"> <a href="#"> <i class="fab fa-instagram fa-2x fa-fw"></i> </a> </li> </ul> </div> </div> </div> </footer> <script> var baseurl = "<?php print site_url();?>"; </script> <!-- Bootstrap core JavaScript --> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/js/bootstrap.bundle.min.js"></script> </body> </html> |