Skip to content

Commit a9d127d

Browse files
committed
added Image::rectangleWH(), filledRectangleWH(), calculateTextBox()
1 parent b7be9b7 commit a9d127d

File tree

4 files changed

+53
-6
lines changed

4 files changed

+53
-6
lines changed

src/Utils/Image.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,49 @@ public function place(self $image, int|string $left = 0, int|string $top = 0, in
614614
}
615615

616616

617+
/**
618+
* Calculates the bounding box for a TrueType text. Returns keys left, top, width and height.
619+
*/
620+
public static function calculateTextBox(
621+
string $text,
622+
string $fontFile,
623+
float $size,
624+
float $angle = 0,
625+
array $options = [],
626+
): array
627+
{
628+
$box = imagettfbbox($size, $angle, $fontFile, $text, $options);
629+
return [
630+
'left' => $minX = min([$box[0], $box[2], $box[4], $box[6]]),
631+
'top' => $minY = min([$box[1], $box[3], $box[5], $box[7]]),
632+
'width' => max([$box[0], $box[2], $box[4], $box[6]]) - $minX + 1,
633+
'height' => max([$box[1], $box[3], $box[5], $box[7]]) - $minY + 1,
634+
];
635+
}
636+
637+
638+
/**
639+
* Draw a rectangle.
640+
*/
641+
public function rectangleWH(int $x, int $y, int $width, int $height, ImageColor $color): void
642+
{
643+
if ($width !== 0 && $height !== 0) {
644+
$this->rectangle($x, $y, $x + $width + ($width > 0 ? -1 : 1), $y + $height + ($height > 0 ? -1 : 1), $color);
645+
}
646+
}
647+
648+
649+
/**
650+
* Draw a filled rectangle.
651+
*/
652+
public function filledRectangleWH(int $x, int $y, int $width, int $height, ImageColor $color): void
653+
{
654+
if ($width !== 0 && $height !== 0) {
655+
$this->filledRectangle($x, $y, $x + $width + ($width > 0 ? -1 : 1), $y + $height + ($height > 0 ? -1 : 1), $color);
656+
}
657+
}
658+
659+
617660
/**
618661
* Saves image to the file. Quality is in the range 0..100 for JPEG (default 85), WEBP (default 80) and AVIF (default 30) and 0..9 for PNG (default 9).
619662
* @param ImageType::*|null $type

tests/Utils/Image.drawing.phpt

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
declare(strict_types=1);
99

1010
use Nette\Utils\Image;
11+
use Nette\Utils\ImageColor;
1112
use Tester\Assert;
1213

1314

@@ -17,8 +18,14 @@ require __DIR__ . '/../bootstrap.php';
1718
$size = 300;
1819
$image = Image::fromBlank($size, $size);
1920

20-
$image->filledRectangle(0, 0, $size - 1, $size - 1, Image::rgb(255, 255, 255));
21-
$image->rectangle(0, 0, $size - 1, $size - 1, Image::rgb(0, 0, 0));
21+
$image->filledRectangleWH(0, 0, 300, 300, ImageColor::rgb(255, 255, 255));
22+
$image->rectangleWH(0, 0, 300, 300, ImageColor::rgb(0, 0, 0));
23+
24+
$image->filledRectangleWH(20, 20, -5, -5, ImageColor::rgb(100, 0, 0));
25+
$image->rectangleWH(20, 20, -5, -5, ImageColor::rgb(100, 255, 255));
26+
27+
$image->filledRectangleWH(30, 30, 0, 0, ImageColor::rgb(127, 127, 0));
28+
$image->rectangleWH(35, 35, 0, 0, ImageColor::rgb(127, 127, 0));
2229

2330
$radius = 150;
2431

@@ -28,10 +35,7 @@ $image->filledEllipse(187, 125, $radius, $radius, Image::rgb(0, 0, 255, 75));
2835

2936
$image->copyResampled($image, 200, 200, 0, 0, 80, 80, $size, $size);
3037

31-
$file = !defined('PHP_WINDOWS_VERSION_BUILD') || PHP_VERSION_ID >= 70424
32-
? '/expected/Image.drawing.1b.png'
33-
: '/expected/Image.drawing.1.png';
34-
Assert::same(file_get_contents(__DIR__ . $file), $image->toString($image::PNG));
38+
Assert::same(file_get_contents(__DIR__ . '/expected/Image.drawing.1.png'), $image->toString($image::PNG));
3539

3640

3741
// palette-based image
49 Bytes
Loading
-5.11 KB
Binary file not shown.

0 commit comments

Comments
 (0)