From 3412e50b54e3daac8745234e21ab6e72be0ed165 Mon Sep 17 00:00:00 2001
From: Thomas Bruederli <thomas@roundcube.net>
Date: Wed, 04 Jun 2014 11:20:33 -0400
Subject: [PATCH] Fix attachment menu structure and aria-attributes

---
 program/lib/Roundcube/rcube_image.php |  143 ++++++++++++++++++++++++++++++++---------------
 1 files changed, 98 insertions(+), 45 deletions(-)

diff --git a/program/lib/Roundcube/rcube_image.php b/program/lib/Roundcube/rcube_image.php
index b72a24c..4e4caae 100644
--- a/program/lib/Roundcube/rcube_image.php
+++ b/program/lib/Roundcube/rcube_image.php
@@ -2,8 +2,6 @@
 
 /*
  +-----------------------------------------------------------------------+
- | program/include/rcube_image.php                                       |
- |                                                                       |
  | This file is part of the Roundcube Webmail client                     |
  | Copyright (C) 2005-2012, The Roundcube Dev Team                       |
  | Copyright (C) 2011-2012, Kolab Systems AG                             |
@@ -14,7 +12,6 @@
  |                                                                       |
  | PURPOSE:                                                              |
  |   Image resizer and converter                                         |
- |                                                                       |
  +-----------------------------------------------------------------------+
  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
  | Author: Aleksander Machniak <alec@alec.pl>                            |
@@ -80,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
@@ -95,6 +93,10 @@
         $convert = $rcube->config->get('im_convert_path', false);
         $props   = $this->props();
 
+        if (empty($props)) {
+            return false;
+        }
+
         if (!$filename) {
             $filename = $this->image_file;
         }
@@ -103,7 +105,6 @@
         if ($convert) {
             $p['out']  = $filename;
             $p['in']   = $this->image_file;
-            $p['size'] = $size.'x'.$size;
             $type      = $props['type'];
 
             if (!$type && ($data = $this->identify())) {
@@ -118,62 +119,105 @@
                 $type = 'jpg';
             }
 
-            $p += array('type' => $type, 'types' => "bmp,eps,gif,jp2,jpg,png,svg,tif", 'quality' => 75);
-            $p['-opts'] = array('-resize' => $p['size'].'>');
+            // If only one dimension is greater than the limit convert doesn't
+            // work as expected, we need to calculate new dimensions
+            $scale = $size / max($props['width'], $props['height']);
 
-            if (in_array($type, explode(',', $p['types']))) { // Valid type?
-                $result = rcube::exec($convert . ' 2>&1 -flatten -auto-orient -colorspace RGB -quality {quality} {-opts} {intype}:{in} {type}:{out}', $p);
+            // if file is smaller than the limit, we do nothing
+            // but copy original file to destination file
+            if ($scale >= 1 && $p['intype'] == $type) {
+                $result = ($this->image_file == $filename || copy($this->image_file, $filename)) ? '' : false;
+            }
+            else {
+                if ($scale >= 1) {
+                    $width  = $props['width'];
+                    $height = $props['height'];
+                }
+                else {
+                    $width  = intval($props['width']  * $scale);
+                    $height = intval($props['height'] * $scale);
+                }
+
+                $valid_types = "bmp,eps,gif,jp2,jpg,png,svg,tif";
+
+                $p += array(
+                    'type'    => $type,
+                    'quality' => 75,
+                    'size'    => $width . 'x' . $height,
+                );
+
+                if (in_array($type, explode(',', $valid_types))) { // Valid type?
+                    $result = rcube::exec($convert . ' 2>&1 -flatten -auto-orient -colorspace sRGB -strip'
+                        . ' -quality {quality} -resize {size} {intype}:{in} {type}:{out}', $p);
+                }
             }
 
             if ($result === '') {
+                @chmod($filename, 0600);
                 return $type;
             }
         }
 
         // use GD extension
-        $gd_types = array(IMAGETYPE_JPEG, IMAGETYPE_GIF, IMAGETYPE_PNG);
-        if ($props['gd_type'] && in_array($props['gd_type'], $gd_types)) {
-            if ($props['gd_type'] == IMAGETYPE_JPEG) {
+        if ($props['gd_type']) {
+            if ($props['gd_type'] == IMAGETYPE_JPEG && function_exists('imagecreatefromjpeg')) {
                 $image = imagecreatefromjpeg($this->image_file);
+                $type  = 'jpg';
             }
-            elseif($props['gd_type'] == IMAGETYPE_GIF) {
+            else if($props['gd_type'] == IMAGETYPE_GIF && function_exists('imagecreatefromgif')) {
                 $image = imagecreatefromgif($this->image_file);
+                $type  = 'gid';
             }
-            elseif($props['gd_type'] == IMAGETYPE_PNG) {
+            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']);
-            $width  = $props['width']  * $scale;
-            $height = $props['height'] * $scale;
-
-            $new_image = imagecreatetruecolor($width, $height);
-
-            // Fix transparency of gif/png image
-            if ($props['gd_type'] != IMAGETYPE_JPEG) {
-                imagealphablending($new_image, false);
-                imagesavealpha($new_image, true);
-                $transparent = imagecolorallocatealpha($new_image, 255, 255, 255, 127);
-                imagefilledrectangle($new_image, 0, 0, $width, $height, $transparent);
+            if ($image === false) {
+                return false;
             }
 
-            imagecopyresampled($new_image, $image, 0, 0, 0, 0, $width, $height, $props['width'], $props['height']);
-            $image = $new_image;
+            $scale = $size / max($props['width'], $props['height']);
 
-            if ($props['gd_type'] == IMAGETYPE_JPEG) {
-                $result = imagejpeg($image, $filename, 75);
-                $type = 'jpg';
+            // 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) {
+                $result = $this->image_file == $filename || copy($this->image_file, $filename);
             }
-            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';
+            else {
+                $width     = intval($props['width']  * $scale);
+                $height    = intval($props['height'] * $scale);
+                $new_image = imagecreatetruecolor($width, $height);
+
+                // Fix transparency of gif/png image
+                if ($props['gd_type'] != IMAGETYPE_JPEG) {
+                    imagealphablending($new_image, false);
+                    imagesavealpha($new_image, true);
+                    $transparent = imagecolorallocatealpha($new_image, 255, 255, 255, 127);
+                    imagefilledrectangle($new_image, 0, 0, $width, $height, $transparent);
+                }
+
+                imagecopyresampled($new_image, $image, 0, 0, 0, 0, $width, $height, $props['width'], $props['height']);
+                $image = $new_image;
+
+                if ($props['gd_type'] == IMAGETYPE_JPEG) {
+                    $result = imagejpeg($image, $filename, 75);
+                }
+                elseif($props['gd_type'] == IMAGETYPE_GIF) {
+                    $result = imagegif($image, $filename);
+                }
+                elseif($props['gd_type'] == IMAGETYPE_PNG) {
+                    $result = imagepng($image, $filename, 6, PNG_ALL_FILTERS);
+                }
             }
 
             if ($result) {
+                @chmod($filename, 0600);
                 return $type;
             }
         }
@@ -211,26 +255,30 @@
             $p['out']  = $filename;
             $p['type'] = self::$extensions[$type];
 
-            $result = rcube::exec($convert . ' 2>&1 -colorspace RGB -quality 75 {in} {type}:{out}', $p);
+            $result = rcube::exec($convert . ' 2>&1 -colorspace sRGB -strip -quality 75 {in} {type}:{out}', $p);
 
             if ($result === '') {
+                @chmod($filename, 0600);
                 return true;
             }
         }
 
         // use GD extension (TIFF isn't supported)
-        $props    = $this->props();
-        $gd_types = array(IMAGETYPE_JPEG, IMAGETYPE_GIF, IMAGETYPE_PNG);
+        $props = $this->props();
 
-        if ($props['gd_type'] && in_array($props['gd_type'], $gd_types)) {
-            if ($props['gd_type'] == IMAGETYPE_JPEG) {
+        if ($props['gd_type']) {
+            if ($props['gd_type'] == IMAGETYPE_JPEG && function_exists('imagecreatefromjpeg')) {
                 $image = imagecreatefromjpeg($this->image_file);
             }
-            else if ($props['gd_type'] == IMAGETYPE_GIF) {
+            else if ($props['gd_type'] == IMAGETYPE_GIF && function_exists('imagecreatefromgif')) {
                 $image = imagecreatefromgif($this->image_file);
             }
-            else if ($props['gd_type'] == IMAGETYPE_PNG) {
+            else if ($props['gd_type'] == IMAGETYPE_PNG && function_exists('imagecreatefrompng')) {
                 $image = imagecreatefrompng($this->image_file);
+            }
+            else {
+                // @TODO: print error to the log?
+                return false;
             }
 
             if ($type == self::TYPE_JPG) {
@@ -242,6 +290,11 @@
             else if ($type == self::TYPE_PNG) {
                 $result = imagepng($image, $filename, 6, PNG_ALL_FILTERS);
             }
+
+            if ($result) {
+                @chmod($filename, 0600);
+                return true;
+            }
         }
 
         // @TODO: print error to the log?

--
Gitblit v1.9.1