Barcode generator in PHP that is easy to use, non-bloated and framework independent.

Overview

PHP Barcode Generator

Build Status Github Actions Total Downloads

This is an easy to use, non-bloated, framework independent, barcode generator in PHP.

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.

No support for...

We do not support any 2D barcodes, like QR codes. We also only generate the 'bars' part of a barcode. If you want text of the code below the barcode, you could add it later to the output of this package.

Installation

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.

Usage

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));

Image types

$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

Accepted barcode types

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.

  • TYPE_CODE_39
  • TYPE_CODE_39_CHECKSUM
  • TYPE_CODE_39E
  • TYPE_CODE_39E_CHECKSUM
  • TYPE_CODE_93
  • TYPE_STANDARD_2_5
  • TYPE_STANDARD_2_5_CHECKSUM
  • TYPE_INTERLEAVED_2_5
  • TYPE_INTERLEAVED_2_5_CHECKSUM
  • TYPE_CODE_128
  • TYPE_CODE_128_A
  • TYPE_CODE_128_B
  • TYPE_CODE_128_C
  • TYPE_EAN_2
  • TYPE_EAN_5
  • TYPE_EAN_8
  • TYPE_EAN_13
  • TYPE_UPC_A
  • TYPE_UPC_E
  • TYPE_MSI
  • TYPE_MSI_CHECKSUM
  • TYPE_POSTNET
  • TYPE_PLANET
  • TYPE_RMS4CC
  • TYPE_KIX
  • TYPE_IMB
  • TYPE_CODABAR
  • TYPE_CODE_11
  • TYPE_PHARMA_CODE
  • TYPE_PHARMA_CODE_TWO_TRACKS

See example images for all supported barcode types

A note about PNG and JPG images

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.

Examples

Embedded PNG image in HTML

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

Save JPG barcode to disk

$generator = new Picqer\Barcode\BarcodeGeneratorJPG();
file_put_contents('barcode.jpg', $generator->getBarcode('081231723897', $generator::TYPE_CODABAR));

Oneliner SVG output to disk

