Skip to content

Commit

Permalink
Add DriverInterface::supports()
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed Apr 13, 2024
1 parent c05ff16 commit 9e44aa5
Show file tree
Hide file tree
Showing 11 changed files with 357 additions and 13 deletions.
28 changes: 28 additions & 0 deletions src/Drivers/Gd/Driver.php
Expand Up @@ -6,14 +6,18 @@

use Intervention\Image\Drivers\AbstractDriver;
use Intervention\Image\Exceptions\DriverException;
use Intervention\Image\Exceptions\NotSupportedException;
use Intervention\Image\Exceptions\RuntimeException;
use Intervention\Image\Format;
use Intervention\Image\FileExtension;
use Intervention\Image\Image;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\ColorProcessorInterface;
use Intervention\Image\Interfaces\ColorspaceInterface;
use Intervention\Image\Interfaces\DriverInterface;
use Intervention\Image\Interfaces\FontProcessorInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\MediaType;

class Driver extends AbstractDriver
{
Expand Down Expand Up @@ -138,4 +142,28 @@ public function fontProcessor(): FontProcessorInterface
{
return new FontProcessor();
}

/**
* {@inheritdoc}
*
* @see DriverInterface::supports()
*/
public function supports(string|Format|FileExtension|MediaType $identifier): bool
{
try {
$format = Format::create($identifier);
} catch (NotSupportedException) {
return false;
}

return match ($format) {
Format::JPEG => boolval(imagetypes() & IMG_JPEG),
Format::WEBP => boolval(imagetypes() & IMG_WEBP),
Format::GIF => boolval(imagetypes() & IMG_GIF),
Format::PNG => boolval(imagetypes() & IMG_PNG),
Format::AVIF => boolval(imagetypes() & IMG_AVIF),
Format::BMP => boolval(imagetypes() & IMG_BMP),
default => false,
};
}
}
15 changes: 15 additions & 0 deletions src/Drivers/Imagick/Driver.php
Expand Up @@ -8,14 +8,18 @@
use ImagickPixel;
use Intervention\Image\Drivers\AbstractDriver;
use Intervention\Image\Exceptions\DriverException;
use Intervention\Image\Exceptions\NotSupportedException;
use Intervention\Image\Exceptions\RuntimeException;
use Intervention\Image\Format;
use Intervention\Image\FileExtension;
use Intervention\Image\Image;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\ColorProcessorInterface;
use Intervention\Image\Interfaces\ColorspaceInterface;
use Intervention\Image\Interfaces\DriverInterface;
use Intervention\Image\Interfaces\FontProcessorInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\MediaType;

class Driver extends AbstractDriver
{
Expand Down Expand Up @@ -141,4 +145,15 @@ public function fontProcessor(): FontProcessorInterface
{
return new FontProcessor();
}

public function supports(string|Format|FileExtension|MediaType $identifier): bool
{
try {
$format = Format::create($identifier);
} catch (NotSupportedException) {
return false;
}

return count(Imagick::queryFormats($format->name)) >= 1;
}
}
2 changes: 1 addition & 1 deletion src/FileExtension.php
Expand Up @@ -50,7 +50,7 @@ public function format(): Format
self::JPG2,
self::J2C,
self::JPC,
self::JPX => Format::JPEG2000,
self::JPX => Format::JP2,
self::HEIC,
self::HEIF => Format::HEIC,
};
Expand Down
40 changes: 38 additions & 2 deletions src/Format.php
Expand Up @@ -4,6 +4,7 @@

namespace Intervention\Image;

use Error;
use Intervention\Image\Encoders\AvifEncoder;
use Intervention\Image\Encoders\BmpEncoder;
use Intervention\Image\Encoders\GifEncoder;
Expand All @@ -13,6 +14,7 @@
use Intervention\Image\Encoders\PngEncoder;
use Intervention\Image\Encoders\TiffEncoder;
use Intervention\Image\Encoders\WebpEncoder;
use Intervention\Image\Exceptions\NotSupportedException;
use Intervention\Image\Interfaces\EncoderInterface;

