diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml
deleted file mode 100644
index a8ff52ab..00000000
--- a/.github/FUNDING.yml
+++ /dev/null
@@ -1 +0,0 @@
-github: casperbakker
diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml
index 5739810e..33b5a11b 100644
--- a/.github/workflows/phpunit.yml
+++ b/.github/workflows/phpunit.yml
@@ -12,7 +12,7 @@ jobs:
strategy:
matrix:
- php-versions: ['8.1', '8.2', '8.3']
+ php-versions: ['8.2', '8.3']
steps:
- name: Checkout code
diff --git a/Readme.md b/Readme.md
index dfdfea25..2692cca2 100644
--- a/Readme.md
+++ b/Readme.md
@@ -7,8 +7,6 @@ This is an easy to use, non-bloated, framework independent, barcode generator in
It creates SVG, PNG, JPG and HTML images, from the most used 1D barcode standards.
-*The codebase is based on the [TCPDF barcode generator](https://github.com/tecnickcom/TCPDF) by Nicola Asuni. This code is therefor licensed under LGPLv3.*
-
## 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.
@@ -23,51 +21,97 @@ 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.
+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 `Barcode` object with one of the barcode types.
+- Then, use one of the renderers to render the image of the bars in the `Barcode` object.
+
+> 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.
```php
getBarcode('081231723897', $generator::TYPE_CODE_128);
+// Make Barcode object of Code128 encoding.
+$barcode = (new Picqer\Barcode\Types\TypeCode128())->getBarcode('081231723897');
+
+// Output the barcode as HTML in the browser with a HTML Renderer
+$renderer = new Picqer\Barcode\Renderers\HtmlRenderer();
+echo $renderer->render($barcode);
```
Will result in this beauty:
![Barcode 081231723897 as Code 128](tests/verified-files/081231723897-ean13.svg)
-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:
-
+Each renderer has their own options. For example, you can set the height, width and color of a PNG:
```php
getBarcode('081231723897', $generator::TYPE_CODE_128, 3, 50, $redColor));
+$barcode = (new Picqer\Barcode\Types\TypeCode128())->getBarcode('081231723897');
+$renderer = new Picqer\Barcode\Renderers\PngRenderer();
+$renderer->setForegroundColor($colorRed);
+
+// Save PNG to the filesystem, with widthFactor 3 and height of 50 pixels
+file_put_contents('barcode.png', $renderer->render($barcode, 3, 50));
```
-## Image types
+## Image renderers
+Available image renderers: SVG, PNG, JPG and HTML.
+
+Each renderer has their own options. Only the barcode is required, the rest is optional. Here are all the options for each renderers:
+
+### SVG
+A vector based SVG image. Gives the best quality to print.
```php
-$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
+$renderer = new Picqer\Barcode\Renderers\SvgRenderer();
+$renderer->setForegroundColor('red'); // Give a color for the bars, the background is always white
+$renderer->setSvgType($renderer::TYPE_SVG_INLINE); // Changes the output to be used inline inside HTML documents, instead of a standalone SVG image (default)
+$renderer->setSvgType($renderer::TYPE_SVG_STANDALONE); // If you want to force the default, create a stand alone SVG image
+
+$renderer->render($barcode, 450.20, 75); // Width and height support floats
+````
+
+### PNG + JPG
+All options for PNG and JPG are the same.
+```php
+$renderer = new Picqer\Barcode\Renderers\PngRenderer();
+$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.
+$renderer->useGd(); // If you have Imagick and GD installed, but want to use GD
+$renderer->useImagick(); // If you have Imagick and GD installed, but want to use Imagick
+
+$renderer->render($barcode, 5, 40); // Width factor (how many pixel wide every bar is), and the height in pixels
+````
+
+### HTML
+Gives HTML to use inline in a full HTML document.
+```php
+$renderer = new Picqer\Barcode\Renderers\HtmlRenderer();
+$renderer->setForegroundColor('red'); // Give a color for the bars, the background is always white
+
+$renderer->render($barcode, 450.20, 75); // Width and height support floats
+````
+
+### 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.
+```php
+$renderer = new Picqer\Barcode\Renderers\DynamicHtmlRenderer();
+$renderer->setForegroundColor('red'); // Give a color for the bars, the background is always white
+
+$renderer->render($barcode);
+````
+
+You can put the rendered HTML inside a div like this:
+```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.
+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](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.
@@ -107,23 +151,122 @@ Most used types are TYPE_CODE_128 and TYPE_CODE_39. Because of the best scanner
[See example images for all supported barcode types](examples.md)
## A note about PNG and JPG images
-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.
+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.
## Examples
### Embedded PNG image in HTML
```php
+$barcode = (new Picqer\Barcode\Types\TypeCode128())->getBarcode('081231723897');
+$renderer = new Picqer\Barcode\Renderers\PngRenderer();
+echo '';
+```
+
+### Save JPG barcode to disk
+```php
+$barcode = (new Picqer\Barcode\Types\TypeCodabar())->getBarcode('081231723897');
+$renderer = new Picqer\Barcode\Renderers\JpgRenderer();
+
+file_put_contents('barcode.jpg', $renderer->render($barcode));
+```
+
+### Oneliner SVG output to disk
+```php
+file_put_contents('barcode.svg', (new Picqer\Barcode\Renderers\SvgRenderer())->render((new Picqer\Barcode\Types\TypeKix())->getBarcode('6825ME601')));
+```
+
+## 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.
+
+If you want to convert to the new style, here is an example:
+```php
+// Old style
+$generator = new Picqer\Barcode\BarcodeGeneratorSVG();
+echo $generator->getBarcode('081231723897', $generator::TYPE_CODE_128);
+
+// New style
+$barcode = (new Picqer\Barcode\Types\TypeCode128())->getBarcode('081231723897');
+$renderer = new Picqer\Barcode\Renderers\SvgRenderer();
+echo $renderer->render($barcode);
+```
+
+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:
+```php
+// Old style
+$generator = new Picqer\Barcode\BarcodeGeneratorSVG();
+echo $generator->getBarcode('081231723897', $generator::TYPE_CODE_128, 2. 30);
+
+// New style
+$barcode = (new Picqer\Barcode\Types\TypeCode128())->getBarcode('081231723897');
+$renderer = new Picqer\Barcode\Renderers\SvgRenderer();
+echo $renderer->render($barcode, $barcode->getWidth() * 2, 30);
+```
+
+---
+
+## 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.
+
+```php
+getBarcode('081231723897', $generator::TYPE_CODE_128);
+```
+
+Will result in this beauty:
+![Barcode 081231723897 as Code 128](tests/verified-files/081231723897-ean13.svg)
+
+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
+getBarcode('081231723897', $generator::TYPE_CODE_128, 3, 50, $redColor));
+```
+
+### Image types
+```php
+$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
+```
+
+#### Embedded PNG image in HTML
+```php
$generator = new Picqer\Barcode\BarcodeGeneratorPNG();
echo '';
```
-### Save JPG barcode to disk
+#### Save JPG barcode to disk
```php
$generator = new Picqer\Barcode\BarcodeGeneratorJPG();
file_put_contents('barcode.jpg', $generator->getBarcode('081231723897', $generator::TYPE_CODABAR));
```
-### Oneliner SVG output to disk
+#### Oneliner SVG output to disk
```php
file_put_contents('barcode.svg', (new Picqer\Barcode\BarcodeGeneratorSVG())->getBarcode('6825ME601', Picqer\Barcode\BarcodeGeneratorSVG::TYPE_KIX));
```
+
+---
+*The codebase is based on the [TCPDF barcode generator](https://github.com/tecnickcom/TCPDF) by Nicola Asuni. This code is therefor licensed under LGPLv3.*
diff --git a/composer.json b/composer.json
index dadc0d2c..e65b8e8c 100644
--- a/composer.json
+++ b/composer.json
@@ -18,7 +18,7 @@
}
],
"require": {
- "php": "^8.1",
+ "php": "^8.2",
"ext-mbstring": "*"
},
"require-dev": {
diff --git a/examples.md b/examples.md
index 00329a5b..689c1928 100644
--- a/examples.md
+++ b/examples.md
@@ -2,137 +2,147 @@
These are examples of supported barcodes with this library.
+All types can be found in the src/Types directory.
-### C39
-![Barcode 1234567890ABC as C39](tests/verified-files/C39-1234567890ABC.svg)
+### TypeCode39
-### C39+
+![Barcode 1234567890ABC as Picqer\Barcode\Types\TypeCode39](tests/verified-files/TypeCode39-1234567890ABC.svg)
-![Barcode 1234567890ABC as C39+](tests/verified-files/C39+-1234567890ABC.svg)
+### TypeCode39Checksum
-### C39E
+![Barcode 1234567890ABC as Picqer\Barcode\Types\TypeCode39Checksum](tests/verified-files/TypeCode39Checksum-1234567890ABC.svg)
-![Barcode 1234567890abcABC as C39E](tests/verified-files/C39E-1234567890abcABC.svg)
+### TypeCode39Extended
-### C39E+
+![Barcode 1234567890abcABC as Picqer\Barcode\Types\TypeCode39Extended](tests/verified-files/TypeCode39Extended-1234567890abcABC.svg)
-![Barcode 1234567890abcABC as C39E+](tests/verified-files/C39E+-1234567890abcABC.svg)
+### TypeCode39ExtendedChecksum
-### C93
+![Barcode 1234567890abcABC as Picqer\Barcode\Types\TypeCode39ExtendedChecksum](tests/verified-files/TypeCode39ExtendedChecksum-1234567890abcABC.svg)
-![Barcode 1234567890abcABC as C93](tests/verified-files/C93-1234567890abcABC.svg)
+### TypeCode93
-### S25
+![Barcode 1234567890abcABC as Picqer\Barcode\Types\TypeCode93](tests/verified-files/TypeCode93-1234567890abcABC.svg)
-![Barcode 1234567890 as S25](tests/verified-files/S25-1234567890.svg)
+### TypeStandard2of5
-### S25+
+![Barcode 1234567890 as Picqer\Barcode\Types\TypeStandard2of5](tests/verified-files/TypeStandard2of5-1234567890.svg)
-![Barcode 1234567890 as S25+](tests/verified-files/S25+-1234567890.svg)
+### TypeStandard2of5Checksum
-### I25
+![Barcode 1234567890 as Picqer\Barcode\Types\TypeStandard2of5Checksum](tests/verified-files/TypeStandard2of5Checksum-1234567890.svg)
-![Barcode 1234567890 as I25](tests/verified-files/I25-1234567890.svg)
+### TypeInterleaved25
-### I25+
+![Barcode 1234567890 as Picqer\Barcode\Types\TypeInterleaved25](tests/verified-files/TypeInterleaved25-1234567890.svg)
-![Barcode 1234567890 as I25+](tests/verified-files/I25+-1234567890.svg)
+### TypeInterleaved25Checksum
-### EAN13
+![Barcode 1234567890 as Picqer\Barcode\Types\TypeInterleaved25Checksum](tests/verified-files/TypeInterleaved25Checksum-1234567890.svg)
-![Barcode 081231723897 as EAN13](tests/verified-files/EAN13-081231723897.svg)
+### TypeEan13
-![Barcode 0049000004632 as EAN13](tests/verified-files/EAN13-0049000004632.svg)
+![Barcode 081231723897 as Picqer\Barcode\Types\TypeEan13](tests/verified-files/TypeEan13-081231723897.svg)
-![Barcode 004900000463 as EAN13](tests/verified-files/EAN13-004900000463.svg)
+![Barcode 0049000004632 as Picqer\Barcode\Types\TypeEan13](tests/verified-files/TypeEan13-0049000004632.svg)
-### ITF14
+![Barcode 004900000463 as Picqer\Barcode\Types\TypeEan13](tests/verified-files/TypeEan13-004900000463.svg)
-![Barcode 00012345600012 as ITF14](tests/verified-files/ITF14-00012345600012.svg)
+### TypeITF14
-![Barcode 05400141288766 as ITF14](tests/verified-files/ITF14-05400141288766.svg)
+![Barcode 00012345600012 as Picqer\Barcode\Types\TypeITF14](tests/verified-files/TypeITF14-00012345600012.svg)
-### C128
+![Barcode 05400141288766 as Picqer\Barcode\Types\TypeITF14](tests/verified-files/TypeITF14-05400141288766.svg)
-![Barcode 081231723897 as C128](tests/verified-files/C128-081231723897.svg)
+### TypeCode128
-![Barcode 1234567890abcABC-283*33 as C128](tests/verified-files/C128-1234567890abcABC-283-33.svg)
+![Barcode 081231723897 as Picqer\Barcode\Types\TypeCode128](tests/verified-files/TypeCode128-081231723897.svg)
-### C128A
+![Barcode 1234567890abcABC-283*33 as Picqer\Barcode\Types\TypeCode128](tests/verified-files/TypeCode128-1234567890abcABC-283-33.svg)
-![Barcode 1234567890 as C128A](tests/verified-files/C128A-1234567890.svg)
+### TypeCode128A
-### C128B
+![Barcode 1234567890 as Picqer\Barcode\Types\TypeCode128A](tests/verified-files/TypeCode128A-1234567890.svg)
-![Barcode 081231723897 as C128B](tests/verified-files/C128B-081231723897.svg)
+### TypeCode128B
-![Barcode 1234567890abcABC-283*33 as C128B](tests/verified-files/C128B-1234567890abcABC-283-33.svg)
+![Barcode 081231723897 as Picqer\Barcode\Types\TypeCode128B](tests/verified-files/TypeCode128B-081231723897.svg)
-### EAN2
+![Barcode 1234567890abcABC-283*33 as Picqer\Barcode\Types\TypeCode128B](tests/verified-files/TypeCode128B-1234567890abcABC-283-33.svg)
-![Barcode 22 as EAN2](tests/verified-files/EAN2-22.svg)
+### TypeUpcExtension2
-### EAN5
+![Barcode 22 as Picqer\Barcode\Types\TypeUpcExtension2](tests/verified-files/TypeUpcExtension2-22.svg)
-![Barcode 1234567890abcABC-283*33 as EAN5](tests/verified-files/EAN5-1234567890abcABC-283-33.svg)
+### TypeUpcExtension5
-### EAN8
+![Barcode 1234567890abcABC-283*33 as Picqer\Barcode\Types\TypeUpcExtension5](tests/verified-files/TypeUpcExtension5-1234567890abcABC-283-33.svg)
-![Barcode 1234568 as EAN8](tests/verified-files/EAN8-1234568.svg)
+### TypeEan8
-### UPCA
+![Barcode 1234568 as Picqer\Barcode\Types\TypeEan8](tests/verified-files/TypeEan8-1234568.svg)
-![Barcode 123456789 as UPCA](tests/verified-files/UPCA-123456789.svg)
+### TypeUpcA
-### UPCE
+![Barcode 123456789 as Picqer\Barcode\Types\TypeUpcA](tests/verified-files/TypeUpcA-123456789.svg)
-![Barcode 123456789 as UPCE](tests/verified-files/UPCE-123456789.svg)
+### TypeUpcE
-### MSI
+![Barcode 123456789 as Picqer\Barcode\Types\TypeUpcE](tests/verified-files/TypeUpcE-123456789.svg)
-![Barcode 123456789 as MSI](tests/verified-files/MSI-123456789.svg)
+### TypeMsi
-### MSI+
+![Barcode 123456789 as Picqer\Barcode\Types\TypeMsi](tests/verified-files/TypeMsi-123456789.svg)
-![Barcode 123456789 as MSI+](tests/verified-files/MSI+-123456789.svg)
+### TypeMsiChecksum
-### POSTNET
+![Barcode 123456789 as Picqer\Barcode\Types\TypeMsiChecksum](tests/verified-files/TypeMsiChecksum-123456789.svg)
-![Barcode 123456789 as POSTNET](tests/verified-files/POSTNET-123456789.svg)
+### TypePostnet
-### PLANET
+![Barcode 123456789 as Picqer\Barcode\Types\TypePostnet](tests/verified-files/TypePostnet-123456789.svg)
-![Barcode 123456789 as PLANET](tests/verified-files/PLANET-123456789.svg)
+### TypePlanet
-### RMS4CC
+![Barcode 123456789 as Picqer\Barcode\Types\TypePlanet](tests/verified-files/TypePlanet-123456789.svg)
-![Barcode 123456789 as RMS4CC](tests/verified-files/RMS4CC-123456789.svg)
+### TypeRms4cc
-### KIX
+![Barcode 123456789 as Picqer\Barcode\Types\TypeRms4cc](tests/verified-files/TypeRms4cc-123456789.svg)
-![Barcode 123456789 as KIX](tests/verified-files/KIX-123456789.svg)
+### TypeKix
-### IMB
+![Barcode 123456789 as Picqer\Barcode\Types\TypeKix](tests/verified-files/TypeKix-123456789.svg)
-![Barcode 123456789 as IMB](tests/verified-files/IMB-123456789.svg)
+### TypeIntelligentMailBarcode
-### CODABAR
+![Barcode 123456789 as Picqer\Barcode\Types\TypeIntelligentMailBarcode](tests/verified-files/TypeIntelligentMailBarcode-123456789.svg)
-![Barcode 123456789 as CODABAR](tests/verified-files/CODABAR-123456789.svg)
+### TypeCodabar
-### CODE11
+![Barcode 123456789 as Picqer\Barcode\Types\TypeCodabar](tests/verified-files/TypeCodabar-123456789.svg)
-![Barcode 123456789 as CODE11](tests/verified-files/CODE11-123456789.svg)
+### TypeCode11
-### PHARMA
+![Barcode 123456789 as Picqer\Barcode\Types\TypeCode11](tests/verified-files/TypeCode11-123456789.svg)
-![Barcode 123456789 as PHARMA](tests/verified-files/PHARMA-123456789.svg)
+### TypePharmacode
-### PHARMA2T
+![Barcode 123456789 as Picqer\Barcode\Types\TypePharmacode](tests/verified-files/TypePharmacode-123456789.svg)
-![Barcode 123456789 as PHARMA2T](tests/verified-files/PHARMA2T-123456789.svg)
+### TypePharmacodeTwoCode
+
+![Barcode 123456789 as Picqer\Barcode\Types\TypePharmacodeTwoCode](tests/verified-files/TypePharmacodeTwoCode-123456789.svg)
+
+### TypeTelepen
+
+![Barcode 1234567890ASCD as Picqer\Barcode\Types\TypeTelepen](tests/verified-files/TypeTelepen-1234567890ASCD.svg)
+
+### TypeTelepenNumeric
+
+![Barcode 1234567890 as Picqer\Barcode\Types\TypeTelepenNumeric](tests/verified-files/TypeTelepenNumeric-1234567890.svg)
diff --git a/generate-examples.php b/generate-examples.php
index 5929908f..7be0f009 100644
--- a/generate-examples.php
+++ b/generate-examples.php
@@ -1,9 +1,5 @@
getBarcode('081231723897', $generatorSVG::TYPE_EAN_13));
-file_put_contents('tests/verified-files/081231723897-ean13-fractional-width.svg', $generatorSVG->getBarcode('081231723897', $generatorSVG::TYPE_EAN_13, 0.25, 25.75));
+$barcode = $typeEncoderEan13->getBarcode('081231723897');
+file_put_contents('tests/verified-files/081231723897-ean13.svg', $svgRenderer->render($barcode, $barcode->getWidth() * 2));
+file_put_contents('tests/verified-files/081231723897-ean13-fractional-width.svg', $svgRenderer->render($barcode, $barcode->getWidth() * 0.25, 25.75));
-$generatorHTML = new Picqer\Barcode\BarcodeGeneratorHTML();
-file_put_contents('tests/verified-files/081231723897-code128.html', $generatorHTML->getBarcode('081231723897', $generatorHTML::TYPE_CODE_128));
+$barcode = $typeEncoderCode128->getBarcode('081231723897');
+file_put_contents('tests/verified-files/081231723897-code128.html', $htmlRenderer->render($barcode, $barcode->getWidth() * 2));
-file_put_contents('tests/verified-files/12345678903-imb.html', $generatorHTML->getBarcode('12345678903', $generatorHTML::TYPE_IMB));
+$barcode = $typeEncoderIMB->getBarcode('12345678903');
+file_put_contents('tests/verified-files/12345678903-imb.html', $htmlRenderer->render($barcode, $barcode->getWidth() * 2));
-$generatorDynamicHTML = new Picqer\Barcode\BarcodeGeneratorDynamicHTML();
-file_put_contents('tests/verified-files/081231723897-dynamic-code128.html', $generatorDynamicHTML->getBarcode('081231723897', $generatorDynamicHTML::TYPE_CODE_128));
+$barcode = $typeEncoderCode128->getBarcode('081231723897');
+file_put_contents('tests/verified-files/081231723897-dynamic-code128.html', $dynamicHtmlRenderer->render($barcode));
-file_put_contents('tests/verified-files/12345678903-dynamic-imb.html', $generatorDynamicHTML->getBarcode('12345678903', $generatorDynamicHTML::TYPE_IMB));
+$barcode = $typeEncoderIMB->getBarcode('12345678903');
+file_put_contents('tests/verified-files/12345678903-dynamic-imb.html', $dynamicHtmlRenderer->render($barcode));
-$generatorSVG = new Picqer\Barcode\BarcodeGeneratorSVG();
-file_put_contents('tests/verified-files/0049000004632-ean13.svg', $generatorSVG->getBarcode('0049000004632', $generatorSVG::TYPE_EAN_13));
+$barcode = $typeEncoderEan13->getBarcode('0049000004632');
+file_put_contents('tests/verified-files/0049000004632-ean13.svg', $svgRenderer->render($barcode, $barcode->getWidth() * 2));
-// New style of verified files
+// New style of verified files, defined in VerifiedBarcodeTest.php
require(__DIR__ . '/tests/VerifiedBarcodeTest.php');
$verifiedFiles = VerifiedBarcodeTest::$supportedBarcodes;
-$generatorSVG = new Picqer\Barcode\BarcodeGeneratorSVG();
foreach ($verifiedFiles as $verifiedFile) {
- foreach ($verifiedFile['barcodes'] as $barcode) {
- file_put_contents('tests/verified-files/' . getSaveFilename($verifiedFile['type'] . '-' . $barcode) . '.svg', $generatorSVG->getBarcode($barcode, $verifiedFile['type']));
+ foreach ($verifiedFile['barcodes'] as $barcodeText) {
+ $barcode = (new $verifiedFile['type']())->getBarcode($barcodeText);
+ $result = $svgRenderer->render($barcode, $barcode->getWidth() * 2);
+
+ file_put_contents('tests/verified-files/' . Picqer\Barcode\Helpers\StringHelpers::getSafeFilenameFrom($verifiedFile['type'] . '-' . $barcodeText) . '.svg', $result);
}
}
diff --git a/phpstan.neon.dist b/phpstan.neon.dist
index 04053543..0a16ff99 100644
--- a/phpstan.neon.dist
+++ b/phpstan.neon.dist
@@ -1,4 +1,4 @@
parameters:
paths:
- src
- level: 4
+ level: 5
diff --git a/src/Barcode.php b/src/Barcode.php
index 4ab9f20e..5b210b33 100644
--- a/src/Barcode.php
+++ b/src/Barcode.php
@@ -4,17 +4,18 @@
class Barcode
{
- protected $barcode;
- protected $width = 0;
- protected $height = 0;
- protected $bars = [];
+ protected string $barcode;
+ protected int $width = 0;
+ protected int $height = 0;
+ protected array $bars = [];
public function __construct(string $barcode)
{
$this->barcode = $barcode;
}
- public function addBar(BarcodeBar $bar)
+ // Add a bar to the barcode, either a bar or a space, at the right side of the barcode
+ public function addBar(BarcodeBar $bar): void
{
$this->bars[] = $bar;
$this->width += $bar->getWidth();
@@ -40,4 +41,4 @@ public function getBars(): array
{
return $this->bars;
}
-}
\ No newline at end of file
+}
diff --git a/src/BarcodeBar.php b/src/BarcodeBar.php
index 332a6612..28bd595e 100644
--- a/src/BarcodeBar.php
+++ b/src/BarcodeBar.php
@@ -2,12 +2,13 @@
namespace Picqer\Barcode;
-class BarcodeBar
+// Represents a single bar or space in a barcode
+readonly class BarcodeBar
{
- protected $width;
- protected $height;
- protected $positionVertical;
- protected $type;
+ protected int $width;
+ protected int $height;
+ protected int $positionVertical;
+ protected int $type;
const TYPE_BAR = 1;
const TYPE_SPACING = 0;
@@ -39,4 +40,4 @@ public function isBar(): bool
{
return $this->type === self::TYPE_BAR;
}
-}
\ No newline at end of file
+}
diff --git a/src/BarcodeGenerator.php b/src/BarcodeGenerator.php
index d7c1b2e8..8555b061 100644
--- a/src/BarcodeGenerator.php
+++ b/src/BarcodeGenerator.php
@@ -45,6 +45,7 @@
use Picqer\Barcode\Types\TypeEan13;
use Picqer\Barcode\Types\TypeEan8;
use Picqer\Barcode\Types\TypeIntelligentMailBarcode;
+use Picqer\Barcode\Types\TypeInterface;
use Picqer\Barcode\Types\TypeInterleaved25;
use Picqer\Barcode\Types\TypeInterleaved25Checksum;
use Picqer\Barcode\Types\TypeITF14;
@@ -101,14 +102,17 @@ abstract class BarcodeGenerator
const TYPE_PHARMA_CODE = 'PHARMA';
const TYPE_PHARMA_CODE_TWO_TRACKS = 'PHARMA2T';
+ /**
+ * @throws UnknownTypeException
+ */
protected function getBarcodeData(string $code, string $type): Barcode
{
$barcodeDataBuilder = $this->createDataBuilderForType($type);
- return $barcodeDataBuilder->getBarcodeData($code);
+ return $barcodeDataBuilder->getBarcode($code);
}
- protected function createDataBuilderForType(string $type)
+ protected function createDataBuilderForType(string $type): TypeInterface
{
switch (strtoupper($type)) {
case self::TYPE_CODE_32:
diff --git a/src/BarcodeGeneratorDynamicHTML.php b/src/BarcodeGeneratorDynamicHTML.php
index 57ce23ea..c08a4edf 100644
--- a/src/BarcodeGeneratorDynamicHTML.php
+++ b/src/BarcodeGeneratorDynamicHTML.php
@@ -2,10 +2,10 @@
namespace Picqer\Barcode;
+use Picqer\Barcode\Exceptions\UnknownTypeException;
+
class BarcodeGeneratorDynamicHTML extends BarcodeGenerator
{
- private const WIDTH_PRECISION = 6;
-
/**
* Return an HTML representation of barcode.
* This 'dynamic' version uses percentage based widths and heights, resulting in a vector-y qualitative result.
@@ -14,31 +14,15 @@ class BarcodeGeneratorDynamicHTML extends BarcodeGenerator
* @param BarcodeGenerator::TYPE_* $type (string) type of barcode
* @param string $foregroundColor Foreground color for bar elements as '#333' or 'orange' for example (background is transparent).
* @return string HTML code.
+ * @throws UnknownTypeException
*/
public function getBarcode(string $barcode, $type, string $foregroundColor = 'black'): string
{
$barcodeData = $this->getBarcodeData($barcode, $type);
- $html = '
' . PHP_EOL;
+ $renderer = new \Picqer\Barcode\Renderers\DynamicHtmlRenderer();
+ $renderer->setForegroundColor($foregroundColor);
- return $html;
+ return $renderer->render($barcodeData);
}
}
diff --git a/src/BarcodeGeneratorHTML.php b/src/BarcodeGeneratorHTML.php
index 75a98fe6..bc30f252 100644
--- a/src/BarcodeGeneratorHTML.php
+++ b/src/BarcodeGeneratorHTML.php
@@ -2,6 +2,8 @@
namespace Picqer\Barcode;
+use Picqer\Barcode\Exceptions\UnknownTypeException;
+
class BarcodeGeneratorHTML extends BarcodeGenerator
{
/**
@@ -14,31 +16,17 @@ class BarcodeGeneratorHTML extends BarcodeGenerator
* @param int $height Height of a single bar element in pixels.
* @param string $foregroundColor Foreground color for bar elements as '#333' or 'orange' for example (background is transparent).
* @return string HTML code.
+ * @throws UnknownTypeException
*/
- public function getBarcode($barcode, $type, int $widthFactor = 2, int $height = 30, string $foregroundColor = 'black'): string
+ public function getBarcode(string $barcode, $type, int $widthFactor = 2, int $height = 30, string $foregroundColor = 'black'): string
{
$barcodeData = $this->getBarcodeData($barcode, $type);
- $html = '