Skip to content

Commit d06a28f

Browse files
committed
Add new interface usage to readme
1 parent 40cfc3f commit d06a28f

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

Diff for: Readme.md

+22-13
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Install through [composer](https://getcomposer.org/doc/00-intro.md):
1818
composer require picqer/php-barcode-generator
1919
```
2020

21-
If you want to generate PNG or JPG images, you need the GD library or Imagick installed on your system as well.
21+
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.
2222

2323
## Usage
2424
You want a barcode for a specific "type" (for example Code 128 or UPC) in a specific image format (for example PNG or SVG).
@@ -54,21 +54,28 @@ $barcode = (new Picqer\Barcode\Types\TypeCode128())->getBarcode('081231723897');
5454
$renderer = new Picqer\Barcode\Renderers\PngRenderer();
5555
$renderer->setForegroundColor($colorRed);
5656

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));
57+
// Save PNG to the filesystem, with widthFactor 3 (width of the barcode x 3) and height of 50 pixels
58+
file_put_contents('barcode.png', $renderer->render($barcode, $barcode->getWidth() * 3, 50));
5959
```
6060

6161
## Image renderers
6262
Available image renderers: SVG, PNG, JPG and HTML.
6363

64-
Each renderer has their own options. Only the barcode is required, the rest is optional. Here are all the options for each renderers:
64+
They all conform to the RendererInterface and have the same `render()` method. Some renderers have extra options as well, via set*() methods.
65+
66+
### Widths
67+
The render() method needs the Barcode object, the width and height. **For JPG/PNG images**, you only get a valid barcode if you give a width that is a factor of the width of the Barcode object. That is why the examples show `$barcode->getWidth() * 2` to make the image 2 times wider in pixels then the width of the barcode data. You *can* give an arbitrary number as width and the image will be scaled as best as possible, but without anti-aliasing, it will not be perfectly valid.
68+
69+
HTML and SVG renderers can handle any width and height, even floats.
70+
71+
Here are all the options for each renderer:
6572

6673
### SVG
6774
A vector based SVG image. Gives the best quality to print.
6875
```php
6976
$renderer = new Picqer\Barcode\Renderers\SvgRenderer();
70-
$renderer->setForegroundColor('red'); // Give a color for the bars, default is black
71-
$renderer->setBackgroundColor('blue'); // Give a color for the background, default is transparent
77+
$renderer->setForegroundColor([255, 0, 0]); // Give a color red for the bars, default is black. Give it as 3 times 0-255 values for red, green and blue.
78+
$renderer->setBackgroundColor([0, 0, 255]); // Give a color blue for the background, default is transparent. Give it as 3 times 0-255 values for red, green and blue.
7279
$renderer->setSvgType($renderer::TYPE_SVG_INLINE); // Changes the output to be used inline inside HTML documents, instead of a standalone SVG image (default)
7380
$renderer->setSvgType($renderer::TYPE_SVG_STANDALONE); // If you want to force the default, create a stand alone SVG image
7481

@@ -91,8 +98,8 @@ $renderer->render($barcode, 5, 40); // Width factor (how many pixel wide every b
9198
Gives HTML to use inline in a full HTML document.
9299
```php
93100
$renderer = new Picqer\Barcode\Renderers\HtmlRenderer();
94-
$renderer->setForegroundColor('red'); // Give a color for the bars, default is black
95-
$renderer->setBackgroundColor('blue'); // Give a color for the background, default is transparent
101+
$renderer->setForegroundColor([255, 0, 0]); // Give a color red for the bars, default is black. Give it as 3 times 0-255 values for red, green and blue.
102+
$renderer->setBackgroundColor([0, 0, 255]); // Give a color blue for the background, default is transparent. Give it as 3 times 0-255 values for red, green and blue.
96103

97104
$renderer->render($barcode, 450.20, 75); // Width and height support floats
98105
````
@@ -101,8 +108,8 @@ $renderer->render($barcode, 450.20, 75); // Width and height support floats
101108
Give HTML here the barcode is using the full width and height, to put inside a container/div that has a fixed size.
102109
```php
103110
$renderer = new Picqer\Barcode\Renderers\DynamicHtmlRenderer();
104-
$renderer->setForegroundColor('red'); // Give a color for the bars, default is black
105-
$renderer->setBackgroundColor('blue'); // Give a color for the background, default is transparent
111+
$renderer->setForegroundColor([255, 0, 0]); // Give a color red for the bars, default is black. Give it as 3 times 0-255 values for red, green and blue.
112+
$renderer->setBackgroundColor([0, 0, 255]); // Give a color blue for the background, default is transparent. Give it as 3 times 0-255 values for red, green and blue.
106113

107114
$renderer->render($barcode);
108115
````
@@ -163,15 +170,15 @@ If you want to use PNG or JPG images, you need to install [Imagick](https://www.
163170
```php
164171
$barcode = (new Picqer\Barcode\Types\TypeCode128())->getBarcode('081231723897');
165172
$renderer = new Picqer\Barcode\Renderers\PngRenderer();
166-
echo '<img src="data:image/png;base64,' . base64_encode($renderer->render($barcode)) . '">';
173+
echo '<img src="data:image/png;base64,' . base64_encode($renderer->render($barcode, $barcode->getWidth() * 2)) . '">';
167174
```
168175

169176
### Save JPG barcode to disk
170177
```php
171178
$barcode = (new Picqer\Barcode\Types\TypeCodabar())->getBarcode('081231723897');
172179
$renderer = new Picqer\Barcode\Renderers\JpgRenderer();
173180

174-
file_put_contents('barcode.jpg', $renderer->render($barcode));
181+
file_put_contents('barcode.jpg', $renderer->render($barcode, $barcode->getWidth() * 2));
175182
```
176183

177184
### Oneliner SVG output to disk
@@ -182,6 +189,8 @@ file_put_contents('barcode.svg', (new Picqer\Barcode\Renderers\SvgRenderer())->r
182189
## Upgrading to v3
183190
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.
184191

192+
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.
193+
185194
If you want to convert to the new style, here is an example:
186195
```php
187196
// Old style
@@ -194,7 +203,7 @@ $renderer = new Picqer\Barcode\Renderers\SvgRenderer();
194203
echo $renderer->render($barcode);
195204
```
196205

197-
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:
206+
The width in the 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:
198207
```php
199208
// Old style
200209
$generator = new Picqer\Barcode\BarcodeGeneratorSVG();

0 commit comments

Comments
 (0)