Skip to content

Commit d4f51c2

Browse files
committed
added Image::isTypeSupported()
1 parent b5a3d51 commit d4f51c2

File tree

5 files changed

+41
-5
lines changed

5 files changed

+41
-5
lines changed

src/Utils/Image.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,23 @@ public static function typeToMimeType(int $type): string
310310
}
311311

312312

313+
/**
314+
* @param ImageType::* $type
315+
*/
316+
public static function isTypeSupported(int $type): bool
317+
{
318+
return (bool) (imagetypes() & match ($type) {
319+
ImageType::JPEG => IMG_JPG,
320+
ImageType::PNG => IMG_PNG,
321+
ImageType::GIF => IMG_GIF,
322+
ImageType::WEBP => IMG_WEBP,
323+
ImageType::AVIF => 256, // IMG_AVIF,
324+
ImageType::BMP => IMG_BMP,
325+
default => 0,
326+
});
327+
}
328+
329+
313330
/**
314331
* Wraps GD image.
315332
*/

tests/Utils/Image.factories.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ test('', function () {
2727

2828

2929
test('', function () {
30-
if (!function_exists('imagecreatefromwebp')) {
30+
if (!Image::isTypeSupported(Image::WEBP)) {
3131
return;
3232
}
3333

@@ -39,7 +39,7 @@ test('', function () {
3939

4040

4141
test('', function () {
42-
if (!function_exists('imagecreatefromavif')) {
42+
if (!Image::isTypeSupported(Image::AVIF)) {
4343
return;
4444
}
4545

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/**
4+
* @phpExtension gd
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
use Nette\Utils\Image;
10+
use Tester\Assert;
11+
12+
13+
require __DIR__ . '/../bootstrap.php';
14+
15+
16+
Assert::true(Image::isTypeSupported(Image::GIF));
17+
Assert::true(Image::isTypeSupported(Image::JPEG));
18+
Assert::same(function_exists('imagecreatefromwebp'), Image::isTypeSupported(Image::WEBP));
19+
Assert::same(function_exists('imagecreatefromavif'), Image::isTypeSupported(Image::AVIF));

tests/Utils/Image.save.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ test('', function () use ($main) {
3232

3333

3434
test('', function () use ($main) {
35-
if (!function_exists('imagewebp')) {
35+
if (!Image::isTypeSupported(Image::WEBP)) {
3636
return;
3737
}
3838

@@ -47,7 +47,7 @@ test('', function () use ($main) {
4747

4848

4949
test('', function () use ($main) {
50-
if (!function_exists('imageavif')) {
50+
if (!Image::isTypeSupported(Image::AVIF)) {
5151
return;
5252
}
5353

tests/Utils/Image.send.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ test('', function () use ($main) {
4242

4343

4444
test('', function () use ($main) {
45-
if (!function_exists('imagewebp')) {
45+
if (!Image::isTypeSupported(Image::WEBP)) {
4646
return;
4747
}
4848

0 commit comments

Comments
 (0)