diff options
author | Dries Buytaert <dries@buytaert.net> | 2005-02-01 16:27:43 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2005-02-01 16:27:43 +0000 |
commit | 7ccc5a6b1b4541a11b858e09cbd8244909d89c92 (patch) | |
tree | 0742993a6678e544fb6bb2b28787414d38378b7d /modules | |
parent | 7931c778d75cec00bd0d186da18c08e5dcf9a5c4 (diff) | |
download | brdo-7ccc5a6b1b4541a11b858e09cbd8244909d89c92.tar.gz brdo-7ccc5a6b1b4541a11b858e09cbd8244909d89c92.tar.bz2 |
- 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).
Diffstat (limited to 'modules')
-rw-r--r-- | modules/system.module | 13 | ||||
-rw-r--r-- | modules/system/system.module | 13 | ||||
-rw-r--r-- | modules/upload.module | 28 | ||||
-rw-r--r-- | modules/upload/upload.module | 28 | ||||
-rw-r--r-- | modules/user.module | 37 | ||||
-rw-r--r-- | modules/user/user.module | 37 |
6 files changed, 94 insertions, 62 deletions
diff --git a/modules/system.module b/modules/system.module index b0dcfb443..e42fd19d8 100644 --- a/modules/system.module +++ b/modules/system.module @@ -239,6 +239,17 @@ function system_view_general() { $group .= form_radios(t('Download method'), 'file_downloads', variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC), array(FILE_DOWNLOADS_PUBLIC => t('Public - files are available using http directly.'), FILE_DOWNLOADS_PRIVATE => t('Private - files are transferred by Drupal.')), t('If you want any sort of access control on the downloading of files, this needs to be set to <em>private</em>. You can change this at any time, however all download URLs will change and there may be unexpected problems so it is not recommended.')); $output .= form_group(t('File system settings'), $group); + // image handling: + $group = ''; + $toolkits_available = image_get_available_toolkits(); + if (count($toolkits_available) > 1) { + $group .= form_radios(t('Select an image processing toolkit'), 'image_toolkit', variable_get('image_toolkit', image_get_toolkit()), $toolkits_available); + } + $group .= image_toolkit_invoke('settings'); + if ($group) { + $output .= form_group(t('Image handling'), $group); + } + // date settings: $zones = _system_zonelist(); @@ -612,7 +623,7 @@ function system_theme_settings() { // Check for a new uploaded logo, and use that instead. if ($file = file_check_upload('logo_upload')) { - if (in_array($file->filemime, array('image/jpeg', 'image/gif', 'image/png'))) { + if ($info = image_get_info($file->filepath)) { $parts = pathinfo($file->filename); $filename = ($key) ? str_replace('/', '_', $key) . '_logo.' . $parts['extension'] : 'logo.' . $parts['extension']; diff --git a/modules/system/system.module b/modules/system/system.module index b0dcfb443..e42fd19d8 100644 --- a/modules/system/system.module +++ b/modules/system/system.module @@ -239,6 +239,17 @@ function system_view_general() { $group .= form_radios(t('Download method'), 'file_downloads', variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC), array(FILE_DOWNLOADS_PUBLIC => t('Public - files are available using http directly.'), FILE_DOWNLOADS_PRIVATE => t('Private - files are transferred by Drupal.')), t('If you want any sort of access control on the downloading of files, this needs to be set to <em>private</em>. You can change this at any time, however all download URLs will change and there may be unexpected problems so it is not recommended.')); $output .= form_group(t('File system settings'), $group); + // image handling: + $group = ''; + $toolkits_available = image_get_available_toolkits(); + if (count($toolkits_available) > 1) { + $group .= form_radios(t('Select an image processing toolkit'), 'image_toolkit', variable_get('image_toolkit', image_get_toolkit()), $toolkits_available); + } + $group .= image_toolkit_invoke('settings'); + if ($group) { + $output .= form_group(t('Image handling'), $group); + } + // date settings: $zones = _system_zonelist(); @@ -612,7 +623,7 @@ function system_theme_settings() { // Check for a new uploaded logo, and use that instead. if ($file = file_check_upload('logo_upload')) { - if (in_array($file->filemime, array('image/jpeg', 'image/gif', 'image/png'))) { + if ($info = image_get_info($file->filepath)) { $parts = pathinfo($file->filename); $filename = ($key) ? str_replace('/', '_', $key) . '_logo.' . $parts['extension'] : 'logo.' . $parts['extension']; diff --git a/modules/upload.module b/modules/upload.module index c1fc08641..559b46d87 100644 --- a/modules/upload.module +++ b/modules/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; +} + ?> 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; +} + ?> diff --git a/modules/user.module b/modules/user.module index d896367bc..785d72b80 100644 --- a/modules/user.module +++ b/modules/user.module @@ -227,20 +227,19 @@ function user_validate_picture($file, &$edit, $user) { // Check that uploaded file is an image, with a maximum file size // and maximum height/width. - $extension = strtolower(strrchr($file->filename, '.')); - $size = @getimagesize($file->filepath); + $info = image_get_info($file->filepath); list($maxwidth, $maxheight) = explode('x', variable_get('user_picture_dimensions', '85x85')); - if ((!in_array($size[2], array(1, 2, 3))) || (!in_array($extension, array('.gif', '.jpg', '.png', '.jpeg')))) { + if (!$info || !$info['extension']) { form_set_error('picture', t('The uploaded file was not an image.')); } - else if ($file->size > (variable_get('user_picture_file_size', '30') * 1000)) { - form_set_error('picture', t('The uploaded image is too large; the maximum file size is %size kB.', array('%size' => variable_get('user_picture_file_size', '30')))); - } - else if ($size[0] > $maxwidth || $size[1] > $maxheight) { + else if (!image_scale($file->filepath, $file->filepath, $maxwidth, $maxheight)) { form_set_error('picture', t('The uploaded image is too large; the maximum dimensions are %dimensions pixels.', array('%dimensions' => variable_get('user_picture_dimensions', '85x85')))); } - else if ($file = file_save_upload('picture', variable_get('user_picture_path', 'pictures') .'/picture-'. $user->uid . $extension, 1)) { + else if (filesize($file->filepath) > (variable_get('user_picture_file_size', '30') * 1000)) { + form_set_error('picture', t('The uploaded image is too large; the maximum file size is %size kB.', array('%size' => variable_get('user_picture_file_size', '30')))); + } + else if ($file = file_save_upload('picture', variable_get('user_picture_path', 'pictures') .'/picture-'. $user->uid . '.' . $info['extension'], 1)) { $edit['picture'] = $file->filepath; } else { @@ -403,26 +402,8 @@ function user_perm() { */ function user_file_download($file) { if (strpos($file, variable_get('user_picture_path', 'pictures') .'/picture-') === 0) { - list($width, $height, $type, $attr) = @getimagesize(file_create_path($file)); - $types = array( - IMAGETYPE_GIF => 'image/gif', - IMAGETYPE_JPEG => 'image/jpeg', - IMAGETYPE_PNG => 'image/png', - IMAGETYPE_SWF => 'application/x-shockwave-flash', - IMAGETYPE_PSD => 'image/psd', - IMAGETYPE_BMP => 'image/bmp', - IMAGETYPE_TIFF_II => 'image/tiff', - IMAGETYPE_TIFF_MM => 'image/tiff', - IMAGETYPE_JPC => 'application/octet-stream', - IMAGETYPE_JP2 => 'image/jp2', - IMAGETYPE_JPX => 'application/octet-stream', - IMAGETYPE_JB2 => 'application/octet-stream', - IMAGETYPE_SWC => 'application/x-shockwave-flash', - IMAGETYPE_IFF => 'image/iff', - IMAGETYPE_WBMP => 'image/vnd.wap.wbmp', - IMAGETYPE_XBM => 'image/xbm' - ); - return array('Content-type: '. $types[$type]); + $info = image_get_info(file_create_path($file)); + return array('Content-type: '. $info['mime_type']); } } diff --git a/modules/user/user.module b/modules/user/user.module index d896367bc..785d72b80 100644 --- a/modules/user/user.module +++ b/modules/user/user.module @@ -227,20 +227,19 @@ function user_validate_picture($file, &$edit, $user) { // Check that uploaded file is an image, with a maximum file size // and maximum height/width. - $extension = strtolower(strrchr($file->filename, '.')); - $size = @getimagesize($file->filepath); + $info = image_get_info($file->filepath); list($maxwidth, $maxheight) = explode('x', variable_get('user_picture_dimensions', '85x85')); - if ((!in_array($size[2], array(1, 2, 3))) || (!in_array($extension, array('.gif', '.jpg', '.png', '.jpeg')))) { + if (!$info || !$info['extension']) { form_set_error('picture', t('The uploaded file was not an image.')); } - else if ($file->size > (variable_get('user_picture_file_size', '30') * 1000)) { - form_set_error('picture', t('The uploaded image is too large; the maximum file size is %size kB.', array('%size' => variable_get('user_picture_file_size', '30')))); - } - else if ($size[0] > $maxwidth || $size[1] > $maxheight) { + else if (!image_scale($file->filepath, $file->filepath, $maxwidth, $maxheight)) { form_set_error('picture', t('The uploaded image is too large; the maximum dimensions are %dimensions pixels.', array('%dimensions' => variable_get('user_picture_dimensions', '85x85')))); } - else if ($file = file_save_upload('picture', variable_get('user_picture_path', 'pictures') .'/picture-'. $user->uid . $extension, 1)) { + else if (filesize($file->filepath) > (variable_get('user_picture_file_size', '30') * 1000)) { + form_set_error('picture', t('The uploaded image is too large; the maximum file size is %size kB.', array('%size' => variable_get('user_picture_file_size', '30')))); + } + else if ($file = file_save_upload('picture', variable_get('user_picture_path', 'pictures') .'/picture-'. $user->uid . '.' . $info['extension'], 1)) { $edit['picture'] = $file->filepath; } else { @@ -403,26 +402,8 @@ function user_perm() { */ function user_file_download($file) { if (strpos($file, variable_get('user_picture_path', 'pictures') .'/picture-') === 0) { - list($width, $height, $type, $attr) = @getimagesize(file_create_path($file)); - $types = array( - IMAGETYPE_GIF => 'image/gif', - IMAGETYPE_JPEG => 'image/jpeg', - IMAGETYPE_PNG => 'image/png', - IMAGETYPE_SWF => 'application/x-shockwave-flash', - IMAGETYPE_PSD => 'image/psd', - IMAGETYPE_BMP => 'image/bmp', - IMAGETYPE_TIFF_II => 'image/tiff', - IMAGETYPE_TIFF_MM => 'image/tiff', - IMAGETYPE_JPC => 'application/octet-stream', - IMAGETYPE_JP2 => 'image/jp2', - IMAGETYPE_JPX => 'application/octet-stream', - IMAGETYPE_JB2 => 'application/octet-stream', - IMAGETYPE_SWC => 'application/x-shockwave-flash', - IMAGETYPE_IFF => 'image/iff', - IMAGETYPE_WBMP => 'image/vnd.wap.wbmp', - IMAGETYPE_XBM => 'image/xbm' - ); - return array('Content-type: '. $types[$type]); + $info = image_get_info(file_create_path($file)); + return array('Content-type: '. $info['mime_type']); } } |