Stars: 1530
Forks: 415
Pull Requests: 47
Issues: 142
Watchers: 62
Last Updated: 2023-09-16 09:04:14
Barcode generator in PHP that is easy to use, non-bloated and framework independent.
License: GNU Lesser General Public License v3.0
Languages: PHP, HTML
This is an easy to use, non-bloated, framework independent, barcode generator in PHP. It uses zero(!) composer dependencies and is only a handful of files. Probably the reason that this is the most downloaded barcode generator for PHP on Packagist. ;)
It creates SVG, PNG, JPG and HTML images, from the most used 1D barcode standards.
The codebase is based on the TCPDF barcode generator by Nicola Asuni. This code is therefor licensed under LGPLv3.
Install through composer:
composer require picqer/php-barcode-generator
If you want to generate PNG or JPG images, you need the GD library or Imagick installed on your system as well.
Initiate the barcode generator for the output you want, then call the ->getBarcode() routine as many times as you want.
<?php
require 'vendor/autoload.php';
// This will output the barcode as HTML output to display in the browser
$generator = new Picqer\Barcode\BarcodeGeneratorHTML();
echo $generator->getBarcode('081231723897', $generator::TYPE_CODE_128);
The getBarcode()
method accepts the following parameters:
$barcode
String needed to encode in the barcode$type
Type of barcode, use the constants defined in the class$widthFactor
Width is based on the length of the data, with this factor you can make the barcode bars wider than default$height
The total height of the barcode in pixels$foregroundColor
Hex code as string, or array of RGB, of the colors of the bars (the foreground color)Example of usage of all parameters:
<?php
require 'vendor/autoload.php';
$redColor = [255, 0, 0];
$generator = new Picqer\Barcode\BarcodeGeneratorPNG();
file_put_contents('barcode.png', $generator->getBarcode('081231723897', $generator::TYPE_CODE_128, 3, 50, $redColor));
$generatorSVG = new Picqer\Barcode\BarcodeGeneratorSVG(); // Vector based SVG
$generatorPNG = new Picqer\Barcode\BarcodeGeneratorPNG(); // Pixel based PNG
$generatorJPG = new Picqer\Barcode\BarcodeGeneratorJPG(); // Pixel based JPG
$generatorHTML = new Picqer\Barcode\BarcodeGeneratorHTML(); // Pixel based HTML
$generatorHTML = new Picqer\Barcode\BarcodeGeneratorDynamicHTML(); // Vector based HTML
These barcode types are supported. All types support different character sets or have mandatory lengths. Please see wikipedia for supported chars and lengths per type.
Most used types are TYPE_CODE_128 and TYPE_CODE_39. Because of the best scanner support, variable length and most chars supported.
See example images for all supported barcode types
If you want to use PNG or JPG images, you need to install Imagick or the GD library. This package will use Imagick if that is installed, or fall back to GD. If you have both installed but you want a specific method, you can use $generator->useGd()
or $generator->useImagick()
to force your preference.
$generator = new Picqer\Barcode\BarcodeGeneratorPNG();
echo '<img src="data:image/png;base64,' . base64_encode($generator->getBarcode('081231723897', $generator::TYPE_CODE_128)) . '">';
$generator = new Picqer\Barcode\BarcodeGeneratorJPG();
file_put_contents('barcode.jpg', $generator->getBarcode('081231723897', $generator::TYPE_CODABAR));
file_put_contents('barcode.svg', (new Picqer\Barcode\BarcodeGeneratorSVG())->getBarcode('6825ME601', Picqer\Barcode\BarcodeGeneratorSVG::TYPE_KIX));