How to Build Barcode Generator in PHP

Posted on

Barcode is a machine readable code which generated using items details like item name, item code, price etc. The related Barcode is tagged on item to get item details using Barcode reader machine. 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, so in this tutorial you will learn how to implement Barcode generator using PHP. We will cover this tutorial with live demo to create Barcode generator with PHP.

As we will cover this tutorial with live demo to implement Barcode generator using PHP, so the file structure for this example is following.

  • index.php
  • barcode.php

Step1: Create Barcode Generate Form
First in index.php, we will create Barcode generate HTML Form to create Barcode on form submit with options.
<div class="row">
<div class="col-md-4">
<form method="post">
<div class="row">
<div class="col-md-8">
<div class="form-group">
<label>Product Name or Number</label>
<input type="text" name="barcodeText" class="form-control" value="<?php echo @$_POST['barcodeText'];?>">
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>Barcode Type</label>
<select name="barcodeType" id="barcodeType" class="form-control">
<option value="codabar" <?php echo (@$_POST['barcodeType'] == 'codabar' ? 'selected="selected"' : ''); ?>>Codabar</option>
<option value="code128" <?php echo (@$_POST['barcodeType'] == 'code128' ? 'selected="selected"' : ''); ?>>Code128</option>
<option value="code39" <?php echo (@$_POST['barcodeType'] == 'code39' ? 'selected="selected"' : ''); ?>>Code39</option>
</select>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>Barcode Display</label>
<select name="barcodeDisplay" class="form-control" required>
<option value="horizontal" <?php echo (@$_POST['barcodeDisplay'] == 'horizontal' ? 'selected="selected"' : ''); ?>>Horizontal</option>
<option value="vertical" <?php echo (@$_POST['barcodeDisplay'] == 'vertical' ? 'selected="selected"' : ''); ?>>Vertical</option>
</select>
</div>
</div>
</div>
<div class="row">
<div class="col-md-7">
<input type="hidden" name="barcodeSize" id="barcodeSize" value="20">
<input type="hidden" name="printText" id="printText" value="true">
<input type="submit" name="generateBarcode" class="btn btn-success form-control" value="Generate Barcode">
</div>
</div>
</form>
</div>
</div>

Step2: Get Barcode Generator Library
We need to download Barcode generate PHP library to create Barcode . In this example we have already downloaded barcode.php and used to create Barcode. The library has
options to create Barcode with type Codabar, Code128, Code39 and Barcode display type Horizontal and Vertical. It also supports options for size (10, 20 and 400 maximum) and option to display Barcode with text. So we will handle all these options in this example.

 

Step3: Generate Barcode with PHP
Now on Form submit, we will generate Barcode by passing items details to library file barcode.php and display created Barcode in img.
<?php
if(isset($_POST['generateBarcode'])) {
$barcodeText = trim($_POST['barcodeText']);
$barcodeType=$_POST['barcodeType'];
$barcodeDisplay=$_POST['barcodeDisplay'];
$barcodeSize=$_POST['barcodeSize'];
$printText=$_POST['printText'];
if($barcodeText != '') {
echo '<h4>Barcode:</h4>';
echo '<img class="barcode" alt="'.$barcodeText.'" src="barcode.php?text='.$barcodeText.'&codetype='.$barcodeType.'&orientation='.$barcodeDisplay.'&size='.$barcodeSize.'&print='.$printText.'"/>';
} else {
echo '<div class="alert alert-danger">Enter product name or number to generate barcode!</div>';
}
}
?>

You can view the live demo from the Demo link and can download the script from the Download link below.
Demo [sociallocker]Download[/sociallocker]

2 thoughts on “How to Build Barcode Generator in PHP

  1. Really informative and helpful material. Thanks for providing such an amazing solution for barcode generator funtion.

Leave a Reply

Your email address will not be published. Required fields are marked *