Commit 8d563697 authored by catch's avatar catch
Browse files

Issue #3510582 by mstrelan, catch, smustgrave: AVIF conversion with WEBP fallback

(cherry picked from commit b92344d4)
parent 3df1b8d2
Loading
Loading
Loading
Loading
Loading
+82 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\image\Plugin\ImageEffect;

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Image\ImageInterface;
use Drupal\Core\ImageToolkit\ImageToolkitManager;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\image\Attribute\ImageEffect;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Converts an image resource to AVIF, with fallback.
 */
#[ImageEffect(
  id: "image_convert_avif",
  label: new TranslatableMarkup("Convert to AVIF"),
  description: new TranslatableMarkup("Converts an image to AVIF, with a fallback if AVIF is not supported."),
)]
class AvifImageEffect extends ConvertImageEffect {

  /**
   * The image toolkit manager.
   *
   * @var \Drupal\Core\ImageToolkit\ImageToolkitManager
   */
  protected ImageToolkitManager $imageToolkitManager;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): static {
    $instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
    $instance->imageToolkitManager = $container->get(ImageToolkitManager::class);
    return $instance;
  }

  /**
   * {@inheritdoc}
   */
  public function applyEffect(ImageInterface $image) {
    // If avif is not supported fallback to the parent.
    if (!$this->isAvifSupported()) {
      return parent::applyEffect($image);
    }

    if (!$image->convert('avif')) {
      $this->logger->error('Image convert failed using the %toolkit toolkit on %path (%mimetype)', ['%toolkit' => $image->getToolkitId(), '%path' => $image->getSource(), '%mimetype' => $image->getMimeType()]);
      return FALSE;
    }

    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function getDerivativeExtension($extension) {
    return $this->isAvifSupported() ? 'avif' : $this->configuration['extension'];
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildConfigurationForm($form, $form_state);
    unset($form['extension']['#options']['avif']);
    $form['extension']['#title'] = $this->t('Fallback format');
    $form['extension']['#description'] = $this->t('Format to use if AVIF is not available.');
    return $form;
  }

  /**
   * Is AVIF supported by the image toolkit.
   */
  protected function isAvifSupported(): bool {
    return in_array('avif', $this->imageToolkitManager->getDefaultToolkit()->getSupportedExtensions());
  }

}
+25 −0
Original line number Diff line number Diff line
@@ -119,6 +119,31 @@ public function testConvertEffect(): void {
    $this->assertEquals('jpeg', $calls['convert'][0][0]);
  }

  /**
   * Tests the 'image_convert_avif' effect when avif is supported.
   */
  public function testConvertAvifEffect(): void {
    $this->container->get('keyvalue')->get('image_test')->set('avif_enabled', TRUE);
    $this->assertImageEffect(['convert'], 'image_convert_avif', [
      'extension' => 'webp',
    ]);

    $calls = $this->imageTestGetAllCalls();
    $this->assertEquals('avif', $calls['convert'][0][0]);
  }

  /**
   * Tests the 'image_convert_avif' effect with webp fallback.
   */
  public function testConvertAvifEffectFallback(): void {
    $this->assertImageEffect(['convert'], 'image_convert_avif', [
      'extension' => 'webp',
    ]);

    $calls = $this->imageTestGetAllCalls();
    $this->assertEquals('webp', $calls['convert'][0][0]);
  }

  /**
   * Tests the 'image_scale_and_crop' effect.
   */
+6 −6
Original line number Diff line number Diff line
@@ -439,9 +439,6 @@ public function getRequirements() {
      IMG_AVIF => 'AVIF',
    ];
    $supported_formats = array_filter($check_formats, fn($type) => imagetypes() & $type, ARRAY_FILTER_USE_KEY);
    if (isset($supported_formats[IMG_AVIF]) && !$this->checkAvifSupport()) {
      unset($supported_formats[IMG_AVIF]);
    }
    $unsupported_formats = array_diff_key($check_formats, $supported_formats);

    $descriptions = [];
@@ -556,7 +553,7 @@ public function extensionToImageType($extension) {
   * @return bool
   *   TRUE if AVIF is fully supported, FALSE otherwise.
   */
  protected function checkAvifSupport(): bool {
  protected static function checkAvifSupport(): bool {
    static $supported = NULL;

    if ($supported !== NULL) {
@@ -578,13 +575,16 @@ protected function checkAvifSupport(): bool {
   *   IMAGETYPE_* constant (e.g. IMAGETYPE_JPEG, IMAGETYPE_PNG, etc.).
   */
  protected static function supportedTypes() {
    return [
    $types = [
      IMAGETYPE_PNG,
      IMAGETYPE_JPEG,
      IMAGETYPE_GIF,
      IMAGETYPE_WEBP,
      IMAGETYPE_AVIF,
    ];
    if (static::checkAvifSupport()) {
      $types[] = IMAGETYPE_AVIF;
    }
    return $types;
  }

}
+5 −1
Original line number Diff line number Diff line
@@ -253,7 +253,11 @@ public static function getSupportedExtensions() {
   *   IMAGETYPE_* constant (e.g. IMAGETYPE_JPEG, IMAGETYPE_PNG, etc.).
   */
  protected static function supportedTypes() {
    return [IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF];
    $types = [IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF];
    if (\Drupal::keyValue('image_test')->get('avif_enabled', FALSE)) {
      $types[] = IMAGETYPE_AVIF;
    }
    return $types;
  }

  /**