How to Create a Barcode in PHP

May 30, 2020
PHP
create barcode using php

Getting Started

To be able to generate barcode, I’ve used an existing Barcode class in Github.

I have downloaded the files in the Repo and are included in the downloadable of this tutorial, but if you want, you can download the class yourself.

1. Install a composer. If you have not installed composer yet, you may download it using this link.

2. Once the composer is installed, create your directory, in my case barcode_php.

3. Navigate to your created directory in command prompt and type in:

composer require picqer/php-barcode-generator

It would help if you had a folder vendor that contains all the necessary files to generate barcode.

Generating a Barcode

Next, we are going to generate our barcode, as you can see in the Github repo, you can use different barcode types.

As an example, please create a new file, name it as index.php and paste the codes below.

<?php
 
    require 'vendor/autoload.php';
 
    $generator = new \Picqer\Barcode\BarcodeGeneratorPNG();
    echo '<img src="data:image/png;base64,' . base64_encode($generator->getBarcode('081231723897', $generator::TYPE_CODE_128)) . '">';
 
?>

In here we have used TYPE_CODE_128 as our barcode type and ‘081231723897’ is the text that our barcode is created from.

That ends this tutorial. Happy Coding!

Download Code

 

Facebook Comments

Leave a Reply

Your email address will not be published.