How to Create a 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 composer. If you have not installed composer yet, you may download it using this link. 2. Once composer is installed, create your directory, in my case barcode_php. 3. Navigate to your created directory in command prompt and type in:
  1. composer require picqer/php-barcode-generator
You should have a folder vendor that contains all the necessary files to generate barcode.

Generating our 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, create a new file, name it as index.php and paste the codes below.
  1. <?php
  2.  
  3.         require 'vendor/autoload.php';
  4.  
  5.         $generator = new \Picqer\Barcode\BarcodeGeneratorPNG();
  6.         echo '<img src="data:image/png;base64,' . base64_encode($generator->getBarcode('081231723897', $generator::TYPE_CODE_128)) . '">';
  7.  
  8. ?>
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 :)

Add new comment