' . 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 226a7b7..4fd6e68 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 0000000..eaba33d
--- /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 427ff40..5c46159 100644
--- a/tests/verified-files/0049000004632-ean13.svg
+++ b/tests/verified-files/0049000004632-ean13.svg
@@ -2,7 +2,7 @@