Index: image.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/image/image.install,v
retrieving revision 1.13
diff -u -p -u -p -r1.13 image.install
--- image.install	27 Aug 2007 22:50:39 -0000	1.13
+++ image.install	20 Sep 2007 21:39:07 -0000
@@ -15,6 +15,21 @@ function image_install() {
           PRIMARY KEY (`nid`, `image_size`),
           INDEX image_fid(`fid`)
         ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
+      db_query("CREATE TABLE {image_size} (
+          `id` varchar(32) NOT NULL,
+          `label` varchar(32) NOT NULL,
+          `height` INTEGER unsigned NOT NULL default '0',
+          `width` INTEGER unsigned NOT NULL default '0',
+          `operation` varchar(32) NOT NULL,
+          `link` INTEGER NOT NULL default '1',
+          `updated` INTEGER unsigned NOT NULL,
+          `weight` INTEGER unsigned NOT NULL default '0',
+          PRIMARY KEY  (`id`)
+        ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
+      db_query("INSERT INTO {image_size} (key, label, height, width, operation, link, updated, weight) VALUES ('%s', '%s', %d, %d, '%s', %d, %d, %d)", IMAGE_ORIGINAL, t('Original'), 0, 0, 'scale', IMAGE_LINK_SHOWN, time(), -1);
+      db_query("INSERT INTO {image_size} (key, label, height, width, operation, link, updated, weight) VALUES ('%s', '%s', %d, %d, '%s', %d, %d, %d)", IMAGE_THUMBNAIL, t('Thumbnail'), 100, 100, 'scale', IMAGE_LINK_SHOWN, time(), -1);
+      db_query("INSERT INTO {image_size} (key, label, height, width, operation, link, updated, weight) VALUES ('%s', '%s', %d, %d, '%s', %d, %d, %d)", IMAGE_PREVIEW, t('Preview'), 640, 640, 'scale', IMAGE_LINK_SHOWN, time(), -1);
+
       break;
   }
 }
