You are here

function imagepicker_import_batch in Image Picker 6.2

Same name and namespace in other branches
  1. 7 imagepicker.import.inc \imagepicker_import_batch()
1 string reference to 'imagepicker_import_batch'
imagepicker_import_form_submit in ./imagepicker.import.inc
Submit form

File

./imagepicker.import.inc, line 248

Code

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

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

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

    // Check the file is OK
    $filename = $index;
    $source_filepath = file_check_location($options['sourcedir'] . DIRECTORY_SEPARATOR . $filename, $options['sourcedir']);
    if ($source_filepath and file_validate_is_image($source_filepath)) {
      $source = $source_filepath;
      $file = $filename;

      // copy original
      if (file_copy($source, $options['origdir'], FILE_EXISTS_RENAME)) {
        $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, $options['destdir'] . $file, $scaleto);
        }
        else {

          // no scaling, save direct from $origdir to $destdir
          file_copy($source, $options['destdir'], FILE_EXISTS_RENAME);
          $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, $options['thumbsdir'] . $file, $maxthumbsize)) {

          //
          imagepicker_scale_image($orig, $options['browserdir'] . $file, 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']) && variable_get('imagepicker_groups_enabled', 1) && variable_get('imagepicker_groups_in_upload_enabled', 1)) {
              $result = db_query("SELECT img_id FROM {imagepicker} WHERE uid = '%d' AND img_name = '%s'", array(
                $user->uid,
                $file,
              ));
              $row = db_fetch_array($result);
              $record['img_id'] = $row['img_id'];
              foreach ($form_state['values']['grouplist'] as $gid) {
                $record['gid'] = $gid;
                imagepicker_insert_group_image($record);
              }
            }

            // delete
            if (variable_get('imagepicker_import_delete', 0)) {
              file_delete($source_filepath);
            }
          }
          else {
            file_delete($options['thumbsdir'] . $file);
            file_delete($options['browserdir'] . $file);
            file_delete($options['origdir'] . $file);
            file_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,
      ));
    }

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