You are here

public static function FocalPointEffectBase::calculateResizeData in Focal Point 8

Calculate the resize dimensions of an image.

The calculated dimensions are based on the longest crop dimension (length or width) so that the aspect ratio is preserved in all cases and that there is always enough image available to the crop.

Parameters

int $image_width: Image width.

int $image_height: Image height.

int $crop_width: Crop width.

int $crop_height: Crop height.

Return value

array Resize data.

2 calls to FocalPointEffectBase::calculateResizeData()
FocalPointEffectsTest::testCalculateResizeData in tests/src/Unit/Effects/FocalPointEffectsTest.php
Test the resize calculation.
FocalPointScaleAndCropImageEffect::applyEffect in src/Plugin/ImageEffect/FocalPointScaleAndCropImageEffect.php
Applies an image effect to the image object.

File

src/FocalPointEffectBase.php, line 130

Class

FocalPointEffectBase
Provides a base class for image effects.

Namespace

Drupal\focal_point

Code

public static function calculateResizeData($image_width, $image_height, $crop_width, $crop_height) {
  $resize_data = [];
  if ($crop_width > $crop_height) {
    $resize_data['width'] = (int) $crop_width;
    $resize_data['height'] = (int) ceil($crop_width * $image_height / $image_width);

    // Ensure there is enough area to crop.
    if ($resize_data['height'] < $crop_height) {
      $resize_data['width'] = (int) ceil($crop_height * $resize_data['width'] / $resize_data['height']);
      $resize_data['height'] = (int) $crop_height;
    }
  }
  else {
    $resize_data['width'] = (int) ceil($crop_height * $image_width / $image_height);
    $resize_data['height'] = (int) $crop_height;

    // Ensure there is enough area to crop.
    if ($resize_data['width'] < $crop_width) {
      $resize_data['height'] = (int) ceil($crop_width * $resize_data['height'] / $resize_data['width']);
      $resize_data['width'] = (int) $crop_width;
    }
  }
  return $resize_data;
}