@@ -343,17 +343,20 @@
  * attempts may create trash.
  */
 function image_attach_validate(&$form, &$form_state) {
-  // Validate the number of attached images. Filter out the 'None' with array_filter.
-  if ($maximum_images = variable_get('image_attach_maximum_' . $form['#node']->type, 0)) {
-    $uploading_new_image = ($form_state['clicked_button']['#value'] == t('Attach'));
-    $num_images = count(array_filter($form_state['values']['iids']));
+  // Validate attached images.
+  if ($form_state['clicked_button']['#value'] == t('Attach')) {
+    $maximum_images  = variable_get('image_attach_maximum_' . $form['#node']->type, 0);
+    $node_type_label = node_get_types('name', $form['#node']->type);
+    $num_images      = isset($form_state['values']['iids']) ? count(array_filter($form_state['values']['iids'])) : 0;
+    // Filter out the "None" value from the count
+    $num_images      = isset($form_state['values']['iids'][0]) ? $num_images-- : $num_images;
 
-    $node_type_label = node_get_types('name', $form['#node']->type);
-    if ($num_images >= $maximum_images && $uploading_new_image) {
+    if ($num_images >= $maximum_images) {
       // This error will be set when attempting to upload a new image.
       // The number already selected may be equal to the maximum, in which case
-      // the error is just to alert the user that their upload has not been performed, and allow
-      // them to unselect an image and proceed to upload the new one.
+      // the error is just to alert the user that their upload has not been
+      // performed, and allow them to unselect an image and proceed to upload the
+      // new one.
       form_set_error('iids', t('You have selected @count_images but the maximum for a %type is @maximum. The new image has not been uploaded.', array(
         '@count_images' => format_plural($num_images, '1 image', '@count images'),
         '@maximum'      => $maximum_images,
@@ -375,20 +378,18 @@
       'file_validate_is_image' => array(),
     );
     if ($file = file_save_upload('image', $validators)) {
-      $image_title = $_POST['image_title'] ? $_POST['image_title'] : basename($file->filepath);
+      $image_title = $_POST['image_title'] ? check_plain($_POST['image_title']) : basename($file->filepath);
       // Initialize an image properly.
-      $image = image_create_node_from($file->filepath, $image_title, '');
+      $image = image_create_node_from($file->filepath, $image_title);
       if ($image && !form_get_errors()) {
-        drupal_set_message(t("Created new image to attach to this node. !image_link", array('!image_link' => l($image_title, 'node/'. $image->nid) )));
+        $message = t("Created new image to attach to this node. !image_link", array('!image_link' => l($image_title, 'node/'. $image->nid)));
+        drupal_set_message($message);
         // Append image nid to array of images.
         $form_state['values']['iids'][$image->nid] = $image->nid;
       }
     }
     else {
-      // Only raise error if user clicked specific Attach button.
-      if ($form_state['clicked_button']['#value'] == t('Attach')) {
-        form_set_error('image_attach', t('Invalid or missing image file for upload and attach.'));
-      }
+      form_set_error('image_attach', t('Invalid or missing image file for upload and attach.'));
     }
   }
 }
@@ -399,7 +400,9 @@
 function image_attach_node_form_submit(&$form, &$form_state) {
   // Clear the 0 key in the iids array that arises from selecting the 'None'
   // option. We do this here so image_attach_nodeapi() gets clean data.
-  unset($form_state['values']['iids'][0]);
+  if (isset($form_state['values']['iids'][0])) {
+    unset($form_state['values']['iids'][0]);
+  }
 }
 
 /**
