function image_scale_and_crop

7.x image.inc image_scale_and_crop(stdClass $image, $width, $height)
6.x image.inc image_scale_and_crop($source, $destination, $width, $height)

Scales an image to the exact width and height given.

This function achieves the target aspect ratio by cropping the original image equally on both sides, or equally on the top and bottom. This function is useful to create uniform sized avatars from larger images.

The resulting image always has the exact target dimensions.

Parameters

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

$width: The target width, in pixels.

$height: The target height, in pixels.

Return value

TRUE on success, FALSE on failure.

See also

image_load()

image_resize()

image_crop()

Related topics

2 calls to image_scale_and_crop()
ImageToolkitUnitTest::testScaleAndCrop in drupal-7.x/modules/simpletest/tests/image.test
Test the image_scale_and_crop() function.
image_scale_and_crop_effect in drupal-7.x/modules/image/image.effects.inc
Image effect callback; Scale and crop an image resource.
2 string references to 'image_scale_and_crop'
ImageAdminStylesUnitTest::testStyle in drupal-7.x/modules/image/image.test
General test to add a style, add/remove/edit effects to it, then delete it.
image_image_effect_info in drupal-7.x/modules/image/image.effects.inc
Implements hook_image_effect_info().

File

drupal-7.x/includes/image.inc, line 169
API for manipulating images.

Code

function image_scale_and_crop(stdClass $image, $width, $height) {
  $scale = max($width / $image->info['width'], $height / $image->info['height']);
  $x = ($image->info['width'] * $scale - $width) / 2;
  $y = ($image->info['height'] * $scale - $height) / 2;

  if (image_resize($image, $image->info['width'] * $scale, $image->info['height'] * $scale)) {
    return image_crop($image, $x, $y, $width, $height);
  }
  return FALSE;
}