You are here

public function AutoOrientImageEffect::transformDimensions in Image Effects 8

Same name and namespace in other branches
  1. 8.3 src/Plugin/ImageEffect/AutoOrientImageEffect.php \Drupal\image_effects\Plugin\ImageEffect\AutoOrientImageEffect::transformDimensions()
  2. 8.2 src/Plugin/ImageEffect/AutoOrientImageEffect.php \Drupal\image_effects\Plugin\ImageEffect\AutoOrientImageEffect::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

src/Plugin/ImageEffect/AutoOrientImageEffect.php, line 163

Class

AutoOrientImageEffect
Automatically adjusts the orientation of an image resource.

Namespace

Drupal\image_effects\Plugin\ImageEffect

Code

public function transformDimensions(array &$dimensions, $uri) {

  // Test to see if EXIF is supported by the image format.
  $mime_type = $this->mimeTypeGuesser
    ->guess($uri);
  if (!in_array($mime_type, [
    'image/jpeg',
    'image/tiff',
  ])) {

    // Not an EXIF enabled image, return.
    return;
  }
  if ($dimensions['width'] && $dimensions['height'] && $this->configuration['scan_exif']) {

    // Both dimensions in input, and effect is configured to check the
    // the input file. Read EXIF data, and determine image orientation.
    if (($file_path = $this->fileSystem
      ->realpath($uri)) && function_exists('exif_read_data')) {
      if ($exif_data = @exif_read_data($file_path)) {
        $orientation = isset($exif_data['Orientation']) ? $exif_data['Orientation'] : NULL;
        if (in_array($orientation, [
          5,
          6,
          7,
          8,
        ])) {
          $tmp = $dimensions['width'];
          $dimensions['width'] = $dimensions['height'];
          $dimensions['height'] = $tmp;
        }
        return;
      }
    }
  }

  // Either no full dimensions in input, or effect is configured to skip
  // checking the input file, or EXIF extension is missing. Set both
  // dimensions to NULL.
  $dimensions['width'] = $dimensions['height'] = NULL;
}