‪TYPO3CMS  ‪main
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 
23 
28 {
34  protected static ‪$defaultConfiguration = [
35  'width' => 64,
36  'height' => 64,
37  ];
38 
42  public static function ‪preProcessConfiguration(array $configuration): array
43  {
44  $configuration = array_replace(static::$defaultConfiguration, $configuration);
45  $configuration['width'] = ‪MathUtility::forceIntegerInRange($configuration['width'], 1, 1000);
46  $configuration['height'] = ‪MathUtility::forceIntegerInRange($configuration['height'], 1, 1000);
47 
48  return array_filter(
49  $configuration,
50  static function ($value, $name) {
51  return !empty($value) && in_array($name, ['width', 'height'], true);
52  },
53  ARRAY_FILTER_USE_BOTH
54  );
55  }
56 
76  public function ‪process(‪TaskInterface $task)
77  {
78  $sourceFile = $task->‪getSourceFile();
79  $configuration = static::preProcessConfiguration($task->‪getConfiguration());
80 
81  // Do not scale up if the source file has a size and the target size is larger
82  if ($sourceFile->getProperty('width') > 0 && $sourceFile->getProperty('height') > 0
83  && $configuration['width'] > $sourceFile->getProperty('width')
84  && $configuration['height'] > $sourceFile->getProperty('height')) {
85  return null;
86  }
87 
88  return $this->‪generatePreviewFromFile($sourceFile, $configuration, $this->‪getTemporaryFilePath($task));
89  }
90 
95  public function ‪processWithLocalFile(‪TaskInterface $task, string $localFile): ?array
96  {
97  return $this->‪generatePreviewFromLocalFile($localFile, $task->‪getConfiguration(), $this->getTemporaryFilePath($task));
98  }
99 
105  protected function ‪getTemporaryFilePath(‪TaskInterface $task)
106  {
107  return GeneralUtility::tempnam('preview_', '.' . $task->‪getTargetFileExtension());
108  }
109 
118  protected function ‪generatePreviewFromFile(‪File $file, array $configuration, string $targetFilePath)
119  {
120  // Check file extension
121  if ($file->‪getType() !== ‪File::FILETYPE_IMAGE && !$file->‪isImage()) {
122  // Create a default image
123  $graphicalFunctions = GeneralUtility::makeInstance(GifBuilder::class);
124  $graphicalFunctions->getTemporaryImageWithText(
125  $targetFilePath,
126  'Not imagefile!',
127  'No ext!',
128  $file->‪getName()
129  );
130  return [
131  'filePath' => $targetFilePath,
132  ];
133  }
134 
135  return $this->‪generatePreviewFromLocalFile($file->‪getForLocalProcessing(false), $configuration, $targetFilePath);
136  }
137 
146  protected function ‪generatePreviewFromLocalFile(string $originalFileName, array $configuration, string $targetFilePath)
147  {
148  // Create the temporary file
149  if (‪$GLOBALS['TYPO3_CONF_VARS']['GFX']['processor_enabled']) {
150  $imageService = GeneralUtility::makeInstance(GraphicalFunctions::class);
151  $result = $imageService->imageMagickConvert($originalFileName, 'WEB', $configuration['width'], $configuration['height'], '', '0', ['sample' => true]);
152  if ($result) {
153  $targetFilePath = $result[3];
154  }
155  if (!file_exists($targetFilePath)) {
156  // Create an error gif
157  $graphicalFunctions = GeneralUtility::makeInstance(GifBuilder::class);
158  $graphicalFunctions->getTemporaryImageWithText(
159  $targetFilePath,
160  'No thumb',
161  'generated!'
162  );
163  }
164  }
165 
166  return [
167  'filePath' => $targetFilePath,
168  ];
169  }
170 }
‪TYPO3\CMS\Core\Resource\Processing\LocalPreviewHelper
Definition: LocalPreviewHelper.php:28
‪TYPO3\CMS\Core\Resource\AbstractFile\getType
‪int getType()
Definition: AbstractFile.php:272
‪TYPO3\CMS\Core\Resource\AbstractFile\getForLocalProcessing
‪non empty string getForLocalProcessing(bool $writable=true)
Definition: AbstractFile.php:551
‪TYPO3\CMS\Core\Resource\Processing\LocalPreviewHelper\processWithLocalFile
‪processWithLocalFile(TaskInterface $task, string $localFile)
Definition: LocalPreviewHelper.php:94
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface
Definition: TaskInterface.php:33
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface\getSourceFile
‪Resource File getSourceFile()
‪TYPO3\CMS\Core\Resource\AbstractFile\getName
‪getName()
Definition: AbstractFile.php:150
‪TYPO3\CMS\Core\Resource\AbstractFile\isImage
‪bool isImage()
Definition: AbstractFile.php:309
‪TYPO3\CMS\Core\Resource\Processing\LocalPreviewHelper\generatePreviewFromFile
‪array generatePreviewFromFile(File $file, array $configuration, string $targetFilePath)
Definition: LocalPreviewHelper.php:117
‪TYPO3\CMS\Core\Resource\AbstractFile\FILETYPE_IMAGE
‪const FILETYPE_IMAGE
Definition: AbstractFile.php:81
‪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:145
‪TYPO3\CMS\Core\Resource\Processing\LocalPreviewHelper\preProcessConfiguration
‪static preProcessConfiguration(array $configuration)
Definition: LocalPreviewHelper.php:41
‪TYPO3\CMS\Core\Imaging\GraphicalFunctions
Definition: GraphicalFunctions.php:33
‪TYPO3\CMS\Core\Resource\Processing\LocalPreviewHelper\process
‪array null process(TaskInterface $task)
Definition: LocalPreviewHelper.php:75
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:26
‪TYPO3\CMS\Core\Resource\Processing\LocalPreviewHelper\$defaultConfiguration
‪static array $defaultConfiguration
Definition: LocalPreviewHelper.php:33
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface\getConfiguration
‪array getConfiguration()
‪TYPO3\CMS\Frontend\Imaging\GifBuilder
Definition: GifBuilder.php:59
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Resource\Processing\LocalPreviewHelper\getTemporaryFilePath
‪non empty string getTemporaryFilePath(TaskInterface $task)
Definition: LocalPreviewHelper.php:104
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:24
‪TYPO3\CMS\Core\Utility\MathUtility\forceIntegerInRange
‪static int forceIntegerInRange(mixed $theInt, int $min, int $max=2000000000, int $defaultValue=0)
Definition: MathUtility.php:34
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:51
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface\getTargetFileExtension
‪string getTargetFileExtension()