* Implemented a wrapper for executing ImageMagick
authorMichael Stucki <michael.stucki@typo3.org>
Sun, 1 May 2005 17:50:46 +0000 (17:50 +0000)
committerMichael Stucki <michael.stucki@typo3.org>
Sun, 1 May 2005 17:50:46 +0000 (17:50 +0000)
* New feature #0001034: GraphicsMagick finally works with TYPO3! This can be enabled by setting TYPO3_CONF_VARS[GFX][im_version_5] to 'gm'.
* The Install Tool is now more tolerant when comparing image file sizes. Warnings are only displayed if the created image is more than 10K larger than its reference. In this case you should consider to change your IM/GD version...

git-svn-id: https://svn.typo3.org/TYPO3v4/Core/trunk@699 709f56b5-9817-0410-a4d7-c38de5d9e867

ChangeLog
t3lib/class.t3lib_div.php
t3lib/class.t3lib_extmgm.php
t3lib/class.t3lib_htmlmail.php
t3lib/class.t3lib_iconworks.php
t3lib/class.t3lib_stdgraphic.php
t3lib/config_default.php
t3lib/thumbs.php
typo3/sysext/install/mod/class.tx_install.php

index 059cf45..956323b 100755 (executable)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2005-05-01  Michael Stucki  <michael@typo3.org>
+
+       * Implemented a wrapper for executing ImageMagick
+       * New feature #0001034: GraphicsMagick finally works with TYPO3! This can be enabled by setting TYPO3_CONF_VARS[GFX][im_version_5] to 'gm'.
+       * The Install Tool is now more tolerant when comparing image file sizes. Warnings are only displayed if the created image is more than 10K larger than its reference. In this case you should consider to change your IM/GD version...
+
 2005-04-30  Michael Stucki  <michael@typo3.org>
 
        * Changed the layout of the security warning box which is displayed in alt_intro.php
