You are here

function imagepicker_upload_form_submit in Image Picker 5

Same name and namespace in other branches
  1. 5.2 imagepicker.module \imagepicker_upload_form_submit()
  2. 6.2 imagepicker.upload.inc \imagepicker_upload_form_submit()
  3. 7 imagepicker.upload.inc \imagepicker_upload_form_submit()

Submit form

File

./imagepicker.module, line 236
Enables permitted roles to upload images for insertion into configured nodes.

Code

function imagepicker_upload_form_submit($form_id, $form_values) {
  if ($form_values['op'] == t('Upload')) {
    global $user;
    $destination = imagepicker_get_path(FALSE, TRUE);
    $thumbsdir = $destination . 'thumbs';
    $browserdir = $destination . 'browser';
    if (file_check_directory($destination, TRUE) && file_check_directory($thumbsdir, TRUE) && file_check_directory($browserdir, TRUE)) {

      // Add DIRECTORY_SEPARATORS here because drupals' functions remove trailing slashes
      $destination .= DIRECTORY_SEPARATOR;
      $thumbsdir = $thumbsdir . DIRECTORY_SEPARATOR;
      $browserdir = $browserdir . DIRECTORY_SEPARATOR;
      $maxthumbsize = $form_values['thumb'] ? $form_values['thumb'] : 100;
      $scaleto = $form_values['scale'] ? $form_values['scale'] : FALSE;
      if (!$scaleto) {

        // Use $path instead of original $destination variable cause this
        // variable's value will be changed during copying file, so we won't
        // loose it.
        $path = $destination;
        $imagemoved = imagepicker_copy_uploaded_file($path, 'file_upload');
        $file = basename($path);
      }
      else {
        $source = $_FILES['files']['tmp_name']['file_upload'];
        $file = imagepicker_get_uploaded_file_name($destination, 'file_upload');
        $imagescaled = imagepicker_scale_image($source, $destination . $file, $scaleto);
      }
      if (!$scaleto && $imagemoved || $scaleto && $imagescaled) {

        // Source file should still be an uploaded one, as scaled image
        // might have some watermarks etc. from drupal's filters/hooks.
        $source = $_FILES['files']['tmp_name']['file_upload'];
        if (imagepicker_scale_image($source, $thumbsdir . $file, $maxthumbsize)) {
          imagepicker_scale_image($source, $browserdir . $file, variable_get('imagepicker_default_browser_thumbnail_size', 100));
          $nextimgid = db_next_id('{imagepicker}_img_id');
          $title = htmlspecialchars($form_values['title']);
          $description = htmlspecialchars($form_values['description']);
          if (db_query("INSERT INTO {imagepicker} (uid, img_name, img_title, img_description) VALUES ('%d', '%s', '%s', '%s')", array(
            $user->uid,
            $file,
            $title,
            $description,
          ))) {
            drupal_set_message(t('Image was successfully uploaded.'));
            if ($form_values['account']) {
              drupal_goto('user/' . $user->uid . '/imagepicker/browse');
            }
            else {
              drupal_goto('imagepicker/browse/' . $nextimgid);
            }
          }
          else {
            file_delete($thumbsdir . $file);
            file_delete($browserdir . $file);
            drupal_set_message(t('Error while saving information to database for uploaded image.'), 'error');
          }
        }
        else {
          drupal_set_message(t('Error while creating a thumbnail for uploaded image.'), 'error');
        }
      }
      else {
        if (!$scaleto && !$imagemoved) {
          drupal_set_message(t('Error while moving uploaded file to its destination.'), 'error');
        }
        else {
          drupal_set_message(t('Error while scaling uploaded file.'), 'error');
        }
      }
      file_delete($destination . $file);
    }
    else {
      drupal_set_message(t('Unable to create a directory structure for your images.'), 'error');
    }
  }
  if ($form_values['account']) {
    drupal_goto('user/' . $user->uid . '/imagepicker');
  }
  else {
    drupal_goto('imagepicker');
  }
}