Skip to content

Commit 2a69d33

Browse files
committed
TextImage: Enable to set transparent background.
1 parent 88c45af commit 2a69d33

File tree

2 files changed

+51
-15
lines changed

2 files changed

+51
-15
lines changed

src/Rendering/TextImageRenderer.php

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ public static function render(TextImage $textImage)
3232
$textImage->getFullHeight(),
3333
$textImage->getBorder(),
3434
$textImage->getBackgroundColor(),
35-
$textImage->getBorderColor()
35+
$textImage->getBorderColor(),
36+
$textImage->getTransparentBackground()
3637
);
3738
$textOffset = $textImage->getTextOffset();
3839
foreach ($textImage->getLines() as $line) {
@@ -62,24 +63,35 @@ public static function render(TextImage $textImage)
6263
* @param array $border
6364
* @param Objects\Color $backgroundColor
6465
* @param Objects\Color $borderColor
66+
* @param bool $transparentBackground
6567
* @return resource
6668
*/
67-
private static function createEmptyImage($width, $height, array $border, Objects\Color $backgroundColor, Objects\Color $borderColor)
69+
private static function createEmptyImage($width, $height, array $border, Objects\Color $backgroundColor, Objects\Color $borderColor, $transparentBackground)
6870
{
6971
$image = imagecreatetruecolor($width, $height);
70-
$backColor = Objects\Color::allocateToImage($image, $backgroundColor);
71-
$bordColor = Objects\Color::allocateToImage($image, $borderColor);
72-
// Border
73-
imagefilledrectangle($image, 0, 0, $width, $height, $bordColor);
74-
// Background
75-
imagefilledrectangle(
76-
$image,
77-
$border[TextImage::OPT_LEFT],
78-
$border[TextImage::OPT_TOP],
79-
($width - $border[TextImage::OPT_RIGHT]),
80-
($height - $border[TextImage::OPT_BOTTOM]),
81-
$backColor
82-
);
72+
73+
if ($transparentBackground === TRUE) {
74+
imagealphablending($image, false);
75+
$transparency = imagecolorallocatealpha($image, 0, 0, 0, 127);
76+
imagefill($image, 0, 0, $transparency);
77+
imagesavealpha($image, true);
78+
} else {
79+
$backColor = Objects\Color::allocateToImage($image, $backgroundColor);
80+
$bordColor = Objects\Color::allocateToImage($image, $borderColor);
81+
82+
// Border
83+
imagefilledrectangle($image, 0, 0, $width, $height, $bordColor);
84+
// Background
85+
imagefilledrectangle(
86+
$image,
87+
$border[TextImage::OPT_LEFT],
88+
$border[TextImage::OPT_TOP],
89+
($width - $border[TextImage::OPT_RIGHT]),
90+
($height - $border[TextImage::OPT_BOTTOM]),
91+
$backColor
92+
);
93+
}
94+
8395
return $image;
8496
}
8597

src/TextImage.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ class TextImage
3737
/** @var Objects\Color */
3838
private $backgroundColor;
3939

40+
/** @var bool */
41+
private $transparentBackground = FALSE;
42+
4043
/** @var Objects\Color */
4144
private $textColor;
4245

@@ -413,6 +416,15 @@ public function getTextOffset()
413416
}
414417

415418

419+
/**
420+
* @return bool
421+
*/
422+
public function getTransparentBackground()
423+
{
424+
return $this->transparentBackground;
425+
}
426+
427+
416428
/**
417429
* @param string $text
418430
* @throws \BadMethodCallException
@@ -447,6 +459,7 @@ public function setFormat($format)
447459
public function setBackgroundColor(Objects\Color $backgroundColor)
448460
{
449461
$this->backgroundColor = $backgroundColor;
462+
$this->transparentBackground = FALSE;
450463
return $this;
451464
}
452465

@@ -610,4 +623,15 @@ public function setStripTextString($stripTextString)
610623
}
611624

612625

626+
/**
627+
* @param bool $isTransparent
628+
* @return TextImage
629+
*/
630+
public function setTransparentBackground($isTransparent)
631+
{
632+
$this->transparentBackground = $isTransparent;
633+
return $this;
634+
}
635+
636+
613637
}

0 commit comments

Comments
 (0)