Skip to content

Commit 9599fe8

Browse files
authored
Merge pull request #198 from picqer/v3
V3
2 parents e9d3968 + 8a7e79d commit 9599fe8

File tree

104 files changed

+1366
-535
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

104 files changed

+1366
-535
lines changed

.github/FUNDING.yml

Lines changed: 0 additions & 1 deletion
This file was deleted.

.github/workflows/phpunit.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212

1313
strategy:
1414
matrix:
15-
php-versions: ['8.1', '8.2', '8.3']
15+
php-versions: ['8.2', '8.3']
1616

1717
steps:
1818
- name: Checkout code

Readme.md

Lines changed: 172 additions & 29 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,97 @@ 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+
- First, encode the string you want the barcode of into a `Barcode` object with one of the barcode types.
27+
- Then, use one of the renderers to render the image of the bars in the `Barcode` object.
28+
29+
> 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.
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

60-
## Image types
61+
## Image renderers
62+
Available image renderers: SVG, PNG, JPG and HTML.
63+
64+
Each renderer has their own options. Only the barcode is required, the rest is optional. Here are all the options for each renderers:
65+
66+
### SVG
67+
A vector based SVG image. Gives the best quality to print.
6168
```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
69+
$renderer = new Picqer\Barcode\Renderers\SvgRenderer();
70+
$renderer->setForegroundColor('red'); // Give a color for the bars, the background is always white
71+
$renderer->setSvgType($renderer::TYPE_SVG_INLINE); // Changes the output to be used inline inside HTML documents, instead of a standalone SVG image (default)
72+
$renderer->setSvgType($renderer::TYPE_SVG_STANDALONE); // If you want to force the default, create a stand alone SVG image
73+
74+
$renderer->render($barcode, 450.20, 75); // Width and height support floats
75+
````
76+
77+
### PNG + JPG
78+
All options for PNG and JPG are the same.
79+
```php
80+
$renderer = new Picqer\Barcode\Renderers\PngRenderer();
81+
$renderer->setForegroundColor([255, 0, 0]); // Give a color for the bars, the background is always white. Give it as 3 times 0-255 values for red, green and blue.
82+
$renderer->useGd(); // If you have Imagick and GD installed, but want to use GD
83+
$renderer->useImagick(); // If you have Imagick and GD installed, but want to use Imagick
84+
85+
$renderer->render($barcode, 5, 40); // Width factor (how many pixel wide every bar is), and the height in pixels
86+
````
87+
88+
### HTML
89+
Gives HTML to use inline in a full HTML document.
90+
```php
91+
$renderer = new Picqer\Barcode\Renderers\HtmlRenderer();
92+
$renderer->setForegroundColor('red'); // Give a color for the bars, the background is always white
93+
94+
$renderer->render($barcode, 450.20, 75); // Width and height support floats
95+
````
96+
97+
### Dynamic HTML
98+
Give HTML here the barcode is using the full width and height, to put inside a container/div that has a fixed size.
99+
```php
100+
$renderer = new Picqer\Barcode\Renderers\DynamicHtmlRenderer();
101+
$renderer->setForegroundColor('red'); // Give a color for the bars, the background is always white
102+
103+
$renderer->render($barcode);
104+
````
105+
106+
You can put the rendered HTML inside a div like this:
107+
```html
108+
<div style="width: 400px; height: 75px"><?php echo $renderedBarcode; ?></div>
67109
```
68110

69111
## 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.
112+
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.
113+
114+
You can find all supported types in the [src/Types](src/Types) folder.
71115

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

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

109153
## 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.
154+
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.
111155

112156
## Examples
113157

