You are here

function image_form_validate in Image 6

1 string reference to 'image_form_validate'
image_form in ./image.module
Implementation of hook_form().

File

./image.module, line 373

Code

function image_form_validate($form, &$form_state) {

  // Avoid blocking node deletion with missing image.
  if ($form_state['values']['op'] == t('Delete')) {
    return;
  }

  // Validators for file_save_upload().
  $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.
  $temporary_file_path = file_create_path(file_directory_path() . '/' . variable_get('image_default_path', 'images') . '/temp');
  if ($file = file_save_upload('image', $validators, $temporary_file_path)) {

    // 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);

    // Store the new file into the session.
    $_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.'));
    }
  }
}