‪TYPO3CMS  10.4
LocalPreviewHelper.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
24 
29 {
35  protected static ‪$defaultConfiguration = [
36  'width' => 64,
37  'height' => 64,
38  ];
39 
46  public static function ‪preProcessConfiguration(array $configuration): array
47  {
48  $configuration = array_replace(static::$defaultConfiguration, $configuration);
49  $configuration['width'] = ‪MathUtility::forceIntegerInRange($configuration['width'], 1, 1000);
50  $configuration['height'] = ‪MathUtility::forceIntegerInRange($configuration['height'], 1, 1000);
51 
52  return array_filter(
53  $configuration,
54  function ($value, $name) {
55  return !empty($value) && in_array($name, ['width', 'height'], true);
56  },
57  ARRAY_FILTER_USE_BOTH
58  );
59  }
60 
80  public function ‪process(‪TaskInterface $task)
81  {
82  $sourceFile = $task->‪getSourceFile();
83  $configuration = static::preProcessConfiguration($task->‪getConfiguration());
84 
85  // Do not scale up if the source file has a size and the target size is larger
86  if ($sourceFile->getProperty('width') > 0 && $sourceFile->getProperty('height') > 0
87  && $configuration['width'] > $sourceFile->getProperty('width')
88  && $configuration['height'] > $sourceFile->getProperty('height')) {
89  return null;
90  }
91 
92  return $this->‪generatePreviewFromFile($sourceFile, $configuration, $this->‪getTemporaryFilePath($task));
93  }
94 
103  public function ‪processWithLocalFile(‪TaskInterface $task, string $localFile): ?array
104  {
105  return $this->‪generatePreviewFromLocalFile($localFile, $task->‪getConfiguration(), $this->getTemporaryFilePath($task));
106  }
107 
114  protected function ‪getTemporaryFilePath(‪TaskInterface $task)
115  {
116  return GeneralUtility::tempnam('preview_', '.' . $task->‪getTargetFileExtension());
117  }
118 
127  protected function ‪generatePreviewFromFile(‪File $file, array $configuration, string $targetFilePath)
128  {
129  // Check file extension
130  if ($file->‪getType() !== ‪File::FILETYPE_IMAGE && !$file->‪isImage()) {
131  // Create a default image
132  $graphicalFunctions = GeneralUtility::makeInstance(GraphicalFunctions::class);
133  $graphicalFunctions->getTemporaryImageWithText(
134  $targetFilePath,
135  'Not imagefile!',
136  'No ext!',
137  $file->‪getName()
138  );
139  return [
140  'filePath' => $targetFilePath,
141  ];
142  }
143 
144  return $this->‪generatePreviewFromLocalFile($file->‪getForLocalProcessing(false), $configuration, $targetFilePath);
145  }
146 
155  protected function ‪generatePreviewFromLocalFile(string $originalFileName, array $configuration, string $targetFilePath)
156  {
157  // Create the temporary file
158  if (‪$GLOBALS['TYPO3_CONF_VARS']['GFX']['processor_enabled']) {
160  'width' => $configuration['width'],
161  'height' => $configuration['height'],
162  ]);
163  $parameters = '-sample ' . $arguments['width'] . 'x' . $arguments['height']
164  . ' ' . ‪ImageMagickFile::fromFilePath($originalFileName, 0)
165  . ' ' . ‪CommandUtility::escapeShellArgument($targetFilePath);
166 
167  $cmd = ‪CommandUtility::imageMagickCommand('convert', $parameters) . ' 2>&1';
169 
170  if (!file_exists($targetFilePath)) {
171  // Create an error gif
172  $graphicalFunctions = GeneralUtility::makeInstance(GraphicalFunctions::class);
173  $graphicalFunctions->getTemporaryImageWithText(
174  $targetFilePath,
175  'No thumb',
176  'generated!',
177  ''
178  );
179  }
180  }
181 
182  return [
183  'filePath' => $targetFilePath,
184  ];
185  }
186 }
‪TYPO3\CMS\Core\Resource\Processing\LocalPreviewHelper
Definition: LocalPreviewHelper.php:29
‪TYPO3\CMS\Core\Resource\AbstractFile\getType
‪int getType()
Definition: AbstractFile.php:286
‪TYPO3\CMS\Core\Resource\AbstractFile\getForLocalProcessing
‪string getForLocalProcessing($writable=true)
Definition: AbstractFile.php:577
‪TYPO3\CMS\Core\Resource\AbstractFile\getName
‪string getName()
Definition: AbstractFile.php:161
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface
Definition: TaskInterface.php:33
‪TYPO3\CMS\Core\Utility\MathUtility\forceIntegerInRange
‪static int forceIntegerInRange($theInt, $min, $max=2000000000, $defaultValue=0)
Definition: MathUtility.php:32
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface\getSourceFile
‪Resource File getSourceFile()
‪TYPO3\CMS\Core\Resource\AbstractFile\isImage
‪bool isImage()
Definition: AbstractFile.php:323
‪TYPO3\CMS\Core\Resource\Processing\LocalPreviewHelper\generatePreviewFromFile
‪array generatePreviewFromFile(File $file, array $configuration, string $targetFilePath)
Definition: LocalPreviewHelper.php:126
‪TYPO3\CMS\Core\Resource\AbstractFile\FILETYPE_IMAGE
‪const FILETYPE_IMAGE
Definition: AbstractFile.php:78
‪TYPO3\CMS\Core\Resource\Processing
Definition: AbstractGraphicalTask.php:16
‪TYPO3\CMS\Core\Resource\Processing\LocalPreviewHelper\generatePreviewFromLocalFile
‪array generatePreviewFromLocalFile(string $originalFileName, array $configuration, string $targetFilePath)
Definition: LocalPreviewHelper.php:154
‪TYPO3\CMS\Core\Imaging\GraphicalFunctions
Definition: GraphicalFunctions.php:37
‪TYPO3\CMS\Core\Resource\Processing\LocalPreviewHelper\process
‪array null process(TaskInterface $task)
Definition: LocalPreviewHelper.php:79
‪TYPO3\CMS\Core\Utility\CommandUtility\exec
‪static string exec($command, &$output=null, &$returnValue=0)
Definition: CommandUtility.php:81
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:24
‪TYPO3\CMS\Core\Utility\CommandUtility\escapeShellArgument
‪static string escapeShellArgument($input)
Definition: CommandUtility.php:504
‪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\Core\Resource\Processing\LocalPreviewHelper\preProcessConfiguration
‪static array preProcessConfiguration(array $configuration)
Definition: LocalPreviewHelper.php:45
‪TYPO3\CMS\Core\Imaging\ImageMagickFile\fromFilePath
‪static ImageMagickFile fromFilePath(string $filePath, int $frame=null)
Definition: ImageMagickFile.php:113
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Imaging\ImageMagickFile
Definition: ImageMagickFile.php:30
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:22
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Resource\Processing\LocalPreviewHelper\getTemporaryFilePath
‪string getTemporaryFilePath(TaskInterface $task)
Definition: LocalPreviewHelper.php:113
‪TYPO3\CMS\Core\Resource\Processing\LocalPreviewHelper\processWithLocalFile
‪array null processWithLocalFile(TaskInterface $task, string $localFile)
Definition: LocalPreviewHelper.php:102
‪TYPO3\CMS\Core\Utility\CommandUtility
Definition: CommandUtility.php:49
‪TYPO3\CMS\Core\Utility\CommandUtility\imageMagickCommand
‪static string imageMagickCommand($command, $parameters, $path='')
Definition: CommandUtility.php:94
‪TYPO3\CMS\Core\Utility\CommandUtility\escapeShellArguments
‪static string[] escapeShellArguments(array $input)
Definition: CommandUtility.php:448
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface\getTargetFileExtension
‪string getTargetFileExtension()