You are here

function focal_point_effect_calculate_anchor in Focal Point 7

Calculate the anchor offset for the given dimension.

Parameters

int $image_size: The dimension of the full-sized image.

int $crop_size: The dimension of the crop.

int $focal_point_offset: The corresponding focal point percentage value for the given dimension.

int $focal_point_shift: The pixel value for shifting the focal point for the given dimension.

Return value

int The anchor offset for the given dimension.

2 calls to focal_point_effect_calculate_anchor()
focalPointTest::testCalculateEffectAnchor in test/FocalPointTest.php
Test Calculate Effect Anchor.
focal_point_effect_crop_data in ./focal_point.effects.inc
Compile the necessary data for the image crop effect.

File

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

Code

function focal_point_effect_calculate_anchor($image_size, $crop_size, $focal_point_offset, $focal_point_shift = 0) {
  $focal_point_pixel = (int) $focal_point_offset * $image_size / 100;
  $focal_point_pixel -= (int) $focal_point_shift;

  // If the crop size is larger than the image size, use the image size to avoid
  // stretching. This will cause the excess space to be filled with black.
  $crop_size = min($image_size, $crop_size);

  // Define the anchor as half the crop width to the left.
  $offset = (int) ($focal_point_pixel - 0.5 * $crop_size);

  // Ensure the anchor doesn't fall off the left edge of the image.
  $offset = max($offset, 0);

  // Ensure the anchor doesn't fall off the right side of the image.
  if ($offset + $crop_size > $image_size) {
    $offset = $image_size - $crop_size;
  }
  return $offset;
}