‪TYPO3CMS  9.5
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 
24 
29 {
35  protected static ‪$defaultConfiguration = [
36  'width' => 64,
37  'height' => 64,
38  ];
39 
43  protected ‪$processor;
44 
49  {
50  $this->processor = ‪$processor;
51  }
52 
59  public static function ‪preProcessConfiguration(array $configuration): array
60  {
61  $configuration = array_replace(static::$defaultConfiguration, $configuration);
62  $configuration['width'] = ‪MathUtility::forceIntegerInRange($configuration['width'], 1, 1000);
63  $configuration['height'] = ‪MathUtility::forceIntegerInRange($configuration['height'], 1, 1000);
64  return $configuration;
65  }
66 
86  public function ‪process(‪TaskInterface $task)
87  {
88  $sourceFile = $task->‪getSourceFile();
89  $configuration = static::preProcessConfiguration($task->‪getConfiguration());
90 
91  // Do not scale up if the source file has a size and the target size is larger
92  if ($sourceFile->getProperty('width') > 0 && $sourceFile->getProperty('height') > 0
93  && $configuration['width'] > $sourceFile->getProperty('width')
94  && $configuration['height'] > $sourceFile->getProperty('height')) {
95  return null;
96  }
97 
98  return $this->‪generatePreviewFromFile($sourceFile, $configuration, $this->‪getTemporaryFilePath($task));
99  }
100 
107  protected function ‪getTemporaryFilePath(‪TaskInterface $task)
108  {
109  return GeneralUtility::tempnam('preview_', '.' . $task->‪getTargetFileExtension());
110  }
111 
120  protected function ‪generatePreviewFromFile(‪File $file, array $configuration, $targetFilePath)
121  {
122  // Check file extension
123  if ($file->‪getType() !== ‪File::FILETYPE_IMAGE
124  && !GeneralUtility::inList(‪$GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'], $file->‪getExtension())
125  ) {
126  // Create a default image
127  $graphicalFunctions = GeneralUtility::makeInstance(GraphicalFunctions::class);
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') {
141  $gifBuilder = GeneralUtility::makeInstance(GifBuilder::class);
142  $info = $gifBuilder->getImageDimensions($originalFileName);
143  $newInfo = $gifBuilder->getImageScale($info, $configuration['width'], $configuration['height'], []);
144  $result = [
145  'width' => $newInfo[0],
146  'height' => $newInfo[1],
147  'filePath' => '' // no file = use original
148  ];
149  } else {
150  // Create the temporary file
151  if (‪$GLOBALS['TYPO3_CONF_VARS']['GFX']['processor_enabled']) {
153  'width' => $configuration['width'],
154  'height' => $configuration['height'],
155  ]);
156  $parameters = '-sample ' . $arguments['width'] . 'x' . $arguments['height']
157  . ' ' . ‪ImageMagickFile::fromFilePath($originalFileName, 0)
158  . ' ' . ‪CommandUtility::escapeShellArgument($targetFilePath);
159 
160  $cmd = ‪CommandUtility::imageMagickCommand('convert', $parameters) . ' 2>&1';
162 
163  if (!file_exists($targetFilePath)) {
164  // Create an error gif
165  $graphicalFunctions = GeneralUtility::makeInstance(GraphicalFunctions::class);
166  $graphicalFunctions->getTemporaryImageWithText(
167  $targetFilePath,
168  'No thumb',
169  'generated!',
170  $file->‪getName()
171  );
172  }
173  }
174  $result = [
175  'filePath' => $targetFilePath,
176  ];
177  }
178 
179  return $result;
180  }
181 }
‪TYPO3\CMS\Core\Resource\Processing\LocalPreviewHelper
Definition: LocalPreviewHelper.php:29
‪TYPO3\CMS\Core\Resource\AbstractFile\getType
‪int getType()
Definition: AbstractFile.php:284
‪TYPO3\CMS\Core\Resource\AbstractFile\getForLocalProcessing
‪string getForLocalProcessing($writable=true)
Definition: AbstractFile.php:548
‪TYPO3\CMS\Core\Resource\AbstractFile\getName
‪string getName()
Definition: AbstractFile.php:159
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface
Definition: TaskInterface.php:31
‪TYPO3\CMS\Core\Utility\MathUtility\forceIntegerInRange
‪static int forceIntegerInRange($theInt, $min, $max=2000000000, $defaultValue=0)
Definition: MathUtility.php:31
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface\getSourceFile
‪Resource File getSourceFile()
‪TYPO3\CMS\Core\Resource\AbstractFile\FILETYPE_IMAGE
‪const FILETYPE_IMAGE
Definition: AbstractFile.php:76
‪TYPO3\CMS\Core\Resource\Processing
Definition: AbstractGraphicalTask.php:2
‪TYPO3\CMS\Core\Resource\Processing\LocalImageProcessor
Definition: LocalImageProcessor.php:24
‪TYPO3\CMS\Core\Resource\Processing\LocalPreviewHelper\generatePreviewFromFile
‪array generatePreviewFromFile(File $file, array $configuration, $targetFilePath)
Definition: LocalPreviewHelper.php:118
‪TYPO3\CMS\Core\Imaging\GraphicalFunctions
Definition: GraphicalFunctions.php:35
‪TYPO3\CMS\Core\Resource\Processing\LocalPreviewHelper\process
‪array null process(TaskInterface $task)
Definition: LocalPreviewHelper.php:84
‪TYPO3\CMS\Core\Utility\CommandUtility\exec
‪static string exec($command, &$output=null, &$returnValue=0)
Definition: CommandUtility.php:80
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:23
‪TYPO3\CMS\Core\Resource\Processing\LocalPreviewHelper\$processor
‪LocalImageProcessor $processor
Definition: LocalPreviewHelper.php:41
‪TYPO3\CMS\Core\Utility\CommandUtility\escapeShellArgument
‪static string escapeShellArgument($input)
Definition: CommandUtility.php:503
‪TYPO3\CMS\Core\Resource\Processing\LocalPreviewHelper\$defaultConfiguration
‪static array $defaultConfiguration
Definition: LocalPreviewHelper.php:34
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface\getConfiguration
‪array getConfiguration()
‪TYPO3\CMS\Frontend\Imaging\GifBuilder
Definition: GifBuilder.php:52
‪TYPO3\CMS\Core\Resource\Processing\LocalPreviewHelper\preProcessConfiguration
‪static array preProcessConfiguration(array $configuration)
Definition: LocalPreviewHelper.php:57
‪TYPO3\CMS\Core\Resource\AbstractFile\getExtension
‪string getExtension()
Definition: AbstractFile.php:252
‪TYPO3\CMS\Core\Imaging\ImageMagickFile\fromFilePath
‪static ImageMagickFile fromFilePath(string $filePath, int $frame=null)
Definition: ImageMagickFile.php:111
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Imaging\ImageMagickFile
Definition: ImageMagickFile.php:28
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:21
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Resource\Processing\LocalPreviewHelper\getTemporaryFilePath
‪string getTemporaryFilePath(TaskInterface $task)
Definition: LocalPreviewHelper.php:105
‪TYPO3\CMS\Core\Utility\CommandUtility
Definition: CommandUtility.php:48
‪TYPO3\CMS\Core\Utility\CommandUtility\imageMagickCommand
‪static string imageMagickCommand($command, $parameters, $path='')
Definition: CommandUtility.php:93
‪TYPO3\CMS\Core\Utility\CommandUtility\escapeShellArguments
‪static string[] escapeShellArguments(array $input)
Definition: CommandUtility.php:447
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface\getTargetFileExtension
‪string getTargetFileExtension()
‪TYPO3\CMS\Core\Resource\Processing\LocalPreviewHelper\__construct
‪__construct(LocalImageProcessor $processor)
Definition: LocalPreviewHelper.php:46