' . PHP_EOL;
+ $html = '
' . PHP_EOL;
$positionHorizontal = 0;
/** @var BarcodeBar $bar */
@@ -36,10 +37,17 @@ 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
{
$this->foregroundColor = $color;
-
+ return $this;
+ }
+
+ // Use HTML color definitions, like 'red' or '#ff0000'
+ public function setBackgroundColor(?string $color): self
+ {
+ $this->backgroundColor = $color;
return $this;
}
}
diff --git a/src/Renderers/JpgRenderer.php b/src/Renderers/JpgRenderer.php
index 132ec10..1c835f7 100644
--- a/src/Renderers/JpgRenderer.php
+++ b/src/Renderers/JpgRenderer.php
@@ -9,7 +9,14 @@ class JpgRenderer extends PngRenderer
protected function createImagickImageObject(int $width, int $height): Imagick
{
$image = new Imagick();
- $image->newImage($width, $height, 'none', 'JPG');
+ if ($this->backgroundColor !== null) {
+ // Colored background
+ $backgroundColor = new ImagickPixel('rgb(' . implode(',', $this->backgroundColor) . ')');
+ } else {
+ // Use transparent background
+ $backgroundColor = new ImagickPixel('none');
+ }
+ $image->newImage($width, $height, $backgroundColor, 'JPG');
return $image;
}
diff --git a/src/Renderers/PngRenderer.php b/src/Renderers/PngRenderer.php
index 8297d88..226a7b7 100644
--- a/src/Renderers/PngRenderer.php
+++ b/src/Renderers/PngRenderer.php
@@ -12,6 +12,8 @@
class PngRenderer
{
protected array $foregroundColor = [0, 0, 0];
+ protected ?array $backgroundColor = null;
+
protected bool $useImagick;
/**
@@ -52,6 +54,7 @@ public function render(Barcode $barcode, int $widthFactor = 2, int $height = 30)
$width = (int)round($barcode->getWidth() * $widthFactor);
if ($this->useImagick) {
+ $image = $this->createImagickImageObject($width, $height);
$imagickBarsShape = new ImagickDraw();
$imagickBarsShape->setFillColor(new ImagickPixel('rgb(' . implode(',', $this->foregroundColor) .')'));
} else {
@@ -80,7 +83,6 @@ public function render(Barcode $barcode, int $widthFactor = 2, int $height = 30)
}
if ($this->useImagick) {
- $image = $this->createImagickImageObject($width, $height);
$image->drawImage($imagickBarsShape);
return $image->getImageBlob();
} else {
@@ -90,18 +92,34 @@ public function render(Barcode $barcode, int $widthFactor = 2, int $height = 30)
}
}
+ // Use RGB color definitions, like [0, 0, 0] or [255, 255, 255]
public function setForegroundColor(array $color): self
{
$this->foregroundColor = $color;
-
+ return $this;
+ }
+
+ // Use RGB color definitions, like [0, 0, 0] or [255, 255, 255]
+ // If no color is set, the background will be transparent
+ public function setBackgroundColor(?array $color): self
+ {
+ $this->backgroundColor = $color;
return $this;
}
protected function createGdImageObject(int $width, int $height)
{
$image = \imagecreate($width, $height);
- $colorBackground = \imagecolorallocate($image, 255, 255, 255);
- \imagecolortransparent($image, $colorBackground);
+
+ if ($this->backgroundColor !== null) {
+ // Colored background
+ $backgroundColor = \imagecolorallocate($image, $this->backgroundColor[0], $this->backgroundColor[1], $this->backgroundColor[2]);
+ \imagefill($image, 0, 0, $backgroundColor);
+ } else {
+ // Use transparent background
+ $backgroundColor = \imagecolorallocate($image, 255, 255, 255);
+ \imagecolortransparent($image, $backgroundColor);
+ }
return $image;
}
@@ -109,7 +127,14 @@ protected function createGdImageObject(int $width, int $height)
protected function createImagickImageObject(int $width, int $height): Imagick
{
$image = new Imagick();
- $image->newImage($width, $height, 'none', 'PNG');
+ if ($this->backgroundColor !== null) {
+ // Colored background
+ $backgroundColor = new ImagickPixel('rgb(' . implode(',', $this->backgroundColor) . ')');
+ } else {
+ // Use transparent background
+ $backgroundColor = new ImagickPixel('none');
+ }
+ $image->newImage($width, $height, $backgroundColor, 'PNG');
return $image;
}
diff --git a/src/Renderers/SvgRenderer.php b/src/Renderers/SvgRenderer.php
index 6af44b3..418333a 100644
--- a/src/Renderers/SvgRenderer.php
+++ b/src/Renderers/SvgRenderer.php
@@ -9,6 +9,7 @@
class SvgRenderer
{
protected string $foregroundColor = 'black';
+ protected ?string $backgroundColor = null;
protected string $svgType = self::TYPE_SVG_STANDALONE;
public const TYPE_SVG_STANDALONE = 'standalone';
@@ -25,6 +26,12 @@ public function render(Barcode $barcode, float $width = 200, float $height = 30)
}
$svg .= '