TYPO3 API  SVNRelease
class.t3lib_utility_command.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003  * Copyright notice
00004  *
00005  * (c) 2010-2011 Steffen Kamper <steffen@typo3.org>
00006  * All rights reserved
00007  *
00008  * This script is part of the TYPO3 project. The TYPO3 project is
00009  * free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * The GNU General Public License can be found at
00015  * http://www.gnu.org/copyleft/gpl.html.
00016  * A copy is found in the textfile GPL.txt and important notices to the license
00017  * from the author is found in LICENSE.txt distributed with these scripts.
00018  *
00019  *
00020  * This script is distributed in the hope that it will be useful,
00021  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023  * GNU General Public License for more details.
00024  *
00025  * This copyright notice MUST APPEAR in all copies of the script!
00026  ***************************************************************/
00027 
00028 /**
00029  * Class to handle system commands.
00030  *
00031  * $Id: class.t3lib_utility_command.php $
00032  *
00033  * @author  Steffen Kamper <steffen@typo3.org>
00034  */
00035 final class t3lib_utility_Command {
00036 
00037 
00038     /**
00039      * Wrapper function for php exec function
00040      * Needs to be central to have better control and possible fix for safe_mode/low php version restrictions as occurred with IM/GM issues
00041      *
00042      * @static
00043      * @param  string  $command
00044      * @param  null|array $output
00045      * @param  integer $returnValue
00046      * @return null|array
00047      */
00048     public static function exec($command, &$output = NULL, &$returnValue = 0) {
00049         if (TYPO3_OS == 'WIN' && version_compare(phpversion(), '5.3.0', '<')) {
00050             $command = '"' . $command . '"';
00051         }
00052         $lastLine = exec($command, $output, $returnValue);
00053         return $lastLine;
00054     }
00055 
00056     /**
00057      * Compile the command for running ImageMagick/GraphicsMagick.
00058      *
00059      * @param   string      Command to be run: identify, convert or combine/composite
00060      * @param   string      The parameters string
00061      * @param   string      Override the default path (e.g. used by the install tool)
00062      * @return  string      Compiled command that deals with IM6 & GraphicsMagick
00063      */
00064     public static function imageMagickCommand($command, $parameters, $path = '') {
00065         $gfxConf = $GLOBALS['TYPO3_CONF_VARS']['GFX'];
00066         $isExt = (TYPO3_OS == 'WIN' ? '.exe' : '');
00067         $switchCompositeParameters = FALSE;
00068 
00069         if (!$path) {
00070             $path = $gfxConf['im_path'];
00071         }
00072         $path = t3lib_div::fixWindowsFilePath($path);
00073 
00074         $im_version = strtolower($gfxConf['im_version_5']);
00075         $combineScript = $gfxConf['im_combine_filename'] ? trim($gfxConf['im_combine_filename']) : 'combine';
00076 
00077         if ($command === 'combine') { // This is only used internally, has no effect outside
00078             $command = 'composite';
00079         }
00080 
00081             // Compile the path & command
00082         if ($im_version === 'gm') {
00083             $switchCompositeParameters = TRUE;
00084             $path = escapeshellarg($path . 'gm' . $isExt) . ' ' . $command;
00085         } else {
00086             if ($im_version === 'im6') {
00087                 $switchCompositeParameters = TRUE;
00088             }
00089             $path = escapeshellarg($path . (($command == 'composite') ? $combineScript : $command) . $isExt);
00090         }
00091 
00092             // strip profile information for thumbnails and reduce their size
00093         if ($parameters && $command != 'identify' && $gfxConf['im_useStripProfileByDefault'] && $gfxConf['im_stripProfileCommand'] != '') {
00094             if (strpos($parameters, $gfxConf['im_stripProfileCommand']) === FALSE) {
00095                     // Determine whether the strip profile action has be disabled by TypoScript:
00096                 if ($parameters !== '-version' && strpos($parameters, '###SkipStripProfile###') === FALSE) {
00097                     $parameters = $gfxConf['im_stripProfileCommand'] . ' ' . $parameters;
00098                 } else {
00099                     $parameters = str_replace('###SkipStripProfile###', '', $parameters);
00100                 }
00101             }
00102         }
00103 
00104         $cmdLine = $path . ' ' . $parameters;
00105 
00106         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
00107             $paramsArr = t3lib_div::unQuoteFilenames($parameters);
00108 
00109             if (count($paramsArr) > 5) { // The mask image has been specified => swap the parameters
00110                 $tmp = $paramsArr[count($paramsArr) - 3];
00111                 $paramsArr[count($paramsArr) - 3] = $paramsArr[count($paramsArr) - 4];
00112                 $paramsArr[count($paramsArr) - 4] = $tmp;
00113             }
00114 
00115             $cmdLine = $path . ' ' . implode(' ', $paramsArr);
00116         }
00117 
00118         return $cmdLine;
00119     }
00120 
00121 }
00122 
00123 ?>