"Fossies" - the Fresh Open Source Software Archive

Member "drupal-8.9.10/core/modules/image/src/Plugin/ImageEffect/ConvertImageEffect.php" (26 Nov 2020, 2185 Bytes) of package /linux/www/drupal-8.9.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ConvertImageEffect.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 
    3 namespace Drupal\image\Plugin\ImageEffect;
    4 
    5 use Drupal\Core\Form\FormStateInterface;
    6 use Drupal\Core\Image\ImageInterface;
    7 use Drupal\image\ConfigurableImageEffectBase;
    8 
    9 /**
   10  * Converts an image resource.
   11  *
   12  * @ImageEffect(
   13  *   id = "image_convert",
   14  *   label = @Translation("Convert"),
   15  *   description = @Translation("Converts an image between extensions (e.g. from PNG to JPEG).")
   16  * )
   17  */
   18 class ConvertImageEffect extends ConfigurableImageEffectBase {
   19 
   20   /**
   21    * {@inheritdoc}
   22    */
   23   public function applyEffect(ImageInterface $image) {
   24     if (!$image->convert($this->configuration['extension'])) {
   25       $this->logger->error('Image convert failed using the %toolkit toolkit on %path (%mimetype)', ['%toolkit' => $image->getToolkitId(), '%path' => $image->getSource(), '%mimetype' => $image->getMimeType()]);
   26       return FALSE;
   27     }
   28     return TRUE;
   29   }
   30 
   31   /**
   32    * {@inheritdoc}
   33    */
   34   public function getDerivativeExtension($extension) {
   35     return $this->configuration['extension'];
   36   }
   37 
   38   /**
   39    * {@inheritdoc}
   40    */
   41   public function getSummary() {
   42     $summary = [
   43       '#markup' => mb_strtoupper($this->configuration['extension']),
   44     ];
   45     $summary += parent::getSummary();
   46 
   47     return $summary;
   48   }
   49 
   50   /**
   51    * {@inheritdoc}
   52    */
   53   public function defaultConfiguration() {
   54     return [
   55       'extension' => NULL,
   56     ];
   57   }
   58 
   59   /**
   60    * {@inheritdoc}
   61    */
   62   public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
   63     $extensions = \Drupal::service('image.toolkit.manager')->getDefaultToolkit()->getSupportedExtensions();
   64     $options = array_combine(
   65       $extensions,
   66       array_map('mb_strtoupper', $extensions)
   67     );
   68     $form['extension'] = [
   69       '#type' => 'select',
   70       '#title' => t('Extension'),
   71       '#default_value' => $this->configuration['extension'],
   72       '#required' => TRUE,
   73       '#options' => $options,
   74     ];
   75     return $form;
   76   }
   77 
   78   /**
   79    * {@inheritdoc}
   80    */
   81   public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
   82     parent::submitConfigurationForm($form, $form_state);
   83     $this->configuration['extension'] = $form_state->getValue('extension');
   84   }
   85 
   86 }