You are here

function imce_resize_image in IMCE 5

Same name and namespace in other branches
  1. 6.2 inc/imce.page.inc \imce_resize_image()
  2. 6 inc/page.inc \imce_resize_image()
  3. 7 inc/imce.page.inc \imce_resize_image()

Resize file:$file in directory:$dir to dimensions $w x $h

2 calls to imce_resize_image()
imce_copy_uploaded in ./imce.module
copy uploaded file to the specified directory.
imce_menu in ./imce.module
Implementation of hook_menu().

File

./imce.module, line 348

Code

function imce_resize_image($set, $filename, $w, $h, $copy = FALSE) {

  //check if file exists
  $path = $set->dir . '/' . $filename;
  if (!is_file($path)) {
    drupal_set_message(t('File does not exist.'), 'error');
    return FALSE;
  }
  $_SESSION['imcelatest'] = $path;

  // check resize dimensions and file type
  if (!($dot = strrpos($filename, '.')) || !in_array(strtolower(substr($filename, $dot)), $GLOBALS['imce_ext'])) {
    drupal_set_message(t('File type is not supported.'), 'error');
    return FALSE;
  }
  if (!($w > 0 && $h > 0) || ($w > $set->width || $h > $set->height) && !$set->nolimit) {
    drupal_set_message(t('Image dimensions exceed the limit.'), 'error');
    return FALSE;
  }

  //resize image to a temp file
  $temp = tempnam(file_directory_temp(), 'tmp_');
  register_shutdown_function('file_delete', realpath($temp));
  if (!image_resize($path, $temp, $w, $h)) {
    drupal_set_message(t('Error resizing image.'), 'error');
    return FALSE;
  }

  //check quota
  $newtotal = $_SESSION['imcetotal'] + ($copy ? @filesize($temp) : @filesize($temp) - @filesize($path));
  if ($newtotal > $set->quota * 1024 && !$set->nolimit) {
    drupal_set_message(t('You dont have enough free space.'), 'error');
    return FALSE;
  }

  //copy file
  $newpath = $copy ? file_create_filename($filename, $set->dir) : $path;
  if (!@copy($temp, $newpath)) {
    drupal_set_message(t('Error copying the file.'), 'error');
    return FALSE;
  }

  //success
  @chmod($newpath, 0664);
  $_SESSION['imcetotal'] = $newtotal;
  $_SESSION['imcelatest'] = $newpath;
  drupal_set_message(t('Resizing successful.'));
  return TRUE;
}