How to Generate Barcode Generator using PHP
Barcode is a machine-readable code in the form of numbers and a pattern of parallel lines of varying widths. Barcodes are often used to help organize and index information or prices about an object. The Barcode is very useful for fast-selling items to identify items quickly and also useful to manage inventory to prevent inventory build-up for slow selling items. As the Barcode is very useful and needed to implement in many PHP projects to manage inventories etc. In this tutorial, we will explain how to Generate Barcode Generator using PHP. This is a very simple example, you can just copy-paste, and change according to your requirement.
download Barcode generate PHP library to create Barcode.
Before started to implement the Generate Barcode Generator using PHP, look files structure:
- generate-barcode-using-php
- css
- style.css
- vendor
- templates
- header.php
- footer.php
- index.php
- function.php
- css
Create Barcode Generate Form 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 |
<?php require_once "function.php"; include('templates/header.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-3"> <label for="inputEmail4">MRP</label> <input type="text" class="form-control" id="mrp" name="mrp" value="<?php if(!empty($_POST["mrp"])) { print $_POST["mrp"]; } else { print 10.00; } ?>"> </div> <div class="form-group col-md-3"> <label for="inputEmail4">SKU</label> <input type="text" class="form-control" id="sku" name="sku" value="<?php if(!empty($_POST["sku"])) { print $_POST["sku"]; } else { print 'RED10MDL00009'; } ?>"> </div> <div class="form-group col-md-3"> <label for="inputEmail4">Model</label> <input type="text" class="form-control" id="model" name="model" value="<?php if(!empty($_POST["model"])) { print $_POST["model"]; } else { print 'MDL00009'; } ?>"> </div> <div class="col"> <button type="submit" class="btn btn-primary mt-3 float-left">Generate</button> </div> </div> <div class="row align-items-center"> <div class="form-group col-md-6"> <label for="inputEmail4"> <strong>Output: </strong> <hr> <?php // generate QR Codes if(!empty($_POST['mrp'])) { $barcode = generateBarcode($data = array('mrp'=>$_POST['mrp'], 'sku'=>$_POST['sku'], 'model'=>$_POST['model']), 'tmp'); echo '<img src="tmp/'.basename($barcode).'" />'; } ?> </label> </div> </div> </div> </section> </form> <?php include('templates/footer.php');?> |
Generate Barcode 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 |
<?php function generateBarcode($data, $folder) { // include QR Codes Lib require ('vendor/autoload.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); $timestamp = time(); $filename = $PNG_TEMP_DIR.$timestamp.'.png'; $MRP = $data["mrp"]; $SKU = $data["sku"]; $MODEL = $data["model"]; $productData = "098{$MRP}10{$SKU}55{$MODEL}"; $barcode = new \Com\Tecnick\Barcode\Barcode(); $bobj = $barcode->getBarcodeObj('C128B', "{$productData}", 450, 70, 'black', array(0, 0, 0, 0)); $imageData = $bobj->getPngData(); file_put_contents($filename, $imageData); 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="https://techarise.com/wp-content/themes/v1/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 |
<!-- 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> <!-- 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> |