TYPO3 CMS  TYPO3_8-7
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 
23 
28 {
34  protected static $defaultConfiguration = [
35  'width' => 64,
36  'height' => 64,
37  ];
38 
42  protected $processor;
43 
48  {
49  $this->processor = $processor;
50  }
51 
58  public static function preProcessConfiguration(array $configuration): array
59  {
60  $configuration = array_replace(static::$defaultConfiguration, $configuration);
61  $configuration['width'] = MathUtility::forceIntegerInRange($configuration['width'], 1, 1000);
62  $configuration['height'] = MathUtility::forceIntegerInRange($configuration['height'], 1, 1000);
63  return $configuration;
64  }
65 
85  public function process(TaskInterface $task)
86  {
87  $sourceFile = $task->getSourceFile();
88  $configuration = static::preProcessConfiguration($task->getConfiguration());
89 
90  // Do not scale up if the source file has a size and the target size is larger
91  if ($sourceFile->getProperty('width') > 0 && $sourceFile->getProperty('height') > 0
92  && $configuration['width'] > $sourceFile->getProperty('width')
93  && $configuration['height'] > $sourceFile->getProperty('height')) {
94  return null;
95  }
96 
97  return $this->generatePreviewFromFile($sourceFile, $configuration, $this->getTemporaryFilePath($task));
98  }
99 
106  protected function getTemporaryFilePath(TaskInterface $task)
107  {
108  return GeneralUtility::tempnam('preview_', '.' . $task->getTargetFileExtension());
109  }
110 
119  protected function generatePreviewFromFile(File $file, array $configuration, $targetFilePath)
120  {
121  // Check file extension
122  if ($file->getType() !== File::FILETYPE_IMAGE
123  && !GeneralUtility::inList($GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], $file->getExtension())
124  ) {
125  // Create a default image
126  $graphicalFunctions = GeneralUtility::makeInstance(GraphicalFunctions::class);
127  $graphicalFunctions->init();
128  $graphicalFunctions->getTemporaryImageWithText(
129  $targetFilePath,
130  'Not imagefile!',
131  'No ext!',
132  $file->getName()
133  );
134  return [
135  'filePath' => $targetFilePath,
136  ];
137  }
138 
139  $originalFileName = $file->getForLocalProcessing(false);
140  if ($file->getExtension() === 'svg') {
142  $gifBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Frontend\Imaging\GifBuilder::class);
143  $gifBuilder->init();
144  $gifBuilder->absPrefix = PATH_site;
145  $info = $gifBuilder->getImageDimensions($originalFileName);
146  $newInfo = $gifBuilder->getImageScale($info, $configuration['width'], $configuration['height'], []);
147  $result = [
148  'width' => $newInfo[0],
149  'height' => $newInfo[1],
150  'filePath' => '' // no file = use original
151  ];
152  } else {
153  // Create the temporary file
154  if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['processor_enabled']) {
156  'width' => $configuration['width'],
157  'height' => $configuration['height'],
158  ]);
159  $parameters = '-sample ' . $arguments['width'] . 'x' . $arguments['height']
160  . ' ' . ImageMagickFile::fromFilePath($originalFileName, 0)
161  . ' ' . CommandUtility::escapeShellArgument($targetFilePath);
162 
163  $cmd = CommandUtility::imageMagickCommand('convert', $parameters) . ' 2>&1';
164  CommandUtility::exec($cmd);
165 
166  if (!file_exists($targetFilePath)) {
167  // Create an error gif
168  $graphicalFunctions = GeneralUtility::makeInstance(GraphicalFunctions::class);
169  $graphicalFunctions->init();
170  $graphicalFunctions->getTemporaryImageWithText(
171  $targetFilePath,
172  'No thumb',
173  'generated!',
174  $file->getName()
175  );
176  }
177  }
178  $result = [
179  'filePath' => $targetFilePath,
180  ];
181  }
182 
183  return $result;
184  }
185 }
static forceIntegerInRange($theInt, $min, $max=2000000000, $defaultValue=0)
Definition: MathUtility.php:31
static exec($command, &$output=null, &$returnValue=0)
static makeInstance($className,... $constructorArguments)
static imageMagickCommand($command, $parameters, $path='')
static tempnam($filePrefix, $fileSuffix='')
static fromFilePath(string $filePath, int $frame=null)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']