‪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 
18 use TYPO3\CMS\Core\Imaging\GraphicalFunctions;
24 
29 {
35  protected static array ‪$defaultConfiguration = [
36  'width' => 64,
37  'height' => 64,
38  ];
39 
45  public static function ‪preProcessConfiguration(array $configuration): array
46  {
47  $configuration = array_replace(static::$defaultConfiguration, $configuration);
48  $configuration['width'] = ‪MathUtility::forceIntegerInRange($configuration['width'], 1, 1000);
49  $configuration['height'] = ‪MathUtility::forceIntegerInRange($configuration['height'], 1, 1000);
50 
51  return array_filter(
52  $configuration,
53  static function (string|int|bool|array|null $value, string $name): bool {
54  return !empty($value) && in_array($name, ['width', 'height'], true);
55  },
56  ARRAY_FILTER_USE_BOTH
57  );
58  }
59 
79  public function ‪process(‪TaskInterface $task)
80  {
81  $sourceFile = $task->‪getSourceFile();
82  $task->‪sanitizeConfiguration();
83  $configuration = $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 
99  public function ‪processWithLocalFile(‪TaskInterface $task, string $localFile): ?array
100  {
101  return $this->‪generatePreviewFromLocalFile($localFile, $task->‪getConfiguration(), $this->getTemporaryFilePath($task));
102  }
103 
109  protected function ‪getTemporaryFilePath(‪TaskInterface $task)
110  {
111  return GeneralUtility::tempnam('preview_', '.' . $task->‪getTargetFileExtension());
112  }
113 
122  protected function ‪generatePreviewFromFile(‪File $file, array $configuration, string $targetFilePath)
123  {
124  // Check file extension
125  if (!$file->‪isType(FileType::IMAGE) && !$file->‪isImage()) {
126  // Create a default image
127  $graphicalFunctions = GeneralUtility::makeInstance(GifBuilder::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  return $this->‪generatePreviewFromLocalFile($file->‪getForLocalProcessing(false), $configuration, $targetFilePath);
140  }
141 
150  protected function ‪generatePreviewFromLocalFile(string $originalFileName, array $configuration, string $targetFilePath)
151  {
152  // Create the temporary file
153  $imageService = GeneralUtility::makeInstance(GraphicalFunctions::class);
154  $result = $imageService->resize($originalFileName, 'WEB', $configuration['width'], $configuration['height'], '', ['sample' => true]);
155  if ($result) {
156  $targetFilePath = $result->getRealPath();
157  }
158  if (!file_exists($targetFilePath)) {
159  // Create an error gif
160  $graphicalFunctions = GeneralUtility::makeInstance(GifBuilder::class);
161  $graphicalFunctions->getTemporaryImageWithText(
162  $targetFilePath,
163  'No thumb',
164  'generated!'
165  );
166  }
167 
168  return [
169  'filePath' => $targetFilePath,
170  ];
171  }
172 }
‪TYPO3\CMS\Core\Resource\Processing\LocalPreviewHelper
Definition: LocalPreviewHelper.php:29
‪TYPO3\CMS\Core\Resource\AbstractFile\getForLocalProcessing
‪non empty string getForLocalProcessing(bool $writable=true)
Definition: AbstractFile.php:576
‪TYPO3\CMS\Core\Resource\Processing\LocalPreviewHelper\processWithLocalFile
‪processWithLocalFile(TaskInterface $task, string $localFile)
Definition: LocalPreviewHelper.php:99
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface\getTargetFileExtension
‪getTargetFileExtension()
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface
Definition: TaskInterface.php:34
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface\getConfiguration
‪getConfiguration()
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface\sanitizeConfiguration
‪sanitizeConfiguration()
‪TYPO3\CMS\Core\Resource\AbstractFile\getName
‪getName()
Definition: AbstractFile.php:157
‪TYPO3\CMS\Core\Resource\AbstractFile\isImage
‪bool isImage()
Definition: AbstractFile.php:301
‪TYPO3\CMS\Core\Resource\Processing\LocalPreviewHelper\generatePreviewFromFile
‪array generatePreviewFromFile(File $file, array $configuration, string $targetFilePath)
Definition: LocalPreviewHelper.php:122
‪TYPO3\CMS\Core\Resource\Processing
Definition: AbstractTask.php:18
‪TYPO3\CMS\Core\Resource\Processing\LocalPreviewHelper\generatePreviewFromLocalFile
‪array generatePreviewFromLocalFile(string $originalFileName, array $configuration, string $targetFilePath)
Definition: LocalPreviewHelper.php:150
‪TYPO3\CMS\Core\Resource\AbstractFile\isType
‪isType(FileType $fileType)
Definition: AbstractFile.php:292
‪TYPO3\CMS\Core\Resource\Processing\LocalPreviewHelper\preProcessConfiguration
‪static preProcessConfiguration(array $configuration)
Definition: LocalPreviewHelper.php:45
‪TYPO3\CMS\Core\Resource\Processing\LocalPreviewHelper\process
‪array null process(TaskInterface $task)
Definition: LocalPreviewHelper.php:79
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:26
‪TYPO3\CMS\Core\Resource\Processing\LocalPreviewHelper\$defaultConfiguration
‪static array $defaultConfiguration
Definition: LocalPreviewHelper.php:35
‪TYPO3\CMS\Frontend\Imaging\GifBuilder
Definition: GifBuilder.php:58
‪TYPO3\CMS\Core\Resource\Processing\LocalPreviewHelper\getTemporaryFilePath
‪non empty string getTemporaryFilePath(TaskInterface $task)
Definition: LocalPreviewHelper.php:109
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:24
‪TYPO3\CMS\Core\Resource\FileType
‪FileType
Definition: FileType.php:21
‪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:52
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface\getSourceFile
‪getSourceFile()