enum Format
Expand All @@ -21,12 +23,46 @@ enum Format
case BMP;
case GIF;
case HEIC;
case JPEG2000;
case JP2;
case JPEG;
case PNG;
case TIFF;
case WEBP;

/**
* Create format from given identifier
*
* @param string|Format|MediaType|FileExtension $identifier
* @throws NotSupportedException
* @return Format
*/
public static function create(string|self|MediaType|FileExtension $identifier): self
{
if ($identifier instanceof self) {
return $identifier;
}

if ($identifier instanceof MediaType) {
return $identifier->format();
}

if ($identifier instanceof FileExtension) {
return $identifier->format();
}

try {
$format = MediaType::from(strtolower($identifier))->format();
} catch (Error) {
try {
$format = FileExtension::from(strtolower($identifier))->format();
} catch (Error) {
throw new NotSupportedException('Unable to create format from "' . $identifier . '".');
}
}

return $format;
}

/**
* Return the possible media (MIME) types for the current format
*
Expand Down Expand Up @@ -64,7 +100,7 @@ public function encoder(mixed ...$options): EncoderInterface
self::BMP => new BmpEncoder(...$options),
self::GIF => new GifEncoder(...$options),
self::HEIC => new HeicEncoder(...$options),
self::JPEG2000 => new Jpeg2000Encoder(...$options),
self::JP2 => new Jpeg2000Encoder(...$options),
self::JPEG => new JpegEncoder(...$options),
self::PNG => new PngEncoder(...$options),
self::TIFF => new TiffEncoder(...$options),
Expand Down
12 changes: 12 additions & 0 deletions src/Interfaces/DriverInterface.php
Expand Up @@ -7,6 +7,9 @@
use Intervention\Image\Exceptions\DriverException;
use Intervention\Image\Exceptions\NotSupportedException;
use Intervention\Image\Exceptions\RuntimeException;
use Intervention\Image\FileExtension;
use Intervention\Image\Format;
use Intervention\Image\MediaType;

interface DriverInterface
{
Expand Down Expand Up @@ -87,4 +90,13 @@ public function fontProcessor(): FontProcessorInterface;
* @return void
*/
public function checkHealth(): void;

/**
* Check if the current driver supports the given format and if the
* underlying PHP extension was built with support for the format.
*
* @param string|Format|FileExtension|MediaType $identifier
* @return bool
*/
public function supports(string|Format|FileExtension|MediaType $identifier): bool;
}
2 changes: 1 addition & 1 deletion src/MediaType.php
Expand Up @@ -60,7 +60,7 @@ public function format(): Format
self::IMAGE_TIFF => Format::TIFF,
self::IMAGE_JP2,
self::IMAGE_JPX,
self::IMAGE_JPM => Format::JPEG2000,
self::IMAGE_JPM => Format::JP2,
self::IMAGE_HEIF,
self::IMAGE_HEIC => Format::HEIC,
};
Expand Down
117 changes: 117 additions & 0 deletions tests/Unit/Drivers/Gd/DriverTest.php
Expand Up @@ -7,10 +7,14 @@
use Intervention\Image\Colors\Rgb\Colorspace;
use Intervention\Image\Colors\Rgb\Decoders\HexColorDecoder;
use Intervention\Image\Drivers\Gd\Driver;
use Intervention\Image\FileExtension;
use Intervention\Image\Format;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\ColorProcessorInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\MediaType;
use Intervention\Image\Tests\BaseTestCase;
use PHPUnit\Framework\Attributes\DataProvider;

final class DriverTest extends BaseTestCase
{
Expand Down Expand Up @@ -81,4 +85,117 @@ public function testColorProcessor(): void
$result = $this->driver->colorProcessor(new Colorspace());
$this->assertInstanceOf(ColorProcessorInterface::class, $result);
}

