TYPO3 CMS  TYPO3_7-6
LocalPreviewHelper.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
22 
27 {
33  protected static $defaultConfiguration = [
34  'width' => 64,
35  'height' => 64,
36  ];
37 
41  protected $processor;
42 
47  {
48  $this->processor = $processor;
49  }
50 
57  public static function preProcessConfiguration(array $configuration)
58  {
59  $configuration = array_replace(static::$defaultConfiguration, $configuration);
60  $configuration['width'] = MathUtility::forceIntegerInRange($configuration['width'], 1, 1000);
61  $configuration['height'] = MathUtility::forceIntegerInRange($configuration['height'], 1, 1000);
62  return $configuration;
63  }
64 
84  public function process(TaskInterface $task)
85  {
86  $sourceFile = $task->getSourceFile();
87  $configuration = static::preProcessConfiguration($task->getConfiguration());
88 
89  // Do not scale up if the source file has a size and the target size is larger
90  if ($sourceFile->getProperty('width') > 0 && $sourceFile->getProperty('height') > 0
91  && $configuration['width'] > $sourceFile->getProperty('width')
92  && $configuration['height'] > $sourceFile->getProperty('height')) {
93  return null;
94  }
95 
96  return $this->generatePreviewFromFile($sourceFile, $configuration, $this->getTemporaryFilePath($task));
97  }
98 
105  protected function getTemporaryFilePath(TaskInterface $task)
106  {
107  return GeneralUtility::tempnam('preview_', '.' . $task->getTargetFileExtension());
108  }
109 
118  protected function generatePreviewFromFile(File $file, array $configuration, $targetFilePath)
119  {
120  // Check file extension
121  if ($file->getType() !== File::FILETYPE_IMAGE
122  && !GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], $file->getExtension())
123  ) {
124  // Create a default image
125  $graphicalFunctions = GeneralUtility::makeInstance(GraphicalFunctions::class);
126  $graphicalFunctions->getTemporaryImageWithText(
127  $targetFilePath,
128  'Not imagefile!',
129  'No ext!',
130  $file->getName()
131  );
132  return [
133  'filePath' => $targetFilePath,
134  ];
135  }
136 
137  $originalFileName = $file->getForLocalProcessing(false);
138  if ($file->getExtension() === 'svg') {
140  $gifBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Frontend\Imaging\GifBuilder::class);
141  $gifBuilder->init();
142  $gifBuilder->absPrefix = PATH_site;
143  $info = $gifBuilder->getImageDimensions($originalFileName);
144  $newInfo = $gifBuilder->getImageScale($info, $configuration['width'], $configuration['height'], []);
145  $result = [
146  'width' => $newInfo[0],
147  'height' => $newInfo[1],
148  'filePath' => '' // no file = use original
149  ];
150  } else {
151  // Create the temporary file
152  if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['im']) {
153  $parameters = '-sample ' . $configuration['width'] . 'x' . $configuration['height'] . ' '
154  . CommandUtility::escapeShellArgument($originalFileName) . '[0] ' . CommandUtility::escapeShellArgument($targetFilePath);
155 
156  $cmd = GeneralUtility::imageMagickCommand('convert', $parameters) . ' 2>&1';
157  CommandUtility::exec($cmd);
158 
159  if (!file_exists($targetFilePath)) {
160  // Create an error gif
161  $graphicalFunctions = GeneralUtility::makeInstance(GraphicalFunctions::class);
162  $graphicalFunctions->getTemporaryImageWithText(
163  $targetFilePath,
164  'No thumb',
165  'generated!',
166  $file->getName()
167  );
168  }
169  }
170  $result = [
171  'filePath' => $targetFilePath,
172  ];
173  }
174 
175  return $result;
176  }
177 }
static imageMagickCommand($command, $parameters, $path='')
static forceIntegerInRange($theInt, $min, $max=2000000000, $defaultValue=0)
Definition: MathUtility.php:31
static exec($command, &$output=null, &$returnValue=0)
static tempnam($filePrefix, $fileSuffix='')
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']