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.
object $image: An image object returned by image_load().
int $width: The target width, in pixels.
int $height: The target height, in pixels.
bool TRUE on success, FALSE on failure.
function 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 (image_resize($image, $image->info['width'] * $scale, $image->info['height'] * $scale)) {
return image_crop($image, $x, $y, $width, $height);
}
return FALSE;
}