Mercurial > hg > cmmr2012-drupal-site
diff core/modules/image/src/Plugin/ImageEffect/ConvertImageEffect.php @ 0:c75dbcec494b
Initial commit from drush-created site
author | Chris Cannam |
---|---|
date | Thu, 05 Jul 2018 14:24:15 +0000 |
parents | |
children | a9cd425dd02b |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/core/modules/image/src/Plugin/ImageEffect/ConvertImageEffect.php Thu Jul 05 14:24:15 2018 +0000 @@ -0,0 +1,87 @@ +<?php + +namespace Drupal\image\Plugin\ImageEffect; + +use Drupal\Component\Utility\Unicode; +use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Image\ImageInterface; +use Drupal\image\ConfigurableImageEffectBase; + +/** + * Converts an image resource. + * + * @ImageEffect( + * id = "image_convert", + * label = @Translation("Convert"), + * description = @Translation("Converts an image between extensions (e.g. from PNG to JPEG).") + * ) + */ +class ConvertImageEffect extends ConfigurableImageEffectBase { + + /** + * {@inheritdoc} + */ + public function applyEffect(ImageInterface $image) { + if (!$image->convert($this->configuration['extension'])) { + $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->configuration['extension']; + } + + /** + * {@inheritdoc} + */ + public function getSummary() { + $summary = [ + '#markup' => Unicode::strtoupper($this->configuration['extension']), + ]; + $summary += parent::getSummary(); + + return $summary; + } + + /** + * {@inheritdoc} + */ + public function defaultConfiguration() { + return [ + 'extension' => NULL, + ]; + } + + /** + * {@inheritdoc} + */ + public function buildConfigurationForm(array $form, FormStateInterface $form_state) { + $extensions = \Drupal::service('image.toolkit.manager')->getDefaultToolkit()->getSupportedExtensions(); + $options = array_combine( + $extensions, + array_map(['\Drupal\Component\Utility\Unicode', 'strtoupper'], $extensions) + ); + $form['extension'] = [ + '#type' => 'select', + '#title' => t('Extension'), + '#default_value' => $this->configuration['extension'], + '#required' => TRUE, + '#options' => $options, + ]; + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { + parent::submitConfigurationForm($form, $form_state); + $this->configuration['extension'] = $form_state->getValue('extension'); + } + +}