Index: modules/image/image.module
===================================================================
RCS file: /cvs/drupal/contributions/modules/image/image.module,v
retrieving revision 1.197.2.6
diff -u -r1.197.2.6 image.module
--- modules/image/image.module	1 Mar 2007 04:05:48 -0000	1.197.2.6
+++ modules/image/image.module	21 Jul 2007 15:39:03 -0000
@@ -361,19 +361,81 @@
 
 /**
  * Create an <img> tag for an image.
+ *
+ * Image created will be no larger than the width and height specified for the label,
+ * and a reduced-size derivative will be produced as necessary
+ *
+ * @param &$node
+ *   Image node to display
+ * @param $label
+ *   Image size label: defaults to "preview"
+ * @param $attributes
+ *   Drupal attribute array, passed on to theme('image_display', ...).
+ *   The 'width' and 'height' attributes will be overwritted by the requested image size,
+ *   While the 'class' attribute will have "image ${label}" prepended to it.
+ * @return
+ *   Themed image
  */
 function image_display(&$node, $label = 'preview', $attributes = array()) {
-  // regenerate images?
+  // Do we have the filename for the image derivative for the requested size label?
+  if (empty($node->images[$label])) {
+    // Filename for image derivative is not known. 
+    // This means either (a) the original is already at the required size,
+    // so no derivative image is required, 
+    // or (b) the derivatives have not been generated yet.
+    
+    // To check whether it is the same size as the original, get the original info
+    $info = image_get_info(file_create_path($node->images['_original']));
+    if (!$info) {
+      // Failed to get size info about original image
+      return;
+    }
+    // Get the images size array for requested image size
+    $size = _image_get_dimensions($label);
+    if (empty($size)) {
+      // Failed to get info about required label size
+      return;
+    }
+    //assert($info && $size);
+    // Does the original image already fit into the requested label size?
+    if ($info['width'] <= $size['width'] && $info['height'] <= $size['height']) {
+      // Original is already small enough, so use the original size directly.
+      $node->images[$label] = $node->images['_original'];
+    }    
+    else {
+      // Original is larger than required size, so build the image derivatives
+      // (including the derivative we want)
+      // TODO: Only build the requested derivative
+      _image_build_derivatives($node);
+      // Check the derivative we wanted was built successfully
+      if (empty($node->images[$label])) {
+        // Failed to build requested derivative
+        return;
+      }
+    }
+  }
+  //assert($node->images[$label]);
+  // If we get this far, 
+  // the required image derivative filename in $node->images[$label] is valid
+
+  // If image filename is not the original image, and either 
+  // (a) derivative image file doesn't exist yet or 
+  // (b) image settings have changed since derivative image file was last updated,
+  // (i.e. if the derivative is out of date), then regenerate the image derivatives
   if ($node->images[$label] != $node->images['_original'] &&
       (!file_exists(file_create_path($node->images[$label])) ||
        filemtime(file_create_path($node->images[$label])) < variable_get('image_updated', 0))) {
     _image_build_derivatives($node);
   }
 
+  // Make sure we still have the required image derivative filename 
   if (empty($node->images[$label])) {
+    // Looks like we lost the derivative during _image_build_derivatives()
     return;
   }
+  // If we get here, $node->images[$label] is valid and the image (or derivative) file exists
 
+  // Theme the image, with the appropriate class, width and height
   $info = image_get_info(file_create_path($node->images[$label]));
   $attributes['class'] = "image $label". (isset($attributes['class']) ? " ". $attributes['class'] : "");
   $attributes['width'] = $info['width'];