#[DataProvider('supportsDataProvider')]
public function testSupports(bool $result, mixed $identifier): void
{
$this->assertEquals($result, $this->driver->supports($identifier));
}

public static function supportsDataProvider(): array
{
return [
[true, Format::JPEG],
[true, MediaType::IMAGE_JPEG],
[true, MediaType::IMAGE_JPG],
[true, FileExtension::JPG],
[true, FileExtension::JPEG],
[true, 'jpg'],
[true, 'jpeg'],
[true, 'image/jpg'],
[true, 'image/jpeg'],

[true, Format::WEBP],
[true, MediaType::IMAGE_WEBP],
[true, MediaType::IMAGE_X_WEBP],
[true, FileExtension::WEBP],
[true, 'webp'],
[true, 'image/webp'],
[true, 'image/x-webp'],

[true, Format::GIF],
[true, MediaType::IMAGE_GIF],
[true, FileExtension::GIF],
[true, 'gif'],
[true, 'image/gif'],

[true, Format::PNG],
[true, MediaType::IMAGE_PNG],
[true, MediaType::IMAGE_X_PNG],
[true, FileExtension::PNG],
[true, 'png'],
[true, 'image/png'],
[true, 'image/x-png'],

[true, Format::AVIF],
[true, MediaType::IMAGE_AVIF],
[true, MediaType::IMAGE_X_AVIF],
[true, FileExtension::AVIF],
[true, 'avif'],
[true, 'image/avif'],
[true, 'image/x-avif'],

[true, Format::BMP],
[true, FileExtension::BMP],
[true, MediaType::IMAGE_BMP],
[true, MediaType::IMAGE_MS_BMP],
[true, MediaType::IMAGE_X_BITMAP],
[true, MediaType::IMAGE_X_BMP],
[true, MediaType::IMAGE_X_MS_BMP],
[true, MediaType::IMAGE_X_WINDOWS_BMP],
[true, MediaType::IMAGE_X_WIN_BITMAP],
[true, MediaType::IMAGE_X_XBITMAP],
[true, 'bmp'],
[true, 'image/bmp'],
[true, 'image/ms-bmp'],
[true, 'image/x-bitmap'],
[true, 'image/x-bmp'],
[true, 'image/x-ms-bmp'],
[true, 'image/x-windows-bmp'],
[true, 'image/x-win-bitmap'],
[true, 'image/x-xbitmap'],

[false, Format::TIFF],
[false, MediaType::IMAGE_TIFF],
[false, FileExtension::TIFF],
[false, FileExtension::TIF],
[false, 'tif'],
[false, 'tiff'],
[false, 'image/tiff'],

[false, Format::JP2],
[false, MediaType::IMAGE_JP2],
[false, MediaType::IMAGE_JPX],
[false, MediaType::IMAGE_JPM],
[false, FileExtension::TIFF],
[false, FileExtension::TIF],
[false, FileExtension::JP2],
[false, FileExtension::J2K],
[false, FileExtension::JPF],
[false, FileExtension::JPM],
[false, FileExtension::JPG2],
[false, FileExtension::J2C],
[false, FileExtension::JPC],
[false, FileExtension::JPX],
[false, 'jp2'],
[false, 'j2k'],
[false, 'jpf'],
[false, 'jpm'],
[false, 'jpg2'],
[false, 'j2c'],
[false, 'jpc'],
[false, 'jpx'],

[false, Format::HEIC],
[false, MediaType::IMAGE_HEIC],
[false, MediaType::IMAGE_HEIF],
[false, FileExtension::HEIC],
[false, FileExtension::HEIF],
[false, 'heic'],
[false, 'heif'],
[false, 'image/heic'],
[false, 'image/heif'],

];
}
}

0 comments on commit 9e44aa5

Please sign in to comment.