--- \old\emimage.theme.inc	Mon Sep 15 04:07:59 2008
+++ \new\emimage.theme.inc	Thu Mar 19 14:15:06 2009
@@ -4,12 +4,45 @@
 function theme_emimage_image($field, $item, $formatter, $node, $code, $width, $height, $title = '', $link = NULL) {
   $url = module_invoke('emfield', 'include_invoke', 'emimage', $item['provider'], 'image_url', $code, $width, $height, $formatter, $field, $item, $node);
   $attributes = array();
-  if ($width) {
+  
+  // Set the image's width or height attribute for the browser, and let the browser scale 
+  // the image, preserving its original aspect ratio.
+  // There are three options allowed in the admin settings:
+  // 1. width == 0. Display image at specified height, 
+  //    with width scaled according to original aspect ratio.
+  // 2. height == 0. Display image at specified width, 
+  //    with height scaled according to original aspect ratio.
+  // 3. Both width and height are non-zero. Change it to display the image 
+  //    at the largest size that fits within the specified height & width, 
+  //    and preserves the original aspect ratio.
+
+  if ($width == 0) {
+    $attributes['height'] = $height;
+  } 
+  elseif ($height == 0) {
     $attributes['width'] = $width;
   }
-  if ($height) {
-    $attributes['height'] = $height;
+  else {
+    // Find out the size of the actual image file, and scale accordingly
+    if ( $item['data']['width'] && $item['data']['height'] ) {
+      // The image's width & height are known
+      $scale_width = $item['data']['width'] / $width ;
+      $scale_height = $item['data']['height'] / $height ;
+      if ($scale_width > $scale_height) {
+        $attributes['width'] = $width;
+      } 
+      else {
+        $attributes['height'] = $height;
+      }
+    }
+    else {
+      // We don't know the size of the image, so just make it fill the space available.
+      // It will probably be stretched in one direction, making it look odd.
+      $attributes['width'] = $width;
+      $attributes['height'] = $height;	  
+    }
   }
+
   if ($item['class']){
       $attributes['class'] = $item['class'];
   }
