From 9f742a2cb8a2f5193337c9a48ac274d0beaa047f Mon Sep 17 00:00:00 2001 From: webchick Date: Sat, 24 Dec 2011 18:00:17 -0800 Subject: Issue #1338428 by jbrown, BTMash, Tor Arne Thune: Fixed image_dimensions_scale() and image_scale_effect() are ungrokable and buggy. --- includes/image.inc | 42 ++++++++++++++++++------------------------ 1 file changed, 18 insertions(+), 24 deletions(-) (limited to 'includes') diff --git a/includes/image.inc b/includes/image.inc index 8dc36b995..f6ae7f19b 100644 --- a/includes/image.inc +++ b/includes/image.inc @@ -186,14 +186,14 @@ function image_scale_and_crop(stdClass $image, $width, $height) { * Dimensions to be modified - an array with components width and height, in * pixels. * @param $width - * The target width, in pixels. This value is omitted then the scaling will + * The target width, in pixels. If this value is NULL then the scaling will be * based only on the height value. * @param $height - * The target height, in pixels. This value is omitted then the scaling will - * based only on the width value. + * The target height, in pixels. If this value is NULL then the scaling will + * be based only on the width value. * @param $upscale - * Boolean indicating that files smaller than the dimensions will be scaled - * up. This generally results in a low quality image. + * Boolean indicating that images smaller than the target dimensions will be + * scaled up. This generally results in a low quality image. * * @return * TRUE if $dimensions was modified, FALSE otherwise. @@ -203,31 +203,25 @@ function image_scale_and_crop(stdClass $image, $width, $height) { function image_dimensions_scale(array &$dimensions, $width = NULL, $height = NULL, $upscale = FALSE) { $aspect = $dimensions['height'] / $dimensions['width']; - if ($upscale) { - // Set width/height according to aspect ratio if either is empty. - $width = !empty($width) ? $width : $height / $aspect; - $height = !empty($height) ? $height : $width / $aspect; + // Calculate one of the dimensions from the other target dimension, + // ensuring the same aspect ratio as the source dimensions. If one of the + // target dimensions is missing, that is the one that is calculated. If both + // are specified then the dimension calculated is the one that would not be + // calculated to be bigger than its target. + if (($width && !$height) || ($width && $height && $aspect < $height / $width)) { + $height = (int) round($width * $aspect); } else { - // Set impossibly large values if the width and height aren't set. - $width = !empty($width) ? $width : 9999999; - $height = !empty($height) ? $height : 9999999; - - // Don't scale up. - if (round($width) >= $dimensions['width'] && round($height) >= $dimensions['height']) { - return FALSE; - } + $width = (int) round($height / $aspect); } - if ($aspect < $height / $width) { - $dimensions['width'] = $width; - $dimensions['height'] = (int) round($width * $aspect); - } - else { - $dimensions['width'] = (int) round($height / $aspect); - $dimensions['height'] = $height; + // Don't upscale if the option isn't enabled. + if (!$upscale && ($width >= $dimensions['width'] || $height >= $dimensions['height'])) { + return FALSE; } + $dimensions['width'] = $width; + $dimensions['height'] = $height; return TRUE; } -- cgit v1.2.3