You are here

function imagepicker_postlet_do in Image Picker 6.2

Function to process the postlet applet callbacks

Parameters

$path: Can be admin, user or iframe.

$account: Optional, used by admin.

Return value

None.

1 string reference to 'imagepicker_postlet_do'
imagepicker_postlet_menu in contribs/imagepicker_postlet/imagepicker_postlet.module
Implementation of hook_menu().

File

contribs/imagepicker_postlet/imagepicker_postlet.module, line 206
Enables upload of images using Postlet java applet. http://www.postlet.com/ http://sourceforge.net/projects/postlet/

Code

function imagepicker_postlet_do($path, $account = FALSE) {
  if ($path == 'admin' && $account) {
    $user = user_load(array(
      'uid' => $account,
    ));
    $userdir = array(
      'uid' => $user->uid,
      'name' => $user->name,
    );
  }
  else {
    global $user;
    $userdir = TRUE;
  }
  module_load_include('inc', 'imagepicker', 'imagepicker.upload');
  $destdir = imagepicker_get_path(FALSE, $userdir);
  $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 .= DIRECTORY_SEPARATOR;
    $thumbsdir = $thumbsdir . DIRECTORY_SEPARATOR;
    $browserdir = $browserdir . DIRECTORY_SEPARATOR;
    $origdir = $origdir . DIRECTORY_SEPARATOR;
    $maxthumbsize = variable_get('imagepicker_default_thumbnail_size', 100);
    $scaleto = variable_get('imagepicker_default_scale', FALSE);
    $watermark = FALSE;
    if (imagepicker_image_check_functions(TRUE) && variable_get('imagepicker_watermark_enable', 0)) {
      if (variable_get('imagepicker_watermark_image', '')) {
        $watermark = TRUE;
      }
      elseif ($user->imagepicker_watermark_enable && ($user->imagepicker_watermark_image ? $user->imagepicker_watermark_image : FALSE)) {
        $watermark = TRUE;
      }
    }

    // $destination gets altered in imagepicker_copy_uploaded_file to give us the final path + name
    $file = basename($_FILES['userfile']['name']);
    $destination = $origdir;

    // save the original and use that from here on
    $imagemoved = imagepicker_postlet_copy_uploaded_file($destination);
    if ($imagemoved) {

      // $destination has been changed in imagepicker_copy_uploaded_file()
      // to point to the new file
      $file = basename($destination);
      $source = $origdir . $file;

      // enforce a max size
      $max_scale = variable_get('imagepicker_max_scale', '');
      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($source, $destdir . $file, $scaleto);
      }
      else {

        // no scaling, save direct from $origdir to $destdir
        $res = file_copy($source, $destdir, FILE_EXISTS_RENAME);
      }
    }

    // if watermark is enabled just apply to destdir image, not orig or the thumbs
    if ($watermark && !imagepicker_watermark_do($destdir . $file, $user)) {
      watchdog('imagepicker_postlet', 'Error while watermarking an uploaded image.', WATCHDOG_ERROR);
    }
    if (!$scaleto && $imagemoved || $scaleto && $imagescaled) {

      // not sure why this has to be restated, but if not done the thumbs
      // get wmarked too when not scaling image above
      $source = $origdir . $file;
      if (imagepicker_scale_image($source, $thumbsdir . $file, $maxthumbsize)) {
        imagepicker_scale_image($source, $browserdir . $file, variable_get('imagepicker_default_browser_thumbnail_size', 100));
        $title = '';
        $description = '';
        $nextimgid = imagepicker_insert_image($user->uid, $file, $title, $description);
        if ($nextimgid) {

          #drupal_set_message(t('Image was successfully uploaded.'));
        }
        else {
          file_delete($thumbsdir . $file);
          file_delete($browserdir . $file);
          file_delete($origdir . $file);
          file_delete($destdir . $file);
          watchdog('imagepicker_postlet', 'Error while saving information to database for uploaded image.', WATCHDOG_ERROR);
        }
      }
      else {
        watchdog('imagepicker_postlet', 'Error while creating a thumbnail for uploaded image.', WATCHDOG_ERROR);
      }
    }
    else {
      if (!$scaleto && !$imagemoved) {
        watchdog('imagepicker_postlet', 'Error while moving uploaded file to its destination.', WATCHDOG_ERROR);
      }
      else {
        watchdog('imagepicker_postlet', 'Error while scaling uploaded file.', WATCHDOG_ERROR);
      }
    }
  }
  else {
    watchdog('imagepicker_postlet', 'Unable to create a directory structure for your images.', WATCHDOG_ERROR);
  }

  // we exit as this is a json call
  exit;
}