function imageapi_image_scale_and_crop in ImageAPI 6
Same name and namespace in other branches
- 5 imageapi.module \imageapi_image_scale_and_crop()
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 imageapi_image_open().
$width: The target width, in pixels.
$height: The target height, in pixels.
Return value
TRUE or FALSE, based on success.
File
- ./
imageapi.module, line 189 - An ImageAPI supporting additional image plugins as modules. Images are treated as objects, and images are not written per manipulation as Drupal's core image handling works.
Code
function imageapi_image_scale_and_crop(&$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 (imageapi_image_resize($image, $image->info['width'] * $scale, $image->info['height'] * $scale)) {
return imageapi_image_crop($image, $x, $y, $width, $height);
}
return FALSE;
}