function imce_resize_image in IMCE 6.2
Same name and namespace in other branches
- 5 imce.module \imce_resize_image()
- 6 inc/page.inc \imce_resize_image()
- 7 inc/imce.page.inc \imce_resize_image()
Resize an image in the file list. Also used for thumbnail creation.
1 call to imce_resize_image()
- imce_create_thumbnail in inc/
imce.page.inc - Create a thumbnail.
1 string reference to 'imce_resize_image'
- imce_resize_submit in inc/
imce.page.inc - Submit resize form.
File
- inc/
imce.page.inc, line 480 - Implements the file browser.
Code
function imce_resize_image($filename, &$imce, $width, $height, $copy = TRUE, $dest = FALSE, $op = 'resize') {
$dirpath = file_directory_path() . ($imce['dir'] == '.' ? '' : '/' . $imce['dir']);
$filepath = $dirpath . '/' . $filename;
//check if the file is an image
if (!$imce['files'][$filename]['width'] || !($img = imce_image_info($filepath))) {
drupal_set_message(t('%filename is not an image.', array(
'%filename' => utf8_encode($filename),
)), 'error', FALSE);
return FALSE;
}
if (substr($op, 0, 5) == 'scale' && !($width < $img['width'] || $height < $img['height'])) {
drupal_set_message(t('Scaling up is not allowed.'), 'error', FALSE);
return FALSE;
}
//create file object
$file = new stdClass();
$file->filepath = $dirpath . '/' . $dest;
if (!$dest || $dest == $filename) {
$file->filepath = $copy ? file_create_filename($filename, $dirpath) : $filepath;
}
$file->filename = basename($file->filepath);
//check if a file having the same properties exists already.
if (isset($imce['files'][$file->filename])) {
if (($f = $imce['files'][$file->filename]) && $f['width'] == $width && $f['height'] == $height) {
drupal_set_message(t('%filename(%dimensions) already exists.', array(
'%filename' => utf8_encode($file->filename),
'%dimensions' => $width . 'x' . $height,
)), 'error');
return FALSE;
}
}
//validate file name
$errors = file_validate_name_length($file);
if (!empty($errors)) {
drupal_set_message($errors[0], 'error');
return FALSE;
}
//resize image to a temp file
$temp = tempnam(realpath(file_directory_temp()), 'imc');
register_shutdown_function('file_delete', $temp);
$function = 'image_' . $op;
if (!$function($filepath, $temp, $width, $height)) {
drupal_set_message(t('%filename cannot be resized to %dimensions', array(
'%filename' => utf8_encode($filename),
'%dimensions' => $width . 'x' . $height,
)), 'error', FALSE);
return FALSE;
}
//validate quota
$file->filesize = filesize($temp);
$overwrite = $file->filename == $filename;
if (!imce_validate_quotas($file, $imce, $overwrite ? -$imce['files'][$filename]['size'] : 0)) {
return FALSE;
}
//copy from temp to filepath
if (!@copy($temp, $file->filepath)) {
drupal_set_message(t('The selected file %file could not be copied.', array(
'%file' => utf8_encode($file->filename),
)), 'error', FALSE);
return FALSE;
}
@chmod($file->filepath, 0664);
//build the rest of the file object
$file->uid = $imce['uid'];
$file->filemime = $img['mime'];
$file->status = FILE_STATUS_PERMANENT;
$file->timestamp = time();
//if we are overwriting the file and it is already in database.
$update = array();
if ($overwrite && ($_file = db_fetch_object(db_query("SELECT f.* FROM {files} f WHERE f.filepath = '%s'", $file->filepath)))) {
$file->fid = $_file->fid;
$file->uid = $_file->uid;
$update[] = 'fid';
}
//save the file
drupal_write_record('files', $file, $update);
imce_file_register($file);
//update file list
//if the file was scaled get the new dimensions
if ($op == 'scale') {
$img = imce_image_info($file->filepath);
$width = $img['width'];
$height = $img['height'];
}
$file->width = $width;
$file->height = $height;
imce_add_file($file, $imce);
return $file;
}