You are here

public function AutoAspectEffect::transformDimensions in Auto image style 8

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

src/Plugin/ImageEffect/AutoAspectEffect.php, line 55

Class

AutoAspectEffect
Resizes an image resource.

Namespace

Drupal\auto_image_style\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);
  }
}