summaryrefslogtreecommitdiff
path: root/program/include/rcube_image.php
diff options
context:
space:
mode:
authorAleksander Machniak <alec@alec.pl>2012-05-30 11:22:18 +0200
committerAleksander Machniak <alec@alec.pl>2012-05-30 11:22:18 +0200
commit19cc5b91c963736fd06dd701d4a583ccdf4b5bfa (patch)
tree22730a489c8821a01aa235074e86f13cf735f29e /program/include/rcube_image.php
parentb9854b8cbecb21ada9ba4f25090d53f520ac6e9e (diff)
Display Tiff as Jpeg in browsers without Tiff support (#1488452)
Diffstat (limited to 'program/include/rcube_image.php')
-rw-r--r--program/include/rcube_image.php87
1 files changed, 83 insertions, 4 deletions
diff --git a/program/include/rcube_image.php b/program/include/rcube_image.php
index 21cb5f136..80e8bd4f3 100644
--- a/program/include/rcube_image.php
+++ b/program/include/rcube_image.php
@@ -13,7 +13,7 @@
| See the README file for a full license statement. |
| |
| PURPOSE: |
- | Image resizer |
+ | Image resizer and converter |
| |
+-----------------------------------------------------------------------+
| Author: Thomas Bruederli <roundcube@gmail.com> |
@@ -25,6 +25,19 @@ 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)
{
$this->image_file = $filename;
@@ -66,7 +79,7 @@ class rcube_image
* @param int $size Max width/height size
* @param string $filename Output filename
*
- * @return Success of convert as true/false
+ * @return bool True on success, False on failure
*/
public function resize($size, $filename = null)
{
@@ -95,10 +108,10 @@ class rcube_image
$p['-opts'] = array('-resize' => $size.'>');
if (in_array($type, explode(',', $p['types']))) { // Valid type?
- $result = rcube::exec($convert . ' 2>&1 -flatten -auto-orient -colorspace RGB -quality {quality} {-opts} {in} {type}:{out}', $p) === '';
+ $result = rcube::exec($convert . ' 2>&1 -flatten -auto-orient -colorspace RGB -quality {quality} {-opts} {in} {type}:{out}', $p);
}
- if ($result) {
+ if ($result === '') {
return true;
}
}
@@ -148,6 +161,71 @@ class rcube_image
}
}
+ // @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;
@@ -169,4 +247,5 @@ class rcube_image
}
}
}
+
}