‪TYPO3CMS  9.5
PreviewProcessing.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 
32 
37 {
41  protected ‪$processor;
42 
47  protected function ‪needsReprocessing($processedFile)
48  {
49  return $processedFile->isNew()
50  || (!$processedFile->usesOriginalFile() && !$processedFile->exists())
51  || $processedFile->isOutdated();
52  }
53 
65  public function ‪processFile(‪FileProcessingService $fileProcessingService, ‪DriverInterface $driver, ‪ProcessedFile $processedFile, ‪File $file, $taskType, array $configuration)
66  {
68  return;
69  }
70  // Check if processing is needed
71  if (!$this->‪needsReprocessing($processedFile)) {
72  return;
73  }
74  // Check if there is a OnlineMediaHelper registered for this file type
76  if ($helper === false) {
77  return;
78  }
79  // Check if helper provides a preview image
80  $temporaryFileName = $helper->‪getPreviewImage($file);
81  if (empty($temporaryFileName) || !file_exists($temporaryFileName)) {
82  return;
83  }
84  $temporaryFileNameForResizedThumb = uniqid(‪Environment::getVarPath() . '/transient/online_media_' . $file->‪getHashedIdentifier()) . '.jpg';
85  $configuration = $processedFile->‪getProcessingConfiguration();
86  switch ($taskType) {
88  $this->‪resizeImage($temporaryFileName, $temporaryFileNameForResizedThumb, $configuration);
89  break;
90 
92  $this->‪cropScaleImage($temporaryFileName, $temporaryFileNameForResizedThumb, $configuration);
93  break;
94  }
95  GeneralUtility::unlink_tempfile($temporaryFileName);
96  if (is_file($temporaryFileNameForResizedThumb)) {
97  $processedFile->‪setName($this->‪getTargetFileName($processedFile));
98  $imageInfo = GeneralUtility::makeInstance(ImageInfo::class, $temporaryFileNameForResizedThumb);
99  $processedFile->‪updateProperties(
100  [
101  'width' => $imageInfo->getWidth(),
102  'height' => $imageInfo->getHeight(),
103  'size' => filesize($temporaryFileNameForResizedThumb),
104  'checksum' => $processedFile->‪getTask()->getConfigurationChecksum()
105  ]
106  );
107  $processedFile->‪updateWithLocalFile($temporaryFileNameForResizedThumb);
108  GeneralUtility::unlink_tempfile($temporaryFileNameForResizedThumb);
109 
111  $processedFileRepository = GeneralUtility::makeInstance(ProcessedFileRepository::class);
112  $processedFileRepository->add($processedFile);
113  }
114  }
115 
121  protected function ‪getTargetFileName(‪ProcessedFile $processedFile, $prefix = 'preview_')
122  {
123  return $prefix . $processedFile->‪getTask()->getConfigurationChecksum() . '_' . $processedFile->‪getOriginalFile()->‪getNameWithoutExtension() . '.jpg';
124  }
125 
131  protected function ‪resizeImage($originalFileName, $temporaryFileName, $configuration)
132  {
133  // Create the temporary file
134  if (empty(‪$GLOBALS['TYPO3_CONF_VARS']['GFX']['processor_enabled'])) {
135  return;
136  }
137 
138  if (file_exists($originalFileName)) {
140  'width' => $configuration['width'],
141  'height' => $configuration['height'],
142  ]);
143  $parameters = '-sample ' . $arguments['width'] . 'x' . $arguments['height']
144  . ' ' . ‪ImageMagickFile::fromFilePath($originalFileName, 0)
145  . ' ' . ‪CommandUtility::escapeShellArgument($temporaryFileName);
146 
147  $cmd = ‪CommandUtility::imageMagickCommand('convert', $parameters) . ' 2>&1';
149  }
150 
151  if (!file_exists($temporaryFileName)) {
152  // Create a error image
153  $graphicalFunctions = $this->‪getGraphicalFunctionsObject();
154  $graphicalFunctions->getTemporaryImageWithText($temporaryFileName, 'No thumb', 'generated!', ‪PathUtility::basename($originalFileName));
155  }
156  }
157 
165  protected function ‪cropScaleImage($originalFileName, $temporaryFileName, $configuration)
166  {
167  if (file_exists($originalFileName)) {
168  $gifBuilder = GeneralUtility::makeInstance(GifBuilder::class);
169 
170  $options = $this->‪getConfigurationForImageCropScaleMask($configuration, $gifBuilder);
171  $info = $gifBuilder->getImageDimensions($originalFileName);
172  $data = $gifBuilder->getImageScale($info, $configuration['width'], $configuration['height'], $options);
173 
174  $info[0] = $data[0];
175  $info[1] = $data[1];
176  $frame = '';
177  $params = $gifBuilder->cmds['jpg'];
178 
179  // Cropscaling:
180  if ($data['crs']) {
181  if (!$data['origW']) {
182  $data['origW'] = $data[0];
183  }
184  if (!$data['origH']) {
185  $data['origH'] = $data[1];
186  }
187  $offsetX = (int)(($data[0] - $data['origW']) * ($data['cropH'] + 100) / 200);
188  $offsetY = (int)(($data[1] - $data['origH']) * ($data['cropV'] + 100) / 200);
189  $params .= ' -crop ' . $data['origW'] . 'x' . $data['origH'] . '+' . $offsetX . '+' . $offsetY . '! ';
190  }
191  $command = $gifBuilder->scalecmd . ' ' . $info[0] . 'x' . $info[1] . '! ' . $params . ' ';
192  $gifBuilder->imageMagickExec($originalFileName, $temporaryFileName, $command, $frame);
193  }
194  if (!file_exists($temporaryFileName)) {
195  // Create a error image
196  $graphicalFunctions = $this->‪getGraphicalFunctionsObject();
197  $graphicalFunctions->getTemporaryImageWithText($temporaryFileName, 'No thumb', 'generated!', ‪PathUtility::basename($originalFileName));
198  }
199  }
200 
208  protected function ‪getConfigurationForImageCropScaleMask(array $configuration, ‪GifBuilder $gifBuilder)
209  {
210  if (!empty($configuration['useSample'])) {
211  $gifBuilder->scalecmd = '-sample';
212  }
213  $options = [];
214  if (!empty($configuration['maxWidth'])) {
215  $options['maxW'] = $configuration['maxWidth'];
216  }
217  if (!empty($configuration['maxHeight'])) {
218  $options['maxH'] = $configuration['maxHeight'];
219  }
220  if (!empty($configuration['minWidth'])) {
221  $options['minW'] = $configuration['minWidth'];
222  }
223  if (!empty($configuration['minHeight'])) {
224  $options['minH'] = $configuration['minHeight'];
225  }
226 
227  $options['noScale'] = $configuration['noScale'];
228 
229  return $options;
230  }
231 
235  protected function ‪getProcessor()
236  {
237  if (!$this->processor) {
238  $this->processor = GeneralUtility::makeInstance(LocalImageProcessor::class);
239  }
240  return ‪$this->processor;
241  }
242 
247  {
248  return GeneralUtility::makeInstance(GraphicalFunctions::class);
249  }
250 }
‪TYPO3\CMS\Core\Resource\OnlineMedia\Processing\PreviewProcessing\resizeImage
‪resizeImage($originalFileName, $temporaryFileName, $configuration)
Definition: PreviewProcessing.php:130
‪TYPO3\CMS\Core\Resource\OnlineMedia\Processing
Definition: PreviewProcessing.php:2
‪TYPO3\CMS\Core\Resource\OnlineMedia\Processing\PreviewProcessing
Definition: PreviewProcessing.php:37
‪TYPO3\CMS\Core\Resource\ProcessedFile\getOriginalFile
‪File getOriginalFile()
Definition: ProcessedFile.php:293
‪TYPO3\CMS\Core\Resource\ProcessedFileRepository
Definition: ProcessedFileRepository.php:29
‪TYPO3\CMS\Core\Resource\ProcessedFile\CONTEXT_IMAGEPREVIEW
‪const CONTEXT_IMAGEPREVIEW
Definition: ProcessedFile.php:50
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:23
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperRegistry
Definition: OnlineMediaHelperRegistry.php:25
‪TYPO3\CMS\Core\Resource\OnlineMedia\Processing\PreviewProcessing\getGraphicalFunctionsObject
‪GraphicalFunctions getGraphicalFunctionsObject()
Definition: PreviewProcessing.php:245
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperInterface\getPreviewImage
‪string getPreviewImage(File $file)
‪TYPO3\CMS\Core\Resource\ProcessedFile\CONTEXT_IMAGECROPSCALEMASK
‪const CONTEXT_IMAGECROPSCALEMASK
Definition: ProcessedFile.php:56
‪TYPO3\CMS\Core\Resource\OnlineMedia\Processing\PreviewProcessing\getConfigurationForImageCropScaleMask
‪array getConfigurationForImageCropScaleMask(array $configuration, GifBuilder $gifBuilder)
Definition: PreviewProcessing.php:207
‪TYPO3\CMS\Core\Resource\Driver\DriverInterface
Definition: DriverInterface.php:22
‪TYPO3\CMS\Core\Resource\AbstractFile\getHashedIdentifier
‪string getHashedIdentifier()
Definition: AbstractFile.php:149
‪TYPO3\CMS\Core\Resource\ProcessedFile\setName
‪setName($name)
Definition: ProcessedFile.php:244
‪TYPO3\CMS\Core\Resource\OnlineMedia\Processing\PreviewProcessing\processFile
‪processFile(FileProcessingService $fileProcessingService, DriverInterface $driver, ProcessedFile $processedFile, File $file, $taskType, array $configuration)
Definition: PreviewProcessing.php:64
‪TYPO3\CMS\Core\Utility\PathUtility\basename
‪static string basename($path)
Definition: PathUtility.php:164
‪TYPO3\CMS\Core\Resource\ProcessedFile\getProcessingConfiguration
‪array getProcessingConfiguration()
Definition: ProcessedFile.php:517
‪TYPO3\CMS\Core\Resource\Processing\LocalImageProcessor
Definition: LocalImageProcessor.php:24
‪TYPO3\CMS\Core\Imaging\GraphicalFunctions
Definition: GraphicalFunctions.php:35
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperRegistry\getOnlineMediaHelper
‪bool OnlineMediaHelperInterface getOnlineMediaHelper(File $file)
Definition: OnlineMediaHelperRegistry.php:42
‪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\OnlineMedia\Processing\PreviewProcessing\$processor
‪LocalImageProcessor $processor
Definition: PreviewProcessing.php:40
‪TYPO3\CMS\Core\Resource\OnlineMedia\Processing\PreviewProcessing\getProcessor
‪LocalImageProcessor getProcessor()
Definition: PreviewProcessing.php:234
‪TYPO3\CMS\Core\Utility\CommandUtility\escapeShellArgument
‪static string escapeShellArgument($input)
Definition: CommandUtility.php:503
‪TYPO3\CMS\Core\Resource\OnlineMedia\Processing\PreviewProcessing\getTargetFileName
‪string getTargetFileName(ProcessedFile $processedFile, $prefix='preview_')
Definition: PreviewProcessing.php:120
‪TYPO3\CMS\Frontend\Imaging\GifBuilder
Definition: GifBuilder.php:52
‪TYPO3\CMS\Core\Type\File\ImageInfo
Definition: ImageInfo.php:25
‪TYPO3\CMS\Core\Resource\ProcessedFile\updateWithLocalFile
‪updateWithLocalFile($filePath)
Definition: ProcessedFile.php:176
‪TYPO3\CMS\Core\Resource\ProcessedFile
Definition: ProcessedFile.php:42
‪TYPO3\CMS\Core\Resource\OnlineMedia\Helpers\OnlineMediaHelperRegistry\getInstance
‪static OnlineMediaHelperRegistry getInstance()
Definition: OnlineMediaHelperRegistry.php:31
‪TYPO3\CMS\Core\Imaging\ImageMagickFile\fromFilePath
‪static ImageMagickFile fromFilePath(string $filePath, int $frame=null)
Definition: ImageMagickFile.php:111
‪TYPO3\CMS\Core\Resource\OnlineMedia\Processing\PreviewProcessing\needsReprocessing
‪bool needsReprocessing($processedFile)
Definition: PreviewProcessing.php:46
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:39
‪TYPO3\CMS\Core\Resource\ProcessedFile\getTask
‪Processing TaskInterface getTask()
Definition: ProcessedFile.php:538
‪TYPO3\CMS\Core\Imaging\ImageMagickFile
Definition: ImageMagickFile.php:28
‪TYPO3\CMS\Core\Resource\Service\FileProcessingService
Definition: FileProcessingService.php:27
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Utility\CommandUtility
Definition: CommandUtility.php:48
‪TYPO3\CMS\Core\Resource\AbstractFile\getNameWithoutExtension
‪string getNameWithoutExtension()
Definition: AbstractFile.php:171
‪TYPO3\CMS\Core\Resource\ProcessedFile\updateProperties
‪updateProperties(array $properties)
Definition: ProcessedFile.php:335
‪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\Core\Environment\getVarPath
‪static string getVarPath()
Definition: Environment.php:165
‪TYPO3\CMS\Core\Resource\OnlineMedia\Processing\PreviewProcessing\cropScaleImage
‪cropScaleImage($originalFileName, $temporaryFileName, $configuration)
Definition: PreviewProcessing.php:164