@@ -26,9 +41,7 @@ function image_uninstall() {
   db_query('DROP TABLE {image}');
 
   variable_del('image_max_upload_size');
-  variable_del('image_updated');
   variable_del('image_default_path');
-  variable_del('image_sizes');
 }
 
 /**
@@ -217,3 +230,46 @@ function image_update_5201() {
   
   return array();
 }
+
+/**
+ * Create a table for the image sizes.
+ */
+function image_update_5202() {
+  $ret = array();
+
+  switch ($GLOBALS['db_type']) {
+    case 'mysql':
+    case 'mysqli':
+      // Create the table.
+      $ret[] = update_sql("CREATE TABLE {image_size} (
+          `id` varchar(32) NOT NULL,
+          `label` varchar(32) NOT NULL,
+          `height` INTEGER unsigned NOT NULL default '0',
+          `width` INTEGER unsigned NOT NULL default '0',
+          `operation` varchar(32) NOT NULL,
+          `link` INTEGER NOT NULL default '1',
+          `updated` INTEGER unsigned NOT NULL,
+          PRIMARY KEY  (`id`)
+        ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
+
+      // Migrate the settings.
+      $updated = variable_get('image_updated', time());
+      $defaults = array(
+        IMAGE_ORIGINAL => array('width' => '', 'height' => '', 'label' => t('Original'), 'operation' => 'scale', 'link' => IMAGE_LINK_SHOWN),
+        IMAGE_THUMBNAIL => array('width' => 100, 'height' => 100, 'label' => t('Thumbnail'), 'operation' => 'scale', 'link' => IMAGE_LINK_SHOWN),
+        IMAGE_PREVIEW   => array('width' => 640, 'height' => 640, 'label' => t('Preview'), 'operation' => 'scale', 'link' => IMAGE_LINK_SHOWN),
+      );
+      foreach (variable_get('image_sizes', $defaults) as $key => $size) {
+        if (!empty($size['label'])) {
+          $sql = "INSERT INTO {image_size} (id, label, height, width, operation, link, updated, weight) VALUES ('%s', '%s', %d, %d, '%s', %d, %d, -1)";
+          $result = db_query($sql, $key, $size['label'], $size['height'], $size['width'], $size['operation'], $size['link'], $updated);
+          $ret[] = array('success' => $result !== FALSE, 'query' => check_plain($sql));          
+        }
+      }
+      variable_del('image_updated');
+      variable_del('image_sizes');
+      break;
+  }
+
+  return $ret;
+}
\ No newline at end of file
Index: image.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/image/image.module,v
retrieving revision 1.257
diff -u -p -u -p -r1.257 image.module
--- image.module	4 Sep 2007 23:37:45 -0000	1.257
+++ image.module	20 Sep 2007 21:41:16 -0000
@@ -124,7 +124,7 @@ function image_admin_settings() {
   $sizes = image_get_sizes();
   // Add some empty rows for user defined sizes.
   for ($i = count($sizes); $i < 6; $i++) {
-    $sizes['new'. $i] = array(
+    $sizes['_new'. $i] = array(
       'label' => '',
       'operation' => 'scale',
       'width' => '',
@@ -152,13 +152,13 @@ function image_admin_settings() {
     );
     $form['image_sizes'][$key]['width'] = array(
       '#type' => 'textfield',
-      '#default_value' => $size['width'],
+      '#default_value' => empty($size['width']) ? '' : $size['width'],
       '#size' => 5,
       '#maxlength' => 5,
     );
     $form['image_sizes'][$key]['height'] = array(
       '#type' => 'textfield',
-      '#default_value' => $size['height'],
+      '#default_value' => empty($size['height']) ? '' : $size['height'],
       '#size' => 5,
       '#maxlength' => 5,
     );
@@ -189,53 +189,10 @@ function image_settings_sizes_validate(&
 /**
  * Make changes to the settings before passing them off to
  * system_settings_form_submit().
- *
- * Remove deleted sizes, and use the label as indexes for new sizes.
  */
 function image_settings_sizes_submit($form_id, &$form_values) {
-  $old_sizes = image_get_sizes();
-
-  // If the size's operation, or dimensions change we need to rebuild.
-  $rebuild = FALSE;
-
-  foreach ($form_values['image_sizes'] as $key => $size) {
-    // Changed to the original setting only affect new images and they
-    // shouldn't be able to add or remove it.
-    if ($key == IMAGE_ORIGINAL) {
-      continue;
-    }
-
-    // Remove sizes without labels.
-    if (empty($size['label'])) {
-      unset($form_values['image_sizes'][$key]);
-    }
-
-    // Check if only one is set, indicating an addition or removal.
-    if (isset($form_values['image_sizes'][$key]) ^ isset($old_sizes[$key])) {
-      $rebuild |= TRUE;
-
-      // When adding a new size, we need to assign a key.
-      if (isset($form_values['image_sizes'][$key])) {
-        unset($form_values['image_sizes'][$key]);
-        $new_key = drupal_strtolower(drupal_substr($size['label'], 0, 32));
-        $form_values['image_sizes'][$new_key] = $size;
-      }
-    }
-    // Check for changes.
-    else if (isset($form_values['image_sizes'][$key]) && isset($old_sizes[$key])) {
-      // Did the operation, height or width change?
-      foreach (array('operation', 'height', 'width') as $field) {
-        $rebuild |= ($form_values['image_sizes'][$key][$field] != $old_sizes[$key][$field]);
-      }
-    }
-  }
-
-  // If we've changed anything update the image_update variable so the
-  // derivative images are rebuilt.
-  if ($rebuild) {
-    drupal_set_message(t('Changes to the images sizes mean that the derivative images will need to be regenerated.'));
-    $form_values['image_updated'] = time();
-  }
+  image_save_sizes($form_values['image_sizes']);
+  unset($form_values['image_sizes']);
 
   return system_settings_form_submit($form_id, $form_values);
 }
@@ -258,6 +215,59 @@ function theme_image_settings_sizes_form
 }
 
 /**
+ * Remove deleted sizes, and use the label as indexes for new sizes.
+ *
+ */
+function image_save_sizes($sizes) {
+  $old_sizes = image_get_sizes();
+
+  foreach ($sizes as $key => $size) {
+    // Changes to the original setting only affect new images and they
+    // shouldn't be able to add or remove it.
+    if ($key == IMAGE_ORIGINAL) {
+      continue;
+    }
+
+    // Deletions.
+    if (empty($size['label'])) {
+      if (isset($old_sizes[$key])) {
+        // Locate and remove all the derivative images.
+        $result = db_query("SELECT f.fid, f.filepath FROM {image} i INNER JOIN {files} f ON i.fid = f.fid WHERE i.image_size = '%s'", $key);
+        while ($file = db_fetch_object($result)) {
+          file_delete(file_create_path($file->filepath));
+          db_query("DELETE FROM {files} WHERE fid = %d", $file->fid);
+        }
+        db_query("DELETE FROM {image} WHERE image_size = '%s'", $key);
+
+        // Then remove the size record.
+        db_query("DELETE FROM {image_size} WHERE id = '%s'", $key);
+        unset($sizes[$key]);
+        drupal_set_message(t('The %label derivative images have been removed.', array('%label' => $old_sizes[$key]['label'])));
+      }
+    }
+    // Additions.
+    else if (!isset($old_sizes[$key])) {
+      $new_key = drupal_strtolower(drupal_substr($size['label'], 0, 32));
+      $size['updated'] = time();
+      $result = db_query("INSERT INTO {image_size} (id, label, height, width, operation, link, updated) VALUES ('%s', '%s', %d, %d, '%s', %d, %d)", $new_key, $size['label'], $size['height'], $size['width'], $size['operation'], $size['link'], $size['updated']);
+      drupal_set_message(t('The %label derivative hs been added and needs to be generated.', array('%label' => $size['label'])));
+    }
+    // Changes.
+    else {
+      // Did the operation, height or width change?
+      $changed = ($size['operation'] != $old_sizes[$key]['operation']);
+      $changed |= ((int) $size['height'] != (int) $old_sizes[$key]['height']);
+      $changed |= ((int) $size['width'] != (int) $old_sizes[$key]['width']);
+      if ($changed) {
+        $size['updated'] = time();
+        drupal_set_message(t('The %label derivatives will need to be regenerated.', array('%label' => $size['label'])));
+      }
+      db_query("UPDATE {image_size} SET label = '%s', operation = '%s', width = %d, height = %d, link = %d, updated = %d WHERE id = '%s'",  $size['label'], $size['operation'], $size['width'], $size['height'], $size['link'], $size['updated'], $key);
+    }
+  }
+}
+
+/**
  * Implementation of hook_menu
  */
 function image_menu($may_cache) {
@@ -1019,27 +1029,25 @@ function _image_filename($filename, $lab
  *   returned.
  */
 function image_get_sizes($size = NULL, $aspect_ratio = NULL) {
-  $defaults = array(
-    IMAGE_ORIGINAL => array('width' => '', 'height' => '', 'label' => t('Original'), 'operation' => 'scale', 'link' => IMAGE_LINK_SHOWN),
-    IMAGE_THUMBNAIL => array('width' => 100, 'height' => 100, 'label' => t('Thumbnail'), 'operation' => 'scale', 'link' => IMAGE_LINK_SHOWN),
-    IMAGE_PREVIEW   => array('width' => 640, 'height' => 640, 'label' => t('Preview'), 'operation' => 'scale', 'link' => IMAGE_LINK_SHOWN),
-  );
+  static $sizes;
 
-  $sizes = array();
-  foreach (variable_get('image_sizes', $defaults) as $key => $val) {
-    // Only return sizes with a label.
-    if (!empty($val['label'])) {
+  if (!isset($sizes)) {
+    $sizes = array();
+    $result = db_query("SELECT * FROM {image_size} ORDER BY weight, label");
+    while ($row = db_fetch_array($result)) {
+      $key = $row['id'];
+      unset($row['key']);
       // For a size with only one dimension specified, compute the other 
       // dimension based on an aspect ratio.
-      if ($aspect_ratio && (empty($val['width']) || empty($val['height']))) {
-        if (empty($val['height']) && !empty($val['width'])) {
-          $val['height'] = (int)round($val['width'] * $aspect_ratio);
+      if ($aspect_ratio && (empty($row['width']) || empty($row['height']))) {
+        if (empty($row['height']) && !empty($row['width'])) {
+          $row['height'] = (int)round($row['width'] * $aspect_ratio);
         }
         elseif (empty($val['width']) && !empty($val['height'])) {
-          $val['width'] = (int)round($val['height'] / $aspect_ratio);
+          $row['width'] = (int)round($row['height'] / $aspect_ratio);
         }
       }
-      $sizes[$key] = $val;
+      $sizes[$key] = $row;
     }
   }
 
@@ -1062,7 +1070,6 @@ function _image_get_sizes($size = NULL, 
   return image_get_sizes($size, $aspect_ratio);
 }
 
-
 /**
  * Is a given size a built-in, required size?
  * @param $size
Index: contrib/image_attach/image_attach.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/image/contrib/image_attach/image_attach.module,v
retrieving revision 1.28
diff -u -p -u -p -r1.28 image_attach.module
--- contrib/image_attach/image_attach.module	18 Aug 2007 19:00:41 -0000	1.28
+++ contrib/image_attach/image_attach.module	10 Sep 2007 21:32:37 -0000
@@ -126,14 +126,14 @@ function image_attach_form_alter($form_i
         '#title' => t('Attached Images'),
         '#collapsible' => TRUE,
         '#collapsed' => !$node->iid
-        );
+      );
       if ($node->iid) {
         $image = node_load($node->iid);
         $form['image_attach']['image_thumbnail'] = array(
           '#type' => 'item',
           '#title' => t('Thumbnail'),
           '#value' => image_display($image, 'thumbnail')
-          );
+        );
       }
       if (variable_get('image_attach_existing', 1) && user_access('access content')) {
         $form['image_attach']['iid'] = array(
@@ -152,7 +152,7 @@ function image_attach_form_alter($form_i
       else {
         $form['image_attach']['iid'] = array(
           '#type' => 'hidden',
-          '#value' => $node->iid
+          $value => $node->iid
         );
       }
       $form['image_attach']['image'] = array(
