Commit 20168b6a authored by Dries's avatar Dries

- Modified patch #142798 by Steven and sime: introduced a new image API...

- Modified patch #142798 by Steven and sime: introduced a new image API function: image_scale_and_crop()
parent b09fbd8e
......@@ -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;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment