PHP - Simple QR Code Generator

In this tutorial we will create a Simple QR Code Generator using PHP. PHP is a server-side scripting language designed primarily for web development. Using PHP, you can let your user directly interact with the script and easily to learned its syntax. It is mostly used by a newly coders for its user friendly environment. So Let's do the coding.

Before we started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. To use the Endroid/QR code library make sure you already installed the composer php library. Here's the link for the composer library https://getcomposer.org/. Then after installing the composer go to the command prompt and cd to your target project then after that enter this line composer require endroid/qr-code. After the installation you can now use the Endroid Librabry.

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into you text editor, then save it as shown below. index.php.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  5.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  6.         </head>
  7. <body>
  8.         <nav class="navbar navbar-default">
  9.                 <div class="container-fluid">
  10.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11.                 </div>
  12.         </nav>
  13.         <div class="col-md-3"></div>
  14.         <div class="col-md-6 well">
  15.                 <h3 class="text-primary">PHP - Simple QR Code Generator</h3>
  16.                 <hr style="border-top:1px dotted #ccc;"/>
  17.                 <div class="col-md-2"></div>
  18.                 <div class="col-md-8">
  19.                         <div class="form-group">
  20.                                 <form method="POST">
  21.                                         <input type="text" name="code" class="form-control"/>
  22.                                         <br >
  23.                                         <button class="btn btn-primary form-control" name="generate">Generate</button>
  24.                                 </form>
  25.                                
  26.                                 <?php
  27.                                         if(ISSET($_POST['generate'])){
  28.                                                 if($_POST['code'] != ""){
  29.                                                        
  30.                                                
  31.                                 ?>
  32.                                         <center><img src="generate.php?code=<?php echo $_POST['code']?>" alt=""></center>
  33.                                 <?php
  34.                                                 }
  35.                                         }
  36.                                 ?>
  37.                         </div>
  38.                 </div>
  39.         </div>
  40. </body>
  41. </html>

Creating the Generator function

This code contains the specific script for the upload. This code will generate a QR code from the Endroid/QrCode library everytime the user enter some inputs. To do that write these block of codes inside the Text editor and call it as generate.php.
  1. <?php
  2.         header("Content-Type: image/png");
  3.         require "vendor/autoload.php";
  4.        
  5.         use Endroid\QrCode\QrCode;
  6.        
  7.         $qrCode = new QrCode($_GET['code']);
  8.         echo $qrCode->writeString();
  9. ?>
There you have it we successfully created a Simple QR Code Generator using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!

Comments

Hello....

Add new comment