You are here

function imagepicker_import_batch in Image Picker 7

Same name and namespace in other branches
  1. 6.2 imagepicker.import.inc \imagepicker_import_batch()

Parameters

array $form_state:

array $selected:

array $options:

user object $account:

array $context:

Return value

none

1 string reference to 'imagepicker_import_batch'
imagepicker_import_form_submit in ./imagepicker.import.inc
Submit form

File

./imagepicker.import.inc, line 269

Code

function imagepicker_import_batch($form_state, $selected, $options, $account, &$context) {
  if ($account) {
    $user = $account;
  }
  else {
    global $user;
  }
  $max = imagepicker_variable_get('imagepicker_import_max', 5);
  if (empty($context['sandbox'])) {
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['max'] = count($selected);
    $context['sandbox']['images'] = $selected;
  }

  // enforce a max size
  $max_scale = imagepicker_variable_get('imagepicker_max_scale', '');

  // Process images by groups of $max.
  $count = min($max, count($context['sandbox']['images']));
  for ($i = 0; $i < $count; $i++) {
    $filename = array_shift($context['sandbox']['images']);

    // Check the file is OK
    $source_filepath = imagepicker_file_check_location($options['sourcedir'] . DIRECTORY_SEPARATOR . $filename, $options['sourcedir']);
    if ($source_filepath) {
      $object = new stdClass();
      $object->uri = $source_filepath;
      $err = file_validate_is_image($object);
      if (!count($err)) {
        $source = $source_filepath;

        // copy original
        if (imagepicker_file_unmanaged_copy($source, IMAGEPICKER_FILE_SCHEME . $options['origdirscheme'])) {
          $file = basename($source);
          $orig = $options['origdir'] . $file;
          $scaleto = $form_state['values']['scale'] != "" ? $form_state['values']['scale'] : FALSE;
          if ($max_scale) {
            if ($scaleto && $scaleto > $max_scale) {
              $scaleto = $max_scale;
            }
            else {
              if ($info = image_get_info($source)) {
                if ($info['width'] > $max_scale || $info['height'] > $max_scale) {
                  $scaleto = $max_scale;
                }
              }
            }
          }
          if ($scaleto) {
            $imagescaled = imagepicker_scale_image($orig, IMAGEPICKER_FILE_SCHEME . $options['destdirscheme'] . $file, $scaleto);
          }
          else {

            // no scaling, save direct from $origdir to $destdir
            imagepicker_file_unmanaged_copy($source, IMAGEPICKER_FILE_SCHEME . $options['destdirscheme']);
            $file = basename($source);
          }

          // if watermark is enabled just apply to destdir image, not orig or the thumbs
          if ($form_state['values']['watermark']) {
            if (!imagepicker_watermark_do($options['destdir'] . $file, $user)) {
              $_SESSION['imagepicker_import_status'] = t('Error while watermarking imported image %im.', array(
                '%im' => $file,
              ));
            }
          }

          // thumbsdir
          // not sure why this has to be restated, but if not done the thumbs
          // get wmarked too when not scaling image above
          $orig = $options['origdir'] . $file;
          $maxthumbsize = $form_state['values']['thumb'] ? $form_state['values']['thumb'] : 100;
          if (imagepicker_scale_image($orig, IMAGEPICKER_FILE_SCHEME . $options['thumbsdirscheme'] . $file, $maxthumbsize)) {
            imagepicker_scale_image($orig, IMAGEPICKER_FILE_SCHEME . $options['browserdirscheme'] . $file, imagepicker_variable_get('imagepicker_default_browser_thumbnail_size', 100));
            $title = htmlspecialchars($form_state['values']['title']);
            $description = htmlspecialchars($form_state['values']['description']);
            $nextimgid = imagepicker_insert_image($user->uid, $file, $title, $description);
            if ($nextimgid) {
              if (is_array($form_state['values']['grouplist']) && imagepicker_variable_get('imagepicker_groups_enabled', 1) && imagepicker_variable_get('imagepicker_groups_in_upload_enabled', 1)) {

                // SELECT img_id FROM {imagepicker} WHERE uid = '%d' AND img_name = '%s'
                $query = db_select('imagepicker', 'i');
                $query
                  ->fields('i', array(
                  'img_id',
                ));
                $query
                  ->condition('uid', $user->uid)
                  ->condition('img_name', $file);
                $record = $query
                  ->execute()
                  ->fetchObject();

                #$record['img_id']  = $row['img_id'];
                foreach ($form_state['values']['grouplist'] as $gid) {
                  $record->gid = $gid;
                  imagepicker_insert_group_image($record);
                }
              }

              // delete
              if (imagepicker_variable_get('imagepicker_import_delete', 0)) {
                file_unmanaged_delete($source_filepath);
              }
            }
            else {
              file_unmanaged_delete($options['thumbsdir'] . $file);
              file_unmanaged_delete($options['browserdir'] . $file);
              file_unmanaged_delete($options['origdir'] . $file);
              file_unmanaged_delete($options['destdir'] . $file);
              $_SESSION['imagepicker_import_status'] = t('Error while saving information to database for imported image %im.', array(
                '%im' => $file,
              ));
            }
          }
          else {
            $_SESSION['imagepicker_import_status'] = t('Could not scale %file', array(
              '%file' => $source_filepath,
            ));
          }
        }
        else {
          $_SESSION['imagepicker_import_status'] = t('Could not copy to original: %file', array(
            '%file' => $source_filepath,
          ));
        }
      }
      else {
        $_SESSION['imagepicker_import_status'] = t('Invalid image: %file', array(
          '%file' => $source_filepath,
        ));
      }
    }
    else {
      $_SESSION['imagepicker_import_status'] = t('File not found');
    }

    // Update our progress information.
    $context['sandbox']['progress']++;
    $context['results'][] = $source_filepath;
    if ($context['sandbox']['progress'] < $context['sandbox']['max']) {
      $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
    }
    else {
      $context['finished'] = 1;
    }
  }
}