You are here

function focal_point_effect_crop_data in Focal Point 7

Compile the necessary data for the image crop effect.

Parameters

object $image: The image object provided to an effect callback.

array $data: The data array provided to an effect callback.

Return value

array|bool An array containing the following keys:

  • width
  • height
  • anchor (in the form xoffset-yoffset)

If no image file can be located, FALSE is returned.

2 calls to focal_point_effect_crop_data()
focal_point_crop_effect in ./focal_point.effects.inc
Image effect callback.
focal_point_scale_and_crop_effect in ./focal_point.effects.inc
Image effect callback.

File

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

Code

function focal_point_effect_crop_data($image, $data) {
  $files = file_load_multiple(array(), array(
    'uri' => $image->source,
  ));
  if (count($files)) {
    $file = reset($files);
    $focal_point = $file->focal_point;

    // Special handling for preview images.
    $parameters = drupal_get_query_parameters();

    // Query parameters overwrite the stored focal point.
    if (!empty($parameters['focal_point'])) {
      $focal_point = focal_point_validate($parameters['focal_point']) ? $parameters['focal_point'] : $focal_point;
    }
    elseif (empty($focal_point)) {

      // If no stored focal point is available we use a detected one.
      $focal_point = _focal_point_guess_default($file->fid);
    }
    $focal_point = focal_point_parse($focal_point);
    $crop_data = array(
      'width' => (int) $data['width'],
      'height' => (int) $data['height'],
    );

    // Get the pixel location of the focal point for the current image taking
    // the image boundaries into account.
    $shift_x = isset($data['focal_point_advanced']) && isset($data['focal_point_advanced']['shift_x']) ? $data['focal_point_advanced']['shift_x'] : 0;
    $shift_y = isset($data['focal_point_advanced']) && isset($data['focal_point_advanced']['shift_y']) ? $data['focal_point_advanced']['shift_y'] : 0;
    $anchor_x = focal_point_effect_calculate_anchor($image->info['width'], $crop_data['width'], $focal_point['x-offset'], $shift_x);
    $anchor_y = focal_point_effect_calculate_anchor($image->info['height'], $crop_data['height'], $focal_point['y-offset'], $shift_y);
    $crop_data['anchor'] = $anchor_x . '-' . $anchor_y;
    return $crop_data;
  }
  return FALSE;
}