class RotateImageEffect

Same name and namespace in other branches
  1. 9 core/modules/image/src/Plugin/ImageEffect/RotateImageEffect.php \Drupal\image\Plugin\ImageEffect\RotateImageEffect
  2. 8.9.x core/modules/image/src/Plugin/ImageEffect/RotateImageEffect.php \Drupal\image\Plugin\ImageEffect\RotateImageEffect
  3. 11.x core/modules/image/src/Plugin/ImageEffect/RotateImageEffect.php \Drupal\image\Plugin\ImageEffect\RotateImageEffect

Rotates an image resource.

Hierarchy

Expanded class hierarchy of RotateImageEffect

File

core/modules/image/src/Plugin/ImageEffect/RotateImageEffect.php, line 16

Namespace

Drupal\image\Plugin\ImageEffect
View source
class RotateImageEffect extends ConfigurableImageEffectBase {
  
  /**
   * {@inheritdoc}
   */
  public function applyEffect(ImageInterface $image) {
    if (!empty($this->configuration['random'])) {
      $degrees = abs((double) $this->configuration['degrees']);
      $this->configuration['degrees'] = rand(-$degrees, $degrees);
    }
    if (!$image->rotate($this->configuration['degrees'], $this->configuration['bgcolor'])) {
      $this->logger
        ->error('Image rotate failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', [
        '%toolkit' => $image->getToolkitId(),
        '%path' => $image->getSource(),
        '%mimetype' => $image->getMimeType(),
        '%dimensions' => $image->getWidth() . 'x' . $image->getHeight(),
      ]);
      return FALSE;
    }
    return TRUE;
  }
  
  /**
   * {@inheritdoc}
   */
  public function transformDimensions(array &$dimensions, $uri) {
    // If the rotate is not random and current dimensions are set,
    // then the new dimensions can be determined.
    if (!$this->configuration['random'] && $dimensions['width'] && $dimensions['height']) {
      $rect = new Rectangle($dimensions['width'], $dimensions['height']);
      $rect = $rect->rotate($this->configuration['degrees']);
      $dimensions['width'] = $rect->getBoundingWidth();
      $dimensions['height'] = $rect->getBoundingHeight();
    }
    else {
      $dimensions['width'] = $dimensions['height'] = NULL;
    }
  }
  
  /**
   * {@inheritdoc}
   */
  public function getSummary() {
    $summary = [
      '#theme' => 'image_rotate_summary',
      '#data' => $this->configuration,
    ];
    $summary += parent::getSummary();
    return $summary;
  }
  
  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'degrees' => 0,
      'bgcolor' => NULL,
      'random' => FALSE,
    ];
  }
  
  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form['degrees'] = [
      '#type' => 'number',
      '#default_value' => $this->configuration['degrees'],
      '#title' => $this->t('Rotation angle'),
      '#description' => $this->t('The number of degrees the image should be rotated. Positive numbers are clockwise, negative are counter-clockwise.'),
      '#field_suffix' => '°',
      '#required' => TRUE,
    ];
    $form['bgcolor'] = [
      '#type' => 'textfield',
      '#default_value' => $this->configuration['bgcolor'],
      '#title' => $this->t('Background color'),
      '#description' => $this->t('The background color to use for exposed areas of the image. Use web-style hex colors (#FFFFFF for white, #000000 for black). Leave blank for transparency on image types that support it.'),
      '#size' => 7,
      '#maxlength' => 7,
    ];
    $form['random'] = [
      '#type' => 'checkbox',
      '#default_value' => $this->configuration['random'],
      '#title' => $this->t('Randomize'),
      '#description' => $this->t('Randomize the rotation angle for each image. The angle specified above is used as a maximum.'),
    ];
    return $form;
  }
  
  /**
   * {@inheritdoc}
   */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
    if (!$form_state->isValueEmpty('bgcolor') && !Color::validateHex($form_state->getValue('bgcolor'))) {
      $form_state->setErrorByName('bgcolor', $this->t('Background color must be a hexadecimal color value.'));
    }
  }
  
  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    parent::submitConfigurationForm($form, $form_state);
    $this->configuration['degrees'] = $form_state->getValue('degrees');
    $this->configuration['bgcolor'] = $form_state->getValue('bgcolor');
    $this->configuration['random'] = $form_state->getValue('random');
  }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
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::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
ImageEffectBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
ImageEffectBase::getDerivativeExtension public function Returns the extension of the derivative after applying this image effect. Overrides ImageEffectInterface::getDerivativeExtension 1
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::__construct public function
PluginInspectionInterface::getPluginDefinition public function Gets the definition of the plugin implementation. 6
PluginInspectionInterface::getPluginId public function Gets the plugin ID of the plugin instance. 2
RotateImageEffect::applyEffect public function Applies an image effect to the image object. Overrides ImageEffectInterface::applyEffect
RotateImageEffect::buildConfigurationForm public function
RotateImageEffect::defaultConfiguration public function Gets default configuration for this plugin. Overrides ImageEffectBase::defaultConfiguration
RotateImageEffect::getSummary public function Returns a render array summarizing the configuration of the image effect. Overrides ImageEffectBase::getSummary
RotateImageEffect::submitConfigurationForm public function Overrides ConfigurableImageEffectBase::submitConfigurationForm
RotateImageEffect::transformDimensions public function Determines the dimensions of the styled image. Overrides ImageEffectBase::transformDimensions
RotateImageEffect::validateConfigurationForm public function Overrides ConfigurableImageEffectBase::validateConfigurationForm

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