From 8fdf289416a520f746af3363335da5cbeac97a20 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Sun, 22 Sep 2024 09:52:34 +0200 Subject: [PATCH 1/4] Add a RendererInterface --- generate-verified-files.php | 4 +- src/Renderers/DynamicHtmlRenderer.php | 19 ++- src/Renderers/HtmlRenderer.php | 16 +-- src/Renderers/PngRenderer.php | 18 ++- src/Renderers/RendererInterface.php | 14 ++ src/Renderers/SvgRenderer.php | 14 +- tests/verified-files/0049000004632-ean13.svg | 2 +- .../081231723897-code128-red-background.html | 58 ++++---- .../verified-files/081231723897-code128.html | 56 ++++---- .../081231723897-dynamic-code128.html | 56 ++++---- .../081231723897-ean13-fractional-width.svg | 2 +- .../081231723897-ean13-red-background.svg | 4 +- tests/verified-files/081231723897-ean13.svg | 2 +- .../12345678903-dynamic-imb.html | 130 +++++++++--------- tests/verified-files/12345678903-imb.html | 130 +++++++++--------- .../verified-files/TypeCodabar-123456789.svg | 2 +- tests/verified-files/TypeCode11-123456789.svg | 2 +- .../TypeCode128-081231723897.svg | 2 +- .../TypeCode128-1234567890abcABC-283-33.svg | 2 +- .../TypeCode128A-1234567890.svg | 2 +- .../TypeCode128B-081231723897.svg | 2 +- .../TypeCode128B-1234567890abcABC-283-33.svg | 2 +- .../TypeCode39-1234567890ABC.svg | 2 +- .../TypeCode39Checksum-1234567890ABC.svg | 2 +- .../TypeCode39Extended-1234567890abcABC.svg | 2 +- ...ode39ExtendedChecksum-1234567890abcABC.svg | 2 +- .../TypeCode93-1234567890abcABC.svg | 2 +- .../verified-files/TypeEan13-004900000463.svg | 2 +- .../TypeEan13-0049000004632.svg | 2 +- .../verified-files/TypeEan13-081231723897.svg | 2 +- tests/verified-files/TypeEan8-1234568.svg | 2 +- .../TypeITF14-00012345600012.svg | 2 +- .../TypeITF14-05400141288766.svg | 2 +- .../TypeIntelligentMailBarcode-123456789.svg | 2 +- .../TypeInterleaved25-1234567890.svg | 2 +- .../TypeInterleaved25Checksum-1234567890.svg | 2 +- tests/verified-files/TypeKix-123456789.svg | 2 +- tests/verified-files/TypeMsi-123456789.svg | 2 +- .../TypeMsiChecksum-123456789.svg | 2 +- .../TypePharmacode-123456789.svg | 2 +- .../TypePharmacodeTwoCode-123456789.svg | 2 +- tests/verified-files/TypePlanet-123456789.svg | 2 +- .../verified-files/TypePostnet-123456789.svg | 2 +- tests/verified-files/TypeRms4cc-123456789.svg | 2 +- .../TypeStandard2of5-1234567890.svg | 2 +- .../TypeStandard2of5Checksum-1234567890.svg | 2 +- .../TypeTelepen-1234567890ASCD.svg | 2 +- .../TypeTelepenNumeric-1234567890.svg | 2 +- tests/verified-files/TypeUpcA-123456789.svg | 2 +- tests/verified-files/TypeUpcE-123456789.svg | 2 +- tests/verified-files/TypeUpcExtension2-22.svg | 2 +- ...eUpcExtension5-1234567890abcABC-283-33.svg | 2 +- 52 files changed, 308 insertions(+), 291 deletions(-) create mode 100644 src/Renderers/RendererInterface.php diff --git a/generate-verified-files.php b/generate-verified-files.php index 3d02f76b..d5135460 100644 --- a/generate-verified-files.php +++ b/generate-verified-files.php @@ -15,13 +15,13 @@ file_put_contents('tests/verified-files/081231723897-ean13-fractional-width.svg', $svgRenderer->render($barcode, $barcode->getWidth() * 0.25, 25.75)); $svgRendererRed = new Picqer\Barcode\Renderers\SvgRenderer(); -$svgRendererRed->setBackgroundColor('red'); +$svgRendererRed->setBackgroundColor([255, 0, 0]); file_put_contents('tests/verified-files/081231723897-ean13-red-background.svg', $svgRendererRed->render($barcode, $barcode->getWidth() * 2)); $barcode = $typeEncoderCode128->getBarcode('081231723897'); file_put_contents('tests/verified-files/081231723897-code128.html', $htmlRenderer->render($barcode, $barcode->getWidth() * 2)); $htmlRendererRed = new Picqer\Barcode\Renderers\HtmlRenderer(); -$htmlRendererRed->setBackgroundColor('red'); +$htmlRendererRed->setBackgroundColor([255, 0, 0]); file_put_contents('tests/verified-files/081231723897-code128-red-background.html', $htmlRendererRed->render($barcode, $barcode->getWidth() * 2)); $barcode = $typeEncoderIMB->getBarcode('12345678903'); diff --git a/src/Renderers/DynamicHtmlRenderer.php b/src/Renderers/DynamicHtmlRenderer.php index 45500e56..c19de85e 100644 --- a/src/Renderers/DynamicHtmlRenderer.php +++ b/src/Renderers/DynamicHtmlRenderer.php @@ -5,16 +5,17 @@ use Picqer\Barcode\Barcode; use Picqer\Barcode\BarcodeBar; -class DynamicHtmlRenderer +class DynamicHtmlRenderer implements RendererInterface { protected const WIDTH_PRECISION = 6; - protected string $foregroundColor = 'black'; - protected ?string $backgroundColor = null; + protected array $foregroundColor = [0, 0, 0]; + protected ?array $backgroundColor = null; - public function render(Barcode $barcode): string + // Width and height are ignored in this renderer + public function render(Barcode $barcode, float $width = 200, float $height = 30): string { - $html = '
' . PHP_EOL; + $html = '
' . PHP_EOL; $positionHorizontal = 0; /** @var BarcodeBar $bar */ @@ -26,7 +27,7 @@ public function render(Barcode $barcode): string $positionVertical = round(($bar->getPositionVertical() / $barcode->getHeight() * 100), 3); // draw a vertical bar - $html .= '
 
' . PHP_EOL; + $html .= '
 
' . PHP_EOL; } $positionHorizontal += $barWidth; @@ -37,15 +38,13 @@ public function render(Barcode $barcode): string return $html; } - // Use HTML color definitions, like 'red' or '#ff0000' - public function setForegroundColor(string $color): self + public function setForegroundColor(array $color): self { $this->foregroundColor = $color; return $this; } - // Use HTML color definitions, like 'red' or '#ff0000' - public function setBackgroundColor(?string $color): self + public function setBackgroundColor(?array $color): self { $this->backgroundColor = $color; return $this; diff --git a/src/Renderers/HtmlRenderer.php b/src/Renderers/HtmlRenderer.php index fae8ffa2..9b93651d 100644 --- a/src/Renderers/HtmlRenderer.php +++ b/src/Renderers/HtmlRenderer.php @@ -5,16 +5,16 @@ use Picqer\Barcode\Barcode; use Picqer\Barcode\BarcodeBar; -class HtmlRenderer +class HtmlRenderer implements RendererInterface { - protected string $foregroundColor = 'black'; - protected ?string $backgroundColor = null; + protected array $foregroundColor = [0, 0, 0]; + protected ?array $backgroundColor = null; public function render(Barcode $barcode, float $width = 200, float $height = 30): string { $widthFactor = $width / $barcode->getWidth(); - $html = '
' . PHP_EOL; + $html = '
' . PHP_EOL; $positionHorizontal = 0; /** @var BarcodeBar $bar */ @@ -26,7 +26,7 @@ public function render(Barcode $barcode, float $width = 200, float $height = 30) $positionVertical = round(($bar->getPositionVertical() * $height / $barcode->getHeight()), 3); // draw a vertical bar - $html .= '
 
' . PHP_EOL; + $html .= '
 
' . PHP_EOL; } $positionHorizontal += $barWidth; @@ -37,15 +37,13 @@ public function render(Barcode $barcode, float $width = 200, float $height = 30) return $html; } - // Use HTML color definitions, like 'red' or '#ff0000' - public function setForegroundColor(string $color): self + public function setForegroundColor(array $color): self { $this->foregroundColor = $color; return $this; } - // Use HTML color definitions, like 'red' or '#ff0000' - public function setBackgroundColor(?string $color): self + public function setBackgroundColor(?array $color): self { $this->backgroundColor = $color; return $this; diff --git a/src/Renderers/PngRenderer.php b/src/Renderers/PngRenderer.php index 226a7b7e..4fd6e684 100644 --- a/src/Renderers/PngRenderer.php +++ b/src/Renderers/PngRenderer.php @@ -9,7 +9,7 @@ use Picqer\Barcode\BarcodeBar; use Picqer\Barcode\Exceptions\BarcodeException; -class PngRenderer +class PngRenderer implements RendererInterface { protected array $foregroundColor = [0, 0, 0]; protected ?array $backgroundColor = null; @@ -49,9 +49,15 @@ public function useGd(): self return $this; } - public function render(Barcode $barcode, int $widthFactor = 2, int $height = 30): string + // Floats in width and height will be rounded to integers + // For best (and valid) result, use a width as a factor of the width of the Barcode object + // Example: $width = $barcode->getWidth() * 3 + public function render(Barcode $barcode, float $width = 200, float $height = 30): string { - $width = (int)round($barcode->getWidth() * $widthFactor); + $width = round($width); + $height = round($height); + + $widthFactor = $width / $barcode->getWidth(); if ($this->useImagick) { $image = $this->createImagickImageObject($width, $height); @@ -66,7 +72,7 @@ public function render(Barcode $barcode, int $widthFactor = 2, int $height = 30) $positionHorizontal = 0; /** @var BarcodeBar $bar */ foreach ($barcode->getBars() as $bar) { - $barWidth = (int)round(($bar->getWidth() * $widthFactor)); + $barWidth = $bar->getWidth() * $widthFactor; if ($bar->isBar() && $barWidth > 0) { $y = (int)round(($bar->getPositionVertical() * $height / $barcode->getHeight())); @@ -74,9 +80,9 @@ public function render(Barcode $barcode, int $widthFactor = 2, int $height = 30) // draw a vertical bar if ($this->useImagick) { - $imagickBarsShape->rectangle($positionHorizontal, $y, ($positionHorizontal + $barWidth - 1), ($y + $barHeight)); + $imagickBarsShape->rectangle(round($positionHorizontal), $y, round($positionHorizontal + $barWidth - 1), ($y + $barHeight)); } else { - \imagefilledrectangle($image, $positionHorizontal, $y, ($positionHorizontal + $barWidth - 1), ($y + $barHeight), $gdForegroundColor); + \imagefilledrectangle($image, round($positionHorizontal), $y, round($positionHorizontal + $barWidth - 1), ($y + $barHeight), $gdForegroundColor); } } $positionHorizontal += $barWidth; diff --git a/src/Renderers/RendererInterface.php b/src/Renderers/RendererInterface.php new file mode 100644 index 00000000..eaba33d6 --- /dev/null +++ b/src/Renderers/RendererInterface.php @@ -0,0 +1,14 @@ +backgroundColor !== null) { - $svg .= "\t" . '' . PHP_EOL; + $svg .= "\t" . '' . PHP_EOL; } - $svg .= "\t" . '' . PHP_EOL; + $svg .= "\t" . '' . PHP_EOL; // print bars $positionHorizontal = 0; @@ -56,13 +56,13 @@ public function render(Barcode $barcode, float $width = 200, float $height = 30) return $svg; } - public function setForegroundColor(string $color): self + public function setForegroundColor(array $color): self { $this->foregroundColor = $color; return $this; } - public function setBackgroundColor(?string $color): self + public function setBackgroundColor(?array $color): self { $this->backgroundColor = $color; return $this; diff --git a/tests/verified-files/0049000004632-ean13.svg b/tests/verified-files/0049000004632-ean13.svg index 427ff401..5c46159d 100644 --- a/tests/verified-files/0049000004632-ean13.svg +++ b/tests/verified-files/0049000004632-ean13.svg @@ -2,7 +2,7 @@ 0049000004632 - + diff --git a/tests/verified-files/081231723897-code128-red-background.html b/tests/verified-files/081231723897-code128-red-background.html index 6023c225..45f6b312 100644 --- a/tests/verified-files/081231723897-code128-red-background.html +++ b/tests/verified-files/081231723897-code128-red-background.html @@ -1,30 +1,30 @@ -
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
+
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
diff --git a/tests/verified-files/081231723897-code128.html b/tests/verified-files/081231723897-code128.html index e9cf37a8..7b3f9f20 100644 --- a/tests/verified-files/081231723897-code128.html +++ b/tests/verified-files/081231723897-code128.html @@ -1,30 +1,30 @@
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
diff --git a/tests/verified-files/081231723897-dynamic-code128.html b/tests/verified-files/081231723897-dynamic-code128.html index cdf2d940..6d88107c 100644 --- a/tests/verified-files/081231723897-dynamic-code128.html +++ b/tests/verified-files/081231723897-dynamic-code128.html @@ -1,30 +1,30 @@
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
diff --git a/tests/verified-files/081231723897-ean13-fractional-width.svg b/tests/verified-files/081231723897-ean13-fractional-width.svg index d6164900..2ff29675 100644 --- a/tests/verified-files/081231723897-ean13-fractional-width.svg +++ b/tests/verified-files/081231723897-ean13-fractional-width.svg @@ -2,7 +2,7 @@ 0812317238973 - + diff --git a/tests/verified-files/081231723897-ean13-red-background.svg b/tests/verified-files/081231723897-ean13-red-background.svg index 406af573..52bad4d7 100644 --- a/tests/verified-files/081231723897-ean13-red-background.svg +++ b/tests/verified-files/081231723897-ean13-red-background.svg @@ -2,8 +2,8 @@ 0812317238973 - - + + diff --git a/tests/verified-files/081231723897-ean13.svg b/tests/verified-files/081231723897-ean13.svg index 96e16193..a3964392 100644 --- a/tests/verified-files/081231723897-ean13.svg +++ b/tests/verified-files/081231723897-ean13.svg @@ -2,7 +2,7 @@ 0812317238973 - + diff --git a/tests/verified-files/12345678903-dynamic-imb.html b/tests/verified-files/12345678903-dynamic-imb.html index c06f1fb8..7142156f 100644 --- a/tests/verified-files/12345678903-dynamic-imb.html +++ b/tests/verified-files/12345678903-dynamic-imb.html @@ -1,67 +1,67 @@
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
diff --git a/tests/verified-files/12345678903-imb.html b/tests/verified-files/12345678903-imb.html index eba3400b..7620abc9 100644 --- a/tests/verified-files/12345678903-imb.html +++ b/tests/verified-files/12345678903-imb.html @@ -1,67 +1,67 @@
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
-
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
+
 
diff --git a/tests/verified-files/TypeCodabar-123456789.svg b/tests/verified-files/TypeCodabar-123456789.svg index 0d9606ad..0bbe6d1c 100644 --- a/tests/verified-files/TypeCodabar-123456789.svg +++ b/tests/verified-files/TypeCodabar-123456789.svg @@ -2,7 +2,7 @@ 123456789 - + diff --git a/tests/verified-files/TypeCode11-123456789.svg b/tests/verified-files/TypeCode11-123456789.svg index f30d9191..8c8eb3fb 100644 --- a/tests/verified-files/TypeCode11-123456789.svg +++ b/tests/verified-files/TypeCode11-123456789.svg @@ -2,7 +2,7 @@ 123456789 - + diff --git a/tests/verified-files/TypeCode128-081231723897.svg b/tests/verified-files/TypeCode128-081231723897.svg index 64b544fa..d7267abf 100644 --- a/tests/verified-files/TypeCode128-081231723897.svg +++ b/tests/verified-files/TypeCode128-081231723897.svg @@ -2,7 +2,7 @@ 081231723897 - + diff --git a/tests/verified-files/TypeCode128-1234567890abcABC-283-33.svg b/tests/verified-files/TypeCode128-1234567890abcABC-283-33.svg index 4a3708ad..32b8c1fc 100644 --- a/tests/verified-files/TypeCode128-1234567890abcABC-283-33.svg +++ b/tests/verified-files/TypeCode128-1234567890abcABC-283-33.svg @@ -2,7 +2,7 @@ 1234567890abcABC-283*33 - + diff --git a/tests/verified-files/TypeCode128A-1234567890.svg b/tests/verified-files/TypeCode128A-1234567890.svg index 22d2cf41..9964f3dc 100644 --- a/tests/verified-files/TypeCode128A-1234567890.svg +++ b/tests/verified-files/TypeCode128A-1234567890.svg @@ -2,7 +2,7 @@ 1234567890 - + diff --git a/tests/verified-files/TypeCode128B-081231723897.svg b/tests/verified-files/TypeCode128B-081231723897.svg index f3ee4e03..99133e9d 100644 --- a/tests/verified-files/TypeCode128B-081231723897.svg +++ b/tests/verified-files/TypeCode128B-081231723897.svg @@ -2,7 +2,7 @@ 081231723897 - + diff --git a/tests/verified-files/TypeCode128B-1234567890abcABC-283-33.svg b/tests/verified-files/TypeCode128B-1234567890abcABC-283-33.svg index ec25033c..9d79a944 100644 --- a/tests/verified-files/TypeCode128B-1234567890abcABC-283-33.svg +++ b/tests/verified-files/TypeCode128B-1234567890abcABC-283-33.svg @@ -2,7 +2,7 @@ 1234567890abcABC-283*33 - + diff --git a/tests/verified-files/TypeCode39-1234567890ABC.svg b/tests/verified-files/TypeCode39-1234567890ABC.svg index 448792d4..a0285647 100644 --- a/tests/verified-files/TypeCode39-1234567890ABC.svg +++ b/tests/verified-files/TypeCode39-1234567890ABC.svg @@ -2,7 +2,7 @@ *1234567890ABC* - + diff --git a/tests/verified-files/TypeCode39Checksum-1234567890ABC.svg b/tests/verified-files/TypeCode39Checksum-1234567890ABC.svg index 280551ac..00a99a7a 100644 --- a/tests/verified-files/TypeCode39Checksum-1234567890ABC.svg +++ b/tests/verified-files/TypeCode39Checksum-1234567890ABC.svg @@ -2,7 +2,7 @@ *1234567890ABCZ* - + diff --git a/tests/verified-files/TypeCode39Extended-1234567890abcABC.svg b/tests/verified-files/TypeCode39Extended-1234567890abcABC.svg index d85cc717..39fc3e3c 100644 --- a/tests/verified-files/TypeCode39Extended-1234567890abcABC.svg +++ b/tests/verified-files/TypeCode39Extended-1234567890abcABC.svg @@ -2,7 +2,7 @@ *1234567890+A+B+CABC* - + diff --git a/tests/verified-files/TypeCode39ExtendedChecksum-1234567890abcABC.svg b/tests/verified-files/TypeCode39ExtendedChecksum-1234567890abcABC.svg index a1a87c5d..a6880d6a 100644 --- a/tests/verified-files/TypeCode39ExtendedChecksum-1234567890abcABC.svg +++ b/tests/verified-files/TypeCode39ExtendedChecksum-1234567890abcABC.svg @@ -2,7 +2,7 @@ *1234567890+A+B+CABCJ* - + diff --git a/tests/verified-files/TypeCode93-1234567890abcABC.svg b/tests/verified-files/TypeCode93-1234567890abcABC.svg index 120c36fb..efa0b6e7 100644 --- a/tests/verified-files/TypeCode93-1234567890abcABC.svg +++ b/tests/verified-files/TypeCode93-1234567890abcABC.svg @@ -2,7 +2,7 @@ *1234567890dAdBdCABC6-* - + diff --git a/tests/verified-files/TypeEan13-004900000463.svg b/tests/verified-files/TypeEan13-004900000463.svg index 427ff401..5c46159d 100644 --- a/tests/verified-files/TypeEan13-004900000463.svg +++ b/tests/verified-files/TypeEan13-004900000463.svg @@ -2,7 +2,7 @@ 0049000004632 - + diff --git a/tests/verified-files/TypeEan13-0049000004632.svg b/tests/verified-files/TypeEan13-0049000004632.svg index 427ff401..5c46159d 100644 --- a/tests/verified-files/TypeEan13-0049000004632.svg +++ b/tests/verified-files/TypeEan13-0049000004632.svg @@ -2,7 +2,7 @@ 0049000004632 - + diff --git a/tests/verified-files/TypeEan13-081231723897.svg b/tests/verified-files/TypeEan13-081231723897.svg index 96e16193..a3964392 100644 --- a/tests/verified-files/TypeEan13-081231723897.svg +++ b/tests/verified-files/TypeEan13-081231723897.svg @@ -2,7 +2,7 @@ 0812317238973 - + diff --git a/tests/verified-files/TypeEan8-1234568.svg b/tests/verified-files/TypeEan8-1234568.svg index 9c4e210d..2dc590c9 100644 --- a/tests/verified-files/TypeEan8-1234568.svg +++ b/tests/verified-files/TypeEan8-1234568.svg @@ -2,7 +2,7 @@ 12345687 - + diff --git a/tests/verified-files/TypeITF14-00012345600012.svg b/tests/verified-files/TypeITF14-00012345600012.svg index 25553f09..c2a317be 100644 --- a/tests/verified-files/TypeITF14-00012345600012.svg +++ b/tests/verified-files/TypeITF14-00012345600012.svg @@ -2,7 +2,7 @@ 00012345600012 - + diff --git a/tests/verified-files/TypeITF14-05400141288766.svg b/tests/verified-files/TypeITF14-05400141288766.svg index ece67b75..dd1a0b9c 100644 --- a/tests/verified-files/TypeITF14-05400141288766.svg +++ b/tests/verified-files/TypeITF14-05400141288766.svg @@ -2,7 +2,7 @@ 05400141288766 - + diff --git a/tests/verified-files/TypeIntelligentMailBarcode-123456789.svg b/tests/verified-files/TypeIntelligentMailBarcode-123456789.svg index a82dcd97..8637b41e 100644 --- a/tests/verified-files/TypeIntelligentMailBarcode-123456789.svg +++ b/tests/verified-files/TypeIntelligentMailBarcode-123456789.svg @@ -2,7 +2,7 @@ 123456789 - + diff --git a/tests/verified-files/TypeInterleaved25-1234567890.svg b/tests/verified-files/TypeInterleaved25-1234567890.svg index 77af1b41..3d0c1ed3 100644 --- a/tests/verified-files/TypeInterleaved25-1234567890.svg +++ b/tests/verified-files/TypeInterleaved25-1234567890.svg @@ -2,7 +2,7 @@ AA1234567890ZA - + diff --git a/tests/verified-files/TypeInterleaved25Checksum-1234567890.svg b/tests/verified-files/TypeInterleaved25Checksum-1234567890.svg index 55f32b0e..394bf7d1 100644 --- a/tests/verified-files/TypeInterleaved25Checksum-1234567890.svg +++ b/tests/verified-files/TypeInterleaved25Checksum-1234567890.svg @@ -2,7 +2,7 @@ AA012345678905ZA - + diff --git a/tests/verified-files/TypeKix-123456789.svg b/tests/verified-files/TypeKix-123456789.svg index 0c051776..953a3309 100644 --- a/tests/verified-files/TypeKix-123456789.svg +++ b/tests/verified-files/TypeKix-123456789.svg @@ -2,7 +2,7 @@ 123456789 - + diff --git a/tests/verified-files/TypeMsi-123456789.svg b/tests/verified-files/TypeMsi-123456789.svg index 89c00904..a48095ed 100644 --- a/tests/verified-files/TypeMsi-123456789.svg +++ b/tests/verified-files/TypeMsi-123456789.svg @@ -2,7 +2,7 @@ 123456789 - + diff --git a/tests/verified-files/TypeMsiChecksum-123456789.svg b/tests/verified-files/TypeMsiChecksum-123456789.svg index de5eae86..6cd6062f 100644 --- a/tests/verified-files/TypeMsiChecksum-123456789.svg +++ b/tests/verified-files/TypeMsiChecksum-123456789.svg @@ -2,7 +2,7 @@ 1234567892 - + diff --git a/tests/verified-files/TypePharmacode-123456789.svg b/tests/verified-files/TypePharmacode-123456789.svg index 46044b0a..3dac6de4 100644 --- a/tests/verified-files/TypePharmacode-123456789.svg +++ b/tests/verified-files/TypePharmacode-123456789.svg @@ -2,7 +2,7 @@ 123456789 - + diff --git a/tests/verified-files/TypePharmacodeTwoCode-123456789.svg b/tests/verified-files/TypePharmacodeTwoCode-123456789.svg index 39975d48..6a409d35 100644 --- a/tests/verified-files/TypePharmacodeTwoCode-123456789.svg +++ b/tests/verified-files/TypePharmacodeTwoCode-123456789.svg @@ -2,7 +2,7 @@ 123456789 - + diff --git a/tests/verified-files/TypePlanet-123456789.svg b/tests/verified-files/TypePlanet-123456789.svg index 51de3aab..a6a3fe8f 100644 --- a/tests/verified-files/TypePlanet-123456789.svg +++ b/tests/verified-files/TypePlanet-123456789.svg @@ -2,7 +2,7 @@ 123456789 - + diff --git a/tests/verified-files/TypePostnet-123456789.svg b/tests/verified-files/TypePostnet-123456789.svg index ca895ecb..5cefece1 100644 --- a/tests/verified-files/TypePostnet-123456789.svg +++ b/tests/verified-files/TypePostnet-123456789.svg @@ -2,7 +2,7 @@ 123456789 - + diff --git a/tests/verified-files/TypeRms4cc-123456789.svg b/tests/verified-files/TypeRms4cc-123456789.svg index ee91be75..5e7c698d 100644 --- a/tests/verified-files/TypeRms4cc-123456789.svg +++ b/tests/verified-files/TypeRms4cc-123456789.svg @@ -2,7 +2,7 @@ 123456789 - + diff --git a/tests/verified-files/TypeStandard2of5-1234567890.svg b/tests/verified-files/TypeStandard2of5-1234567890.svg index 26d48c56..8aee6352 100644 --- a/tests/verified-files/TypeStandard2of5-1234567890.svg +++ b/tests/verified-files/TypeStandard2of5-1234567890.svg @@ -2,7 +2,7 @@ 1234567890 - + diff --git a/tests/verified-files/TypeStandard2of5Checksum-1234567890.svg b/tests/verified-files/TypeStandard2of5Checksum-1234567890.svg index dcdc32af..650cc9f3 100644 --- a/tests/verified-files/TypeStandard2of5Checksum-1234567890.svg +++ b/tests/verified-files/TypeStandard2of5Checksum-1234567890.svg @@ -2,7 +2,7 @@ 12345678905 - + diff --git a/tests/verified-files/TypeTelepen-1234567890ASCD.svg b/tests/verified-files/TypeTelepen-1234567890ASCD.svg index d44f436d..22429b29 100644 --- a/tests/verified-files/TypeTelepen-1234567890ASCD.svg +++ b/tests/verified-files/TypeTelepen-1234567890ASCD.svg @@ -2,7 +2,7 @@ 1234567890ASCD - + diff --git a/tests/verified-files/TypeTelepenNumeric-1234567890.svg b/tests/verified-files/TypeTelepenNumeric-1234567890.svg index 079766bc..34c44795 100644 --- a/tests/verified-files/TypeTelepenNumeric-1234567890.svg +++ b/tests/verified-files/TypeTelepenNumeric-1234567890.svg @@ -2,7 +2,7 @@ 1234567890 - + diff --git a/tests/verified-files/TypeUpcA-123456789.svg b/tests/verified-files/TypeUpcA-123456789.svg index d7ed6dc3..38b420c1 100644 --- a/tests/verified-files/TypeUpcA-123456789.svg +++ b/tests/verified-files/TypeUpcA-123456789.svg @@ -2,7 +2,7 @@ 0001234567895 - + diff --git a/tests/verified-files/TypeUpcE-123456789.svg b/tests/verified-files/TypeUpcE-123456789.svg index 4347e926..4333be74 100644 --- a/tests/verified-files/TypeUpcE-123456789.svg +++ b/tests/verified-files/TypeUpcE-123456789.svg @@ -2,7 +2,7 @@ 012349 - + diff --git a/tests/verified-files/TypeUpcExtension2-22.svg b/tests/verified-files/TypeUpcExtension2-22.svg index d7f285d0..1fb84e7d 100644 --- a/tests/verified-files/TypeUpcExtension2-22.svg +++ b/tests/verified-files/TypeUpcExtension2-22.svg @@ -2,7 +2,7 @@ 22 - + diff --git a/tests/verified-files/TypeUpcExtension5-1234567890abcABC-283-33.svg b/tests/verified-files/TypeUpcExtension5-1234567890abcABC-283-33.svg index df18eae3..a4287a51 100644 --- a/tests/verified-files/TypeUpcExtension5-1234567890abcABC-283-33.svg +++ b/tests/verified-files/TypeUpcExtension5-1234567890abcABC-283-33.svg @@ -2,7 +2,7 @@ 1234567890abcABC-283*33 - + From 1a6071ef5fd74247e6f5a6035e8ab35961430185 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Tue, 1 Oct 2024 21:10:45 +0200 Subject: [PATCH 2/4] Add ColorHelper to convert textual colors --- src/BarcodeGeneratorDynamicHTML.php | 3 +- src/BarcodeGeneratorHTML.php | 3 +- src/BarcodeGeneratorJPG.php | 2 +- src/BarcodeGeneratorPNG.php | 2 +- src/BarcodeGeneratorSVG.php | 3 +- src/Exceptions/UnknownColorException.php | 5 +++ src/Helpers/ColorHelper.php | 46 ++++++++++++++++++++++++ tests/HtmlRendererTest.php | 2 +- tests/JpgRendererTest.php | 16 ++++----- tests/PngRendererTest.php | 14 ++++---- tests/SvgRendererTest.php | 2 +- 11 files changed, 76 insertions(+), 22 deletions(-) create mode 100644 src/Exceptions/UnknownColorException.php create mode 100644 src/Helpers/ColorHelper.php diff --git a/src/BarcodeGeneratorDynamicHTML.php b/src/BarcodeGeneratorDynamicHTML.php index c08a4edf..5cebc2b5 100644 --- a/src/BarcodeGeneratorDynamicHTML.php +++ b/src/BarcodeGeneratorDynamicHTML.php @@ -3,6 +3,7 @@ namespace Picqer\Barcode; use Picqer\Barcode\Exceptions\UnknownTypeException; +use Picqer\Barcode\Helpers\ColorHelper; class BarcodeGeneratorDynamicHTML extends BarcodeGenerator { @@ -21,7 +22,7 @@ public function getBarcode(string $barcode, $type, string $foregroundColor = 'bl $barcodeData = $this->getBarcodeData($barcode, $type); $renderer = new \Picqer\Barcode\Renderers\DynamicHtmlRenderer(); - $renderer->setForegroundColor($foregroundColor); + $renderer->setForegroundColor(ColorHelper::getArrayFromColorString($foregroundColor)); return $renderer->render($barcodeData); } diff --git a/src/BarcodeGeneratorHTML.php b/src/BarcodeGeneratorHTML.php index bc30f252..01b8b95e 100644 --- a/src/BarcodeGeneratorHTML.php +++ b/src/BarcodeGeneratorHTML.php @@ -3,6 +3,7 @@ namespace Picqer\Barcode; use Picqer\Barcode\Exceptions\UnknownTypeException; +use Picqer\Barcode\Helpers\ColorHelper; class BarcodeGeneratorHTML extends BarcodeGenerator { @@ -25,7 +26,7 @@ public function getBarcode(string $barcode, $type, int $widthFactor = 2, int $he $width = round(($barcodeData->getWidth() * $widthFactor), 3); $renderer = new \Picqer\Barcode\Renderers\HtmlRenderer(); - $renderer->setForegroundColor($foregroundColor); + $renderer->setForegroundColor(ColorHelper::getArrayFromColorString($foregroundColor)); return $renderer->render($barcodeData, $width, $height); } diff --git a/src/BarcodeGeneratorJPG.php b/src/BarcodeGeneratorJPG.php index 2e270016..51eaf48e 100644 --- a/src/BarcodeGeneratorJPG.php +++ b/src/BarcodeGeneratorJPG.php @@ -34,7 +34,7 @@ public function getBarcode(string $barcode, $type, int $widthFactor = 2, int $he } } - return $renderer->render($barcodeData, $widthFactor, $height); + return $renderer->render($barcodeData, $barcodeData->getWidth() * $widthFactor, $height); } /** diff --git a/src/BarcodeGeneratorPNG.php b/src/BarcodeGeneratorPNG.php index 31edc567..f8f3d0c2 100644 --- a/src/BarcodeGeneratorPNG.php +++ b/src/BarcodeGeneratorPNG.php @@ -34,7 +34,7 @@ public function getBarcode(string $barcode, $type, int $widthFactor = 2, int $he } } - return $renderer->render($barcodeData, $widthFactor, $height); + return $renderer->render($barcodeData, $barcodeData->getWidth() * $widthFactor, $height); } /** diff --git a/src/BarcodeGeneratorSVG.php b/src/BarcodeGeneratorSVG.php index ef7d63f6..64b725ea 100644 --- a/src/BarcodeGeneratorSVG.php +++ b/src/BarcodeGeneratorSVG.php @@ -3,6 +3,7 @@ namespace Picqer\Barcode; use Picqer\Barcode\Exceptions\UnknownTypeException; +use Picqer\Barcode\Helpers\ColorHelper; class BarcodeGeneratorSVG extends BarcodeGenerator { @@ -25,7 +26,7 @@ public function getBarcode(string $barcode, $type, float $widthFactor = 2, float $width = round(($barcodeData->getWidth() * $widthFactor), 3); $renderer = new \Picqer\Barcode\Renderers\SvgRenderer(); - $renderer->setForegroundColor($foregroundColor); + $renderer->setForegroundColor(ColorHelper::getArrayFromColorString($foregroundColor)); return $renderer->render($barcodeData, $width, $height); } diff --git a/src/Exceptions/UnknownColorException.php b/src/Exceptions/UnknownColorException.php new file mode 100644 index 00000000..04018574 --- /dev/null +++ b/src/Exceptions/UnknownColorException.php @@ -0,0 +1,5 @@ +getBarcode('081231723897'); $renderer = new Picqer\Barcode\Renderers\HtmlRenderer(); - $renderer->setBackgroundColor('red'); + $renderer->setBackgroundColor([255, 0, 0]); $generated = $renderer->render($barcode, $barcode->getWidth() * 2); $this->assertStringEqualsFile('tests/verified-files/081231723897-code128-red-background.html', $generated); diff --git a/tests/JpgRendererTest.php b/tests/JpgRendererTest.php index 46aa939d..c6ca4c6d 100644 --- a/tests/JpgRendererTest.php +++ b/tests/JpgRendererTest.php @@ -10,7 +10,7 @@ public function test_jpg_barcode_generator_can_generate_code_128_barcode() $renderer = new Picqer\Barcode\Renderers\JpgRenderer(); $renderer->useGd(); - $generated = $renderer->render($barcode); + $generated = $renderer->render($barcode, $barcode->getWidth() * 2); $imageInfo = getimagesizefromstring($generated); @@ -27,7 +27,7 @@ public function test_jpg_barcode_generator_can_generate_code_39_barcode() $renderer = new Picqer\Barcode\Renderers\JpgRenderer(); $renderer->useGd(); - $generated = $renderer->render($barcode, 1); + $generated = $renderer->render($barcode, $barcode->getWidth()); $imageInfo = getimagesizefromstring($generated); @@ -43,7 +43,7 @@ public function test_jpg_barcode_generator_can_use_different_height() $renderer = new Picqer\Barcode\Renderers\JpgRenderer(); $renderer->useGd(); - $generated = $renderer->render($barcode, 2, 45); + $generated = $renderer->render($barcode, $barcode->getWidth() * 2, 45); $imageInfo = getimagesizefromstring($generated); @@ -59,7 +59,7 @@ public function test_jpg_barcode_generator_can_use_different_width_factor() $renderer = new Picqer\Barcode\Renderers\JpgRenderer(); $renderer->useGd(); - $generated = $renderer->render($barcode, 5); + $generated = $renderer->render($barcode, $barcode->getWidth() * 5); $imageInfo = getimagesizefromstring($generated); @@ -81,7 +81,7 @@ public function test_jpg_barcode_generator_can_generate_code_128_barcode_imagick $renderer = new Picqer\Barcode\Renderers\JpgRenderer(); $renderer->useImagick(); - $generated = $renderer->render($barcode); + $generated = $renderer->render($barcode, $barcode->getWidth() * 2); $imageInfo = getimagesizefromstring($generated); @@ -101,7 +101,7 @@ public function test_jpg_barcode_generator_can_generate_code_39_barcode_imagick( $renderer = new Picqer\Barcode\Renderers\JpgRenderer(); $renderer->useImagick(); - $generated = $renderer->render($barcode, 1); + $generated = $renderer->render($barcode, $barcode->getWidth()); $imageInfo = getimagesizefromstring($generated); @@ -121,7 +121,7 @@ public function test_jpg_barcode_generator_can_use_different_height_imagick() $renderer = new Picqer\Barcode\Renderers\JpgRenderer(); $renderer->useGd(); - $generated = $renderer->render($barcode, 2, 45); + $generated = $renderer->render($barcode, $barcode->getWidth() * 2, 45); $imageInfo = getimagesizefromstring($generated); @@ -141,7 +141,7 @@ public function test_jpg_barcode_generator_can_use_different_width_factor_imagic $renderer = new Picqer\Barcode\Renderers\JpgRenderer(); $renderer->useGd(); - $generated = $renderer->render($barcode, 5); + $generated = $renderer->render($barcode, $barcode->getWidth() * 5); $imageInfo = getimagesizefromstring($generated); diff --git a/tests/PngRendererTest.php b/tests/PngRendererTest.php index db253ce2..fbb3e3cc 100644 --- a/tests/PngRendererTest.php +++ b/tests/PngRendererTest.php @@ -22,7 +22,7 @@ public function test_png_barcode_generator_can_generate_code_39_barcode() $renderer = new Picqer\Barcode\Renderers\PngRenderer(); $renderer->useGd(); - $generated = $renderer->render($barcode, 1); + $generated = $renderer->render($barcode, $barcode->getWidth()); $imageInfo = getimagesizefromstring($generated); @@ -38,7 +38,7 @@ public function test_png_barcode_generator_can_use_different_height() $renderer = new Picqer\Barcode\Renderers\PngRenderer(); $renderer->useGd(); - $generated = $renderer->render($barcode, 2, 45); + $generated = $renderer->render($barcode, $barcode->getWidth() * 2, 45); $imageInfo = getimagesizefromstring($generated); @@ -54,7 +54,7 @@ public function test_png_barcode_generator_can_use_different_width_factor() $renderer = new Picqer\Barcode\Renderers\PngRenderer(); $renderer->useGd(); - $generated = $renderer->render($barcode, 5); + $generated = $renderer->render($barcode, $barcode->getWidth() * 5); $imageInfo = getimagesizefromstring($generated); @@ -76,7 +76,7 @@ public function test_png_barcode_generator_can_generate_code_128_barcode_imagick $renderer = new Picqer\Barcode\Renderers\PngRenderer(); $renderer->useImagick(); - $generated = $renderer->render($barcode); + $generated = $renderer->render($barcode, $barcode->getWidth() * 2); $this->assertEquals('PNG', substr($generated, 1, 3)); } @@ -91,7 +91,7 @@ public function test_png_barcode_generator_can_generate_code_39_barcode_imagick( $renderer = new Picqer\Barcode\Renderers\PngRenderer(); $renderer->useImagick(); - $generated = $renderer->render($barcode, 1); + $generated = $renderer->render($barcode, $barcode->getWidth()); $imageInfo = getimagesizefromstring($generated); @@ -111,7 +111,7 @@ public function test_png_barcode_generator_can_use_different_height_imagick() $renderer = new Picqer\Barcode\Renderers\PngRenderer(); $renderer->useGd(); - $generated = $renderer->render($barcode, 2, 45); + $generated = $renderer->render($barcode, $barcode->getWidth() * 2, 45); $imageInfo = getimagesizefromstring($generated); @@ -131,7 +131,7 @@ public function test_png_barcode_generator_can_use_different_width_factor_imagic $renderer = new Picqer\Barcode\Renderers\PngRenderer(); $renderer->useGd(); - $generated = $renderer->render($barcode, 5); + $generated = $renderer->render($barcode, $barcode->getWidth() * 5); $imageInfo = getimagesizefromstring($generated); diff --git a/tests/SvgRendererTest.php b/tests/SvgRendererTest.php index a8d73e61..984d034e 100644 --- a/tests/SvgRendererTest.php +++ b/tests/SvgRendererTest.php @@ -62,7 +62,7 @@ public function test_svg_barcode_generator_can_use_background_color() $barcode = (new Picqer\Barcode\Types\TypeEan13())->getBarcode('081231723897'); $renderer = new Picqer\Barcode\Renderers\SvgRenderer(); - $renderer->setBackgroundColor('red'); + $renderer->setBackgroundColor([255, 0, 0]); $generated = $renderer->render($barcode, 190); $this->assertStringEqualsFile('tests/verified-files/081231723897-ean13-red-background.svg', $generated); From 40cfc3f649dbff34c078f05148e4a0704940d3fa Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Tue, 1 Oct 2024 21:28:12 +0200 Subject: [PATCH 3/4] Fix phpstan --- src/Renderers/PngRenderer.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Renderers/PngRenderer.php b/src/Renderers/PngRenderer.php index 4fd6e684..72c29094 100644 --- a/src/Renderers/PngRenderer.php +++ b/src/Renderers/PngRenderer.php @@ -54,8 +54,8 @@ public function useGd(): self // Example: $width = $barcode->getWidth() * 3 public function render(Barcode $barcode, float $width = 200, float $height = 30): string { - $width = round($width); - $height = round($height); + $width = (int)round($width); + $height = (int)round($height); $widthFactor = $width / $barcode->getWidth(); @@ -80,9 +80,9 @@ public function render(Barcode $barcode, float $width = 200, float $height = 30) // draw a vertical bar if ($this->useImagick) { - $imagickBarsShape->rectangle(round($positionHorizontal), $y, round($positionHorizontal + $barWidth - 1), ($y + $barHeight)); + $imagickBarsShape->rectangle((int)round($positionHorizontal), $y, (int)round($positionHorizontal + $barWidth - 1), ($y + $barHeight)); } else { - \imagefilledrectangle($image, round($positionHorizontal), $y, round($positionHorizontal + $barWidth - 1), ($y + $barHeight), $gdForegroundColor); + \imagefilledrectangle($image, (int)round($positionHorizontal), $y, (int)round($positionHorizontal + $barWidth - 1), ($y + $barHeight), $gdForegroundColor); } } $positionHorizontal += $barWidth; From d06a28f78d8810246795f19068bdc42215f1a6eb Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Tue, 1 Oct 2024 21:28:35 +0200 Subject: [PATCH 4/4] Add new interface usage to readme --- Readme.md | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/Readme.md b/Readme.md index 2a5b9603..99652ca5 100644 --- a/Readme.md +++ b/Readme.md @@ -18,7 +18,7 @@ Install through [composer](https://getcomposer.org/doc/00-intro.md): 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. +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). @@ -54,21 +54,28 @@ $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)); +// Save PNG to the filesystem, with widthFactor 3 (width of the barcode x 3) and height of 50 pixels +file_put_contents('barcode.png', $renderer->render($barcode, $barcode->getWidth() * 3, 50)); ``` ## 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: +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 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. + +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. ```php $renderer = new Picqer\Barcode\Renderers\SvgRenderer(); -$renderer->setForegroundColor('red'); // Give a color for the bars, default is black -$renderer->setBackgroundColor('blue'); // Give a color for the background, default is transparent +$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. +$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. $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 @@ -91,8 +98,8 @@ $renderer->render($barcode, 5, 40); // Width factor (how many pixel wide every b 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, default is black -$renderer->setBackgroundColor('blue'); // Give a color for the background, default is transparent +$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. +$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. $renderer->render($barcode, 450.20, 75); // Width and height support floats ```` @@ -101,8 +108,8 @@ $renderer->render($barcode, 450.20, 75); // Width and height support floats 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, default is black -$renderer->setBackgroundColor('blue'); // Give a color for the background, default is transparent +$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. +$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. $renderer->render($barcode); ```` @@ -163,7 +170,7 @@ If you want to use PNG or JPG images, you need to install [Imagick](https://www. ```php $barcode = (new Picqer\Barcode\Types\TypeCode128())->getBarcode('081231723897'); $renderer = new Picqer\Barcode\Renderers\PngRenderer(); -echo ''; +echo ''; ``` ### Save JPG barcode to disk @@ -171,7 +178,7 @@ echo '