TYPO3 CMS  TYPO3_8-7
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 
30 
35 {
39  protected $processor;
40 
45  protected function needsReprocessing($processedFile)
46  {
47  return $processedFile->isNew()
48  || (!$processedFile->usesOriginalFile() && !$processedFile->exists())
49  || $processedFile->isOutdated();
50  }
51 
63  public function processFile(FileProcessingService $fileProcessingService, DriverInterface $driver, ProcessedFile $processedFile, File $file, $taskType, array $configuration)
64  {
65  if ($taskType !== 'Image.Preview' && $taskType !== 'Image.CropScaleMask') {
66  return;
67  }
68  // Check if processing is needed
69  if (!$this->needsReprocessing($processedFile)) {
70  return;
71  }
72  // Check if there is a OnlineMediaHelper registered for this file type
73  $helper = OnlineMediaHelperRegistry::getInstance()->getOnlineMediaHelper($file);
74  if ($helper === false) {
75  return;
76  }
77  // Check if helper provides a preview image
78  $temporaryFileName = $helper->getPreviewImage($file);
79  if (empty($temporaryFileName) || !file_exists($temporaryFileName)) {
80  return;
81  }
82  $temporaryFileNameForResizedThumb = uniqid(PATH_site . 'typo3temp/var/transient/online_media_' . $file->getHashedIdentifier()) . '.jpg';
83  $configuration = $processedFile->getProcessingConfiguration();
84  switch ($taskType) {
85  case 'Image.Preview':
86  $this->resizeImage($temporaryFileName, $temporaryFileNameForResizedThumb, $configuration);
87  break;
88 
89  case 'Image.CropScaleMask':
90  $this->cropScaleImage($temporaryFileName, $temporaryFileNameForResizedThumb, $configuration);
91  break;
92  }
93  GeneralUtility::unlink_tempfile($temporaryFileName);
94  if (is_file($temporaryFileNameForResizedThumb)) {
95  $processedFile->setName($this->getTargetFileName($processedFile));
96  $imageInfo = GeneralUtility::makeInstance(ImageInfo::class, $temporaryFileNameForResizedThumb);
97  $processedFile->updateProperties(
98  [
99  'width' => $imageInfo->getWidth(),
100  'height' => $imageInfo->getHeight(),
101  'size' => filesize($temporaryFileNameForResizedThumb),
102  'checksum' => $processedFile->getTask()->getConfigurationChecksum()
103  ]
104  );
105  $processedFile->updateWithLocalFile($temporaryFileNameForResizedThumb);
106  GeneralUtility::unlink_tempfile($temporaryFileNameForResizedThumb);
107 
109  $processedFileRepository = GeneralUtility::makeInstance(ProcessedFileRepository::class);
110  $processedFileRepository->add($processedFile);
111  }
112  }
113 
119  protected function getTargetFileName(ProcessedFile $processedFile, $prefix = 'preview_')
120  {
121  return $prefix . $processedFile->getTask()->getConfigurationChecksum() . '_' . $processedFile->getOriginalFile()->getNameWithoutExtension() . '.jpg';
122  }
123 
129  protected function resizeImage($originalFileName, $temporaryFileName, $configuration)
130  {
131  // Create the temporary file
132  if (empty($GLOBALS['TYPO3_CONF_VARS']['GFX']['processor_enabled'])) {
133  return;
134  }
135 
136  if (file_exists($originalFileName)) {
138  'width' => $configuration['width'],
139  'height' => $configuration['height'],
140  ]);
141  $parameters = '-sample ' . $arguments['width'] . 'x' . $arguments['height']
142  . ' ' . ImageMagickFile::fromFilePath($originalFileName, 0)
143  . ' ' . CommandUtility::escapeShellArgument($temporaryFileName);
144 
145  $cmd = CommandUtility::imageMagickCommand('convert', $parameters) . ' 2>&1';
146  CommandUtility::exec($cmd);
147  }
148 
149  if (!file_exists($temporaryFileName)) {
150  // Create a error image
151  $graphicalFunctions = $this->getGraphicalFunctionsObject();
152  $graphicalFunctions->getTemporaryImageWithText($temporaryFileName, 'No thumb', 'generated!', basename($originalFileName));
153  }
154  }
155 
163  protected function cropScaleImage($originalFileName, $temporaryFileName, $configuration)
164  {
165  if (file_exists($originalFileName)) {
167  $gifBuilder = GeneralUtility::makeInstance(GifBuilder::class);
168  $gifBuilder->init();
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!', 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 
246  protected function getGraphicalFunctionsObject()
247  {
248  static $graphicalFunctionsObject = null;
249  if ($graphicalFunctionsObject === null) {
250  $graphicalFunctionsObject = GeneralUtility::makeInstance(GraphicalFunctions::class);
251  $graphicalFunctionsObject->init();
252  }
253  return $graphicalFunctionsObject;
254  }
255 }
static unlink_tempfile($uploadedTempFileName)
static exec($command, &$output=null, &$returnValue=0)
getConfigurationForImageCropScaleMask(array $configuration, GifBuilder $gifBuilder)
getTargetFileName(ProcessedFile $processedFile, $prefix='preview_')
static makeInstance($className,... $constructorArguments)
static imageMagickCommand($command, $parameters, $path='')
resizeImage($originalFileName, $temporaryFileName, $configuration)
static fromFilePath(string $filePath, int $frame=null)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']