Php

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:

Related Post
  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.

Recent Posts

Configure and Retrieve S3 Information Using Laravel PHP-AWS-SDK

This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More

2 months ago

What is the Purpose of php_eol in PHP?

in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More

8 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

8 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

9 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

9 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

9 months ago

Categories