You are here

function focal_point_effect_resize_data in Focal Point 7

Calculate how to resize the given image using the given heights and widths.

Calculate the resize dimensions of an image based on the longest crop dimension so that the aspect ratio is preserved and that there is always enough image available to the crop.

Parameters

int $image_width: The original width of the image.

int $image_height: The original height of the image.

int $crop_width: The width the image should be after cropping.

int $crop_height: The height the image should be after cropping.

Return value

array An array with the keys width and height representing the final crop size such that the aspect ratio is preserved.

2 calls to focal_point_effect_resize_data()
focalPointTest::testResizeData in test/FocalPointTest.php
Test Resize Data.
focal_point_scale_and_crop_effect in ./focal_point.effects.inc
Image effect callback.

File

./focal_point.effects.inc, line 176
Default image preset.

Code

function focal_point_effect_resize_data($image_width, $image_height, $crop_width, $crop_height) {
  $resize_data = array();
  if ($crop_width > $crop_height) {
    $resize_data['width'] = (int) $crop_width;
    $resize_data['height'] = (int) ($crop_width * $image_height / $image_width);

    // Ensure there is enough area to crop.
    if ($resize_data['height'] < $crop_height) {
      $resize_data['width'] = (int) ($crop_height * $resize_data['width'] / $resize_data['height']);
      $resize_data['height'] = (int) $crop_height;
    }
  }
  else {
    $resize_data['width'] = (int) ($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) ($crop_width * $resize_data['height'] / $resize_data['width']);
      $resize_data['width'] = (int) $crop_width;
    }
  }
  return $resize_data;
}