index f669c3f..b16c412 100755 (executable)
@@ -377,8 +377,10 @@ class t3lib_div {
                $gfxConf = $GLOBALS['TYPO3_CONF_VARS']['GFX'];
                $returnCode='';
                if ($gfxConf['gif_compress'] && strtolower(substr($theFile,-4,4))=='.gif')      {       // GIF...
-                       if (($type=='IM' || !$type) && $GLOBALS['TYPO3_CONF_VARS']['GFX']['im'] && $GLOBALS['TYPO3_CONF_VARS']['GFX']['im_path_lzw'])   {       // IM
-                               exec($GLOBALS['TYPO3_CONF_VARS']['GFX']['im_path_lzw'].'convert "'.$theFile.'" "'.$theFile.'"');
+                       if (($type=='IM' || !$type) && $gfxConf['im'] && $gfxConf['im_path_lzw'])       {       // IM
+                               $cmd = t3lib_div::imageMagickCommand('convert', '"'.$theFile.'" "'.$theFile.'"', $gfxConf['im_path_lzw']);
+                               exec($cmd);
+
                                $returnCode='IM';
                        } elseif (($type=='GD' || !$type) && $gfxConf['gdlib'] && !$gfxConf['gdlib_png'])       {       // GD
                                $tempImage = imageCreateFromGif($theFile);
@@ -406,7 +408,8 @@ class t3lib_div {
                        && strtolower(substr($theFile,-4,4))=='.png'
                        && @is_file($theFile))  {       // IM
                                $newFile = substr($theFile,0,-4).'.gif';
-                               exec($GLOBALS['TYPO3_CONF_VARS']['GFX']['im_path_lzw'].'convert "'.$theFile.'" "'.$newFile.'"');
+                               $cmd = t3lib_div::imageMagickCommand('convert', '"'.$theFile.'" "'.$newFile.'"', $gfxConf['im_path_lzw']);
+                               exec($cmd);
                                $theFile = $newFile;
                                        // unlink old file?? May be bad idea bacause TYPO3 would then recreate the file every time as TYPO3 thinks the file is not generated because it's missing!! So do not unlink $theFile here!!
                }
@@ -3787,6 +3790,81 @@ class t3lib_div {
                }
                return $str;
        }
+
+       /**
+        * Compile the command for running ImageMagick/GraphicsMagick.
+        *
+        * @param       string          Command to be run
+        * @param       string          The parameters string
+        * @param       string          Override the default path
+        * @return      string          Compiled command that deals with IM6 & GraphicsMagick
+        */
+       function imageMagickCommand($command, $parameters, $path='')    {
+               $gfxConf = $GLOBALS['TYPO3_CONF_VARS']['GFX'];
+               $isExt = (TYPO3_OS=='WIN' ? '.exe' : '');
+               $switchCompositeParameters=false;
+
+               if(!$path)      { $path = $gfxConf['im_path']; }
+
+               $im_version = strtolower($gfxConf['im_version_5']);
+               $combineScript = $gfxConf['im_combine_filename'] ? trim($gfxConf['im_combine_filename']) : 'combine';
+
+               if($command==='combine')        {       // This is only used internally, has no effect outside
+                       $command = 'composite';
+               }
+
+                       // Compile the path & command
+               if($im_version==='gm')  {
+                       $switchCompositeParameters=true;
+                       $path .= 'gm'.$isExt.' '.$command;
+               } else  {
+                       if($im_version==='im6') { $switchCompositeParameters=true; }
+                       $path .= (($command=='composite') ? $combineScript : $command).$isExt;
+               }
+
+               $cmdLine = $path.' '.$parameters;
+
+               if($command=='composite' && $switchCompositeParameters) {       // Because of some weird incompatibilities between ImageMagick 4 and 6 (plus GraphicsMagick), it is needed to change the parameters order under some preconditions
+                       $paramsArr = t3lib_div::unQuoteFilenames($parameters);
+
+                       if(count($paramsArr)==6)        {       // The mask image has been specified => swap the parameters
+                               $tmp = $paramsArr[3];
+                               $paramsArr[3] = $paramsArr[2];
+                               $paramsArr[2] = $tmp;
+                       }
+
+                       $cmdLine = $path.' '.implode(' ', $paramsArr);
+               }
+
+               return $cmdLine;
+       }
+
+       /**
+        * Explode a string (normally a list of filenames) with whitespaces by considering quotes in that string. This is mostly needed by the imageMagickCommand function above.
+        *
+        * @param       string          The whole parameters string
+        * @return      array           Exploded parameters
+        */
+       function unQuoteFilenames($parameters)  {
+               $paramsArr = explode(' ', trim($parameters));
+
+               $quoteActive = -1;      // Whenever a quote character (") is found, $quoteActive is set to the element number inside of $params. A value of -1 means that there are not open quotes at the current position.
+               foreach($paramsArr as $k=>$v)   {
+                       if($quoteActive > -1)   {
+                               $paramsArr[$quoteActive] .= ' '.$v;
+                               unset($paramsArr[$k]);
+                               if(ereg('"$', $v))      { $quoteActive = -1; }
+
+                       } elseif(!trim($v))     {
+                               unset($paramsArr[$k]);  // Remove empty elements
+
+                       } elseif(ereg('^"', $v))        {
+                               $quoteActive = $k;
+                       }
+               }
+
+               return $paramsArr;
+       }
 }
 
 ?>
\ No newline at end of file
index 908aef2..868df02 100644 (file)
@@ -946,11 +946,11 @@ tt_content.'.$key.$prefix.' {
                                $temp_extensions = array_unique(t3lib_div::trimExplode(',',$rawExtList,1));
                                foreach($temp_extensions as $temp_extKey)       {
                                                // Check local, global and system locations:
-                                       if (@is_dir(PATH_site.'typo3conf/ext/'.$temp_extKey))   {
+                                       if (@is_dir(PATH_site.'typo3conf/ext/'.$temp_extKey.'/'))       {
                                                $extensions[$temp_extKey] = array('type'=>'L', 'siteRelPath'=>'typo3conf/ext/'.$temp_extKey.'/', 'typo3RelPath'=>'../typo3conf/ext/'.$temp_extKey.'/');
-                                       } elseif (@is_dir(PATH_site.TYPO3_mainDir.'ext/'.$temp_extKey)) {
+                                       } elseif (@is_dir(PATH_site.TYPO3_mainDir.'ext/'.$temp_extKey.'/'))     {
                                                $extensions[$temp_extKey] = array('type'=>'G', 'siteRelPath'=>TYPO3_mainDir.'ext/'.$temp_extKey.'/', 'typo3RelPath'=>'ext/'.$temp_extKey.'/');
-                                       } elseif (@is_dir(PATH_site.TYPO3_mainDir.'sysext/'.$temp_extKey))      {
+                                       } elseif (@is_dir(PATH_site.TYPO3_mainDir.'sysext/'.$temp_extKey.'/'))  {
                                                $extensions[$temp_extKey] = array('type'=>'S', 'siteRelPath'=>TYPO3_mainDir.'sysext/'.$temp_extKey.'/', 'typo3RelPath'=>'sysext/'.$temp_extKey.'/');
                                        }
 
index 074ee2a..4270348 100755 (executable)
@@ -403,9 +403,9 @@ class t3lib_htmlmail {
                if ($this->from_email)  {
                        if ($this->from_name)   {
                                $name = $this->convertName($this->from_name);
-                               $this->add_header("From: $name <$this->from_email>");
+                               $this->add_header('From: '.$name.' <'.$this->from_email.'>');
                        } else {
-                               $this->add_header("From: $this->from_email");
+                               $this->add_header('From: '.$this->from_email);
                        }
                }
                        // Reply
index 21f587b..472024a 100755 (executable)
@@ -444,18 +444,15 @@ class t3lib_iconWorks     {
        function imagecopyresized(&$im, $cpImg, $Xstart, $Ystart, $cpImgCutX, $cpImgCutY, $w, $h, $w, $h)       {
                if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_2'] && $GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_png'])   {       // Maybe I'll have to change this if GD2/gif does not work either...
                        if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['im_path'])      {
-                               $cmd=$GLOBALS['TYPO3_CONF_VARS']['GFX']['im_path'].
-                                               ($GLOBALS['TYPO3_CONF_VARS']['GFX']['im_combine_filename']?$GLOBALS['TYPO3_CONF_VARS']['GFX']['im_combine_filename']:'combine').
-                                               ' -compose over ';
+
                                $tempBaseName = PATH_site.'typo3temp/ICRZ_'.md5(uniqid('.'));
 
                                ImagePng($im, $tempBaseName.'_im.png');
                                ImagePng($cpImg, $tempBaseName.'_cpImg.png');
-                               exec($cmd.
-                                       $tempBaseName.'_cpImg.png '.
-                                       $tempBaseName.'_im.png '.
-                                       $tempBaseName.'_out.png '
-                               );
+
+                               $cmd = t3lib_exec::imageMagickCommand('combine', '-compose over '.$tempBaseName.'_cpImg.png '.$tempBaseName.'_im.png '.$tempBaseName.'_out.png ');
+                               exec($cmd);
+
                                $im = imagecreatefrompng($tempBaseName.'_out.png');
                                unlink($tempBaseName.'_im.png');
                                unlink($tempBaseName.'_cpImg.png');
index b373f8b..2cccbad 100644 (file)
@@ -155,7 +155,6 @@ class t3lib_stdGraphic      {
        var $gifExtension = 'gif';                                      // This should be changed to 'png' if you want this class to read/make PNG-files instead!
        var $TTFLocaleConv = '';                                        // Used to recode input to TTF-functions for other charsets.
        var $enable_typo3temp_db_tracking = 0;          // If set, then all files in typo3temp will be logged in a database table. In addition to being a log of the files with original filenames, it also serves to secure that the same image is not rendered simultaneously by two different processes.
-       var $imageMagickPath = '';                                      // path to imageMagick, eg: /usr/lib/
        var $imageFileExt = 'gif,jpg,jpeg,png,tif,bmp,tga,pcx,ai,pdf';  // Commalist of file extensions perceived as images by TYPO3. List should be set to 'gif,png,jpeg,jpg' if IM is not available. Lowercase and no spaces between!
        var $webImageExt = 'gif,jpg,jpeg,png';          // Commalist of web image extensions (can be shown by a webbrowser)
        var $maskNegate = '';                                           // Will be ' -negate' if ImageMagick ver 5.2+. See init();
@@ -245,7 +244,6 @@ class t3lib_stdGraphic      {
                        $this->enable_typo3temp_db_tracking = $gfxConf['enable_typo3temp_db_tracking'];
                }
 
-               $this->imageMagickPath = $gfxConf['im_path'];
                $this->imageFileExt = $gfxConf['imagefile_ext'];
 
                        // This should be set if ImageMagick ver. 5+ is used.
@@ -2341,7 +2339,7 @@ class t3lib_stdGraphic    {
        function imageMagickIdentify($imagefile)        {
                if (!$this->NO_IMAGE_MAGICK)    {
                        $frame = $this->noFramePrepended?'':'[0]';
-                       $cmd = $this->imageMagickPath.'identify '.$this->wrapFileName($imagefile).$frame;
+                       $cmd = t3lib_div::imageMagickCommand('identify', $this->wrapFileName($imagefile).$frame);
                        exec($cmd, $returnVal);
                        $splitstring=$returnVal[0];
                        $this->IM_commands[] = Array ('identify',$cmd,$returnVal[0]);
@@ -2374,7 +2372,7 @@ class t3lib_stdGraphic    {
         */
        function imageMagickExec($input,$output,$params)        {
                if (!$this->NO_IMAGE_MAGICK)    {
-                       $cmd = $this->imageMagickPath.'convert '.$params.' '.$this->wrapFileName($input).' '.$this->wrapFileName($output);
+                       $cmd = t3lib_div::imageMagickCommand('convert', $params.' '.$this->wrapFileName($input).' '.$this->wrapFileName($output));
                        $this->IM_commands[] = Array ($output,$cmd);
 
                        $ret = exec($cmd);
@@ -2396,7 +2394,7 @@ class t3lib_stdGraphic    {
         */
        function combineExec($input,$overlay,$mask,$output)     {
                if (!$this->NO_IMAGE_MAGICK)    {
-                       $cmd = $this->imageMagickPath.$this->combineScript.' -compose over '.$this->wrapFileName($input).' '.$this->wrapFileName($overlay).' '.$this->wrapFileName($mask).' '.$this->wrapFileName($output);
+                       $cmd = t3lib_div::imageMagickCommand('combine', '-compose over '.$this->wrapFileName($input).' '.$this->wrapFileName($overlay).' '.$this->wrapFileName($mask).' '.$this->wrapFileName($output));
                        $this->IM_commands[] = Array ($output,$cmd);
 
                        $ret = exec($cmd);
index 5ad780d..390a3ac 100755 (executable)
@@ -33,7 +33,7 @@ $TYPO3_CONF_VARS = Array(
                'im_path' => '/usr/X11R6/bin/',                 // Path to the IM tools 'convert', 'combine', 'identify'. Version 4.2.9 of ImageMagick is highly recommended due to features and speed!
                'im_path_lzw' => '/usr/bin/',                   // Path to the IM tool 'convert' with LZW enabled! See 'gif_compress'. If your version 4.2.9 of ImageMagick is compiled with LZW you may leave this field blank AND disable the flag 'gif_compress'! Tip: You can call LZW 'convert' with a prefix like 'myver_convert' by setting this path with it, eg. '/usr/bin/myver_' instead of just '/usr/bin/'.
 
-               'im_version_5' => 0,                                    // Boolean. Set this if you're using IM 5+. If this is set, 'im_negate_mask', 'im_no_effects' and 'im_mask_temp_ext_gif' are automatically configured for use with ImageMagick version 5 +
+               'im_version_5' => 0,                                    // String/Boolean. Set this if you're using ImageMagick but not IM 4.x. Setting this value will automatically configure some settings for use with the specified ImageMagick. Allowed values are: 0 (v4), 1 (v5), im5, im6. Additionally, "gm" can be set which activates the use of GraphicsMagick instead of ImageMagick.
                'im_negate_mask' => 0,                                  // Boolean. Indicates if the mask images should be inverted first. This depends of the ImageMagick version. Below ver. 5.1 this should be false. Above ImageMagick version 5.2+ it should be true. Just set the flag if the masks works opposite the intension!
                'im_imvMaskState' => 0,                                 // Boolean. If set, the 'im_negate_mask' state is inverted. This is very useful with newer versions of IM5 (at least 5.4.3+) where the 'im_version_5' setting will set 'im_negate_mask' which will eventually be wrong... Halleluja for ImageMagick - have I ever regreted using that package...
                'im_no_effects' => 0,                                   // Boolean. This is necessary if using ImageMagick 5+. Approved version for using effects is version 4.2.9. Effects in Imagemagick 5+ tends to render very slowly! Therefore this must be disabled in order not to perform sharpen, blurring and such. (However lately IM5 is allowed for effects again, but syntax has changed!)
@@ -292,6 +292,11 @@ if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['im_version_5'])    {
        $GLOBALS['TYPO3_CONF_VARS']['GFX']['im_no_effects'] = 1;
        $GLOBALS['TYPO3_CONF_VARS']['GFX']['im_mask_temp_ext_gif'] = 1;
 }
+if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['im_version_5']==='gm') {
+       $GLOBALS['TYPO3_CONF_VARS']['GFX']['im_imvMaskState'] = 1;
+       $GLOBALS['TYPO3_CONF_VARS']['GFX']['im_no_effects'] = 1;
+       $GLOBALS['TYPO3_CONF_VARS']['GFX']['im_v5effects'] = -1;
+}
 if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['im_imvMaskState'])     {
        $GLOBALS['TYPO3_CONF_VARS']['GFX']['im_negate_mask']=$GLOBALS['TYPO3_CONF_VARS']['GFX']['im_negate_mask']?0:1;
 }
index f597cab..b98e4ca 100755 (executable)
@@ -214,10 +214,9 @@ class SC_t3lib_thumbs {
                                        } else {
                                                $colors = ($sizeMax>56)?'-colors 64':'-colors 16';
                                        }
-                                       $cmd = ($TYPO3_CONF_VARS['GFX']['im_path_lzw'] ? $TYPO3_CONF_VARS['GFX']['im_path_lzw'] : $TYPO3_CONF_VARS['GFX']['im_path']).
-                                                               'convert -sample '.$this->size.' '.$colors.' '.$this->wrapFileName($this->input.'[0]').' '.$this->wrapFileName($this->output);
-
-               //                      echo $cmd;
+                                       $path = $TYPO3_CONF_VARS['GFX']['im_path_lzw'] ? $TYPO3_CONF_VARS['GFX']['im_path_lzw'] : $TYPO3_CONF_VARS['GFX']['im_path'];
+                                       $parameters = '-sample '.$this->size.' '.$colors.' '.$this->wrapFileName($this->input.'[0]').' '.$this->wrapFileName($this->output);
+                                       $cmd = t3lib_div::imageMagickCommand('convert', $parameters);
                                        exec($cmd);
                                        if (!@file_exists($this->output))       {
                                                $this->errorGif('No thumb','generated!',basename($this->input));
index c01f5c0..12709c2 100755 (executable)
@@ -1762,7 +1762,7 @@ From sub-directory:
 
                $paths = array_unique($paths);
 
-               $programs = explode(",","convert,combine,composite,identify");  // Added composite (substitution for combine in newer versions?)
+               $programs = explode(',','gm,convert,combine,composite,identify');
                $isExt = TYPO3_OS=="WIN" ? ".exe" : "";
                $this->config_array["im_combine_filename"]="combine";
                reset($paths);
@@ -1770,18 +1770,24 @@ From sub-directory:
                        reset($programs);
                        if (!ereg('[\\\/]$',$v)) $v.='/';
                        while(list(,$filename)=each($programs)) {
-                               if (ini_get("open_basedir")||(@file_exists($v)&& @is_file($v.$filename.$isExt))) {
-                                       if($this->_checkImageMagick_getVersion($v.$filename.$isExt) > 0 ) {
-                                               $index[$v][$filename]=$this->_checkImageMagick_getVersion($v.$filename.$isExt);
+                               if (ini_get('open_basedir') || (@file_exists($v)&&@is_file($v.$filename.$isExt))) {
+                                       $version = $this->_checkImageMagick_getVersion($filename,$v);
+                                       if($version > 0)        {
+                                               if($filename=='gm')     {       // Assume GraphicsMagick
+                                                       $index[$v]['gm']=$version;
+                                                       continue;       // No need to check for "identify" etc.
+                                               } else  {       // Assume ImageMagick
+                                                       $index[$v][$filename]=$version;
+                                               }
                                        }
                                }
                        }
-                       if (count($index[$v])>=3)       {$this->config_array["im"]=1;}
+                       if (count($index[$v])>=3 || $index[$v]['gm'])   { $this->config_array['im']=1; }
 
-                       if ($index[$v]["composite"] && !$index[$v]["combine"])  {
-                               $this->config_array["im_combine_filename"]="composite";
-                       } elseif (!$index[$v]["composite"] && $index[$v]["combine"]) {
-                               $this->config_array["im_combine_filename"]="combine";
+                       if ($index[$v]['gm'] || (!$index[$v]['composite'] && $index[$v]['combine'])) {
+                               $this->config_array['im_combine_filename']='combine';
+                       } elseif ($index[$v]['composite'] && !$index[$v]['combine'])  {
+                               $this->config_array['im_combine_filename']='composite';
                        }
 
                        if (isset($index[$v]["convert"]) && $this->checkIMlzw)  {
@@ -1803,7 +1809,7 @@ From sub-directory:
                                while(list($ka[])=each($v)){}
                                $theCode.='<tr><td>'.$this->fw($p).'</td><td>'.$this->fw(implode($ka,"<BR>")).'</td><td>'.$this->fw(implode($v,"<BR>")).'</td></tr>';
                        }
-                       $this->message($ext, "Available ImageMagick installations:",'<table border=1 cellpadding=2 cellspacing=2>'.$theCode.'</table>',-1);
+                       $this->message($ext, 'Available ImageMagick/GraphicsMagick installations:','<table border="1" cellpadding="2" cellspacing="2">'.$theCode.'</table>',-1);
                }
                $this->message($ext, "Search for ImageMagick:",'
                        <form action="'.$this->action.'" method="POST">'.$content.'<input type="checkbox" name="TYPO3_INSTALL[checkIM][lzw]" value="1"'.($this->INSTALL["checkIM"]["lzw"]?" checked":"").'> Check LZW capabilities.
@@ -1824,14 +1830,14 @@ From sub-directory:
         * @param       [type]          $file: ...
         * @return      [type]          ...
         */
-       function _checkImageMagickGifCapability($file)  {
+       function _checkImageMagickGifCapability($path)  {
                if ($this->config_array["dir_typo3temp"])       {               //  && !$this->config_array["safemode"]
-                       $path = $this->typo3temp_path;
+                       $tempPath = $this->typo3temp_path;
                        $uniqueName = md5(uniqid(microtime()));
-                       $dest = $path.$uniqueName.".gif";
+                       $dest = $tempPath.$uniqueName.'.gif';
                        $src = PATH_t3lib."gfx/typo3logo.gif";
                        if (@is_file($src) && !strstr($src," ") && !strstr($dest," "))  {
-                               $cmd = $file.'convert '.$src.' '.$dest;
+                               $cmd = t3lib_div::imageMagickCommand('convert', $src.' '.$dest, $path);
                                exec($cmd);
                        } else die("No t3lib/gfx/typo3logo.gif file!");
                        $out="";
@@ -1863,14 +1869,35 @@ From sub-directory:
        /**
         * Extracts the version number for imagemagick
         *
-        * @param       string          $file   This is the path to the convert-program to execute in order to find the version number
-        * @return      [type]          ...
+        * @param       string          The program name to execute in order to find out the version number
+        * @param       string          Path for the above program
+        * @return      string          Version number of the found ImageMagick instance
         */
-       function _checkImageMagick_getVersion($file)    {
-               exec($file, $retVal);
+       function _checkImageMagick_getVersion($file, $path)     {
+                       // Temporarily override some settings
+               $im_version = $GLOBALS['TYPO3_CONF_VARS']['GFX']['im_version_5'];
+               $combine_filename = $GLOBALS['TYPO3_CONF_VARS']['GFX']['im_combine_filename'];
+
+               if($file=='gm') {
+                       $GLOBALS['TYPO3_CONF_VARS']['GFX']['im_version_5'] = 'gm';
+                       $file = 'identify';     // Work-around, preventing execution of "gm gm"
+               } else  {
+                       $GLOBALS['TYPO3_CONF_VARS']['GFX']['im_version_5'] = '1';
+
+                       if($file=='combine' || $file=='composite')      {       // Override the combine_filename setting
+                               $GLOBALS['TYPO3_CONF_VARS']['GFX']['im_combine_filename'] = $file;
+                       }
+               }
+
+               $cmd = t3lib_div::imageMagickCommand($file, '', $path);
+               exec($cmd, $retVal);
                $string = $retVal[0];
-               list(,$ver) = explode("ImageMagick", $string);
+               list(,$ver) = explode('Magick', $string);
                list($ver) = explode(" ",trim($ver));
+
+                       // Restore the values
+               $GLOBALS['TYPO3_CONF_VARS']['GFX']['im_version_5'] = $im_version;
+               $GLOBALS['TYPO3_CONF_VARS']['GFX']['im_combine_filename'] = $combine_filename;
                return trim($ver);
        }
 
@@ -1993,6 +2020,7 @@ From sub-directory:
                                        if (is_array($fA["im"]))        {
                                                $out.=$this->wrapInCells("[GFX][im]=", $this->getFormElement($fA["im"], $fA["im"], 'TYPO3_INSTALL[localconf.php][im]', $GLOBALS["TYPO3_CONF_VARS"]["GFX"]["im"]));
                                                $out.=$this->wrapInCells("[GFX][im_combine_filename]=", $this->getFormElement($fA["im_combine_filename"], ($fA["im_combine_filename"]?$fA["im_combine_filename"]:"combine"), 'TYPO3_INSTALL[localconf.php][im_combine_filename]', $GLOBALS["TYPO3_CONF_VARS"]["GFX"]["im_combine_filename"]));
+                                               $out.=$this->wrapInCells('[GFX][im_version_5]=', $this->getFormElement($fA['im_version_5'], ($fA['im_version_5']?$fA['im_version_5']:''), 'TYPO3_INSTALL[localconf.php][im_version_5]', $GLOBALS['TYPO3_CONF_VARS']['GFX']['im_version_5']));
                                                if ($GLOBALS["TYPO3_CONF_VARS"]["GFX"]["im"])   {
                                                        if (is_array($fA["im_path"]))   {
                                                                $out.=$this->wrapInCells("[GFX][im_path]=", $this->getFormElement($this->setLabelValueArray($fA["im_path"],1), $this->setLabelValueArray($fA["im_path"],0), 'TYPO3_INSTALL[localconf.php][im_path]', $GLOBALS["TYPO3_CONF_VARS"]["GFX"]["im_path"]));
@@ -2077,10 +2105,17 @@ From sub-directory:
                                                                        if (strcmp($GLOBALS["TYPO3_CONF_VARS"]["GFX"][$key],$value))    {
                                                                                $this->setValueInLocalconfFile($lines, '$TYPO3_CONF_VARS["GFX"]["'.$key.'"]', $value);
                                                                        }
-                                                                       $value_ext = (doubleval($version)<5)?0:1;
-                                                                       if (strcmp($GLOBALS["TYPO3_CONF_VARS"]["GFX"]["im_version_5"],$value_ext))      {
+                                                                       if(doubleval($version)>0 && doubleval($version)<4)      {       // Assume GraphicsMagick
+                                                                               $value_ext = 'gm';
+                                                                       } elseif(doubleval($version)<5) {       // Assume ImageMagick 4.x
+                                                                               $value_ext = 0;
+                                                                       } else  {       // Assume ImageMagick 5+
+                                                                               $value_ext = 1;
+                                                                       }
+                                                                       if (strcmp(strtolower($GLOBALS['TYPO3_CONF_VARS']['GFX']['im_version_5']),$value_ext))  {
                                                                                $this->setValueInLocalconfFile($lines, '$TYPO3_CONF_VARS["GFX"]["im_version_5"]', $value_ext);
                                                                        }
+       //                                                              if (strcmp(strtolower($GLOBALS['TYPO3_CONF_VARS']['GFX']['im_version_5']),$value))      $this->setValueInLocalconfFile($lines, '$TYPO3_CONF_VARS["GFX"]["im_version_5"]', $value);
                                                                } else $this->messages[]= $errorMessages[] = "Path '".$value."' contains spaces or is longer than 100 chars (...not saved)";
                                                        break;
                                                        case "im_path_lzw":
@@ -2155,12 +2190,24 @@ From sub-directory:
        function setLabelValueArray($arr,$type) {
                reset($arr);
                while(list($k,$v)=each($arr))   {
+                       if($this->config_array['im_versions'][$v]['gm'])        {
+                               $program = 'gm';
+                       } else  {
+                               $program = 'convert';
+                       }
+
                        switch($type)   {
                                case 0: // value, im
-                                       $arr[$k].="|".$this->config_array["im_versions"][$v]["convert"];
+                                       $arr[$k].='|'.$this->config_array['im_versions'][$v][$program];
                                break;
                                case 1: // labels, im
-                                       $arr[$k].=$this->config_array["im_versions"][$v]["convert"] ? " (".$this->config_array["im_versions"][$v]["convert"].($this->config_array["im_versions"][$v]["gif_capability"]?", ".$this->config_array["im_versions"][$v]["gif_capability"]:"").")" : "";
+                                       if($this->config_array['im_versions'][$v][$program])    {
+                                               $arr[$k].= ' ('.$this->config_array['im_versions'][$v][$program];
+                                               $arr[$k].= ($this->config_array['im_versions'][$v]['gif_capability'] ? ', '.$this->config_array['im_versions'][$v]['gif_capability'] : '');
+                                               $arr[$k].= ')';
+                                       } else  {
+                                               $arr[$k].= '';
+                                       }
                                break;
                                case 2: // labels, gd
                                        $arr[$k].=" (".($v==1?"PNG":"GIF").")";
@@ -2229,6 +2276,7 @@ From sub-directory:
                $formArray["im_path"]=array("");
                $formArray["im_path_lzw"]=array("");
                $formArray["im_combine_filename"]=array("");
+               $formArray['im_version_5']=array('');
                $formArray["im"]=array(1);
                $formArray["gdlib"]=array(1);
                if ($this->config_array["gd"] && ($this->config_array["gd_gif"] || $this->config_array["gd_png"]))      {
@@ -2254,6 +2302,7 @@ From sub-directory:
                        while(list($path,$dat)=each($this->config_array["im_versions"]))        {
                                if (count($dat)>=3)     {
                                        if (doubleval($dat["convert"])<5)       {
+                                               $formArray['im_version_5']=array(0);
                                                if ($dat["gif_capability"]=="LZW")      {
                                                        $formArray["im_path"]=array($path);
                                                        $found=2;
@@ -2262,15 +2311,22 @@ From sub-directory:
                                                        $found=1;
                                                }
                                        } elseif (!$found)      {
+                                               $formArray['im_version_5']=array(1);
+                                               $formArray["im_path"]=array($path);
+                                               $found=1;
+                                       }
+                               } elseif($dat['gm'])    {
+                                       $formArray['im_version_5']=array('gm');
+                                       if ($dat['gif_capability']=='LZW')      {
+                                               $formArray['im_path']=array($path);
+                                               $found=2;
+                                       } elseif ($found<2)     {
                                                $formArray["im_path"]=array($path);
                                                $found=1;
                                        }
                                }
                                if ($dat["gif_capability"]=="LZW")      {
-                                       if (doubleval($dat["convert"])<5)       {
-                                               $formArray["im_path_lzw"]=array($path);
-                                               $LZW_found=1;
-                                       } elseif (!$LZW_found) {
+                                       if (doubleval($dat['convert'])<5 || !$LZW_found)        {
                                                $formArray["im_path_lzw"]=array($path);
                                                $LZW_found=1;
                                        }
@@ -3021,15 +3077,9 @@ From sub-directory:
                        $ex_rows.=$this->getTwinImageMessage('', 'Your server:', 'Reference:');
                        $ex_rows.=$this->getTwinImageMessage('', t3lib_div::formatSize($destImg['filesize']).', '.$destImg[0].'x'.$destImg[1].' pixels', t3lib_div::formatSize($verifyImg['filesize']).', '.$verifyImg[0].'x'.$verifyImg[1].' pixels');
 
-                       if ($destImg["filesize"]!=$verifyImg["filesize"])       {
-                               if (intval($destImg["filesize"]) && abs(($verifyImg["filesize"]/$destImg["filesize"])-1) < 0.05)        {
-//                                     debug(abs(($verifyImg["filesize"]/$destImg["filesize"])-1));
-                                       $ex_rows.=$this->getTwinImageMessage("File size is a bit different from reference", $destImg["filesize"], $verifyImg["filesize"]);
-                                       $errorLevels[]=1;
-                               } else {
-                                       $ex_rows.=$this->getTwinImageMessage("File size is very different from reference", $destImg["filesize"], $verifyImg["filesize"]);
-                                       $errorLevels[]=2;
-                               }
+                       if (($destImg['filesize']!=$verifyImg['filesize']) && (intval($destImg['filesize']) && ($destImg['filesize']-$verifyImg['filesize']) > 10240))  {       // Display a warning if the generated image is more than 10KB larger than its reference...
+                               $ex_rows.=$this->getTwinImageMessage('File size is very different from reference', $destImg['filesize'], $verifyImg['filesize']);
+                               $errorLevels[]=2;
                        }
                        if ($destImg[0]!=$verifyImg[0] || $destImg[1]!=$verifyImg[1])   {
                                $ex_rows.=$this->getTwinImageMessage("Pixel dimension are not equal!");