Skip to content

Commit 6813baf

Browse files
committed
Updated readme for v3
1 parent 460f3ee commit 6813baf

File tree

1 file changed

+106
-28
lines changed

1 file changed

+106
-28
lines changed

Readme.md

Lines changed: 106 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ This is an easy to use, non-bloated, framework independent, barcode generator in
77

88
It creates SVG, PNG, JPG and HTML images, from the most used 1D barcode standards.
99

10-
*The codebase is based on the [TCPDF barcode generator](https://github.com/tecnickcom/TCPDF) by Nicola Asuni. This code is therefor licensed under LGPLv3.*
11-
1210
## No support for...
1311
- No support for any **2D** barcodes, like QR codes.
1412
- 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.
@@ -23,51 +21,56 @@ composer require picqer/php-barcode-generator
2321
If you want to generate PNG or JPG images, you need the GD library or Imagick installed on your system as well.
2422

2523
## Usage
26-
Initiate the barcode generator for the output you want, then call the ->getBarcode() routine as many times as you want.
24+
You want a barcode for a specific "type" (for example Code 128 or UPC) in a specific image format (for example PNG or SVG).
25+
26+
> 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](https://en.wikipedia.org/wiki/Code_128) and [EAN/UPC](https://en.wikipedia.org/wiki/International_Article_Number). Not all characters can be encoded into each barcode type, and not all barcode scanners can read all types.
27+
28+
First, encode the string you want the barcode of into a `Barcode` object with one of the barcode types.
29+
Then, use one of the renderers to render the image of the bars in the `Barcode` object.
2730

2831
```php
2932
<?php
3033
require 'vendor/autoload.php';
3134

32-
// This will output the barcode as HTML output to display in the browser
33-
$generator = new Picqer\Barcode\BarcodeGeneratorHTML();
34-
echo $generator->getBarcode('081231723897', $generator::TYPE_CODE_128);
35+
// Make Barcode object of Code128 encoding.
36+
$barcode = (new Picqer\Barcode\Types\TypeCode128())->getBarcode('081231723897');
37+
38+
// Output the barcode as HTML in the browser with a HTML Renderer
39+
$renderer = new Picqer\Barcode\Renderers\HtmlRenderer();
40+
echo $renderer->render($barcode);
3541
```
3642

3743
Will result in this beauty:<br>
3844
![Barcode 081231723897 as Code 128](tests/verified-files/081231723897-ean13.svg)
3945

40-
The `getBarcode()` method accepts the following parameters:
41-
- `$barcode` String needed to encode in the barcode
42-
- `$type` Type of barcode, use the constants defined in the class
43-
- `$widthFactor` Width is based on the length of the data, with this factor you can make the barcode bars wider than default
44-
- `$height` The total height of the barcode in pixels
45-
- `$foregroundColor` Hex code as string, or array of RGB, of the colors of the bars (the foreground color)
46-
47-
Example of usage of all parameters:
48-
46+
Each renderer has their own options. For example, you can set the height, width and color of a PNG:
4947
```php
5048
<?php
51-
5249
require 'vendor/autoload.php';
5350

54-
$redColor = [255, 0, 0];
51+
$colorRed = [255, 0, 0];
5552

56-
$generator = new Picqer\Barcode\BarcodeGeneratorPNG();
57-
file_put_contents('barcode.png', $generator->getBarcode('081231723897', $generator::TYPE_CODE_128, 3, 50, $redColor));
53+
$barcode = (new Picqer\Barcode\Types\TypeCode128())->getBarcode('081231723897');
54+
$renderer = new Picqer\Barcode\Renderers\PngRenderer();
55+
$renderer->setForegroundColor($colorRed);
56+
57+
// Save PNG to the filesystem, with widthFactor 3 and height of 50 pixels
58+
file_put_contents('barcode.png', $renderer->render($barcode, 3, 50));
5859
```
5960

6061
## Image types
6162
```php
62-
$generatorSVG = new Picqer\Barcode\BarcodeGeneratorSVG(); // Vector based SVG
63-
$generatorPNG = new Picqer\Barcode\BarcodeGeneratorPNG(); // Pixel based PNG
64-
$generatorJPG = new Picqer\Barcode\BarcodeGeneratorJPG(); // Pixel based JPG
65-
$generatorHTML = new Picqer\Barcode\BarcodeGeneratorHTML(); // Pixel based HTML
66-
$generatorHTML = new Picqer\Barcode\BarcodeGeneratorDynamicHTML(); // Vector based HTML
63+
$renderer = new Picqer\Barcode\Renderers\SvgRenderer(); // Vector based SVG
64+
$renderer = new Picqer\Barcode\Renderers\PngRenderer(); // Pixel based PNG
65+
$renderer = new Picqer\Barcode\Renderers\JpgRenderer(); // Pixel based JPG
66+
$renderer = new Picqer\Barcode\Renderers\HtmlRenderer(); // Pixel based HTML
67+
$renderer = new Picqer\Barcode\Renderers\DynamicHtmlRenderer(); // Vector based HTML (full 'page' width and height)
6768
```
6869

6970
## Accepted barcode types
70-
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.
71+
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.
72+
73+
You can find all supported types in the [src/Types](src/Types) folder.
7174

7275
Most used types are TYPE_CODE_128 and TYPE_CODE_39. Because of the best scanner support, variable length and most chars supported.
7376

@@ -107,23 +110,98 @@ Most used types are TYPE_CODE_128 and TYPE_CODE_39. Because of the best scanner
107110
[See example images for all supported barcode types](examples.md)
108111

109112
## A note about PNG and JPG images
110-
If you want to use PNG or JPG images, you need to install [Imagick](https://www.php.net/manual/en/intro.imagick.php) or the [GD library](https://www.php.net/manual/en/intro.image.php). 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.
113+
If you want to use PNG or JPG images, you need to install [Imagick](https://www.php.net/manual/en/intro.imagick.php) or the [GD library](https://www.php.net/manual/en/intro.image.php). 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.
111114

112115
## Examples
113116

114117
### Embedded PNG image in HTML
115118
```php
119+
$barcode = (new Picqer\Barcode\Types\TypeCode128())->getBarcode('081231723897');
120+
$renderer = new Picqer\Barcode\Renderers\PngRenderer();
121+
echo '<img src="data:image/png;base64,' . base64_encode($renderer->render($barcode)) . '">';
122+
```
123+
124+
### Save JPG barcode to disk
125+
```php
126+
$barcode = (new Picqer\Barcode\Types\TypeCodabar())->getBarcode('081231723897');
127+
$renderer = new Picqer\Barcode\Renderers\JpgRenderer();
128+
129+
file_put_contents('barcode.jpg', $renderer->render($barcode));
130+
```
131+
132+
### Oneliner SVG output to disk
133+
```php
134+
file_put_contents('barcode.svg', (new Picqer\Barcode\Renderers\SvgRenderer())->render((new Picqer\Barcode\Types\TypeKix())->getBarcode('6825ME601')));
135+
```
136+
137+
## Upgrading to v3
138+
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.
139+
140+
---
141+
142+
## Previous style generators
143+
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.
144+
145+
### Usage
146+
Initiate the barcode generator for the output you want, then call the ->getBarcode() routine as many times as you want.
147+
148+
```php
149+
<?php
150+
require 'vendor/autoload.php';
151+
152+
// This will output the barcode as HTML output to display in the browser
153+
$generator = new Picqer\Barcode\BarcodeGeneratorHTML();
154+
echo $generator->getBarcode('081231723897', $generator::TYPE_CODE_128);
155+
```
156+
157+
Will result in this beauty:<br>
158+
![Barcode 081231723897 as Code 128](tests/verified-files/081231723897-ean13.svg)
159+
160+
The `getBarcode()` method accepts the following parameters:
161+
- `$barcode` String needed to encode in the barcode
162+
- `$type` Type of barcode, use the constants defined in the class
163+
- `$widthFactor` Width is based on the length of the data, with this factor you can make the barcode bars wider than default
164+
- `$height` The total height of the barcode in pixels
165+
- `$foregroundColor` Hex code as string, or array of RGB, of the colors of the bars (the foreground color)
166+
167+
Example of usage of all parameters:
168+
169+
```php
170+
<?php
171+
172+
require 'vendor/autoload.php';
173+
174+
$redColor = [255, 0, 0];
175+
176+
$generator = new Picqer\Barcode\BarcodeGeneratorPNG();
177+
file_put_contents('barcode.png', $generator->getBarcode('081231723897', $generator::TYPE_CODE_128, 3, 50, $redColor));
178+
```
179+
180+
### Image types
181+
```php
182+
$generatorSVG = new Picqer\Barcode\BarcodeGeneratorSVG(); // Vector based SVG
183+
$generatorPNG = new Picqer\Barcode\BarcodeGeneratorPNG(); // Pixel based PNG
184+
$generatorJPG = new Picqer\Barcode\BarcodeGeneratorJPG(); // Pixel based JPG
185+
$generatorHTML = new Picqer\Barcode\BarcodeGeneratorHTML(); // Pixel based HTML
186+
$generatorHTML = new Picqer\Barcode\BarcodeGeneratorDynamicHTML(); // Vector based HTML
187+
```
188+
189+
#### Embedded PNG image in HTML
190+
```php
116191
$generator = new Picqer\Barcode\BarcodeGeneratorPNG();
117192
echo '<img src="data:image/png;base64,' . base64_encode($generator->getBarcode('081231723897', $generator::TYPE_CODE_128)) . '">';
118193
```
119194

120-
### Save JPG barcode to disk
195+
#### Save JPG barcode to disk
121196
```php
122197
$generator = new Picqer\Barcode\BarcodeGeneratorJPG();
123198
file_put_contents('barcode.jpg', $generator->getBarcode('081231723897', $generator::TYPE_CODABAR));
124199
```
125200

126-
### Oneliner SVG output to disk
201+
#### Oneliner SVG output to disk
127202
```php
128203
file_put_contents('barcode.svg', (new Picqer\Barcode\BarcodeGeneratorSVG())->getBarcode('6825ME601', Picqer\Barcode\BarcodeGeneratorSVG::TYPE_KIX));
129204
```
205+
206+
---
207+
*The codebase is based on the [TCPDF barcode generator](https://github.com/tecnickcom/TCPDF) by Nicola Asuni. This code is therefor licensed under LGPLv3.*

0 commit comments

Comments
 (0)