114158
### Embedded PNG image in HTML
115159
```php
160+
$barcode = (new Picqer\Barcode\Types\TypeCode128())->getBarcode('081231723897');
161+
$renderer = new Picqer\Barcode\Renderers\PngRenderer();
162+
echo '<img src="data:image/png;base64,' . base64_encode($renderer->render($barcode)) . '">';
163+
```
164+
165+
### Save JPG barcode to disk
166+
```php
167+
$barcode = (new Picqer\Barcode\Types\TypeCodabar())->getBarcode('081231723897');
168+
$renderer = new Picqer\Barcode\Renderers\JpgRenderer();
169+
170+
file_put_contents('barcode.jpg', $renderer->render($barcode));
171+
```
172+
173+
### Oneliner SVG output to disk
174+
```php
175+
file_put_contents('barcode.svg', (new Picqer\Barcode\Renderers\SvgRenderer())->render((new Picqer\Barcode\Types\TypeKix())->getBarcode('6825ME601')));
176+
```
177+
178+
## Upgrading to v3
179+
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.
180+
181+
If you want to convert to the new style, here is an example:
182+
```php
183+
// Old style
184+
$generator = new Picqer\Barcode\BarcodeGeneratorSVG();
185+
echo $generator->getBarcode('081231723897', $generator::TYPE_CODE_128);
186+
187+
// New style
188+
$barcode = (new Picqer\Barcode\Types\TypeCode128())->getBarcode('081231723897');
189+
$renderer = new Picqer\Barcode\Renderers\SvgRenderer();
190+
echo $renderer->render($barcode);
191+
```
192+
193+
The width in the SVG and HTML renderer is now the width of the end result, instead of the widthFactor. If you want to keep dynamic widths, you can get the width of the encoded Barcode and multiply it by the widthFactor to get the same result as before. See here an example for a widthFactor of 2:
194+
```php
195+
// Old style
196+
$generator = new Picqer\Barcode\BarcodeGeneratorSVG();
197+
echo $generator->getBarcode('081231723897', $generator::TYPE_CODE_128, 2. 30);
198+
199+
// New style
200+
$barcode = (new Picqer\Barcode\Types\TypeCode128())->getBarcode('081231723897');
201+
$renderer = new Picqer\Barcode\Renderers\SvgRenderer();
202+
echo $renderer->render($barcode, $barcode->getWidth() * 2, 30);
203+
```
204+
205+
---
206+
207+
## Previous style generators
208+
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.
209+
210+
### Usage
211+
Initiate the barcode generator for the output you want, then call the ->getBarcode() routine as many times as you want.
212+
213+
```php
214+
<?php
215+
require 'vendor/autoload.php';
216+
217+
// This will output the barcode as HTML output to display in the browser
218+
$generator = new Picqer\Barcode\BarcodeGeneratorHTML();
219+
echo $generator->getBarcode('081231723897', $generator::TYPE_CODE_128);
220+
```
221+
222+
Will result in this beauty:<br>
223+
![Barcode 081231723897 as Code 128](tests/verified-files/081231723897-ean13.svg)
224+
225+
The `getBarcode()` method accepts the following parameters:
226+
- `$barcode` String needed to encode in the barcode
227+
- `$type` Type of barcode, use the constants defined in the class
228+
- `$widthFactor` Width is based on the length of the data, with this factor you can make the barcode bars wider than default
229+
- `$height` The total height of the barcode in pixels
230+
- `$foregroundColor` Hex code as string, or array of RGB, of the colors of the bars (the foreground color)
231+
232+
Example of usage of all parameters:
233+
234+
```php
235+
<?php
236+
237+
require 'vendor/autoload.php';
238+
239+
$redColor = [255, 0, 0];
240+
241+
$generator = new Picqer\Barcode\BarcodeGeneratorPNG();
242+
file_put_contents('barcode.png', $generator->getBarcode('081231723897', $generator::TYPE_CODE_128, 3, 50, $redColor));
243+
```
244+
245+
### Image types
246+
```php
247+
$generatorSVG = new Picqer\Barcode\BarcodeGeneratorSVG(); // Vector based SVG
248+
$generatorPNG = new Picqer\Barcode\BarcodeGeneratorPNG(); // Pixel based PNG
249+
$generatorJPG = new Picqer\Barcode\BarcodeGeneratorJPG(); // Pixel based JPG
250+
$generatorHTML = new Picqer\Barcode\BarcodeGeneratorHTML(); // Pixel based HTML
251+
$generatorHTML = new Picqer\Barcode\BarcodeGeneratorDynamicHTML(); // Vector based HTML
252+
```
253+
254+
#### Embedded PNG image in HTML
255+
```php
116256
$generator = new Picqer\Barcode\BarcodeGeneratorPNG();
117257
echo '<img src="data:image/png;base64,' . base64_encode($generator->getBarcode('081231723897', $generator::TYPE_CODE_128)) . '">';
118258
```
119259

120-
### Save JPG barcode to disk
260+
#### Save JPG barcode to disk
121261
```php
122262
$generator = new Picqer\Barcode\BarcodeGeneratorJPG();
123263
file_put_contents('barcode.jpg', $generator->getBarcode('081231723897', $generator::TYPE_CODABAR));
124264
```
125265

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

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
}
1919
],
2020
"require": {
21-
"php": "^8.1",
21+
"php": "^8.2",
2222
"ext-mbstring": "*"
2323
},
2424
"require-dev": {

0 commit comments

Comments
 (0)