‪TYPO3CMS  11.5
LocalImageProcessor.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 Psr\Log\LoggerAwareInterface;
19 use Psr\Log\LoggerAwareTrait;
22 
26 class ‪LocalImageProcessor implements ‪ProcessorInterface, LoggerAwareInterface
27 {
28  use LoggerAwareTrait;
29 
36  public function ‪canProcessTask(‪TaskInterface $task): bool
37  {
38  return $task->‪getType() === 'Image'
39  && in_array($task->‪getName(), ['Preview', 'CropScaleMask'], true);
40  }
41 
48  public function ‪processTask(‪TaskInterface $task): void
49  {
50  if ($this->‪checkForExistingTargetFile($task)) {
51  return;
52  }
53  $this->‪processTaskWithLocalFile($task, null);
54  }
55 
63  public function ‪processTaskWithLocalFile(‪TaskInterface $task, ?string $localFile): void
64  {
65  $helper = $this->‪getHelperByTaskName($task->‪getName());
66  try {
67  if ($localFile === null) {
68  $result = $helper->process($task);
69  } else {
70  $result = $helper->processWithLocalFile($task, $localFile);
71  }
72  if ($result === null) {
73  $task->‪setExecuted(true);
75  } elseif (!empty($result['filePath']) && file_exists($result['filePath'])) {
76  $task->‪setExecuted(true);
77  $imageDimensions = $this->‪getGraphicalFunctionsObject()->‪getImageDimensions($result['filePath']);
80  'width' => $imageDimensions[0] ?? 0,
81  'height' => $imageDimensions[1] ?? 0,
82  'size' => filesize($result['filePath']),
83  'checksum' => $task->‪getConfigurationChecksum(),
84  ]);
85  $task->‪getTargetFile()->‪updateWithLocalFile($result['filePath']);
86  } else {
87  // Seems we have no valid processing result
88  $task->‪setExecuted(false);
89  }
90  } catch (\‪Exception $e) {
91  // @todo: Swallowing all exceptions including PHP warnings here is a bad idea.
92  // @todo: This should be restricted to more specific exceptions - if at all.
93  // @todo: For now, we at least log the situation.
94  $this->logger->error(sprintf('Processing task of image file'), ['exception' => $e]);
95  $task->‪setExecuted(false);
96  }
97  }
98 
107  {
108  // the storage of the processed file, not of the original file!
109  $storage = $task->‪getTargetFile()->‪getStorage();
110  $processingFolder = $storage->‪getProcessingFolder($task->‪getSourceFile());
111 
112  // explicitly check for the raw filename here, as we check for files that existed before we even started
113  // processing, i.e. that were processed earlier
114  if ($processingFolder->hasFile($task->‪getTargetFileName())) {
115  // When the processed file already exists set it as processed file
117 
118  // If the processed file is stored on a remote server, we must fetch a local copy of the file, as we
119  // have no API for fetching file metadata from a remote file.
120  $localProcessedFile = $storage->getFileForLocalProcessing($task->‪getTargetFile(), false);
121  $task->‪setExecuted(true);
122  $imageDimensions = $this->‪getGraphicalFunctionsObject()->‪getImageDimensions($localProcessedFile);
123  $properties = [
124  'width' => $imageDimensions[0] ?? 0,
125  'height' => $imageDimensions[1] ?? 0,
126  'size' => filesize($localProcessedFile),
127  'checksum' => $task->‪getConfigurationChecksum(),
128  ];
129  $task->‪getTargetFile()->‪updateProperties($properties);
130 
131  return true;
132  }
133  return false;
134  }
135 
141  protected function ‪getHelperByTaskName($taskName)
142  {
143  switch ($taskName) {
144  case 'Preview':
145  $helper = GeneralUtility::makeInstance(LocalPreviewHelper::class);
146  break;
147  case 'CropScaleMask':
148  $helper = GeneralUtility::makeInstance(LocalCropScaleMaskHelper::class);
149  break;
150  default:
151  throw new \InvalidArgumentException('Cannot find helper for task name: "' . $taskName . '"', 1353401352);
152  }
153 
154  return $helper;
155  }
156 
161  {
162  return GeneralUtility::makeInstance(GraphicalFunctions::class);
163  }
164 }
‪TYPO3\CMS\Core\Resource\Processing\LocalPreviewHelper
Definition: LocalPreviewHelper.php:29
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface\getType
‪string getType()
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface
Definition: TaskInterface.php:33
‪TYPO3\CMS\Core\Resource\ResourceStorage\getProcessingFolder
‪Folder getProcessingFolder(File $file=null)
Definition: ResourceStorage.php:2777
‪TYPO3\CMS\Core\Resource\Processing\LocalImageProcessor\processTaskWithLocalFile
‪processTaskWithLocalFile(TaskInterface $task, ?string $localFile)
Definition: LocalImageProcessor.php:63
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface\getSourceFile
‪Resource File getSourceFile()
‪TYPO3\CMS\Core\Resource\Processing\LocalCropScaleMaskHelper
Definition: LocalCropScaleMaskHelper.php:31
‪TYPO3\CMS\Core\Resource\ProcessedFile\setName
‪setName($name)
Definition: ProcessedFile.php:256
‪TYPO3\CMS\Core\Resource\Processing\LocalImageProcessor\getGraphicalFunctionsObject
‪GraphicalFunctions getGraphicalFunctionsObject()
Definition: LocalImageProcessor.php:160
‪TYPO3\CMS\Core\Resource\Processing\LocalImageProcessor\checkForExistingTargetFile
‪bool checkForExistingTargetFile(TaskInterface $task)
Definition: LocalImageProcessor.php:106
‪TYPO3\CMS\Core\Resource\ProcessedFile\setUsesOriginalFile
‪setUsesOriginalFile()
Definition: ProcessedFile.php:421
‪TYPO3\CMS\Core\Imaging\GraphicalFunctions\getImageDimensions
‪array null getImageDimensions($imageFile)
Definition: GraphicalFunctions.php:2163
‪TYPO3\CMS\Core\Resource\Processing
Definition: AbstractGraphicalTask.php:16
‪TYPO3\CMS\Core\Resource\Processing\LocalImageProcessor\getHelperByTaskName
‪LocalCropScaleMaskHelper LocalPreviewHelper getHelperByTaskName($taskName)
Definition: LocalImageProcessor.php:141
‪TYPO3\CMS\Core\Resource\Processing\LocalImageProcessor
Definition: LocalImageProcessor.php:27
‪TYPO3\CMS\Core\Imaging\GraphicalFunctions
Definition: GraphicalFunctions.php:37
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface\getTargetFile
‪Resource ProcessedFile getTargetFile()
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface\getName
‪string getName()
‪TYPO3\CMS\Core\Resource\ProcessedFile\updateWithLocalFile
‪updateWithLocalFile($filePath)
Definition: ProcessedFile.php:188
‪TYPO3\CMS\Core\Resource\Exception
Definition: Exception.php:21
‪TYPO3\CMS\Core\Resource\Processing\LocalImageProcessor\processTask
‪processTask(TaskInterface $task)
Definition: LocalImageProcessor.php:48
‪TYPO3\CMS\Core\Resource\Processing\LocalImageProcessor\canProcessTask
‪bool canProcessTask(TaskInterface $task)
Definition: LocalImageProcessor.php:36
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Resource\ProcessedFile\updateProperties
‪updateProperties(array $properties)
Definition: ProcessedFile.php:347
‪TYPO3\CMS\Core\Resource\Processing\ProcessorInterface
Definition: ProcessorInterface.php:22
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface\setExecuted
‪setExecuted($successful)
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface\getConfigurationChecksum
‪string getConfigurationChecksum()
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface\getTargetFileName
‪string getTargetFileName()
‪TYPO3\CMS\Core\Resource\AbstractFile\getStorage
‪ResourceStorage getStorage()
Definition: AbstractFile.php:395