From 20168b6af478e68224bd8f4d5cd9d002d57d5195 Mon Sep 17 00:00:00 2001 From: Dries Buytaert Date: Fri, 11 May 2007 11:45:11 +0000 Subject: - Modified patch #142798 by Steven and sime: introduced a new image API function: image_scale_and_crop() --- includes/image.inc | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/includes/image.inc b/includes/image.inc index 6f20c082d..8fcb5adae 100644 --- a/includes/image.inc +++ b/includes/image.inc @@ -102,11 +102,41 @@ function image_get_info($file) { return $details; } +/** + * Scales an image to the exact width and height given. Maintains the + * original file's aspect ratio by cropping the image equally on both + * sides, or equally on the top and bottom. This function is, for + * example, useful to create uniform sized avatars from larger images. + * + * The resulting image always has the exact target dimensions. + * + * @param $source The file path of the source image + * @param $destination The file path of the destination image + * @param $width The target width + * @param $height The target height + * + * @return TRUE or FALSE, based on success + */ +function image_scale_and_crop($source, $destination, $width, $height) { + $info = image_get_info($source); + + $scale = max($width / $info['width'], $height / $info['height']); + $x = round(($info['width'] * $scale - $width) / 2); + $y = round(($info['height'] * $scale - $height) / 2); + + if (image_toolkit_invoke('resize', array($source, $destination, $info['width'] * $scale, $info['height'] * $scale))) { + return image_toolkit_invoke('crop', array($destination, $destination, $x, $y, $width, $height)); + } + return FALSE; +} + /** * Scales an image to the given width and height while maintaining aspect * ratio. * - * @param $source The filepath of the source image + * The resulting image can be smaller for one or both target dimensions. + * + * @param $source The file path of the source image * @param $destination The file path of the destination image * @param $width The target width * @param $height The target height @@ -116,7 +146,7 @@ function image_get_info($file) { function image_scale($source, $destination, $width, $height) { $info = image_get_info($source); - // don't scale up + // Don't scale up. if ($width >= $info['width'] && $height >= $info['height']) { return FALSE; } -- cgit v1.2.3