You are here

public function AutoAspectEffect::transformDimensions in Thunder 8.2

Same name and namespace in other branches
  1. 8.5 modules/thunder_media/src/Plugin/ImageEffect/AutoAspectEffect.php \Drupal\thunder_media\Plugin\ImageEffect\AutoAspectEffect::transformDimensions()
  2. 8.3 modules/thunder_media/src/Plugin/ImageEffect/AutoAspectEffect.php \Drupal\thunder_media\Plugin\ImageEffect\AutoAspectEffect::transformDimensions()
  3. 8.4 modules/thunder_media/src/Plugin/ImageEffect/AutoAspectEffect.php \Drupal\thunder_media\Plugin\ImageEffect\AutoAspectEffect::transformDimensions()
  4. 6.2.x modules/thunder_media/src/Plugin/ImageEffect/AutoAspectEffect.php \Drupal\thunder_media\Plugin\ImageEffect\AutoAspectEffect::transformDimensions()
  5. 6.0.x modules/thunder_media/src/Plugin/ImageEffect/AutoAspectEffect.php \Drupal\thunder_media\Plugin\ImageEffect\AutoAspectEffect::transformDimensions()
  6. 6.1.x modules/thunder_media/src/Plugin/ImageEffect/AutoAspectEffect.php \Drupal\thunder_media\Plugin\ImageEffect\AutoAspectEffect::transformDimensions()

Determines the dimensions of the styled image.

Parameters

array &$dimensions: Dimensions to be modified - an array with the following keys:

  • width: the width in pixels, or NULL if unknown
  • height: the height in pixels, or NULL if unknown

When either of the dimensions are NULL, the corresponding HTML attribute will be omitted when an image style using this image effect is used.

string $uri: Original image file URI. It is passed in to allow an effect to optionally use this information to retrieve additional image metadata to determine dimensions of the styled image. ImageEffectInterface::transformDimensions key objective is to calculate styled image dimensions without performing actual image operations, so be aware that performing IO on the URI may lead to decrease in performance.

Overrides ImageEffectBase::transformDimensions

File

modules/thunder_media/src/Plugin/ImageEffect/AutoAspectEffect.php, line 54

Class

AutoAspectEffect
Resizes an image resource.

Namespace

Drupal\thunder_media\Plugin\ImageEffect

Code

public function transformDimensions(array &$dimensions, $uri) {
  if (!isset($dimensions['width']) || !isset($dimensions['height'])) {

    // We cannot know which preset would be executed and thus cannot know the
    // resulting dimensions, unless both styles return the same dimensions:
    $landscape_dimensions = $portrait_dimensions = $dimensions;

    /* @var ImageStyle $landscape_style */
    $landscape_style = ImageStyle::load($this->configuration['landscape']);
    $landscape_style
      ->transformDimensions($landscape_dimensions, $uri);

    /* @var ImageStyle $portrait_style */
    $portrait_style = ImageStyle::load($this->configuration['portrait']);
    $portrait_style
      ->transformDimensions($portrait_dimensions, $uri);
    if ($landscape_dimensions == $portrait_dimensions) {
      $dimensions = $landscape_dimensions;
    }
    else {
      $dimensions['width'] = $dimensions['height'] = NULL;
    }
  }
  else {
    $ratio_adjustment = isset($this->configuration['ratio_adjustment']) ? floatval($this->configuration['ratio_adjustment']) : 1;
    $aspect = $dimensions['width'] / $dimensions['height'];
    $style_name = $aspect * $ratio_adjustment > 1 ? $this->configuration['landscape'] : $this->configuration['portrait'];

    /* @var ImageStyle $style */
    $style = ImageStyle::load($style_name);
    $style
      ->transformDimensions($dimensions, $uri);
  }
}