Index: img_assist.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/img_assist/Attic/img_assist.module,v
retrieving revision 1.68.2.14
diff -u -r1.68.2.14 img_assist.module
--- img_assist.module	17 Jun 2007 22:07:47 -0000	1.68.2.14
+++ img_assist.module	6 Jul 2007 04:05:13 -0000
@@ -255,28 +255,24 @@
     // get maximum size allowed inline
     $max_size = explode('x', variable_get('img_assist_max_size', '640x640'));
     $oversize_count = 0;
-    foreach (_image_get_sizes() as $size) {
-      if ($size['label']) {
-        $dimensions = $size['width'] .'x'. $size['height'];
-        if (($size['width'] <= $max_size[0]) && ($size['height'] <= $max_size[1])) {
-          $derivatives[$dimensions] = $size['label'];
-        }
-        elseif ($size['label'] == 'thumbnail') { // the thumbnail option will be shown even if it is larger than the maximum size
-          $derivatives[$dimensions] = $size['label'];
-        }
-        else {
-          $oversize_count++;
-        }
-        $allsizes[$size['label']] = $size['label'];
+    foreach (_image_get_sizes() as $key => $size) {
+      $dimensions = $size['width'] .'x'. $size['height'];
+      if (($size['width'] <= $max_size[0]) && ($size['height'] <= $max_size[1])) {
+        $derivatives[$dimensions] = $size['label'];
+      }
+      elseif ($key == IMAGE_THUMBNAIL) { // the thumbnail option will be shown even if it is larger than the maximum size
+        $derivatives[$dimensions] = $size['label'];
+      }
+      else {
+        $oversize_count++;
       }
+      $allsizes[$key] = $size['label'];
     }
 
-    $allsizes["_original"] = "original";
-
     $form['image']['img_assist_popup_label'] = array(
       '#type' => 'select',
       '#title' => t('Popup size'),
-      '#default_value' => variable_get('img_assist_popup_label', 'preview'),
+      '#default_value' => variable_get('img_assist_popup_label', IMAGE_PREVIEW),
       '#options' => $allsizes,
       '#description' => t('Select the size of the image that is popped up.'),
     );
@@ -677,7 +673,7 @@
     $result = pager_query($query, $show_amount, $element = 0, $count_query = NULL, $params);
     while ($row = db_fetch_object($result)) {
       $node = node_load(array('nid' => $row->nid));
-      $image = img_assist_display($node, 'thumbnail');
+      $image = img_assist_display($node, IMAGE_THUMBNAIL);
       $output .= l($image, "img_assist/properties/{$node->nid}", $attributes = array(), $query = NULL, $fragment = NULL, $absolute = FALSE, $html = TRUE) ."\n";
     }
     if (!db_num_rows($result)) {
@@ -734,20 +730,21 @@
  * Construct the image properties form
  */
 function img_assist_properties_form($node, $update) {
-  $image_info = image_get_info(file_create_path($node->images['_original']));
+  $image_info = image_get_info(file_create_path($node->images[IMAGE_ORIGINAL]));
 
   // Select (or generate) a preview image
   $img_assist_create_derivatives = variable_get('img_assist_create_derivatives', array());
   if ($img_assist_create_derivatives['properties']) {
-    $properties_size['label'] = 'img_assist_properties';
+    $properties_size['label'] = t('Properties');
+    $properties_size['name'] = 'img_assist_properties';
     $properties_size['width'] = 200;
     $properties_size['height'] = 200;
   }
   else {
-    $properties_size['label'] = 'thumbnail';
+    $properties_size['name'] = IMAGE_THUMBNAIL;
   }
   $properties_image = img_assist_display($node, $properties_size);
-  $properties_size = image_get_info(file_create_path($node->images[$properties_size['label']])); // get the actual size now
+  $properties_size = image_get_info(file_create_path($node->images[$properties_size['name']])); // get the actual size now
 
   // Create an array of image derivative choices
   //
@@ -767,31 +764,33 @@
   // images don't have to snap to the nearest standard size.  You can create any
   // arbitrary size you choose.
   $max_size = explode('x', variable_get('img_assist_max_size', '640x640'));
-  $default_label = variable_get('img_assist_default_label', 'thumbnail');
-  foreach (_image_get_sizes() as $size) {
-    if ($size['label']) {
-      if (($size['width'] <= $max_size[0]) && ($size['height'] <= $max_size[1])) {
+  //$default_label = variable_get('img_assist_default_label', IMAGE_THUMBNAIL);
+  
+  foreach (_image_get_sizes() as $key => $size) {
+    if ($key == IMAGE_ORIGINAL) {
+      if (user_access('use original size') && $image_info['width'] <= $max_size[0] && $image_info['height'] <= $max_size[1]) {
+        $derivatives[$image_info['width']  .'x'. $image_info['height']] = $size['label'];
+        $added_to_derivatives = TRUE;
+      }
+    }
+    elseif ($size['width'] <= $max_size[0] && $size['height'] <= $max_size[1]) {
+      $derivatives[$size['width'] .'x'. $size['height']] = $size['label'];
+      $added_to_derivatives = TRUE;
+    }
+    
+    if ($added_to_derivatives != TRUE) {
+      if ($key == IMAGE_THUMBNAIL) { // the thumbnail option will be shown even if it is larger than the maximum size
         $derivatives[$size['width'] .'x'. $size['height']] = $size['label'];
       }
-      elseif ($size['label'] == 'thumbnail') { // the thumbnail option will be shown even if it is larger than the maximum size
-        $derivatives[$size['width'] .'x'. $size['height']] = $size['label'];
-      }
-      elseif ($size == $default_label) { // the default_label may be set to a size that is too large
-        $default_label == 'thumbnail'; // change the default label to thumbnail
-      }
-    }
-  }
-
-  // Add a choice for 'original' if it's within the maximum size and user has proper permission
-  if (user_access('use original size')) {
-    if ($image_info['width'] <= $max_size[0] && $image_info['height'] <= $max_size[1]) {
-      $derivatives[$image_info['width']  .'x'. $image_info['height']] = t('original');
+      //elseif ($key == $default_label) { // the default_label may be set to a size that is too large
+      //  $default_label == IMAGE_THUMBNAIL; // change the default label to thumbnail
+      //}
     }
   }
 
   // Add a choice for 'other' if the user has the proper permission to create custom sizes
   if ($img_assist_create_derivatives['custom_advanced'] && user_access('access advanced options')) {
-    $derivatives['other'] = t('other');
+    $derivatives['other'] = t('Other');
   }
 
   // Get the default pixel size from the default label size
@@ -800,7 +799,7 @@
   // we are getting the 'preview' size instead of the default size (as in the commented
   // out line above), because of an new derivative size has been setup but the
   // image hasn't been generated this will cause an error here.
-  $default_size = image_get_info(file_create_path($node->images['preview']));
+  $default_size = image_get_info(file_create_path($node->images[IMAGE_PREVIEW]));
   $default_width = $default_size['width'];
   $default_height = $default_size['height'];
 
@@ -1045,17 +1044,17 @@
  *   ~ The size attribute can be a custom size OR a standard size
  */
 function img_assist_display(&$node, $size = NULL, $attributes = array()) {
-  if (is_array($size) && ($size['label']) && ($size['width']) && ($size['height'])) { // custom size: should include values for label, width, and height
-    $label = $size['label'];
+  if (is_array($size) && ($size['name']) && ($size['width']) && ($size['height'])) { // custom size: should include values for label, width, and height
+    $label = $size['name'];
   }
   elseif ($size) { // standard size
     if (is_array($size)) { // size can be an array without the width and/or height
-      $size = $size['label']; // size is no longer an array
+      $size = $size['name']; // size is no longer an array
     }
     $label = $size;
   }
   else { // no size specified: assign preview size
-    $label = 'thumbnail';
+    $label = IMAGE_THUMBNAIL;
   }
 
   // regenerate images?
@@ -1078,7 +1077,7 @@
   }
 
   if ($regen) {
-    _img_assist_build_derivatives($node, false, $size);
+    _img_assist_build_derivatives($node, $size);
   }
 
   if (empty($node->images[$label])) {
@@ -1097,51 +1096,45 @@
  * Generate one image derivative (closely based on _image_build_derivatives() in the image.module)
  * $size is an array, including: $size['label'], $size['width'], $size['height']
  */
-function _img_assist_build_derivatives(&$node, $temp = FALSE, $size = NULL) {
+function _img_assist_build_derivatives(&$node, $size = NULL) {
   // sanity check:
   if (!_image_check_settings()) {
     return false;
   }
-  $info = image_get_info(file_create_path($node->images['_original']));
+  $info = image_get_info(file_create_path($node->images[IMAGE_ORIGINAL]));
 
   if ($size['label'] && $size['width'] && $size['height']) { // custom size
-      $sizes[] = $size;
+      $sizes[$size['name']] = $size;
   }
   elseif ($size) { // standard size
     if (is_array($size)) { // size can be an array without the width and/or height
-      $size = $size['label'];
-    }
-    foreach (_image_get_sizes() as $stdsize) {
-      if ($stdsize['label'] == $size) {
-        $sizes[] = $stdsize;
-      }
+      $size = $size['name'];
     }
+    $sizes = _image_get_sizes();
+    $sizes[$size] = $sizes[$size];
   }
   else { // no size given: rebuild derivatives for all standard sizes
     $sizes = _image_get_sizes();
   }
 
-  foreach ($sizes as $size) {
-    if (!$temp) {
-      _img_assist_remove($node, $size);
-    }
+  foreach ($sizes as $key => $size) {
+    $size['name'] = $key;
+    _img_assist_remove($node, $size);
 
     if (is_array($size) && ($size['label']) && ($size['width']) && ($size['height'])) {
       if ($info['width'] > $size['width'] || $info['height'] > $size['height']) {
-        $source = file_create_path($node->images['_original']);
-        $destination = _image_filename(basename($source), $size['label'], $temp);
+        $source = file_create_path($node->images[IMAGE_ORIGINAL]);
+        $destination = _image_filename(basename($source), $key, FALSE);
         if (!image_scale($source, file_create_path($destination), $size['width'], $size['height'])) {
           drupal_set_message(t('Unable to create %label image', array('%label' => $size['label'])), 'error');
         }
         else {
-          $node->images[$size['label']] = $destination;
-          if (!$temp) {
-            _image_insert($node, $size['label'], file_create_path($destination));
-          }
+          $node->images[$key] = $destination;
+          _image_insert($node, $key, file_create_path($destination));
         }
       }
       else {
-        $node->images[$size['label']] = $node->images['_original'];
+        $node->images[$key] = $node->images[IMAGE_ORIGINAL];
       }
     }
   }
@@ -1149,14 +1142,14 @@
 
 
 function _img_assist_remove($node, $size) {
-  $result = db_query("SELECT * FROM {files} WHERE nid=%d AND filename='%s'", $node->nid, $size['label']);
+  $result = db_query("SELECT * FROM {files} WHERE nid=%d AND filename='%s'", $node->nid, $size['name']);
   while ($file = db_fetch_object($result)) {
     // Never delete the original!
-    if ($file->filepath != $node->images['_original']) {
+    if ($file->filepath != $node->images[IMAGE_ORIGINAL]) {
       file_delete(file_create_path($file->filepath));
     }
   }
-  db_query("DELETE FROM {files} WHERE nid=%d AND filename='%s'", $node->nid, $size['label']);
+  db_query("DELETE FROM {files} WHERE nid=%d AND filename='%s'", $node->nid, $size['name']);
 }
 
 /**
@@ -1179,15 +1172,15 @@
       $height = ($height <= $max_size[1]) ? $height : $max_size[1];
 
       // Get the width & height of the 'original' size
-      $original_size = image_get_info(file_create_path($node->images['_original']));
+      $original_size = image_get_info(file_create_path($node->images[IMAGE_ORIGINAL]));
       if ($width == $original_size['width'] && $height == $original_size['height']) {
         // Nothing to process, this is the original image size
-        $closest_std_size = '_original';
+        $closest_std_size = IMAGE_ORIGINAL;
         $create_custom = false;
       }
       else {
         // Get the width & height of the 'preview' size
-        $preview_size = image_get_info(file_create_path($node->images['preview']));
+        $preview_size = image_get_info(file_create_path($node->images[IMAGE_PREVIEW]));
         $preview_width = $preview_size['width'];
         $preview_height = $preview_size['height'];
 
@@ -1207,24 +1200,22 @@
           $diag_size_new = sqrt(pow($width, 2) + pow($height, 2));
           $closest_difference = 9999;
 
-          foreach (_image_get_sizes() as $stdsize) {
-            if ($stdsize['label']) {
-              $width_std = $stdsize['width'];
-              $height_std = $stdsize['height'];
-              // Get standard width & height, taking into account the aspect ratio
-              if (round($width_std / $aspect_ratio) <= $height_std) { // width is controlling factor
-                $height_std = round($width_std / $aspect_ratio);
-              }
-              else { // height is controlling factor
-                $width_std = round($height_std * $aspect_ratio);
-              }
-              // Get the diagonal size of this standard image
-              $diag_size_std = sqrt(pow($width_std, 2) + pow($height_std, 2));
-              $difference = abs($diag_size_new - $diag_size_std);
-              if ($difference < $closest_difference) {
-                $closest_std_size = $stdsize['label'];
-                $closest_difference = $difference;
-              }
+          foreach (_image_get_sizes() as $key => $stdsize) {
+            $width_std = $stdsize['width'];
+            $height_std = $stdsize['height'];
+            // Get standard width & height, taking into account the aspect ratio
+            if (round($width_std / $aspect_ratio) <= $height_std) { // width is controlling factor
+              $height_std = round($width_std / $aspect_ratio);
+            }
+            else { // height is controlling factor
+              $width_std = round($height_std * $aspect_ratio);
+            }
+            // Get the diagonal size of this standard image
+            $diag_size_std = sqrt(pow($width_std, 2) + pow($height_std, 2));
+            $difference = abs($diag_size_new - $diag_size_std);
+            if ($difference < $closest_difference) {
+              $closest_std_size = $key;
+              $closest_difference = $difference;
             }
           }
 
@@ -1269,20 +1260,19 @@
         // otherwise you can't really have the same image inline in more than one node at different custom sizes
         $page_nid = is_numeric(arg(2)) ? '_'. arg(2) : '';
 
-        $size['label'] = 'img_assist_custom';
+        $size['label'] = t('Custom');
+        $size['name'] = 'img_assist_custom';
         $size['width'] = $width;
         $size['height'] = $height;
       }
       else {
-        $size['label'] = $closest_std_size;
+        $size['name'] = $closest_std_size;
       }
     }
     else { // default to thumbnail if the width and/or height is missing
-      foreach (_image_get_sizes() as $stdsize) {
-        if ($stdsize['label'] == 'thumbnail') {
-          $size = $stdsize;
-        }
-      }
+      $size = _image_get_sizes();
+      $size = $size[IMAGE_THUMBNAIL];
+      $size['name'] = IMAGE_THUMBNAIL;
     }
     return theme('img_assist_inline', $node, $size, $attributes);//$caption, $vars['align']);
   }
@@ -1330,7 +1320,7 @@
 
   }
   elseif ($link[0] == 'popup') {
-    $popup_size = variable_get('img_assist_popup_label', 'preview');
+    $popup_size = variable_get('img_assist_popup_label', IMAGE_PREVIEW);
     $info = image_get_info(file_create_path($node->images[$popup_size]));
     $width = $info['width'];
     $height = $info['height'];
@@ -1345,7 +1335,7 @@
   }
 
   if ($caption) {
-    $info = image_get_info(file_create_path($node->images[$size['label']]));
+    $info = image_get_info(file_create_path($node->images[$size['name']]));
     $width = $info['width'] - 2; // reduce the caption width slightly so the variable width of the text doesn't ever exceed the image width
     $output .= "<span class=\"caption\" style=\"width: {$width}px;\">$caption</span>";
   }
@@ -1357,8 +1347,8 @@
   $nid = arg(2);
   $node = node_load(array('nid' => $nid));
   drupal_set_title($node->title);
-  $size = variable_get('img_assist_popup_label', 'preview');
-  $size = array('label' => $size);
+  $size = variable_get('img_assist_popup_label', IMAGE_PREVIEW);
+  $size = array('name' => $size);
   $content = img_assist_display($node, $size);
   $attributes = array('id' => 'img_assist_popup');
 
@@ -1633,8 +1623,8 @@
         $dim = getimagesize($node->filepath, $info);
         $node->width  = $dim[0];
         $node->height = $dim[1];
-        $image[$node->nid]['thumbnail'] = $node;
-        $image[$node->nid]['_original']  = $node;
+        $image[$node->nid][IMAGE_THUMBNAIL] = $node;
+        $image[$node->nid][IMAGE_ORIGINAL]  = $node;
 
         if ($tid) {
           $tid2 = array();
@@ -1642,8 +1632,8 @@
             $tid2[] = $term->tid;
           }
           if (array_intersect($tid, $tid2) == $tid) {
-            $img[$node->nid]['thumbnail'] = $node;
-            $img[$node->nid]['_original'] = $node;
+            $img[$node->nid][IMAGE_THUMBNAIL] = $node;
+            $img[$node->nid][IMAGE_ORIGINAL] = $node;
           }
         }
       } // End while
@@ -1691,8 +1681,8 @@
       $dim = getimagesize($node->filepath, $info);
       $node->width  = $dim[0];
       $node->height = $dim[1];
-      $image['thumbnail'] = $node;
-      $image['_original'] = $node;
+      $image[IMAGE_THUMBNAIL] = $node;
+      $image[IMAGE_ORIGINAL] = $node;
     }
   }
   return $image;
@@ -1707,7 +1697,7 @@
 function _img_assist_get_thumbnail(&$image) {
   static $thumbs = array();
   if ($thumbs[$image->nid] === NULL) {
-    $thumbpath = file_create_path(db_result(db_query("SELECT filepath FROM {files} WHERE nid = %d AND filename = 'thumbnail'", $image->nid)));
+    $thumbpath = file_create_path(db_result(db_query("SELECT filepath FROM {files} WHERE nid = %d AND filename = '%s'", $image->nid, IMAGE_THUMBNAIL)));
     // In the really old version of image.module thumbnail names were 'thumb_filename.ext'.
     if (!file_exists($thumbpath)) {
       $pos = strrpos($image->filepath, '/') + 1;
