From be9aacaa5296dfca63fb3a01c2dc52538d1546aa Mon Sep 17 00:00:00 2001
From: Thomas Bruederli <thomas@roundcube.net>
Date: Sat, 17 Nov 2012 12:31:31 -0500
Subject: [PATCH] Bring back lost localization for the about page
---
program/include/rcube_image.php | 161 ++++++++++++++++++++++++++++++++++++++++++++---------
1 files changed, 132 insertions(+), 29 deletions(-)
diff --git a/program/include/rcube_image.php b/program/include/rcube_image.php
index 9fe15fe..b72a24c 100644
--- a/program/include/rcube_image.php
+++ b/program/include/rcube_image.php
@@ -13,20 +13,36 @@
| See the README file for a full license statement. |
| |
| PURPOSE: |
- | Image resizer |
+ | Image resizer and converter |
| |
+-----------------------------------------------------------------------+
| Author: Thomas Bruederli <roundcube@gmail.com> |
| Author: Aleksander Machniak <alec@alec.pl> |
+-----------------------------------------------------------------------+
-
- $Id$
-
*/
+/**
+ * Image resizer and converter
+ *
+ * @package Framework
+ * @subpackage Utils
+ */
class rcube_image
{
private $image_file;
+
+ const TYPE_GIF = 1;
+ const TYPE_JPG = 2;
+ const TYPE_PNG = 3;
+ const TYPE_TIF = 4;
+
+ public static $extensions = array(
+ self::TYPE_GIF => 'gif',
+ self::TYPE_JPG => 'jpg',
+ self::TYPE_PNG => 'png',
+ self::TYPE_TIF => 'tif',
+ );
+
function __construct($filename)
{
@@ -40,8 +56,7 @@
*/
public function props()
{
- $rcmail = rcmail::get_instance();
-
+ // use GD extension
if (function_exists('getimagesize') && ($imsize = @getimagesize($this->image_file))) {
$width = $imsize[0];
$height = $imsize[1];
@@ -49,12 +64,9 @@
$type = image_type_to_extension($imsize['2'], false);
}
- if (!$type && ($cmd = $rcmail->config->get('im_identify_path', false))) {
- $id = rcmail::exec($cmd. ' 2>/dev/null {in}', array('in' => $this->image_file));
- list(, $type, $size) = explode(' ', strtolower($id));
- if ($size) {
- list($width, $height) = explode('x', $size);
- }
+ // use ImageMagick
+ if (!$type && ($data = $this->identify())) {
+ list($type, $width, $height) = $data;
}
if ($type) {
@@ -67,22 +79,21 @@
}
}
-
/**
* Resize image to a given size
*
* @param int $size Max width/height size
* @param string $filename Output filename
+ * @param boolean $browser_compat Convert to image type displayable by any browser
*
- * @return Success of convert as true/false
+ * @return mixed Output type on success, False on failure
*/
- public function resize($size, $filename = null)
+ public function resize($size, $filename = null, $browser_compat = false)
{
- $result = false;
- $rcmail = rcmail::get_instance();
- $convert = $rcmail->config->get('im_convert_path', false);
- $identify = $rcmail->config->get('im_identify_path', false);
- $props = $this->props();
+ $result = false;
+ $rcube = rcube::get_instance();
+ $convert = $rcube->config->get('im_convert_path', false);
+ $props = $this->props();
if (!$filename) {
$filename = $this->image_file;
@@ -93,22 +104,29 @@
$p['out'] = $filename;
$p['in'] = $this->image_file;
$p['size'] = $size.'x'.$size;
- $p['type'] = $type = $props['type'];
+ $type = $props['type'];
- if (!$type) {
- list(, $p['type']) = explode(' ', strtolower(rcmail::exec($identify . ' 2>/dev/null {in}', $p))); // for things like eps
+ if (!$type && ($data = $this->identify())) {
+ $type = $data[0];
}
$type = strtr($type, array("jpeg" => "jpg", "tiff" => "tif", "ps" => "eps", "ept" => "eps"));
- $p += array('type' => $type, 'types' => "bmp,eps,gif,jp2,jpg,png,svg,tif", 'quality' => 75);
- $p['-opts'] = array('-resize' => $size.'>');
+ $p['intype'] = $type;
- if (in_array($type, explode(',', $p['types']))) { // Valid type?
- $result = rcmail::exec($convert . ' 2>&1 -flatten -auto-orient -colorspace RGB -quality {quality} {-opts} {in} {type}:{out}', $p) === '';
+ // convert to an image format every browser can display
+ if ($browser_compat && !in_array($type, array('jpg','gif','png'))) {
+ $type = 'jpg';
}
- if ($result) {
- return true;
+ $p += array('type' => $type, 'types' => "bmp,eps,gif,jp2,jpg,png,svg,tif", 'quality' => 75);
+ $p['-opts'] = array('-resize' => $p['size'].'>');
+
+ 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 ($result === '') {
+ return $type;
}
}
@@ -144,22 +162,107 @@
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) {
+ return $type;
+ }
+ }
+
+ // @TODO: print error to the log?
+ return false;
+ }
+
+ /**
+ * Convert image to a given type
+ *
+ * @param int $type Destination file type (see class constants)
+ * @param string $filename Output filename (if empty, original file will be used
+ * and filename extension will be modified)
+ *
+ * @return bool True on success, False on failure
+ */
+ public function convert($type, $filename = null)
+ {
+ $rcube = rcube::get_instance();
+ $convert = $rcube->config->get('im_convert_path', false);
+
+ if (!$filename) {
+ $filename = $this->image_file;
+
+ // modify extension
+ if ($extension = self::$extensions[$type]) {
+ $filename = preg_replace('/\.[^.]+$/', '', $filename) . '.' . $extension;
+ }
+ }
+
+ // use ImageMagick
+ if ($convert) {
+ $p['in'] = $this->image_file;
+ $p['out'] = $filename;
+ $p['type'] = self::$extensions[$type];
+
+ $result = rcube::exec($convert . ' 2>&1 -colorspace RGB -quality 75 {in} {type}:{out}', $p);
+
+ if ($result === '') {
return true;
}
}
+ // use GD extension (TIFF isn't supported)
+ $props = $this->props();
+ $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) {
+ $image = imagecreatefromjpeg($this->image_file);
+ }
+ else if ($props['gd_type'] == IMAGETYPE_GIF) {
+ $image = imagecreatefromgif($this->image_file);
+ }
+ else if ($props['gd_type'] == IMAGETYPE_PNG) {
+ $image = imagecreatefrompng($this->image_file);
+ }
+
+ if ($type == self::TYPE_JPG) {
+ $result = imagejpeg($image, $filename, 75);
+ }
+ else if ($type == self::TYPE_GIF) {
+ $result = imagegif($image, $filename);
+ }
+ else if ($type == self::TYPE_PNG) {
+ $result = imagepng($image, $filename, 6, PNG_ALL_FILTERS);
+ }
+ }
// @TODO: print error to the log?
return false;
}
+ /**
+ * Identify command handler.
+ */
+ private function identify()
+ {
+ $rcube = rcube::get_instance();
+
+ if ($cmd = $rcube->config->get('im_identify_path')) {
+ $args = array('in' => $this->image_file, 'format' => "%m %[fx:w] %[fx:h]");
+ $id = rcube::exec($cmd. ' 2>/dev/null -format {format} {in}', $args);
+
+ if ($id) {
+ return explode(' ', strtolower($id));
+ }
+ }
+ }
+
}
--
Gitblit v1.9.1