summaryrefslogtreecommitdiff
path: root/modules/gallery/helpers
diff options
context:
space:
mode:
authorshadlaws <shad@shadlaws.com>2013-01-25 23:47:08 +0100
committershadlaws <shad@shadlaws.com>2013-01-25 23:47:08 +0100
commitc2d1c2407fdef326cd1f59d6852620f915a5c0e1 (patch)
treec86333e0678530d79a7e9162f4f683cd455d16a5 /modules/gallery/helpers
parent776fe75ceb93f8eb729d13d58fb411d6ceeb9270 (diff)
#1965 - Improve sanity checks and copy/convert/process logic for rotate and resize.
- resize: ensured that resize is skipped *only* if the metadata is valid or the options are well-defined and would upscale. Then, if resize is skipped, check to see if it still needs to be converted. Previous conditions would allow a small PNG to get copied to a JPG, and would allow a corrupted JPG to be copied to the output. - rotate: add checks for empty file or empty options. - use get_file_metadata instead of direct getimagesize call. - add unit tests for rotate and resize, including some for corrupted input files and missing options.
Diffstat (limited to 'modules/gallery/helpers')
-rw-r--r--modules/gallery/helpers/gallery_graphics.php36
1 files changed, 30 insertions, 6 deletions
diff --git a/modules/gallery/helpers/gallery_graphics.php b/modules/gallery/helpers/gallery_graphics.php
index 1f0728c0..e81d5468 100644
--- a/modules/gallery/helpers/gallery_graphics.php
+++ b/modules/gallery/helpers/gallery_graphics.php
@@ -31,6 +31,15 @@ class gallery_graphics_Core {
module::event("graphics_rotate", $input_file, $output_file, $options, $item);
+ if (@filesize($input_file) == 0) {
+ throw new Exception("@todo EMPTY_INPUT_FILE");
+ }
+
+ if (!isset($options["degrees"])) {
+ $options["degrees"] = 0;
+ }
+
+ // Rotate the image. This also implicitly converts its format if needed.
Image::factory($input_file)
->quality(module::get_var("gallery", "image_quality"))
->rotate($options["degrees"])
@@ -57,11 +66,26 @@ class gallery_graphics_Core {
throw new Exception("@todo EMPTY_INPUT_FILE");
}
- $dims = getimagesize($input_file);
- if (max($dims[0], $dims[1]) <= min($options["width"], $options["height"])) {
- // Image would get upscaled; do nothing
- copy($input_file, $output_file);
+ list ($input_width, $input_height, $input_mime, $input_extension) =
+ photo::get_file_metadata($input_file);
+ if ($input_width && $input_height &&
+ (empty($options["width"]) || empty($options["height"]) || empty($options["master"]) ||
+ (max($input_width, $input_height) <= min($options["width"], $options["height"])))) {
+ // Photo dimensions well-defined, but options not well-defined or would upscale the image.
+ // Do not resize. Check mimes to see if we can copy the file or if we need to convert it.
+ // (checking mimes avoids needlessly converting jpg to jpeg, etc.)
+ $output_mime = legal_file::get_photo_types_by_extension(pathinfo($output_file, PATHINFO_EXTENSION));
+ if ($input_mime && $output_mime && ($input_mime == $output_mime)) {
+ // Mimes well-defined and identical - copy input to output
+ copy($input_file, $output_file);
+ } else {
+ // Mimes not well-defined or not the same - convert input to output
+ $image = Image::factory($input_file)
+ ->quality(module::get_var("gallery", "image_quality"))
+ ->save($output_file);
+ }
} else {
+ // Resize the image. This also implicitly converts its format if needed.
$image = Image::factory($input_file)
->resize($options["width"], $options["height"], $options["master"])
->quality(module::get_var("gallery", "image_quality"));
@@ -96,8 +120,8 @@ class gallery_graphics_Core {
module::event("graphics_composite", $input_file, $output_file, $options, $item);
- list ($width, $height) = getimagesize($input_file);
- list ($w_width, $w_height) = getimagesize($options["file"]);
+ list ($width, $height) = photo::get_file_metadata($input_file);
+ list ($w_width, $w_height) = photo::get_file_metadata($options["file"]);
$pad = isset($options["padding"]) ? $options["padding"] : 10;
$top = $pad;