How to Generate Barcode using PHP 7

How to generate barcode in PHP is the topic of this tutorial. The barcode helps in the storage of product-related data such as price, code, and other useful information. This is a machine-readable code that is attached to the product or goods.

To begin, you must first obtain the barcode libs. The library allows you to produce barcodes of the types Codabar, Code128, Code39, and Horizontal and Vertical display types. It also has size settings (10, 20, and 400 maximum) and the ability to display a barcode with text. As a result, we’ll cover all of these possibilities in this example.

Checkout Other useful php tutorials,

We’ll follow the following steps:

  1. Create an HTML file and define the markup.
  2. Create a PHP file to generate a barcode

How to Build a Barcode Generator in PHP

We will create a PHP barcode generator code. I have assumed you have downloaded barcode libs.

Create Barcode Generate Form

Let’s start by adding an index.php file to our /barcode-php project. We’ll use it to create a Barcode generate HTML Form that will produce a barcode when the form is submitted.

<form method="post">
    <label>Product Name or Number</label>
    <input type="text" name="barcodeText" class="form-control" value="<?php echo @$_POST['barcodeText'];?>">
    
    <label>Barcode Type</label>
    <select name="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>
    
    <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>
    
    <input type="hidden" name="barcodeSize" value="20">
    <input type="hidden" name="printText" value="true">
    <input type="submit" name="generateBarcode" class="btn btn-success form-control" value="Generate Barcode">
</form>

Generate Barcode Using PHP

Created a barcode.php file and added the below code into this file. We’ll pass all form data to this file and generate a barcode image.

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.'&amp;codetype='.$barcodeType.'&amp;orientation='.$barcodeDisplay.
'&amp;size='.$barcodeSize.'&amp;print='.$printText.'">';
    } else {
        echo '<div class="alert alert-danger">Enter product name or number to generate barcode!</div>';
    }
}

Conclusion

By following the steps outlined in this tutorial and utilizing barcode libraries, You can efficiently generate barcodes to store and display product information in a machine-readable format.

Leave a Reply

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