class ResizeImageEffect

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

Resizes an image resource.

Attributes

#[ImageEffect(id: "image_resize", label: new TranslatableMarkup("Resize"), description: new TranslatableMarkup("Resizing will make images an exact set of dimensions. This may cause images to be stretched or shrunk disproportionately."))]

Hierarchy

  • class \Drupal\image\Plugin\ImageEffect\ResizeImageEffect extends \Drupal\image\ConfigurableImageEffectBase

Expanded class hierarchy of ResizeImageEffect

File

core/modules/image/src/Plugin/ImageEffect/ResizeImageEffect.php, line 14

Namespace

Drupal\image\Plugin\ImageEffect
View source
class ResizeImageEffect extends ConfigurableImageEffectBase {
  
  /**
   * {@inheritdoc}
   */
  public function applyEffect(ImageInterface $image) {
    if (!$image->resize($this->configuration['width'], $this->configuration['height'])) {
      $this->logger
        ->error('Image resize 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) {
    // The new image will have the exact dimensions defined for the effect.
    $dimensions['width'] = $this->configuration['width'];
    $dimensions['height'] = $this->configuration['height'];
  }
  
  /**
   * {@inheritdoc}
   */
  public function getSummary() {
    $summary = [
      '#theme' => 'image_resize_summary',
      '#data' => $this->configuration,
    ];
    $summary += parent::getSummary();
    return $summary;
  }
  
  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'width' => NULL,
      'height' => NULL,
    ];
  }
  
  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form['width'] = [
      '#type' => 'number',
      '#title' => $this->t('Width'),
      '#default_value' => $this->configuration['width'],
      '#field_suffix' => ' ' . $this->t('pixels'),
      '#required' => TRUE,
      '#min' => 1,
    ];
    $form['height'] = [
      '#type' => 'number',
      '#title' => $this->t('Height'),
      '#default_value' => $this->configuration['height'],
      '#field_suffix' => ' ' . $this->t('pixels'),
      '#required' => TRUE,
      '#min' => 1,
    ];
    return $form;
  }
  
  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    parent::submitConfigurationForm($form, $form_state);
    $this->configuration['height'] = $form_state->getValue('height');
    $this->configuration['width'] = $form_state->getValue('width');
  }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
AutowiredInstanceTrait::createInstanceAutowired public static function Instantiates a new instance of the implementing class using autowiring.
AutowiredInstanceTrait::getAutowireArguments private static function Resolves arguments for a method using autowiring.
ConfigurableImageEffectBase::validateConfigurationForm public function Overrides PluginFormInterface::validateConfigurationForm 2
ConfigurableTrait::$configuration protected property Configuration information passed into the plugin.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
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 Overrides DependentPluginInterface::calculateDependencies
ImageEffectBase::getConfiguration public function Overrides ConfigurableTrait::getConfiguration
ImageEffectBase::getDerivativeExtension public function Overrides ImageEffectInterface::getDerivativeExtension 1
ImageEffectBase::getUuid public function Overrides ImageEffectInterface::getUuid
ImageEffectBase::getWeight public function Overrides ImageEffectInterface::getWeight
ImageEffectBase::label public function Overrides ImageEffectInterface::label
ImageEffectBase::onCollectionDependencyRemoval public function Overrides RemovableDependentPluginInterface::onCollectionDependencyRemoval
ImageEffectBase::setConfiguration public function Overrides ConfigurableTrait::setConfiguration
ImageEffectBase::setWeight public function Overrides ImageEffectInterface::setWeight
ImageEffectBase::__construct public function Overrides ConfigurablePluginBase::__construct
MessengerTrait::$messenger protected property The messenger. 25
MessengerTrait::messenger public function Gets the messenger. 25
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin ID.
PluginBase::create public static function Instantiates a new instance of the implementing class using autowiring. 64
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 2
PluginBase::getPluginId public function Gets the plugin ID of the plugin instance. Overrides PluginInspectionInterface::getPluginId
ResizeImageEffect::applyEffect public function Overrides ImageEffectInterface::applyEffect 2
ResizeImageEffect::buildConfigurationForm public function Overrides PluginFormInterface::buildConfigurationForm 2
ResizeImageEffect::defaultConfiguration public function Overrides ConfigurableTrait::defaultConfiguration 2
ResizeImageEffect::getSummary public function Overrides ImageEffectBase::getSummary 2
ResizeImageEffect::submitConfigurationForm public function Overrides ConfigurableImageEffectBase::submitConfigurationForm 2
ResizeImageEffect::transformDimensions public function Overrides ImageEffectBase::transformDimensions 1
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language. 1

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