Skip to content

Commit 4ed72ee

Browse files
committed
Image: GD extension detection added
1 parent e65ae6f commit 4ed72ee

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

src/Utils/Image.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,7 @@ public static function rgb(int $red, int $green, int $blue, int $transparency =
163163
*/
164164
public static function fromFile(string $file, ?int &$type = null): static
165165
{
166-
if (!extension_loaded('gd')) {
167-
throw new Nette\NotSupportedException('PHP extension GD is not loaded.');
168-
}
169-
166+
self::ensureExtension();
170167
$type = self::detectTypeFromFile($file);
171168
if (!$type) {
172169
throw new UnknownImageFileException(is_file($file) ? "Unknown type of file '$file'." : "File '$file' not found.");
@@ -183,10 +180,7 @@ public static function fromFile(string $file, ?int &$type = null): static
183180
*/
184181
public static function fromString(string $s, ?int &$type = null): static
185182
{
186-
if (!extension_loaded('gd')) {
187-
throw new Nette\NotSupportedException('PHP extension GD is not loaded.');
188-
}
189-
183+
self::ensureExtension();
190184
$type = self::detectTypeFromString($s);
191185
if (!$type) {
192186
throw new UnknownImageFileException('Unknown type of image.');
@@ -221,10 +215,7 @@ private static function invokeSafe(string $func, string $arg, string $message, s
221215
*/
222216
public static function fromBlank(int $width, int $height, ImageColor|array|null $color = null): static
223217
{
224-
if (!extension_loaded('gd')) {
225-
throw new Nette\NotSupportedException('PHP extension GD is not loaded.');
226-
}
227-
218+
self::ensureExtension();
228219
if ($width < 1 || $height < 1) {
229220
throw new Nette\InvalidArgumentException('Image width and height must be greater than zero.');
230221
}
@@ -308,6 +299,7 @@ public static function typeToMimeType(int $type): string
308299
*/
309300
public static function isTypeSupported(int $type): bool
310301
{
302+
self::ensureExtension();
311303
return (bool) (imagetypes() & match ($type) {
312304
ImageType::JPEG => IMG_JPG,
313305
ImageType::PNG => IMG_PNG,
@@ -323,6 +315,7 @@ public static function isTypeSupported(int $type): bool
323315
/** @return ImageType[] */
324316
public static function getSupportedTypes(): array
325317
{
318+
self::ensureExtension();
326319
$flag = imagetypes();
327320
return array_filter([
328321
$flag & IMG_GIF ? ImageType::GIF : null,
@@ -640,6 +633,7 @@ public static function calculateTextBox(
640633
array $options = [],
641634
): array
642635
{
636+
self::ensureExtension();
643637
$box = imagettfbbox($size, $angle, $fontFile, $text, $options);
644638
return [
645639
'left' => $minX = min([$box[0], $box[2], $box[4], $box[6]]),
@@ -826,4 +820,12 @@ public function resolveColor(ImageColor|array $color): int
826820
$color = $color instanceof ImageColor ? $color->toRGBA() : array_values($color);
827821
return imagecolorallocatealpha($this->image, ...$color) ?: imagecolorresolvealpha($this->image, ...$color);
828822
}
823+
824+
825+
private static function ensureExtension(): void
826+
{
827+
if (!extension_loaded('gd')) {
828+
throw new Nette\NotSupportedException('PHP extension GD is not loaded.');
829+
}
830+
}
829831
}

0 commit comments

Comments
 (0)