From a442fbe409ef190e99ba9e2e1c755f6d1a1735bf Mon Sep 17 00:00:00 2001 From: jgen Date: Tue, 1 Apr 2014 19:29:31 -0400 Subject: [PATCH 1/6] Resizing images actually was broken, how nobody noticed I don't know. --- ext/resize/main.php | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/ext/resize/main.php b/ext/resize/main.php index 1bd7e146..5176ec47 100644 --- a/ext/resize/main.php +++ b/ext/resize/main.php @@ -223,24 +223,37 @@ class ResizeImage extends Extension { } /* Resize and resample the image */ + $image_resized = imagecreatetruecolor( $new_width, $new_height ); - if ( ($info[2] == IMAGETYPE_GIF) || ($info[2] == IMAGETYPE_PNG) ) { - $transparency = imagecolortransparent($image); + if ($info[2] == IMAGETYPE_GIF) { + $transparency = imagecolortransparent($image); - if ($transparency >= 0) { - $transparent_color = imagecolorsforindex($image, $trnprt_indx); - $transparency = imagecolorallocate($image_resized, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']); - imagefill($image_resized, 0, 0, $transparency); - imagecolortransparent($image_resized, $transparency); - } - elseif ($info[2] == IMAGETYPE_PNG) { + // If we have a specific transparent color + if ($transparency >= 0) { + // Get the original image's transparent color's RGB values + $transparent_color = imagecolorsforindex($image, $transparency); + + // Allocate the same color in the new image resource + $transparency = imagecolorallocate($image_resized, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']); + + // Completely fill the background of the new image with allocated color. + imagefill($image_resized, 0, 0, $transparency); + + // Set the background color for new image to transparent + imagecolortransparent($image_resized, $transparency); + } + } elseif ($info[2] == IMAGETYPE_PNG) { + // + // More info here: http://stackoverflow.com/questions/279236/how-do-i-resize-pngs-with-transparency-in-php + // imagealphablending($image_resized, false); - $color = imagecolorallocatealpha($image_resized, 0, 0, 0, 127); - imagefill($image_resized, 0, 0, $color); imagesavealpha($image_resized, true); - } + $transparent_color = imagecolorallocatealpha($new_image, 255, 255, 255, 127); + imagefilledrectangle($new_image, 0, 0, $width, $height, $transparent_color); } + + // Resize the image. imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $new_width, $new_height, $image_obj->width, $image_obj->height); /* Temp storage while we resize */ @@ -249,6 +262,7 @@ class ResizeImage extends Extension { throw new ImageResizeException("Unable to save temporary image file."); } + // TODO: Are these checks below necessary? They seem redundant? /* Output to the same format as the original image */ switch ( $info[2] ) { case IMAGETYPE_GIF: imagegif($image_resized, $tmp_filename); break; From 749f846b797e43c1d3dc325c6447ad48c0b82c05 Mon Sep 17 00:00:00 2001 From: jgen Date: Tue, 1 Apr 2014 20:33:36 -0400 Subject: [PATCH 2/6] Fix typo. --- ext/resize/main.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/resize/main.php b/ext/resize/main.php index 5176ec47..7ceb8fba 100644 --- a/ext/resize/main.php +++ b/ext/resize/main.php @@ -249,8 +249,8 @@ class ResizeImage extends Extension { // imagealphablending($image_resized, false); imagesavealpha($image_resized, true); - $transparent_color = imagecolorallocatealpha($new_image, 255, 255, 255, 127); - imagefilledrectangle($new_image, 0, 0, $width, $height, $transparent_color); + $transparent_color = imagecolorallocatealpha($image_resized, 255, 255, 255, 127); + imagefilledrectangle($image_resized, 0, 0, $width, $height, $transparent_color); } // Resize the image. From 225d197790a3b1eb403837607c9fd8f290a64982 Mon Sep 17 00:00:00 2001 From: jgen Date: Tue, 1 Apr 2014 21:41:57 -0400 Subject: [PATCH 3/6] Remove duplicate code. --- ext/resize/main.php | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/ext/resize/main.php b/ext/resize/main.php index 7ceb8fba..3f015270 100644 --- a/ext/resize/main.php +++ b/ext/resize/main.php @@ -262,16 +262,6 @@ class ResizeImage extends Extension { throw new ImageResizeException("Unable to save temporary image file."); } - // TODO: Are these checks below necessary? They seem redundant? - /* Output to the same format as the original image */ - switch ( $info[2] ) { - case IMAGETYPE_GIF: imagegif($image_resized, $tmp_filename); break; - case IMAGETYPE_JPEG: imagejpeg($image_resized, $tmp_filename); break; - case IMAGETYPE_PNG: imagepng($image_resized, $tmp_filename); break; - default: - throw new ImageResizeException("Unsupported image type."); - } - /* Move the new image into the main storage location */ $new_hash = md5_file($tmp_filename); $new_size = filesize($tmp_filename); From 18b8e7912fcf90d03e1e825c11b93ffa27313bdf Mon Sep 17 00:00:00 2001 From: jgen Date: Tue, 1 Apr 2014 21:42:40 -0400 Subject: [PATCH 4/6] Fix warning generated when an image doesn't have bits or channel info (and use sane defaults). --- ext/resize/main.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/ext/resize/main.php b/ext/resize/main.php index 3f015270..008fe894 100644 --- a/ext/resize/main.php +++ b/ext/resize/main.php @@ -192,7 +192,18 @@ class ResizeImage extends Extension { The factor of 2.5 is simply a rough guideline. http://stackoverflow.com/questions/527532/reasonable-php-memory-limit-for-image-resize */ - $memory_use = ($info[0] * $info[1] * ($info['bits'] / 8) * $info['channels'] * 2.5) / 1024; + + if (isset($info['bits']) && isset($info['channels'])) + { + $memory_use = ($info[0] * $info[1] * ($info['bits'] / 8) * $info['channels'] * 2.5) / 1024; + } else { + // + // If we don't have bits and channel info from the image then assume default values + // of 8 bits per color and 4 channels (R,G,B,A) -- ie: regular 24-bit color + // + $memory_use = ($info[0] * $info[1] * 1 * 4 * 2.5) / 1024; + } + $memory_limit = get_memory_limit(); if ($memory_use > $memory_limit) { From 9b1c3db40036a613b9a72c46fc316eaf8d33e370 Mon Sep 17 00:00:00 2001 From: jgen Date: Tue, 1 Apr 2014 21:43:12 -0400 Subject: [PATCH 5/6] Slight re-word of the exception error message. --- ext/resize/main.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/resize/main.php b/ext/resize/main.php index 008fe894..75ec896f 100644 --- a/ext/resize/main.php +++ b/ext/resize/main.php @@ -167,7 +167,7 @@ class ResizeImage extends Extension { $image_obj = Image::by_id($image_id); $hash = $image_obj->hash; if (is_null($hash)) { - throw new ImageResizeException("Image does not have a hash associated with it."); + throw new ImageResizeException("Image does not have a hash associated with it - Aborting Resize."); } $image_filename = warehouse_path("images", $hash); @@ -177,7 +177,7 @@ class ResizeImage extends Extension { $filetype = strtolower($pathinfo['extension']); if (($image_obj->width != $info[0] ) || ($image_obj->height != $info[1])) { - throw new ImageResizeException("The image size does not match what is in the database! - Aborting Resize."); + throw new ImageResizeException("The current image size does not match what is set in the database! - Aborting Resize."); } /* From 1e49cc91228fc4de0692f52d07290a4d8aa55d65 Mon Sep 17 00:00:00 2001 From: jgen Date: Tue, 1 Apr 2014 22:02:36 -0400 Subject: [PATCH 6/6] More comments and fix undefined variables. --- ext/resize/main.php | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/ext/resize/main.php b/ext/resize/main.php index 75ec896f..72579648 100644 --- a/ext/resize/main.php +++ b/ext/resize/main.php @@ -230,10 +230,10 @@ class ResizeImage extends Extension { case IMAGETYPE_JPEG: $image = imagecreatefromjpeg($image_filename); break; case IMAGETYPE_PNG: $image = imagecreatefrompng($image_filename); break; default: - throw new ImageResizeException("Unsupported image type."); + throw new ImageResizeException("Unsupported image type (Only GIF, JPEG, and PNG are supported)."); } - /* Resize and resample the image */ + // Handle transparent images $image_resized = imagecreatetruecolor( $new_width, $new_height ); @@ -246,7 +246,7 @@ class ResizeImage extends Extension { $transparent_color = imagecolorsforindex($image, $transparency); // Allocate the same color in the new image resource - $transparency = imagecolorallocate($image_resized, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']); + $transparency = imagecolorallocate($image_resized, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']); // Completely fill the background of the new image with allocated color. imagefill($image_resized, 0, 0, $transparency); @@ -261,10 +261,10 @@ class ResizeImage extends Extension { imagealphablending($image_resized, false); imagesavealpha($image_resized, true); $transparent_color = imagecolorallocatealpha($image_resized, 255, 255, 255, 127); - imagefilledrectangle($image_resized, 0, 0, $width, $height, $transparent_color); + imagefilledrectangle($image_resized, 0, 0, $new_width, $new_height, $transparent_color); } - // Resize the image. + // Actually resize the image. imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $new_width, $new_height, $image_obj->width, $image_obj->height); /* Temp storage while we resize */ @@ -273,6 +273,15 @@ class ResizeImage extends Extension { throw new ImageResizeException("Unable to save temporary image file."); } + /* Output to the same format as the original image */ + switch ( $info[2] ) { + case IMAGETYPE_GIF: imagegif($image_resized, $tmp_filename); break; + case IMAGETYPE_JPEG: imagejpeg($image_resized, $tmp_filename); break; + case IMAGETYPE_PNG: imagepng($image_resized, $tmp_filename); break; + default: + throw new ImageResizeException("Failed to save the new image - Unsupported image type."); + } + /* Move the new image into the main storage location */ $new_hash = md5_file($tmp_filename); $new_size = filesize($tmp_filename); @@ -308,4 +317,4 @@ class ResizeImage extends Extension { log_info("resize", "Resized Image #{$image_id} - New hash: {$new_hash}"); } } -?> +?> \ No newline at end of file