file_put_contents('barcode.svg', (new Picqer\Barcode\BarcodeGeneratorSVG())->getBarcode('6825ME601', Picqer\Barcode\BarcodeGeneratorSVG::TYPE_KIX));
Comments
  • Feature/2.0

    Feature/2.0

    Possible 2.0 Release

    • Refactored everything. Barcode data is broken into classes, along with render interfaces for the supported file types (jpg, png, html, svg)
    • Factory generators for Types and Renders.
    • PHP 7 type casting for parameters and return values.
    • Smoke tests for all combinations of code types and renders.
    • phpunit updated to include code coverage report.
    • Updated Readme to demonstrate new way initiating and generating a barcode.

    New Usage

    $generator = new BarcodeGenerator('012345678', BarcodeType::TYPE_CODE_128, BarcodeRender::RENDER_JPG);
    
    // Generates our default barcode with width=2, height=30, color=#000000
    $generated = $generator->generate();
    
    // Generates the same code with style updates
    $generated = $generator->generate(4, 50, '#FFCC33');
    

    Things still needed

    • Unit test coverage is more of a smoke test. No errors occur in calling all types with all renders, but wether that is valid is still unsure.
    • Unit test validating proper exceptions are thrown.
    opened by griffithben 9
  • Height & Width Issue

    Height & Width Issue

    I am using picqer bar code generator for generating bar code. My requirement for bar code height is 26.45 pixels and width is 185.19 pixels. Height is adjustable. But I want width to be precise 185 or 186. How can I do that? I have adjusted height as 27 but How can i set the width of bar code of 186. Your early reply will be appreciated.

    opened by rushiwebonomics 9
  • PHP 7.4

    PHP 7.4

    This PR contains two commits related to supporting later versions of PHP.

    The main bulk is feae1e0 which fixes all string and array index access using {} which was deprecated in PHP 7.4.

    The second commit fixes a failing test in PHP 7.1 and onward.

    opened by pilif 8
  • variable barcode type

    variable barcode type

    Hello there, I'm trying to use variable instead of const, but gives me some errors

    $generator = new Picqer\Barcode\BarcodeGeneratorSVG();
    $generated = $generator->getBarcode('1234567890', $generator:: $barcode_type);
    

    error:

    PHP Fatal error: Uncaught Error: Access to undeclared static property: Picqer\Barcode\BarcodeGeneratorSVG::$barcode_type

    thanks

    opened by Mr-TOA 7
  • GS1-128

    GS1-128

    I need to generate a barcode with the standard GS1-128, I had an almost satisfactory result following this post (http://stackoverflow.com/questions/34040995/how-to-properly-generate-a-gs1-128-formerly Barcode-in-tcpdf) but unfortunately the end of the barcode was not the same in comparison to one generated by other system that actually follows the standard and is accepted by our bank.

    I expect to see this: esperado

    but instead I get this: obtenido

    I'm using this code: $code = chr(241)."4157709998016989802000105385555480202016103470"; $generatorPNG = new Picqer\Barcode\BarcodeGeneratorPNG(); echo '<img ' . 'src="data:image/png;base64,' . base64_encode( $generatorPNG->getBarcode($code, Picqer\Barcode\BarcodeGeneratorPNG::TYPE_CODE_128_C)) . '" style="height: 50px; width: 300px ">';

    I appreciate any help you can give me.

    opened by linaocampouam 7
  • Text in the bottom of the barcode

    Text in the bottom of the barcode

    Hello,

    Is there any way I can show the actual code of the generated barcode or a custom text in the bottom part of the generated barcode image?

    I would also like to suggest this as a feature for future versions, which I found useful.

    Thanks in advance.

    opened by robertripoll 7
  • Error with Code 11 using check digit K

    Error with Code 11 using check digit K

    Hi there!

    I have found an error when creating a barcode of type "Code 11", which is at least 10 characters long. In that case, the check digit K is calculated and always (under all circumstances) throws an error.

    The damaged code is in \Picqer\Barcode\Types\TypeCode11::getCheckDigitK() on line 94 (for ($i = strlen($code); $i >= 0; --$i) {), throwing an error on line 95 ($digit = $code[$i];).

    E.g., if the code is 11 characters long (check digit C already included), the code tries to get the 12. character.

    A solution (which is already implemented in ::getCheckDigitC(), for example) would be for ($i = (strlen($code) - 1); $i >= 0; --$i) {.

    Kind regards,

    Bernhard

    opened by Bernhard-Krop 6
  • Mixup A/B/C in 128 barcodes

    Mixup A/B/C in 128 barcodes

    Hi,

    I need to use A+C modes in same 128 barcode. I tried to edit current code but it doesnt seems to work. There is a way to adapt it to get this use case works ?

    Thanks

    opened by simondaigre 6
  • copyright and license violation

    copyright and license violation

    This project is using code copied from: https://github.com/tecnickcom/TCPDF but all copyright and license terms were removed. Please immediately delete all the copied code or adapt it to conform the original copyright and license requirements.

    Please also note that the code you have copied has now been replaced by another project: https://github.com/tecnickcom/tc-lib-barcode

    Please carefully read the license and act promptly.

    opened by nicolaasuni 6
  • [feature] set background color and padding (or border)

    [feature] set background color and padding (or border)

    We are using the library in one of our mobile app and it would be nice if we could set the border around the code. Why? Problem is when on the mobile the dark mode is enabled, because code merges on the left and right side with black background. Is there any workaround with Imagick maybe?

    opened by pubi 4
  • Black block in place of bacorde

    Black block in place of bacorde

    Hello !

    Thanks for the work on this module. I've updated from 0.x to 2.x and now, i've got a black block in place of my barcode image using : $barcode_generator = new Picqer\Barcode\BarcodeGeneratorJPG(); echo '<img src="data:image/jpg;base64,'.base64_encode($barcode_generator->getBarcode('081231723897',$barcode_generator::TYPE_EAN_13)).'"/>';

    Previously, the same code worked well. What's wrong here, please ?

    opened by jomofcw 4
  • Inline svg

    Inline svg

    Thanks for the great lib! I used it to create an inline svg, but had to strip the <!xml and <!DOCTYPE stuff for that.

    No problem, but maybe something like this would be a nice addition in BarcodeGeneratorSVG.php:

    • Add param bool $inline = false to getBarcode()
    • Only add the <!xml and <!DOCTYPE lines when !$inline
    opened by webwit 0
  • Inconsistency in $foregroundColor (string and array)

    Inconsistency in $foregroundColor (string and array)

    In the PNG generator, the signature is

    public function getBarcode($barcode, $type, int $widthFactor = 2, int $height = 30, array $foregroundColor = [0, 0, 0])
    

    But in all the others, it's a string

    public function getBarcode($barcode, $type, int $widthFactor = 2, int $height = 30, string $foregroundColor = 'black')
    

    If they were the same, we could create a BarcodeGeneratorInterface, and each generator could implement it. I think that'd be better than simply extending the abstract class.

    What do you think about creating a version 3 uses an interface and requires that $foregroundColor is a string in all the generators?

    opened by tacman 2
  • Added consistency on PNG/JPG classes and tests

    Added consistency on PNG/JPG classes and tests

    I changed the two classes that export to image file so they are equally important, and create a common abstract class that keeps the common logic.

    Then, when testing, I only had imagemagick enabled, so the test failed when it should be skipped due to having gd disabled, so I reused the same logic used for imagemagick (skip if not installed)

    opened by kuorus 0
  • Added Febraban Type

    Added Febraban Type

    "Boleto" is the one of the most used payment methods in Brazil.

    Febraban is the type of barcode used in boletos. It's is basically a interleaved of 2:5, but the digits order is different, then I ajusted to generate barcode in that order.

    boleto

    opened by devdiogenes 3
  • Adding ability to print UPC-E barcodes with UPC-E codes as input

    Adding ability to print UPC-E barcodes with UPC-E codes as input

    Like I mentioned in issue "UPC-E encoding completely wrong information", UPC-E barcodes could only be generated with UPC-A codes as input. This pull request adds the ability to generate UPC-E barcodes with UPC-E codes as input.

    opened by Bernhard-Krop 2
Releases(v2.2.4)
  • v2.2.4(Jul 1, 2022)

    What's Changed

    • Fixing barcode type "Standard 2 of 5" by @Bernhard-Krop in https://github.com/picqer/php-barcode-generator/pull/161
    • Fixing a bug in Code 39 Extended (with or without checksum) by @Bernhard-Krop in https://github.com/picqer/php-barcode-generator/pull/160

    Full Changelog: https://github.com/picqer/php-barcode-generator/compare/v2.2.3...v2.2.4

    Source code(tar.gz)
    Source code(zip)
  • v2.2.3(Jun 14, 2022)

    What's Changed

    • Solution for Issue: Error with Code 11 using check digit K by @Bernhard-Krop in https://github.com/picqer/php-barcode-generator/pull/158
    • Solution for Issue: Invalid XML when using SVG generator for type Code 93 by @Bernhard-Krop in https://github.com/picqer/php-barcode-generator/pull/159

    New Contributors

    • @Bernhard-Krop made their first contribution in https://github.com/picqer/php-barcode-generator/pull/158

    Full Changelog: https://github.com/picqer/php-barcode-generator/compare/v2.2.2...v2.2.3

    Source code(tar.gz)
    Source code(zip)
  • v2.2.2(May 6, 2022)

    What's Changed

    • Fix encoding of char 11 in Code39e by @casperbakker in https://github.com/picqer/php-barcode-generator/pull/153
    • Fix couple encodings in Code93 by @casperbakker in https://github.com/picqer/php-barcode-generator/pull/154
    • Added Type code32 by @str4to in https://github.com/picqer/php-barcode-generator/pull/142

    New Contributors

    • @str4to made their first contribution in https://github.com/picqer/php-barcode-generator/pull/142

    Full Changelog: https://github.com/picqer/php-barcode-generator/compare/v2.2.1...v2.2.2

    Source code(tar.gz)
    Source code(zip)
  • v2.2.1(Feb 7, 2022)

    What's Changed

    • fix deprecated: strtoupper(): Passing null to parameter (#144) by @turbopixel in https://github.com/picqer/php-barcode-generator/pull/146

    New Contributors

    • @turbopixel made their first contribution in https://github.com/picqer/php-barcode-generator/pull/146

    Full Changelog: https://github.com/picqer/php-barcode-generator/compare/v2.2.0...v2.2.1

    Source code(tar.gz)
    Source code(zip)
  • v2.2.0(Mar 27, 2021)

  • v2.1.0(Dec 24, 2020)

    • Fixed imagick results is different from GD result #103
    • Throw exception on invalid Pharmacode 2 code to prevent infinite loop #118
    • Drop support for PHP 7.2 for phpunit 9 and PHP 8.0
    Source code(tar.gz)
    Source code(zip)
  • v2.0.1(Jan 28, 2020)

  • v2.0.0(Jan 11, 2020)

    Big new version 🎉

    Finally got around to refactoring the original structure from the original tcpdf library.

    Added

    • Introduced Barcode and BarcodeBar classes to standardise generator output.
    • Introduced methods to force use of GD or Imagick, see readme.
    • Loads of new tests added, including tests on Github Actions.

    Changed

    • Splitted all barcode types to different files.
    • Refactored a lot of code for better readability, stricter checking, and to be more efficient.
    • Merged JPG and PNG generators, because of duplicate code.

    Fixed

    • Fixed a bug in Codabar generation 2d1128f5222d9368fc6151d2b51801ea29ba1052
    • Do not draw multiple bars on the same position #74
    • Do not try to draw barcodes for empty strings #42
    • Fixed possible casting issue in Codabar #92
    Source code(tar.gz)
    Source code(zip)
  • v0.4.0(Dec 31, 2019)

  • v0.3(Jan 12, 2019)

    • SVG: Add viewBox attribute to allow svg scaling #68 by @cuchac
    • Adjust CODE_128 to handle odd number of digits #55 by @richayles
    • Bugfix update imagick function #51 by @Keinbockwurst
    Source code(tar.gz)
    Source code(zip)
  • v0.2.2(Sep 28, 2017)

  • v0.2.1(Oct 24, 2016)

  • v0.2(May 14, 2016)

Owner
Picqer
Picqer
Run multiple websites using the same Laravel installation while keeping tenant specific data separated for fully independent multi-domain setups, previously github.com/hyn/multi-tenant

The unobtrusive Laravel package that makes your app multi tenant. Serving multiple websites, each with one or more hostnames from the same codebase. B

Tenancy 2.4k Jan 3, 2023
Run multiple websites using the same Laravel installation while keeping tenant specific data separated for fully independent multi-domain setups.

Tenancy for Laravel Enabling awesome Software as a Service with the Laravel framework. This is the successor of hyn/multi-tenant. Feel free to show su

Tenancy 1.1k Dec 30, 2022
Run multiple websites using the same Laravel installation while keeping tenant specific data separated for fully independent multi-domain setups, previously

Run multiple websites using the same Laravel installation while keeping tenant specific data separated for fully independent multi-domain setups, previously

Tenancy 2.4k Jan 3, 2023
Laravel Design Pattern Generator (api generator)

Laravel Design Pattern Generator (api generator) you can create your restful api easily by using this library and you can filter, sort and include elo

HusseinAlaa 2 Sep 25, 2022
Deploy and execute non-PHP AWS Lambda functions from your Laravel application.

Sidecar for Laravel Deploy and execute non-PHP AWS Lambda functions from your Laravel application. Read the full docs at hammerstone.dev/sidecar/docs.

Hammerstone 624 Dec 30, 2022
A nice GUI for Laravel Artisan, ready out of the box, configurable and handy for non-CLI experienced developers.

Artisan UI A nice GUI for Laravel Artisan, ready out of the box, configurable and handy for non-CLI experienced developers. Supported commands must be

Pablo Leone 1 Dec 3, 2021
A non-blocking stream abstraction for PHP based on Amp.

amphp/byte-stream is a stream abstraction to make working with non-blocking I/O simple. Installation This package can be installed as a Composer depen

Amp 317 Dec 22, 2022
Nebula is a minimalistic and easy to use administration tool for Laravel applications, made with Laravel, Alpine.js, and Tailwind CSS.

Nebula Nebula is a minimalistic and easy to use administration tool for Laravel applications, made with Laravel, Alpine.js, and Tailwind CSS. Nebula m

Nebula 228 Nov 11, 2022
Laravel Setting - Easily save, update and get titles, descriptions, and more. it is very easy to use.

Laravel Setting Easily save, update and get titles, descriptions, and more. it is very easy to use. This is great for storing and receiving general si

Ali Ranjbar 2 Aug 23, 2022
🔹 Generator Template for 🙃 Phony Framework

?? Generator Template This repository contains the Generator Template for ?? Phony Framework. ?? Start generating fake data with ?? Phony Framework, v

Phonyland 2 Jan 30, 2022
🧩 Generator Manager for 🙃 Phony Framework

?? Generator Manager This repository contains the Generator Manager for ?? Phony Framework. ?? Start generating fake data with ?? Phony Framework, vis

Phonyland 2 Jan 7, 2022
Laravel-FCM is an easy to use package working with both Laravel and Lumen for sending push notification with Firebase Cloud Messaging (FCM).

Laravel-FCM Introduction Laravel-FCM is an easy to use package working with both Laravel and Lumen for sending push notification with Firebase Cloud M

Rahul Thapa 2 Oct 16, 2022
A flexible, seamless and easy to use multitenancy solution for Laravel

Main: Develop: Tenanted Laravel A flexible, seamless and easy to use multitenancy solution for Laravel This package is currently under development. Ch

Tenanted Laravel 12 May 29, 2023
A premade, easy to use local development setup to be used for authoring Laravel applications

Laravel Drydock This project is a premade, easy to use local development setup to be used for authoring Laravel applications. The deliverables of this

Alexander Trauzzi 19 Nov 11, 2022
An easy-to-use virtual wallet implementation for Laravel

Laravel Wallet Some apps require a prepayment system like a virtual wallet where customers can recharge credits which they can then use to pay in app

Muath Assawadi 6 Sep 28, 2022
Livewire DataTables components for back-end. Modular, easy to use, with tons of features.

Livewire DataTables Livewire DataTables components for back-end. Modular, easy to use, with tons of features. Inspired by Caleb's Livewire Screencasts

Amir Rami 8 Jul 27, 2022
Laravel User Activity Log - a package for Laravel 8.x that provides easy to use features to log the activities of the users of your Laravel app

Laravel User Activity Log - a package for Laravel 8.x that provides easy to use features to log the activities of the users of your Laravel app

null 9 Dec 14, 2022
An easy-to-use virtual wallet implementation for Laravel.

Laravel Wallet Some apps require a prepayment system like a virtual wallet where customers can recharge credits which they can then use to pay in app

Muathye 1 Feb 6, 2022
A collection of easy-to-use filters with clause conditions to Filament

Filament Advanced Filter A collection of easy-to-use filters with clause conditions to Filament Installation Install the package via composer (require

Webbing Brasil 45 Jan 2, 2023