Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove GD requirement #13187

Draft
wants to merge 1 commit into
base: 5.x
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 30 additions & 16 deletions src/services/Images.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use enshrined\svgSanitize\Sanitizer;
use Imagine\Gd\Imagine as GdImagine;
use Imagine\Image\Format;
use Imagine\Imagick\Imagick;
use Imagine\Imagick\Imagine as ImagickImagine;
use Throwable;
use yii\base\Component;
Expand Down Expand Up @@ -67,7 +66,7 @@ public function init(): void
} elseif ($this->getCanUseImagick()) {
$this->_driver = self::DRIVER_IMAGICK;
} else {
$this->_driver = self::DRIVER_GD;
$this->supportedImageFormats = [];
}

parent::init();
Expand Down Expand Up @@ -95,21 +94,32 @@ public function getIsImagick(): bool

/**
* Returns the version of the image driver.
*
* @return string
*/
public function getVersion(): string
public function getVersion(): ?string
{
if ($this->getIsGd()) {
return App::extensionVersion('gd');
}

$version = App::extensionVersion('imagick');
try {
$version .= ' (ImageMagick ' . $this->getImageMagickApiVersion() . ')';
} catch (Throwable) {
if ($this->getIsImagick()) {
$version = App::extensionVersion('imagick');
try {
$version .= ' (ImageMagick ' . $this->getImageMagickApiVersion() . ')';
} catch (Throwable) {
}
return $version;
}
return $version;

return null;
}

protected function getDriverInfo(): ?\Imagine\Driver\Info
{
return match(true) {
$this->getIsImagick() => ImagickImagine::getDriverInfo(),
$this->getIsGd() => GdImagine::getDriverInfo(),
default => null,
};
}

/**
Expand Down Expand Up @@ -191,8 +201,9 @@ public function getCanUseImagick(): bool
*/
public function getSupportsWebP(): bool
{
$info = $this->getIsImagick() ? ImagickImagine::getDriverInfo() : GdImagine::getDriverInfo();
return $info->isFormatSupported(Format::ID_WEBP);
return
in_array(Format::ID_WEBP, $this->supportedImageFormats, true) ||
(bool) $this->getDriverInfo()?->isFormatSupported(Format::ID_WEBP);
}

/**
Expand All @@ -202,8 +213,9 @@ public function getSupportsWebP(): bool
*/
public function getSupportsAvif(): bool
{
$info = $this->getIsImagick() ? ImagickImagine::getDriverInfo() : GdImagine::getDriverInfo();
return $info->isFormatSupported(Format::ID_AVIF);
return
in_array(Format::ID_AVIF, $this->supportedImageFormats, true) ||
(bool) $this->getDriverInfo()?->isFormatSupported(Format::ID_AVIF);
}

/**
Expand All @@ -214,8 +226,10 @@ public function getSupportsAvif(): bool
*/
public function getSupportsHeic(): bool
{
$info = $this->getIsImagick() ? ImagickImagine::getDriverInfo() : GdImagine::getDriverInfo();
return $info->isFormatSupported(Format::ID_HEIC);
return
in_array(Format::ID_HEIC, $this->supportedImageFormats, true) ||
(bool) ($this->getDriverInfo()?->isFormatSupported(Format::ID_HEIC)
);
}

/**
Expand Down