You are here

public function AutorotateImageEffect::transformDimensions in Imagick 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/AutorotateImageEffect.php, line 38

Class

AutorotateImageEffect
Autorotates an image resource.

Namespace

Drupal\imagick\Plugin\ImageEffect

Code

public function transformDimensions(array &$dimensions, $uri) {
  try {
    $image = new Imagick($uri);
    $orientation = $image
      ->getImageOrientation();
    switch ($orientation) {

      // If the image is rotated 90° or 270°, swap the width and height.
      case Imagick::ORIENTATION_RIGHTTOP:
      case Imagick::ORIENTATION_LEFTTOP:
      case Imagick::ORIENTATION_LEFTBOTTOM:
      case Imagick::ORIENTATION_RIGHTBOTTOM:
        $dimensions = [
          'width' => $dimensions['height'],
          'height' => $dimensions['width'],
        ];
    }
  } catch (\ImagickException $e) {
    $this->logger
      ->error('Image autorotate failed to transform dimensions for %uri: @message', [
      '%uri' => $uri,
      '@message' => $e
        ->getMessage(),
    ]);
  }
}