 includes/image.inc              |   25 +++++++++----------------
 modules/image/image.effects.inc |    6 ++----
 2 files changed, 11 insertions(+), 20 deletions(-)

diff --git a/includes/image.inc b/includes/image.inc
index 8dc36b9..39da084 100644
--- a/includes/image.inc
+++ b/includes/image.inc
@@ -203,30 +203,23 @@ 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.
+  if (($width && $height && $aspect < $height / $width) || ($width && !$height)) {
+    $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;
+    $width = (int) round($height / $aspect);
+  }
 
+  if (!$upscale) {
     // Don't scale up.
-    if (round($width) >= $dimensions['width'] && round($height) >= $dimensions['height']) {
+    if ($width >= $dimensions['width'] && $height >= $dimensions['height']) {
       return FALSE;
     }
   }
 
-  if ($aspect < $height / $width) {
-    $dimensions['width'] = $width;
-    $dimensions['height'] = (int) round($width * $aspect);
-  }
-  else {
-    $dimensions['width'] = (int) round($height / $aspect);
-    $dimensions['height'] = $height;
-  }
+  $dimensions['width'] = $width;
+  $dimensions['height'] = $height;
 
   return TRUE;
 }
diff --git a/modules/image/image.effects.inc b/modules/image/image.effects.inc
index ea898f9..35a6a74 100644
--- a/modules/image/image.effects.inc
+++ b/modules/image/image.effects.inc
@@ -124,13 +124,11 @@ function image_resize_dimensions(array &$dimensions, array $data) {
 function image_scale_effect(&$image, $data) {
   // Set sane default values.
   $data += array(
+    'width' => NULL,
+    'height' => NULL,
     'upscale' => FALSE,
   );
 
-  // Set impossibly large values if the width and height aren't set.
-  $data['width'] = empty($data['width']) ? PHP_INT_MAX : $data['width'];
-  $data['height'] = empty($data['height']) ? PHP_INT_MAX : $data['height'];
-
   if (!image_scale($image, $data['width'], $data['height'], $data['upscale'])) {
     watchdog('image', 'Image scale failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', array('%toolkit' => $image->toolkit, '%path' => $image->source, '%mimetype' => $image->info['mime_type'], '%dimensions' => $image->info['width'] . 'x' . $image->info['height']), WATCHDOG_ERROR);
     return FALSE;
