You are here

class ConvertImageEffect in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/image/src/Plugin/ImageEffect/ConvertImageEffect.php \Drupal\image\Plugin\ImageEffect\ConvertImageEffect
  2. 9 core/modules/image/src/Plugin/ImageEffect/ConvertImageEffect.php \Drupal\image\Plugin\ImageEffect\ConvertImageEffect

Converts an image resource.

Plugin annotation


@ImageEffect(
  id = "image_convert",
  label = @Translation("Convert"),
  description = @Translation("Converts an image to a format (such as JPEG).")
)

Hierarchy

Expanded class hierarchy of ConvertImageEffect

File

core/modules/image/src/Plugin/ImageEffect/ConvertImageEffect.php, line 18

Namespace

Drupal\image\Plugin\ImageEffect
View source
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' => mb_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('mb_strtoupper', $extensions));
    $form['extension'] = [
      '#type' => 'select',
      '#title' => t('Convert to'),
      '#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');
  }

}

Members