Skip to content

Commit

Permalink
working image background
Browse files Browse the repository at this point in the history
  • Loading branch information
sergix44 committed Sep 23, 2023
1 parent a3199a8 commit d38e1ef
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/Alterations/WriteText.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use SergiX44\ImageZen\Drivers\Imagick\ImagickAlteration;
use SergiX44\ImageZen\Drivers\Imagick\ImagickText;
use SergiX44\ImageZen\Image;
use SergiX44\ImageZen\Shapes\Rectangle;

class WriteText extends Alteration implements GdAlteration, ImagickAlteration
{
Expand Down Expand Up @@ -43,6 +44,23 @@ public function applyWithGd(Image $image): null

$color = $driver->parseColor($text->color);
$box = $text->getBox();

if ($text->background !== null) {
$lines = explode("\n", $text->parsedText());
$bgY = $y;
foreach ($lines as $line) {
$box = $text->getBox();
$image->rectangle(
$x,
$bgY,
$x + $box->upperRight->x - 1,
$bgY + $box->upperRight->y - 1,
fn (Rectangle $r) => $r->background($text->background)
);
$bgY -= $box->upperRight->y - $text->getPointSize() * 0.75;
}
}

if ($text->hasFont()) {
if ($text->angle !== 0 || $text->align !== Position::TOP_LEFT) {
switch ($text->align) {
Expand Down Expand Up @@ -257,6 +275,10 @@ public function applyWithImagick(Image $image): null
break;
}

if ($text->background !== null) {
$draw->setTextUnderColor($driver->parseColor($text->background)->getPixel());
}

$image->getCore()->annotateImage($draw, $x, $y, $text->angle * (-1), $this->text);

return null;
Expand Down
21 changes: 21 additions & 0 deletions src/Draws/Position.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,25 @@ enum Position: string
case LEFT = 'left';
case RIGHT = 'right';



public function toImagickGravity(): int
{
return match ($this) {
self::TOP => \Imagick::GRAVITY_NORTH,
self::TOP_MIDDLE => \Imagick::GRAVITY_NORTH,
self::TOP_LEFT => \Imagick::GRAVITY_NORTHWEST,
self::TOP_RIGHT => \Imagick::GRAVITY_NORTHEAST,
self::CENTER => \Imagick::GRAVITY_CENTER,
self::CENTER_MIDDLE => \Imagick::GRAVITY_CENTER,
self::CENTER_LEFT => \Imagick::GRAVITY_WEST,
self::CENTER_RIGHT => \Imagick::GRAVITY_EAST,
self::BOTTOM => \Imagick::GRAVITY_SOUTH,
self::BOTTOM_MIDDLE => \Imagick::GRAVITY_SOUTH,
self::BOTTOM_LEFT => \Imagick::GRAVITY_SOUTHWEST,
self::BOTTOM_RIGHT => \Imagick::GRAVITY_SOUTHEAST,
self::LEFT => \Imagick::GRAVITY_WEST,
self::RIGHT => \Imagick::GRAVITY_EAST,
};
}
}
52 changes: 52 additions & 0 deletions src/Drivers/Gd/GdText.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,56 @@ public function getInternalFont(): ?int
{
return $this->internalFont;
}

private function linebreaks4imagettftext(
$size,
$angle,
$fontfile,
$text,
$maximumWidth,
$lineBreakCharacter = PHP_EOL
): string {
// create an array with all the words
$words = explode(' ', $text);

// process all our words to generate $textWithLineBreaks
$textWithLineBreaks = '';
$currentLine = '';
foreach ($words as $position => $word) {
// place the first word into $currentLine without any processing (we
// always want to include the first word on the first line--obviously)
if ($position === 0) {
$currentLine = $word;
} else {
// calculate the text's size if we were to add the word
$textDimensions = imagettfbbox(
$size,
$angle,
$fontfile,
$currentLine.' '.$word
);
$textLeft = min($textDimensions[0], $textDimensions[6]);
$textRight = max($textDimensions[2], $textDimensions[4]);
$textWidth = $textRight - $textLeft;
if ($textWidth > $maximumWidth) {
// the text is too wide with the added word so we add a line
// break then start a new line with only the added word
$textWithLineBreaks .= $currentLine;
$textWithLineBreaks .= $lineBreakCharacter;

$currentLine = $word;
} else {
// we have space on the current line for the added word so we
// add a space then the word
$currentLine .= ' ';
$currentLine .= $word;
}
}
}
// the current line is still unadded to $textWithLineBreaks so we add it
$textWithLineBreaks .= $currentLine;

// return $text with line breaks added
return $textWithLineBreaks;
}
}
2 changes: 1 addition & 1 deletion src/Drivers/Imagick/ImagickText.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function getBox(): Box
$draw->setFont($this->fontPath);
$draw->setFontSize($this->size);
$draw->setTextKerning($this->kerning);
$draw->setGravity($this->align);
$draw->setGravity($this->align->toImagickGravity());
$draw->setStrokeWidth(0);
$draw->setStrokeAntialias(false);
$draw->setStrokeOpacity(1);
Expand Down
32 changes: 32 additions & 0 deletions tests/DriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -598,3 +598,35 @@ function prepare($instance, string $name, Backend $driver, string $ext = 'png'):
->imageSimilarTo($expected);
unlink($out);
})->with('drivers', 'fruit');

it('can draw a text with a background', function ($driver, $file) {
[$out, $expected] = prepare($this, 'fruit_with_text_background', $driver);

Image::make($file, $driver)
->text("Hello World!", 100, 100, function (Text $text) {
$text->size(18)
->background(Color::fuchsia());
})
->save($out, quality: 100);

expect($out)
->toBeFile()
->imageSimilarTo($expected);
unlink($out);
})->with('drivers', 'fruit');

it('can draw a text with a background with new lines', function ($driver, $file) {
[$out, $expected] = prepare($this, 'fruit_with_text_background_newlines', $driver);

Image::make($file, $driver)
->text("Hello \n World!", 100, 100, function (Text $text) {
$text->size(18)
->background(Color::fuchsia());
})
->save($out, quality: 100);

expect($out)
->toBeFile()
->imageSimilarTo($expected);
unlink($out);
})->with('drivers', 'fruit');
Binary file added tests/Images/Gd/fruit_with_text_background.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d38e1ef

Please sign in to comment.