function imce_resize_image in IMCE 7
Same name and namespace in other branches
- 5 imce.module \imce_resize_image()
- 6.2 inc/imce.page.inc \imce_resize_image()
- 6 inc/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 533 - Implements the file browser.
Code
function imce_resize_image($filename, &$imce, $width, $height, $copy = TRUE, $destname = FALSE, $op = 'resize') {
$destdir = imce_dir_uri($imce);
$imguri = $destdir . $filename;
// Check if the file is an image.
if (!$imce['files'][$filename]['width'] || !($img = image_get_info($imguri))) {
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->uri = $destdir . $destname;
if (!$destname || $destname == $filename) {
$file->uri = $copy ? file_create_filename($filename, $destdir) : $imguri;
}
$file->filename = drupal_basename($file->uri);
// 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.
$image = image_load($imguri);
$function = 'image_' . $op;
if (!$image || !function_exists($function) || !$function($image, $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;
}
// Copy to a temp file.
if (!($tempuri = drupal_tempnam('temporary://', 'imce'))) {
return FALSE;
}
register_shutdown_function('file_unmanaged_delete', $tempuri);
if (!image_save($image, $tempuri) || !$image->info) {
return FALSE;
}
// Validate quota.
$file->filesize = $image->info['file_size'];
$quotadiff = $file->filename == $filename ? -$imce['files'][$filename]['size'] : 0;
if (!imce_validate_quotas($file, $imce, $quotadiff)) {
return FALSE;
}
// Build the rest of the file object.
$file->uid = $imce['uid'];
$file->filemime = $img['mime'];
$file->status = FILE_STATUS_PERMANENT;
$file->timestamp = REQUEST_TIME;
// Copy from temp to file uri.
$destination = $file->uri;
$file->uri = $tempuri;
if (!($file = file_copy($file, $destination, FILE_EXISTS_REPLACE))) {
return FALSE;
}
imce_file_register($file);
// Update file list.
// If the file was scaled get the new dimensions.
if ($op == 'scale') {
$img = imce_image_info($file->uri);
$width = $img['width'];
$height = $img['height'];
}
$file->width = $width;
$file->height = $height;
imce_add_file($file, $imce);
return $file;
}