You are here

public function RelativeCropImageEffect::transformDimensions in Image Effects 8.3

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 ResizeImageEffect::transformDimensions

1 call to RelativeCropImageEffect::transformDimensions()
RelativeCropImageEffect::applyEffect in src/Plugin/ImageEffect/RelativeCropImageEffect.php
Applies an image effect to the image object.

File

src/Plugin/ImageEffect/RelativeCropImageEffect.php, line 75

Class

RelativeCropImageEffect
Provides an image effect that crops images to a ratio.

Namespace

Drupal\image_effects\Plugin\ImageEffect

Code

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

    // If the effect has not been configured, there is nothing we can do.
    return;
  }
  $ratio = $this->configuration['width'] / $this->configuration['height'];

  // Figure out whether the image is too wide or too high.
  $ratio_width = (int) round($dimensions['height'] * $ratio);
  if ($dimensions['width'] > $ratio_width) {
    $dimensions['width'] = $ratio_width;
  }
  elseif ($dimensions['width'] < $ratio_width) {

    // Instead of it being too narrow we consider the image as being too tall.
    $dimensions['height'] = (int) round($dimensions['width'] / $ratio);
  }

  // If the image already fits the ratio, do not change the dimensions.
}