Changeset 19cc5b9 in github


Ignore:
Timestamp:
May 30, 2012 5:22:18 AM (13 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)

Files:
5 edited

Legend:

Unmodified
Added
Removed
  • CHANGELOG

    rb9854b8 r19cc5b9  
    22=========================== 
    33 
     4- Display Tiff as Jpeg in browsers without Tiff support (#1488452) 
    45- Don't display Pdf/Tiff/Flash attachments inline without browser support (#1488452, #1487929) 
    56- Fix html2text conversion of strong|b|a|th|h tags when used in upper case 
  • 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} 
  • program/steps/mail/func.inc

    r7c1231a r19cc5b9  
    10981098  } 
    10991099 
    1100   // Skip TIFF images if browser doesn't support this format 
    1101   // @TODO: we could convert TIFF to JPEG and display it 
    1102   $tiff_support = !empty($_SESSION['browser_caps']) && !empty($_SESSION['browser_caps']['tif']); 
    1103   $mime_regex   = $tiff_support ? '/^image\//i' : '/^image\/(?!tif)/i'; 
    1104   $ext_regex    = '/\.(jpg|jpeg|png|gif|bmp' . ($tiff_support ? '|tif|tiff' : '') .')$/i'; 
    1105  
    11061100  // list images after mail body 
    11071101  if ($CONFIG['inline_images'] && !empty($MESSAGE->attachments)) { 
     
    11131107 
    11141108      // Content-Type: image/*... 
    1115       if (preg_match($mime_regex, $attach_prop->mimetype) || 
    1116         // ...or known file extension: many clients are using application/octet-stream 
    1117         ($attach_prop->filename && 
    1118           preg_match('/^application\/octet-stream$/i', $attach_prop->mimetype) && 
    1119           preg_match($ext_regex, $attach_prop->filename)) 
    1120       ) { 
     1109      if (rcmail_part_image_type($attach_prop)) { 
    11211110        $out .= html::tag('hr') . html::p(array('align' => "center"), 
    11221111          html::img(array( 
     
    11361125} 
    11371126 
     1127function rcmail_part_image_type($part) 
     1128{ 
     1129  $rcmail = rcmail::get_instance(); 
     1130 
     1131  // Skip TIFF images if browser doesn't support this format... 
     1132  $tiff_support = !empty($_SESSION['browser_caps']) && !empty($_SESSION['browser_caps']['tif']); 
     1133  // until we can convert them to JPEG 
     1134  $tiff_support = $tiff_support || $rcmail->config->get('im_convert_path'); 
     1135 
     1136  // Content-type regexp 
     1137  $mime_regex = $tiff_support ? '/^image\//i' : '/^image\/(?!tif)/i'; 
     1138 
     1139  // Content-Type: image/*... 
     1140  if (preg_match($mime_regex, $part->mimetype)) { 
     1141    return $part->mimetype; 
     1142  } 
     1143 
     1144  // Many clients use application/octet-stream, we'll detect mimetype 
     1145  // by checking filename extension 
     1146 
     1147  // Supported image filename extensions to image type map 
     1148  $types = array( 
     1149    'jpg'  => 'image/jpeg', 
     1150    'jpeg' => 'image/jpeg', 
     1151    'png'  => 'image/png', 
     1152    'gif'  => 'image/gif', 
     1153    'bmp'  => 'image/bmp', 
     1154  ); 
     1155  if ($tiff_support) { 
     1156    $types['tif']  = 'image/tiff'; 
     1157    $types['tiff'] = 'image/tiff'; 
     1158  } 
     1159 
     1160  if ($part->filename 
     1161    && preg_match('/^application\/octet-stream$/i', $part->mimetype) 
     1162    && preg_match('/\.([^.])$/i', $part->filename, $m) 
     1163    && ($extension = strtolower($m[1])) 
     1164    && isset($types[$extension]) 
     1165  ) { 
     1166    return $types[$extension]; 
     1167  } 
     1168} 
    11381169 
    11391170/** 
  • program/steps/mail/get.inc

    r041c93c r19cc5b9  
    2323// show loading page 
    2424if (!empty($_GET['_preload'])) { 
    25   $url = preg_replace('/[&?]+_preload=1/', '', $_SERVER['REQUEST_URI']); 
     25  $url = preg_replace('/([&?]+)_preload=/', '\\1_embed=', $_SERVER['REQUEST_URI']); 
    2626  $message = rcube_label('loadingdata'); 
    2727 
     
    7777    // overwrite modified vars from plugin 
    7878    $mimetype = $plugin['mimetype']; 
     79 
     80    // TIFF to JPEG conversion, if needed 
     81    $tiff_support = !empty($_SESSION['browser_caps']) && !empty($_SESSION['browser_caps']['tif']); 
     82    if (!empty($_REQUEST['_embed']) && !$tiff_support 
     83      && $RCMAIL->config->get('im_convert_path') 
     84      && rcmail_part_image_type($part) == 'image/tiff' 
     85    ) { 
     86      $tiff2jpeg = true; 
     87      $mimetype = 'image/jpeg'; 
     88    } 
     89 
    7990    list($ctype_primary, $ctype_secondary) = explode('/', $mimetype); 
    8091    if ($plugin['body']) 
     
    151162      header("Content-Disposition: $disposition; filename=\"$filename\""); 
    152163 
     164      // handle tiff to jpeg conversion 
     165      if (!empty($tiff2jpeg)) { 
     166        $temp_dir  = unslashify($RCMAIL->config->get('temp_dir')); 
     167        $file_path = tempnam($temp_dir, 'rcmAttmnt'); 
     168 
     169        // write content to temp file 
     170        if ($part->body) { 
     171          $saved = file_put_contents($file_path, $part->body); 
     172        } 
     173        else if ($part->size) { 
     174          $fd = fopen($file_path, 'w'); 
     175          $saved = $RCMAIL->storage->get_message_part($MESSAGE->uid, $part->mime_id, $part, false, $fd); 
     176          fclose($fd); 
     177        } 
     178 
     179        // convert image to jpeg and send it to the browser 
     180        if ($saved) { 
     181          $image = new rcube_image($file_path); 
     182          if ($image->convert(rcube_image::TYPE_JPG, $file_path)) { 
     183            header("Content-Length: " . filesize($file_path)); 
     184            readfile($file_path); 
     185          } 
     186          unlink($file_path); 
     187        } 
     188      } 
    153189      // do content filtering to avoid XSS through fake images 
    154       if (!empty($_REQUEST['_embed']) && $browser->ie && $browser->ver <= 8) { 
     190      else if (!empty($_REQUEST['_embed']) && $browser->ie && $browser->ver <= 8) { 
    155191        if ($part->body) { 
    156192          echo preg_match('/<(script|iframe|object)/i', $part->body) ? '' : $part->body; 
  • program/steps/mail/show.inc

    rb9854b8 r19cc5b9  
    6262 
    6363  // mimetypes supported by the browser (default settings) 
    64   $mimetypes = $RCMAIL->config->get('client_mimetypes', 'text/plain,text/html,text/xml,image/jpeg,image/gif,image/png,image/tiff,application/x-javascript,application/pdf,application/x-shockwave-flash'); 
     64  $mimetypes = $RCMAIL->config->get('client_mimetypes', 'text/plain,text/html,text/xml,image/jpeg,image/gif,image/png,image/bmp,image/tiff,application/x-javascript,application/pdf,application/x-shockwave-flash'); 
    6565  $mimetypes = is_string($mimetypes) ? explode(',', $mimetypes) : (array)$mimetypes; 
    6666 
     
    7373    unset($mimetypes[$key]); 
    7474  } 
    75   // @TODO: we could convert TIFF to JPEG and display it 
    7675  if (empty($_SESSION['browser_caps']['tif']) && ($key = array_search('image/tiff', $mimetypes)) !== false) { 
    77     unset($mimetypes[$key]); 
     76    // we can convert tiff to jpeg 
     77    if (!$RCMAIL->config->get('im_convert_path')) { 
     78      unset($mimetypes[$key]); 
     79    } 
    7880  } 
    7981 
Note: See TracChangeset for help on using the changeset viewer.