From 7ccc5a6b1b4541a11b858e09cbd8244909d89c92 Mon Sep 17 00:00:00 2001 From: Dries Buytaert Date: Tue, 1 Feb 2005 16:27:43 +0000 Subject: - Patch #16358 by James: added toolkit to enable better image handling. The avatar code and the upload module have been updated to take advantage of the new image API. There are 5 main functions that modules may now utilize to handle images: * image_get_info() - this function checks a file. If it exists and is a valid image file, it will return an array containing things like the pixel dimensions of the image, plus the 'type' and common extension. * image_scale - resizes a given image to fit within a given width / height dimensions, while maintaining aspect ratio (not distorting the image). This function can be used to generate thumbnails, or ensure a maximum resolution, etc. * image_resize - similar to image_scale (but will not respect aspect ratio - may well distort the image). * image_rotate - rotate an image by X degrees * image_crop - crops an image to a given rectangle (defined as top-left x/y coordinates plus a width & height of the rectangle). Contribution modules will now be able to rely on these base manipulation functions to offer additional functionality (such as image nodes, photo galleries, advanced image manipulation, etc). --- modules/upload/upload.module | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) (limited to 'modules/upload/upload.module') diff --git a/modules/upload/upload.module b/modules/upload/upload.module index c1fc08641..559b46d87 100644 --- a/modules/upload/upload.module +++ b/modules/upload/upload.module @@ -63,7 +63,8 @@ function upload_menu($may_cache) { function upload_admin() { system_settings_save(); - $group .= form_textfield(t('Maximum total file size'), 'upload_maxsize_total', variable_get('upload_maxsize_total', 0), 5, 5, t('The maximum size of a file a user can upload in megabytes. Enter 0 for unlimited.')); + $group .= form_textfield(t('Maximum total file size'), 'upload_maxsize_total', variable_get('upload_maxsize_total', 0), 10, 10, t('The maximum size of a file a user can upload in megabytes. Enter 0 for unlimited.')); + $group .= form_textfield(t('Maximum resolution for uploaded images'), 'upload_max_resolution', variable_get('upload_max_resolution', 0), 10, 10, t('The maximum allowed image size expressed as WIDTHxHEIGHT (e.g. 640x480). Set to 0 for no restriction.')); $output = form_group(t('General settings'), $group); @@ -138,7 +139,9 @@ function upload_nodeapi(&$node, $op, $arg) { if (($file = file_check_upload('upload')) && user_access('upload files')) { global $user; - $max_size = variable_get("upload_maxsize_total", 0); + $file = _upload_image($file); + + $maxsize = variable_get("upload_maxsize_total", 0); $total_size = upload_count_size() + $filesize; $total_usersize = upload_count_size($user->uid) + $filesize; @@ -371,4 +374,25 @@ function upload_load($node) { return $files; } +/** + * Check an upload, if it is an image, make sure it fits within the + * maximum dimensions allowed. + */ +function _upload_image($file) { + $info = image_get_info($file->filepath); + + if ($info) { + list($width, $height) = explode('x', variable_get('upload_max_resolution', 0)); + if ($width && $height) { + $result = image_scale($file->filepath, $file->filepath, $width, $height); + if ($result) { + $file->filesize = filesize($file->filepath); + drupal_set_message(t('Your image was resized to fit within the maximum allowed resolution of %resolution pixels.', array('%resolution' => variable_get('upload_max_resolution', 0)))); + } + } + } + + return $file; +} + ?> -- cgit v1.2.3