Aleksander Machniak
2015-06-17 3f4521bcf4b538b6ac54817cfad22b51e347546d
program/lib/Roundcube/rcube_image.php
@@ -59,11 +59,13 @@
            $height  = $imsize[1];
            $gd_type = $imsize['2'];
            $type    = image_type_to_extension($imsize['2'], false);
            $channels = $imsize['channels'];
        }
        // use ImageMagick
        if (!$type && ($data = $this->identify())) {
            list($type, $width, $height) = $data;
            $channels = null;
        }
        if ($type) {
@@ -72,8 +74,11 @@
                'gd_type' => $gd_type,
                'width'   => $width,
                'height'  => $height,
                'channels' => $channels,
            );
        }
        return null;
    }
    /**
@@ -102,10 +107,10 @@
        }
        // use Imagemagick
        if ($convert) {
            $p['out']  = $filename;
            $p['in']   = $this->image_file;
            $type      = $props['type'];
        if ($convert || class_exists('Imagick', false)) {
            $p['out'] = $filename;
            $p['in']  = $this->image_file;
            $type     = $props['type'];
            if (!$type && ($data = $this->identify())) {
                $type = $data[0];
@@ -129,26 +134,49 @@
                $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 ($scale >= 1) {
                        $width  = $props['width'];
                        $height = $props['height'];
                    }
                    else {
                        $width  = intval($props['width']  * $scale);
                        $height = intval($props['height'] * $scale);
                    }
                    // use ImageMagick in command line
                    if ($convert) {
                        $p += array(
                            'type'    => $type,
                            'quality' => 75,
                            'size'    => $width . 'x' . $height,
                        );
                        $result = rcube::exec($convert . ' 2>&1 -flatten -auto-orient -colorspace sRGB -strip'
                            . ' -quality {quality} -resize {size} {intype}:{in} {type}:{out}', $p);
                    }
                    // use PHP's Imagick class
                    else {
                        try {
                            $image = new Imagick($this->image_file);
                            $image = $image->flattenImages();
                            $image->setImageColorspace(Imagick::COLORSPACE_SRGB);
                            $image->setImageCompressionQuality(75);
                            $image->setImageFormat($type);
                            $image->stripImage();
                            $image->scaleImage($width, $height);
                            if ($image->writeImage($filename)) {
                                $result = '';
                            }
                        }
                        catch (Exception $e) {
                            rcube::raise_error($e, true, false);
                        }
                    }
                }
            }
@@ -156,6 +184,11 @@
                @chmod($filename, 0600);
                return $type;
            }
        }
        // do we have enough memory? (#1489937)
        if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN' && !$this->mem_check($props)) {
            return false;
        }
        // use GD extension
@@ -166,7 +199,7 @@
            }
            else if($props['gd_type'] == IMAGETYPE_GIF && function_exists('imagecreatefromgif')) {
                $image = imagecreatefromgif($this->image_file);
                $type  = 'gid';
                $type  = 'gif';
            }
            else if($props['gd_type'] == IMAGETYPE_PNG && function_exists('imagecreatefrompng')) {
                $image = imagecreatefrompng($this->image_file);
@@ -204,6 +237,24 @@
                imagecopyresampled($new_image, $image, 0, 0, 0, 0, $width, $height, $props['width'], $props['height']);
                $image = $new_image;
                // fix rotation of image if EXIF data exists and specifies rotation (GD strips the EXIF data)
                if ($this->image_file && $type == 'jpg' && function_exists('exif_read_data')) {
                    $exif = exif_read_data($this->image_file);
                    if ($exif && $exif['Orientation']) {
                        switch ($exif['Orientation']) {
                            case 3:
                                $image = imagerotate($image, 180, 0);
                                break;
                            case 6:
                                $image = imagerotate($image, -90, 0);
                                break;
                            case 8:
                                $image = imagerotate($image, 90, 0);
                                break;
                        }
                    }
                }
                if ($props['gd_type'] == IMAGETYPE_JPEG) {
                    $result = imagejpeg($image, $filename, 75);
@@ -249,7 +300,7 @@
            }
        }
        // use ImageMagick
        // use ImageMagick in command line
        if ($convert) {
            $p['in']   = $this->image_file;
            $p['out']  = $filename;
@@ -258,13 +309,39 @@
            $result = rcube::exec($convert . ' 2>&1 -colorspace sRGB -strip -quality 75 {in} {type}:{out}', $p);
            if ($result === '') {
                @chmod($filename, 0600);
                chmod($filename, 0600);
                return true;
            }
        }
        // use PHP's Imagick class
        if (class_exists('Imagick', false)) {
            try {
                $image = new Imagick($this->image_file);
                $image->setImageColorspace(Imagick::COLORSPACE_SRGB);
                $image->setImageCompressionQuality(75);
                $image->setImageFormat(self::$extensions[$type]);
                $image->stripImage();
                if ($image->writeImage($filename)) {
                    @chmod($filename, 0600);
                    return true;
                }
            }
            catch (Exception $e) {
                rcube::raise_error($e, true, false);
            }
        }
        // use GD extension (TIFF isn't supported)
        $props = $this->props();
        // do we have enough memory? (#1489937)
        if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN' && !$this->mem_check($props)) {
            return false;
        }
        if ($props['gd_type']) {
            if ($props['gd_type'] == IMAGETYPE_JPEG && function_exists('imagecreatefromjpeg')) {
@@ -302,12 +379,26 @@
    }
    /**
     * Identify command handler.
     * Checks if image format conversion is supported
     *
     * @return boolean True if specified format can be converted to another format
     */
    public static function is_convertable($mimetype = null)
    {
        $rcube = rcube::get_instance();
        // @TODO: check if specified mimetype is really supported
        return class_exists('Imagick', false) || $rcube->config->get('im_convert_path');
    }
    /**
     * ImageMagick based image properties read.
     */
    private function identify()
    {
        $rcube = rcube::get_instance();
        // use ImageMagick in command line
        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);
@@ -316,6 +407,37 @@
                return explode(' ', strtolower($id));
            }
        }
        // use PHP's Imagick class
        if (class_exists('Imagick', false)) {
            try {
                $image = new Imagick($this->image_file);
                return array(
                    strtolower($image->getImageFormat()),
                    $image->getImageWidth(),
                    $image->getImageHeight(),
                );
            }
            catch (Exception $e) {}
        }
    }
    /**
     * Check if we have enough memory to load specified image
     */
    private function mem_check($props)
    {
        // image size is unknown, we can't calculate required memory
        if (!$props['width']) {
            return true;
        }
        // channels: CMYK - 4, RGB - 3
        $multip = ($props['channels'] ?: 3) + 1;
        // calculate image size in memory (in bytes)
        $size = $props['width'] * $props['height'] * $multip;
        return rcube_utils::mem_check($size);
    }
}