From dedf13879560006118864abd807e6165d7b62b3a Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Tue, 05 Mar 2013 04:26:29 -0500
Subject: [PATCH] Fix thumbnail size when GD extension is used for image resize (#1488985)

---
 CHANGELOG                             |    1 +
 program/steps/mail/get.inc            |   10 +++++-----
 program/lib/Roundcube/rcube_image.php |   23 ++++++++++++++++++-----
 3 files changed, 24 insertions(+), 10 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index 74c0b25..acaa661 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,6 +1,7 @@
 CHANGELOG Roundcube Webmail
 ===========================
 
+- Fix thumbnail size when GD extension is used for image resize (#1488985)
 - Display notice that message is encrypted also for application/pkcs7-mime messages (#1488526)
 
 RELEASE 0.9-rc
diff --git a/program/lib/Roundcube/rcube_image.php b/program/lib/Roundcube/rcube_image.php
index 9695022..a55ba16 100644
--- a/program/lib/Roundcube/rcube_image.php
+++ b/program/lib/Roundcube/rcube_image.php
@@ -77,7 +77,8 @@
     }
 
     /**
-     * Resize image to a given size
+     * Resize image to a given size. Use only to shrink an image.
+     * If an image is smaller than specified size it will be not resized.
      *
      * @param int    $size      Max width/height size
      * @param string $filename  Output filename
@@ -131,19 +132,30 @@
         if ($props['gd_type']) {
             if ($props['gd_type'] == IMAGETYPE_JPEG && function_exists('imagecreatefromjpeg')) {
                 $image = imagecreatefromjpeg($this->image_file);
+                $type  = 'jpg';
             }
             else if($props['gd_type'] == IMAGETYPE_GIF && function_exists('imagecreatefromgif')) {
                 $image = imagecreatefromgif($this->image_file);
+                $type  = 'gid';
             }
             else if($props['gd_type'] == IMAGETYPE_PNG && function_exists('imagecreatefrompng')) {
                 $image = imagecreatefrompng($this->image_file);
+                $type  = 'png';
             }
             else {
                 // @TODO: print error to the log?
                 return false;
             }
 
-            $scale  = $size / max($props['width'], $props['height']);
+            $scale = $size / max($props['width'], $props['height']);
+
+            // Imagemagick resize is implemented in shrinking mode (see -resize argument above)
+            // we do the same here, if an image is smaller than specified size
+            // we do nothing but copy original file to destination file
+            if ($scale > 1) {
+                return $this->image_file == $filename || copy($this->image_file, $filename) ? $type : false;
+            }
+
             $width  = $props['width']  * $scale;
             $height = $props['height'] * $scale;
 
@@ -162,15 +174,12 @@
 
             if ($props['gd_type'] == IMAGETYPE_JPEG) {
                 $result = imagejpeg($image, $filename, 75);
-                $type = 'jpg';
             }
             elseif($props['gd_type'] == IMAGETYPE_GIF) {
                 $result = imagegif($image, $filename);
-                $type = 'gid';
             }
             elseif($props['gd_type'] == IMAGETYPE_PNG) {
                 $result = imagepng($image, $filename, 6, PNG_ALL_FILTERS);
-                $type = 'png';
             }
 
             if ($result) {
@@ -245,6 +254,10 @@
             else if ($type == self::TYPE_PNG) {
                 $result = imagepng($image, $filename, 6, PNG_ALL_FILTERS);
             }
+
+            if ($result) {
+                return true;
+            }
         }
 
         // @TODO: print error to the log?
diff --git a/program/steps/mail/get.inc b/program/steps/mail/get.inc
index 54d1a4e..23dc22b 100644
--- a/program/steps/mail/get.inc
+++ b/program/steps/mail/get.inc
@@ -60,11 +60,11 @@
   $pid = get_input_value('_part', RCUBE_INPUT_GET);
   if ($part = $MESSAGE->mime_parts[$pid]) {
     $thumbnail_size = $RCMAIL->config->get('image_thumbnail_size', 240);
-    $temp_dir = $RCMAIL->config->get('temp_dir');
-    list(,$ext) = explode('/', $part->mimetype);
+    $temp_dir       = $RCMAIL->config->get('temp_dir');
+    list(,$ext)     = explode('/', $part->mimetype);
     $cache_basename = $temp_dir . '/' . md5($MESSAGE->headers->messageID . $part->mime_id . ':' . $RCMAIL->user->ID . ':' . $thumbnail_size);
-    $cache_file = $cache_basename . '.' . $ext;
-    $mimetype = $part->mimetype;
+    $cache_file     = $cache_basename . '.' . $ext;
+    $mimetype       = $part->mimetype;
 
     // render thumbnail image if not done yet
     if (!is_file($cache_file)) {
@@ -73,7 +73,7 @@
       fclose($fp);
 
       $image = new rcube_image($orig_name);
-      if ($imgtype = $image->resize($RCMAIL->config->get('image_thumbnail_size', 240), $cache_file, true)) {
+      if ($imgtype = $image->resize($thumbnail_size, $cache_file, true)) {
         $mimetype = 'image/' . $imgtype;
         unlink($orig_name);
       }

--
Gitblit v1.9.1