Skip to content

Commit

Permalink
Image: GD extension detection added
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Apr 1, 2024
1 parent b8ec3bc commit 06fd7f6
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions src/Utils/Image.php
Expand Up @@ -163,10 +163,7 @@ public static function rgb(int $red, int $green, int $blue, int $transparency =
*/
public static function fromFile(string $file, ?int &$type = null): static
{
if (!extension_loaded('gd')) {
throw new Nette\NotSupportedException('PHP extension GD is not loaded.');
}

self::ensureExtension();
$type = self::detectTypeFromFile($file);
if (!$type) {
throw new UnknownImageFileException(is_file($file) ? "Unknown type of file '$file'." : "File '$file' not found.");
Expand All @@ -183,10 +180,7 @@ public static function fromFile(string $file, ?int &$type = null): static
*/
public static function fromString(string $s, ?int &$type = null): static
{
if (!extension_loaded('gd')) {
throw new Nette\NotSupportedException('PHP extension GD is not loaded.');
}

self::ensureExtension();
$type = self::detectTypeFromString($s);
if (!$type) {
throw new UnknownImageFileException('Unknown type of image.');
Expand Down Expand Up @@ -221,10 +215,7 @@ private static function invokeSafe(string $func, string $arg, string $message, s
*/
public static function fromBlank(int $width, int $height, ImageColor|array|null $color = null): static
{
if (!extension_loaded('gd')) {
throw new Nette\NotSupportedException('PHP extension GD is not loaded.');
}

self::ensureExtension();
if ($width < 1 || $height < 1) {

Check failure on line 219 in src/Utils/Image.php

View workflow job for this annotation

GitHub Actions / PHPStan

Comparison operation "<" between int<1, max> and 1 is always false.

Check failure on line 219 in src/Utils/Image.php

View workflow job for this annotation

GitHub Actions / PHPStan

Comparison operation "<" between int<1, max> and 1 is always false.

Check failure on line 219 in src/Utils/Image.php

View workflow job for this annotation

GitHub Actions / PHPStan

Result of || is always false.
throw new Nette\InvalidArgumentException('Image width and height must be greater than zero.');
}
Expand Down Expand Up @@ -308,6 +299,7 @@ public static function typeToMimeType(int $type): string
*/
public static function isTypeSupported(int $type): bool
{
self::ensureExtension();
return (bool) (imagetypes() & match ($type) {
ImageType::JPEG => IMG_JPG,
ImageType::PNG => IMG_PNG,
Expand All @@ -323,6 +315,7 @@ public static function isTypeSupported(int $type): bool
/** @return ImageType[] */
public static function getSupportedTypes(): array
{
self::ensureExtension();
$flag = imagetypes();
return array_filter([
$flag & IMG_GIF ? ImageType::GIF : null,
Expand Down Expand Up @@ -640,6 +633,7 @@ public static function calculateTextBox(
array $options = [],
): array
{
self::ensureExtension();
$box = imagettfbbox($size, $angle, $fontFile, $text, $options);
return [
'left' => $minX = min([$box[0], $box[2], $box[4], $box[6]]),
Expand Down Expand Up @@ -826,4 +820,12 @@ public function resolveColor(ImageColor|array $color): int
$color = $color instanceof ImageColor ? $color->toRGBA() : array_values($color);
return imagecolorallocatealpha($this->image, ...$color) ?: imagecolorresolvealpha($this->image, ...$color);
}


private static function ensureExtension(): void
{
if (!extension_loaded('gd')) {
throw new Nette\NotSupportedException('PHP extension GD is not loaded.');
}
}
}

0 comments on commit 06fd7f6

Please sign in to comment.