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()); } }