class AvifImageEffect

Converts an image resource to AVIF, with fallback.

Hierarchy

Expanded class hierarchy of AvifImageEffect

File

core/modules/image/src/Plugin/ImageEffect/AvifImageEffect.php, line 17

Namespace

Drupal\image\Plugin\ImageEffect
View source
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());
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
AvifImageEffect::$imageToolkitManager protected property The image toolkit manager.
AvifImageEffect::applyEffect public function Applies an image effect to the image object. Overrides ConvertImageEffect::applyEffect
AvifImageEffect::buildConfigurationForm public function Overrides ConvertImageEffect::buildConfigurationForm
AvifImageEffect::create public static function Creates an instance of the plugin. Overrides ImageEffectBase::create
AvifImageEffect::getDerivativeExtension public function Returns the extension of the derivative after applying this image effect. Overrides ConvertImageEffect::getDerivativeExtension
AvifImageEffect::isAvifSupported protected function Is AVIF supported by the image toolkit.
ConfigurableImageEffectBase::validateConfigurationForm public function 2
ConvertImageEffect::defaultConfiguration public function Gets default configuration for this plugin. Overrides ImageEffectBase::defaultConfiguration
ConvertImageEffect::getSummary public function Returns a render array summarizing the configuration of the image effect. Overrides ImageEffectBase::getSummary
ConvertImageEffect::submitConfigurationForm public function Overrides ConfigurableImageEffectBase::submitConfigurationForm
ImageEffectBase::$logger protected property A logger instance.
ImageEffectBase::$uuid protected property The image effect ID.
ImageEffectBase::$weight protected property The weight of the image effect.
ImageEffectBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies
ImageEffectBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
ImageEffectBase::getUuid public function Returns the unique ID representing the image effect. Overrides ImageEffectInterface::getUuid
ImageEffectBase::getWeight public function Returns the weight of the image effect. Overrides ImageEffectInterface::getWeight
ImageEffectBase::label public function Returns the image effect label. Overrides ImageEffectInterface::label
ImageEffectBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
ImageEffectBase::setWeight public function Sets the weight for this image effect. Overrides ImageEffectInterface::setWeight
ImageEffectBase::transformDimensions public function Determines the dimensions of the styled image. Overrides ImageEffectInterface::transformDimensions 4
ImageEffectBase::__construct public function
PluginInspectionInterface::getPluginDefinition public function Gets the definition of the plugin implementation. 7
PluginInspectionInterface::getPluginId public function Gets the plugin ID of the plugin instance. 3

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.