You are here

public static function ImageUtility::resizeDimensions in Image Effects 8.3

Same name and namespace in other branches
  1. 8 src/Component/ImageUtility.php \Drupal\image_effects\Component\ImageUtility::resizeDimensions()
  2. 8.2 src/Component/ImageUtility.php \Drupal\image_effects\Component\ImageUtility::resizeDimensions()

Determines the dimensions of a resized image.

Based on the current size and resize specification.

Parameters

int|null $source_width: Source image width.

int|null $source_height: Source image height.

string|null $width_specification: The width specification. An integer value or a % specification.

string|null $height_specification: The height specification. An integer value or a % specification.

bool $square: (Optional) when TRUE and one of the specifications is NULL, will return the same value for width and height.

Return value

array Associative array.

  • width: Integer with the resized image width.
  • height: Integer with the resized image height.
7 calls to ImageUtility::resizeDimensions()
ImageUtilityTest::testResizeDimensions in tests/src/Unit/ImageUtilityTest.php
@covers ::resizeDimensions @dataProvider resizeDimensionsProvider
ResizePercentageImageEffect::applyEffect in src/Plugin/ImageEffect/ResizePercentageImageEffect.php
Applies an image effect to the image object.
ResizePercentageImageEffect::transformDimensions in src/Plugin/ImageEffect/ResizePercentageImageEffect.php
Determines the dimensions of the styled image.
ScaleAndSmartCropImageEffect::applyEffect in src/Plugin/ImageEffect/ScaleAndSmartCropImageEffect.php
Applies an image effect to the image object.
ScaleAndSmartCropImageEffect::transformDimensions in src/Plugin/ImageEffect/ScaleAndSmartCropImageEffect.php
Determines the dimensions of the styled image.

... See full list

File

src/Component/ImageUtility.php, line 55

Class

ImageUtility
Image handling methods for image_effects.

Namespace

Drupal\image_effects\Component

Code

public static function resizeDimensions($source_width, $source_height, $width_specification, $height_specification, $square = FALSE) {
  $dimensions = [];
  $dimensions['width'] = static::percentFilter($width_specification, $source_width);
  $dimensions['height'] = static::percentFilter($height_specification, $source_height);
  if (is_null($dimensions['width']) && is_null($dimensions['height'])) {
    return $dimensions;
  }
  if (!$dimensions['width'] || !$dimensions['height']) {
    if (is_null($source_width) || is_null($source_height)) {
      $dimensions['width'] = NULL;
      $dimensions['height'] = NULL;
    }
    else {
      if ($square) {
        $aspect_ratio = 1;
      }
      else {
        $aspect_ratio = $source_height / $source_width;
      }
      if ($dimensions['width'] && !$dimensions['height']) {
        $dimensions['height'] = (int) round($dimensions['width'] * $aspect_ratio);
      }
      elseif (!$dimensions['width'] && $dimensions['height']) {
        $dimensions['width'] = (int) round($dimensions['height'] / $aspect_ratio);
      }
    }
  }
  return $dimensions;
}