Changeset 19cc5b9 in github for program/include/rcube_image.php


Ignore:
Timestamp:
May 30, 2012 5:22:18 AM (12 months ago)
Author:
Aleksander Machniak <alec@…>
Branches:
master, HEAD, dev-browser-capabilities, pdo
Children:
d901205
Parents:
b9854b8
Message:

Display Tiff as Jpeg in browsers without Tiff support (#1488452)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • program/include/rcube_image.php

    r041c93c r19cc5b9  
    1414 |                                                                       | 
    1515 | PURPOSE:                                                              | 
    16  |   Image resizer                                                       | 
     16 |   Image resizer and converter                                         | 
    1717 |                                                                       | 
    1818 +-----------------------------------------------------------------------+ 
     
    2525{ 
    2626    private $image_file; 
     27 
     28    const TYPE_GIF = 1; 
     29    const TYPE_JPG = 2; 
     30    const TYPE_PNG = 3; 
     31    const TYPE_TIF = 4; 
     32 
     33    public static $extensions = array( 
     34        self::TYPE_GIF => 'gif', 
     35        self::TYPE_JPG => 'jpg', 
     36        self::TYPE_PNG => 'png', 
     37        self::TYPE_TIF => 'tif', 
     38    ); 
     39 
    2740 
    2841    function __construct($filename) 
     
    6780     * @param string $filename  Output filename 
    6881     * 
    69      * @return Success of convert as true/false 
     82     * @return bool True on success, False on failure 
    7083     */ 
    7184    public function resize($size, $filename = null) 
     
    96109 
    97110            if (in_array($type, explode(',', $p['types']))) { // Valid type? 
    98                 $result = rcube::exec($convert . ' 2>&1 -flatten -auto-orient -colorspace RGB -quality {quality} {-opts} {in} {type}:{out}', $p) === ''; 
    99             } 
    100  
    101             if ($result) { 
     111                $result = rcube::exec($convert . ' 2>&1 -flatten -auto-orient -colorspace RGB -quality {quality} {-opts} {in} {type}:{out}', $p); 
     112            } 
     113 
     114            if ($result === '') { 
    102115                return true; 
    103116            } 
     
    149162        } 
    150163 
     164        // @TODO: print error to the log? 
     165        return false; 
     166    } 
     167 
     168    /** 
     169     * Convert image to a given type 
     170     * 
     171     * @param int    $type      Destination file type (see class constants) 
     172     * @param string $filename  Output filename (if empty, original file will be used 
     173     *                          and filename extension will be modified) 
     174     * 
     175     * @return bool True on success, False on failure 
     176     */ 
     177    public function convert($type, $filename = null) 
     178    { 
     179        $rcube   = rcube::get_instance(); 
     180        $convert = $rcube->config->get('im_convert_path', false); 
     181 
     182        if (!$filename) { 
     183            $filename = $this->image_file; 
     184 
     185            // modify extension 
     186            if ($extension = self::$extensions[$type]) { 
     187                $filename = preg_replace('/\.[^.]+$/', '', $filename) . '.' . $extension; 
     188            } 
     189        } 
     190 
     191        // use ImageMagick 
     192        if ($convert) { 
     193            $p['in']   = $this->image_file; 
     194            $p['out']  = $filename; 
     195            $p['type'] = self::$extensions[$type]; 
     196 
     197            $result = rcube::exec($convert . ' 2>&1 -colorspace RGB -quality 75 {in} {type}:{out}', $p); 
     198 
     199            if ($result === '') { 
     200                return true; 
     201            } 
     202        } 
     203 
     204        // use GD extension (TIFF isn't supported) 
     205        $props    = $this->props(); 
     206        $gd_types = array(IMAGETYPE_JPEG, IMAGETYPE_GIF, IMAGETYPE_PNG); 
     207 
     208        if ($props['gd_type'] && in_array($props['gd_type'], $gd_types)) { 
     209            if ($props['gd_type'] == IMAGETYPE_JPEG) { 
     210                $image = imagecreatefromjpeg($this->image_file); 
     211            } 
     212            else if ($props['gd_type'] == IMAGETYPE_GIF) { 
     213                $image = imagecreatefromgif($this->image_file); 
     214            } 
     215            else if ($props['gd_type'] == IMAGETYPE_PNG) { 
     216                $image = imagecreatefrompng($this->image_file); 
     217            } 
     218 
     219            if ($type == self::TYPE_JPG) { 
     220                $result = imagejpeg($image, $filename, 75); 
     221            } 
     222            else if ($type == self::TYPE_GIF) { 
     223                $result = imagegif($image, $filename); 
     224            } 
     225            else if ($type == self::TYPE_PNG) { 
     226                $result = imagepng($image, $filename, 6, PNG_ALL_FILTERS); 
     227            } 
     228        } 
    151229 
    152230        // @TODO: print error to the log? 
     
    170248        } 
    171249    } 
     250 
    172251} 
Note: See TracChangeset for help on using the changeset viewer.