Download the PHP package picqer/php-barcode-generator without Composer
On this page you can find all versions of the php package picqer/php-barcode-generator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download picqer/php-barcode-generator
More information about picqer/php-barcode-generator
Files in picqer/php-barcode-generator
Package php-barcode-generator
Short Description An easy to use, non-bloated, barcode generator in PHP. Creates SVG, PNG, JPG and HTML images from the most used 1D barcode standards.
License LGPL-3.0-or-later
Homepage https://github.com/picqer/php-barcode-generator
Rated 4.25 based on 4 reviews
Informations about the package php-barcode-generator
PHP Barcode Generator
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.
No support for...
- No support for any 2D barcodes, like QR codes.
- We only generate the 'bars' part of a barcode, without text below the 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:
If you want to generate PNG or JPG images, you need the GD library or Imagick installed on your system as well. For SVG or HTML renders, there are no dependencies.
Usage
You want a barcode for a specific "type" (for example Code 128 or UPC) in a specific image format (for example PNG or SVG).
- First, encode the string you want the barcode of into a
Barcodeobject with one of the barcode types. - Then, use one of the renderers to render the image of the bars in the
Barcodeobject.
The "type" is a standard that defines which characters you can encode and which bars represent which character. The most used types are code 128 and EAN/UPC. Not all characters can be encoded into each barcode type, and not all barcode scanners can read all types.
Will result in this beauty:
Each renderer has their own options. For example, you can set the height, width and color of a PNG:
Image renderers
Available image renderers: SVG, PNG, JPG and HTML.
They all conform to the RendererInterface and have the same render() method. Some renderers have extra options as well, via set*() methods.
Widths
The render() method accepts the Barcode object, the final output width, and the final output height. The width argument is the total width of the rendered image, not a width factor.
For JPG and PNG images, always pass a width that is an integer multiple of $barcode->getWidth(). For example, $barcode->getWidth() * 2 produces an image where every barcode module is two pixels wide. An arbitrary width can force module boundaries onto fractional pixels, causing uneven bars and a barcode that scanners may not read reliably. Do not rely on the default width for raster images, because it may not be an integer multiple of the barcode's intrinsic width.
HTML and SVG renderers can handle any width and height, even floats.
Here are all the options for each renderer:
SVG
A vector based SVG image. Gives the best quality to print.
PNG + JPG
All options for PNG and JPG are the same.
HTML
Gives HTML to use inline in a full HTML document.
Dynamic HTML
Give HTML here the barcode is using the full width and height, to put inside a container/div that has a fixed size.
You can put the rendered HTML inside a div like this:
Accepted barcode types
These barcode types are supported. All types support different character sets and some have mandatory lengths. Please see wikipedia for supported chars and lengths per type.
You can find all supported types in the src/Types folder.
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_32 (italian pharmaceutical code 'MINSAN')
- 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_ITF14 (Also known as GTIN-14)
- 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
UPC-E input
TypeUpcE and TYPE_UPC_E accept these input forms:
- A 7-digit UPC-E value consisting of the number system digit
0and six encoded digits. The check digit is calculated automatically. - The complete 8-digit UPC-E human-readable value, including its check digit.
- An 11- or 12-digit UPC-A value that can be represented using UPC-E zero suppression. The check digit may be omitted or supplied.
Supplied check digits are validated. UPC-A values that cannot be zero-suppressed, non-numeric input, and ambiguous lengths are rejected instead of being silently padded.
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 $renderer->useGd() or $renderer->useImagick() to force your preference.
Examples
Embedded PNG image in HTML
Save JPG barcode to disk
Oneliner SVG output to disk
Upgrading to v3
There is no need to change anything when upgrading from v2 to v3. Above you find the new preferred way of using this library since v3. But the old style still works.
To give the renderers the same interface, setting colors is now always with an array of RGB colors. If you use the old BarcodeGenerator* classes and use colors with names ('red') or hex codes (#3399ef), these will be converted using the ColorHelper. All hexcodes are supported, but for names of colors only the basic colors are supported.
If you want to convert to the new style, here is an example:
The renderer's width argument is the final output width, not a width factor. To preserve a legacy width factor, multiply the encoded barcode's intrinsic width by that factor. This is especially important for PNG and JPG output, where the final width should be an integer multiple of the intrinsic width. For example, to preserve a width factor of 2:
Previous style generators
In version 3 the barcode type encoders and image renderers are completely separate. This makes building your own renderer way easier. The old way was using "generators". Below are the old examples of these generators, which still works in v3 as well.
Usage
Initiate the barcode generator for the output you want, then call the ->getBarcode() routine as many times as you want.
Will result in this beauty:
The getBarcode() method accepts the following parameters:
$barcodeString needed to encode in the barcode$typeType of barcode, use the constants defined in the class$widthFactorWidth is based on the length of the data, with this factor you can make the barcode bars wider than default$heightThe total height of the barcode in pixels$foregroundColorHex code as string, or array of RGB, of the colors of the bars (the foreground color)
Example of usage of all parameters:
Image types
Embedded PNG image in HTML
Save JPG barcode to disk
Oneliner SVG output to disk
The codebase is based on the TCPDF barcode generator by Nicola Asuni. This code is therefor licensed under LGPLv3.