You are here

function image_max_size_crop_effect in Image max size crop 7

Image effect callback; Resize an image resource.

Parameters

object $image: An image object returned by image_load().

array $data: An array of attributes to use when performing the resize effect with the following items:

  • "width": An integer representing the desired width in pixels.
  • "height": An integer representing the desired height in pixels.
  • "anchor": A string describing where the crop should originate in the form of "XOFFSET-YOFFSET". XOFFSET is either a number of pixels or "left", "center", "right" and YOFFSET is either a number of pixels or "top", "center", "bottom".

Return value

bool TRUE on success. FALSE on failure to resize image.

1 string reference to 'image_max_size_crop_effect'
image_max_size_crop_image_effect_info in ./image_max_size_crop.module
Implements hook_image_effect_info().

File

./image_max_size_crop.module, line 75
Functions needed to create an image style.

Code

function image_max_size_crop_effect(&$image, array $data) {

  // Set sane default values.
  $data += array(
    'width' => NULL,
    'height' => NULL,
    'anchor' => 'center-center',
  );
  $dimensions = $image->info;

  // Crop the dimensions - if they don't change then just return success.
  if (!image_dimensions_max_size_crop($dimensions, $data['width'], $data['height'])) {
    return TRUE;
  }
  list($x, $y) = explode('-', $data['anchor']);
  $x = image_filter_keyword($x, $image->info['width'], $dimensions['width']);
  $y = image_filter_keyword($y, $image->info['height'], $dimensions['height']);
  if (!image_toolkit_invoke('crop', $image, array(
    $x,
    $y,
    (int) $dimensions['width'],
    (int) $dimensions['height'],
  ))) {
    watchdog('image', 'Image crop failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', array(
      '%toolkit' => $image->toolkit,
      '%path' => $image->source,
      '%mimetype' => $image->info['mime_type'],
      '%dimensions' => $image->info['width'] . 'x' . $image->info['height'],
    ), WATCHDOG_ERROR);
    return FALSE;
  }
  return TRUE;
}