--- imagefield.module	2008-01-17 05:49:39.000000000 +0800
+++ imagefield.module	2008-02-21 14:58:51.000000000 +0800
@@ -359,6 +359,20 @@ function imagefield_widget_settings($op,
         '#description' => 
         t('The maximum allowed image size expressed as WIDTHxHEIGHT (e.g. 640x480). Set to 0 for no restriction.')
       );
+      $form['max_filesize'] = array (
+        '#type' => 'textfield', 
+        '#title' => t('Maximum filesize for Images'), 
+        '#default_value' => $widget['max_filesize'] ? $widget['max_filesize'] : 0,
+        '#size' => 6, 
+        '#description' => t('The maximum allowed image file size expressed in kilobytes. Set to 0 for no restriction.')
+      );
+      $form['max_number_images'] = array (
+        '#type' => 'textfield', 
+        '#title' => t('Maximum number of images'), 
+        '#default_value' => $widget['max_number_images'] ? $widget['max_number_images'] : 0,
+        '#size' => 4, 
+        '#description' => t('The maximum number of images allowed. Set to 0 for no restriction.')
+      );
       $form['image_path'] = array(
         '#type' => 'textfield', 
         '#title' => t('Image path'), 
@@ -399,7 +413,7 @@ function imagefield_widget_settings($op,
       break;
 
     case 'save':
-      return array('max_resolution', 'image_path', 'file_extensions', 'custom_alt', 'custom_title');
+      return array('max_resolution', 'max_filesize', 'max_number_images', 'image_path', 'file_extensions', 'custom_alt', 'custom_title');
   }
 }
 
@@ -789,12 +803,30 @@ function _imagefield_widget_upload_valid
     }
   }
 
+  // If max filesize is set
+  if (!empty($field['widget']['max_filesize'])) {
+    if ($file['filesize'] > ($field['widget']['max_filesize'] * 1024)) {
+      form_set_error($field['field_name'], t('The file you uploaded has a filesize greater than the maximum allowed filesize of %sizekb.', array('%size' => $field['widget']['max_filesize'])));
+      $valid = FALSE;
+    }
+  }
+
   // is the mime type a match for image.
   if (strpos($file['filemime'], 'image/') !== 0) {
     // sorry no it isn't. do not pass go, do not collect $200.
     form_set_error($field['field_name'], t('Mime Type mismatch. Only image files may be upload. You uploaded a file with mime type: %mime', array('%mime' => $file['filemime'])));
     $valid = FALSE;
   }
+
+  // If max number of images is set
+  if (!empty($field['widget']['max_number_images'])) {
+    $count = count($items) + count($_SESSION['imagefield'][$field['field_name']]);
+    if ($count >= $field['widget']['max_number_images']) {
+      form_set_error($field['field_name'], t('You are only allowed to upload up to %maximages images.', array('%maximages' => $field['widget']['max_number_images'])));
+      $valid = FALSE;
+    }
+  }
+
   return $valid;
 }
 
