You are here

function imagepicker_copy_form_submit in Image Picker 6.2

Same name and namespace in other branches
  1. 7 imagepicker.functions.inc \imagepicker_copy_form_submit()

Function to submit the copy form

File

./imagepicker.functions.inc, line 2683
Imagepicker functions

Code

function imagepicker_copy_form_submit($form, &$form_state) {
  module_load_include('inc', 'imagepicker', 'imagepicker.imagefuncs');
  if ($form_state['values']['uid']) {
    $user = user_load(array(
      'uid' => $form_state['values']['uid'],
    ));
  }
  else {
    global $user;
  }
  $img_id = $form_state['values']['img_id'];
  $newname = $form_state['values']['imagepicker_copy'];
  $img = _imagepicker_get_img($img_id, TRUE, $user);
  if ($img) {
    $destdir = imagepicker_get_path(FALSE, $form_state['values']['admin'] ? array(
      'name' => $user->name,
      'uid' => $user->uid,
    ) : TRUE);
    $thumbsdir = $destdir . IMAGEPICKER_THUMBS_DIR;
    $browserdir = $destdir . IMAGEPICKER_BROWSER_DIR;
    $origdir = $destdir . IMAGEPICKER_ORIG_DIR;
    if (file_check_directory($destdir, TRUE) && file_check_directory($thumbsdir, TRUE) && file_check_directory($browserdir, TRUE) && file_check_directory($origdir, TRUE)) {

      // clear out the noisy 'created' messages
      drupal_get_messages('status', TRUE);

      // Add DIRECTORY_SEPARATORS here because drupals' functions remove trailing slashes
      $destdir = $destdir . DIRECTORY_SEPARATOR;
      $thumbsdir = $thumbsdir . DIRECTORY_SEPARATOR;
      $browserdir = $browserdir . DIRECTORY_SEPARATOR;
      $origdir = $origdir . DIRECTORY_SEPARATOR;
      $doinsert = TRUE;
      if (file_exists($destdir . $newname)) {
        $doinsert = FALSE;
      }
      $scaleto = $form_state['values']['scale'] ? $form_state['values']['scale'] : FALSE;
      $dest = $destdir . $newname;
      if ($scaleto) {

        // as origdir is quite new...
        $source = $origdir . $img['img_name'];
        if (!file_exists($source)) {
          $source = $destdir . $img['img_name'];
        }

        // enforce a max size
        $max_scale = variable_get('imagepicker_max_scale', '');
        if ($max_scale) {
          if ($info = image_get_info($source)) {
            if ($info['width'] > $max_scale || $info['height'] > $max_scale) {
              $scaleto = $max_scale;
            }
          }
        }
        $imagescaled = imagepicker_scale_image($source, $dest, $scaleto);

        // if watermark is enabled just apply to destdir image, not orig or the thumbs
        if ($form_state['values']['watermark']) {
          if (!imagepicker_watermark_do($dest, $user)) {
            drupal_set_message(t('Error while watermarking an uploaded image.'), 'error');
          }
        }
      }
      else {

        // no scaling, copy direct from $destdir to $destdir
        $source = $destdir . $img['img_name'];
        file_copy($source, $dest, FILE_EXISTS_REPLACE);
      }
      $source = $thumbsdir . $img['img_name'];
      $dest = $thumbsdir . $newname;
      file_copy($source, $dest, FILE_EXISTS_REPLACE);
      $source = $browserdir . $img['img_name'];
      $dest = $browserdir . $newname;
      file_copy($source, $dest, FILE_EXISTS_REPLACE);
      $source = $origdir . $img['img_name'];
      $dest = $origdir . $newname;
      file_copy($source, $dest, FILE_EXISTS_REPLACE);
      if ($doinsert) {
        $nextimgid = imagepicker_insert_image($user->uid, $newname, $img['img_title'], $img['img_description']);
        if ($nextimgid) {
          $record['img_id'] = $nextimgid;
          $gids = imagepicker_get_image_groups($img_id);
          if (count($gids)) {
            foreach ($gids as $gid) {
              $record['gid'] = $gid;
              imagepicker_insert_group_image($record);
            }
          }
          drupal_set_message(t('Copy done.'));
        }
        else {

          // rollback
          file_delete($thumbsdir . $newname);
          file_delete($browserdir . $newname);
          file_delete($origdir . $newname);
          file_delete($destdir . $newname);

          // warn
          drupal_set_message(t('Copy failed.'), 'warning');
        }
      }
    }
  }
}