Index: image/image.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/image/image.module,v retrieving revision 1.287 diff -u -p -r1.287 image.module --- image/image.module 17 Jan 2009 01:00:05 -0000 1.287 +++ image/image.module 17 Jan 2009 11:50:48 -0000 @@ -168,66 +168,6 @@ function image_operations_rebuild($nids) } /** - * TODO: document me... - */ -function image_node_form_submit($form, &$form_state) { - // We need to be aware that a user may try to edit multiple image nodes at - // once. By using the $nid variable each node's files can be stored separately - // in the session. - $nid = $form_state['values']['nid'] ? $form_state['values']['nid'] : 'new_node'; - // When you enter the edit view the first time we need to clear our files in - // session for this node. This is so if you upload a file, then decide you - // don't want it and reload the form (without posting), the files will be - // discarded. - if (count($_POST) == 0) { - unset($_SESSION['image_new_files'][$nid]); - } - - // Validators for file_save_upload(). - $validators = array( - 'file_validate_is_image' => array(), - ); - - if ($file = file_save_upload('image', $validators)) { - // Resize the original. - $image_info = image_get_info($file->filepath); - $aspect_ratio = $image_info['height'] / $image_info['width']; - $original_size = image_get_sizes(IMAGE_ORIGINAL, $aspect_ratio); - if (!empty($original_size['width']) && !empty($original_size['height'])) { - $result = image_scale($file->filepath, $file->filepath, $original_size['width'], $original_size['height']); - if ($result) { - clearstatcache(); - $file->filesize = filesize($file->filepath); - drupal_set_message(t('The original image was resized to fit within the maximum allowed resolution of %width x %height pixels.', array('%width' => $original_size['width'], '%height' => $original_size['height']))); - } - } - - // Check the file size limit. - if ($file->filesize > variable_get('image_max_upload_size', 800) * 1024) { - form_set_error('image', t('The image you uploaded was too big. You are only allowed upload files less than %max_size but your file was %file_size.', array('%max_size' => format_size(variable_get('image_max_upload_size', 800) * 1024), '%file_size' => format_size($file->filesize)))); - file_delete($file->filepath); - return; - } - - // We're good to go. - $form_state['values']['images'][IMAGE_ORIGINAL] = $file->filepath; - $form_state['values']['rebuild_images'] = FALSE; - $form_state['values']['new_file'] = TRUE; - - // Call hook to allow other modules to modify the original image. - module_invoke_all('image_alter', $form_state['values'], $file->filepath, IMAGE_ORIGINAL); - $form_state['values']['images'] = _image_build_derivatives((object) $form_state['values'], TRUE); - - // Store the new file into the session. - $_SESSION['image_new_files'][$nid] = $form_state['values']['images']; - } - // Reload new files uploaded in a previous preview. - else if (isset($_SESSION['image_new_files'][$nid])) { - $form_state['values']['images'] = $_SESSION['image_new_files'][$nid]; - } -} - -/** * Implementation of hook_file_download(). * * Note that in Drupal 5, the upload.module's hook_file_download() checks its @@ -341,12 +281,17 @@ function image_form_add_thumbnail($form, /** * Implementation of hook_form */ -function image_form(&$node, &$param) { +function image_form(&$node, $form_state) { _image_check_settings(); + if (count($_POST) == 0 && !empty($_SESSION['image_upload'])) { + unset($_SESSION['image_upload']); + } + $type = node_get_types('type', $node); - $form['#submit'][] = 'image_node_form_submit'; + $form['#validate'][] = 'image_form_validate'; + $form['#submit'][] = 'image_form_submit'; $form['title'] = array( '#type' => 'textfield', @@ -398,20 +343,56 @@ function image_form(&$node, &$param) { return $form; } -function image_validate($node) { - $nid = ($node->nid) ? $node->nid : 'new_node'; - if (!isset($node->images[IMAGE_ORIGINAL]) && !isset($_SESSION['image_new_files'][$nid])) { - form_set_error('image', t('You must upload an image.')); +function image_form_validate($form, &$form_state) { + $validators = array('file_validate_is_image' => array()); + // New image uploads need to be saved in images/temp in order to be viewable + // during node preview. + $dest = file_create_path(variable_get('image_default_path', 'images') .'/temp'); + + if ($file = file_save_upload('image', $validators, $dest)) { + // Resize the original. + $image_info = image_get_info($file->filepath); + $aspect_ratio = $image_info['height'] / $image_info['width']; + $original_size = image_get_sizes(IMAGE_ORIGINAL, $aspect_ratio); + if (!empty($original_size['width']) && !empty($original_size['height'])) { + $result = image_scale($file->filepath, $file->filepath, $original_size['width'], $original_size['height']); + if ($result) { + clearstatcache(); + $file->filesize = filesize($file->filepath); + drupal_set_message(t('The original image was resized to fit within the maximum allowed resolution of %width x %height pixels.', array('%width' => $original_size['width'], '%height' => $original_size['height']))); + } + } + + // Check the file size limit. + if ($file->filesize > variable_get('image_max_upload_size', 800) * 1024) { + form_set_error('image', t('The image you uploaded was too big. You are only allowed upload files less than %max_size but your file was %file_size.', array('%max_size' => format_size(variable_get('image_max_upload_size', 800) * 1024), '%file_size' => format_size($file->filesize)))); + file_delete($file->filepath); + return; + } + + // We're good to go. + $form_state['values']['images'][IMAGE_ORIGINAL] = $file->filepath; + $form_state['values']['rebuild_images'] = FALSE; + $form_state['values']['new_file'] = TRUE; + + // Call hook to allow other modules to modify the original image. + module_invoke_all('image_alter', $form_state['values'], $form_state['values']['images'][IMAGE_ORIGINAL], IMAGE_ORIGINAL); + $form_state['values']['images'] = _image_build_derivatives((object) $form_state['values'], TRUE); + + $_SESSION['image_upload'] = $form_state['values']['images']; + } + elseif (empty($form_state['values']['images'][IMAGE_ORIGINAL])) { + if (empty($_SESSION['image_upload'])) { + form_set_error('image', t('You must upload an image.')); + } } } -function image_submit($node) { - $nid = ($node->nid) ? $node->nid : 'new_node'; - if (isset($_SESSION['image_new_files'][$nid])) { - $node->new_file = TRUE; - $node->rebuild_images = FALSE; - $node->images = $_SESSION['image_new_files'][$nid]; - unset($_SESSION['image_new_files'][$nid]); +function image_form_submit($form, &$form_state) { + if (!empty($_SESSION['image_upload'])) { + $form_state['values']['images'] = $_SESSION['image_upload']; + $form_state['values']['new_file'] = TRUE; + unset($_SESSION['image_upload']); } }