‪TYPO3CMS  10.4
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 
20 
25 {
32  public function ‪canProcessTask(‪TaskInterface $task): bool
33  {
34  return $task->‪getType() === 'Image'
35  && in_array($task->‪getName(), ['Preview', 'CropScaleMask'], true);
36  }
37 
44  public function ‪processTask(‪TaskInterface $task): void
45  {
46  if ($this->‪checkForExistingTargetFile($task)) {
47  return;
48  }
49  $this->‪processTaskWithLocalFile($task, null);
50  }
51 
59  public function ‪processTaskWithLocalFile(‪TaskInterface $task, ?string $localFile): void
60  {
61  $helper = $this->‪getHelperByTaskName($task->‪getName());
62  try {
63  if ($localFile === null) {
64  $result = $helper->process($task);
65  } else {
66  $result = $helper->processWithLocalFile($task, $localFile);
67  }
68  if ($result === null) {
69  $task->‪setExecuted(true);
71  } elseif (!empty($result['filePath']) && file_exists($result['filePath'])) {
72  $task->‪setExecuted(true);
73  $imageDimensions = $this->‪getGraphicalFunctionsObject()->‪getImageDimensions($result['filePath']);
76  ['width' => $imageDimensions[0], 'height' => $imageDimensions[1], 'size' => filesize($result['filePath']), 'checksum' => $task->‪getConfigurationChecksum()]
77  );
78  $task->‪getTargetFile()->‪updateWithLocalFile($result['filePath']);
79  } else {
80  // Seems we have no valid processing result
81  $task->‪setExecuted(false);
82  }
83  } catch (\‪Exception $e) {
84  $task->‪setExecuted(false);
85  }
86  }
87 
96  {
97  // the storage of the processed file, not of the original file!
98  $storage = $task->‪getTargetFile()->‪getStorage();
99  $processingFolder = $storage->‪getProcessingFolder($task->‪getSourceFile());
100 
101  // explicitly check for the raw filename here, as we check for files that existed before we even started
102  // processing, i.e. that were processed earlier
103  if ($processingFolder->hasFile($task->‪getTargetFileName())) {
104  // When the processed file already exists set it as processed file
106 
107  // If the processed file is stored on a remote server, we must fetch a local copy of the file, as we
108  // have no API for fetching file metadata from a remote file.
109  $localProcessedFile = $storage->getFileForLocalProcessing($task->‪getTargetFile(), false);
110  $task->‪setExecuted(true);
111  $imageDimensions = $this->‪getGraphicalFunctionsObject()->‪getImageDimensions($localProcessedFile);
112  $properties = [
113  'width' => $imageDimensions[0],
114  'height' => $imageDimensions[1],
115  'size' => filesize($localProcessedFile),
116  'checksum' => $task->‪getConfigurationChecksum()
117  ];
118  $task->‪getTargetFile()->‪updateProperties($properties);
119 
120  return true;
121  }
122  return false;
123  }
124 
130  protected function ‪getHelperByTaskName($taskName)
131  {
132  switch ($taskName) {
133  case 'Preview':
134  $helper = GeneralUtility::makeInstance(LocalPreviewHelper::class);
135  break;
136  case 'CropScaleMask':
137  $helper = GeneralUtility::makeInstance(LocalCropScaleMaskHelper::class);
138  break;
139  default:
140  throw new \InvalidArgumentException('Cannot find helper for task name: "' . $taskName . '"', 1353401352);
141  }
142 
143  return $helper;
144  }
145 
150  {
151  return GeneralUtility::makeInstance(GraphicalFunctions::class);
152  }
153 }
‪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:2738
‪TYPO3\CMS\Core\Resource\Processing\LocalImageProcessor\processTaskWithLocalFile
‪processTaskWithLocalFile(TaskInterface $task, ?string $localFile)
Definition: LocalImageProcessor.php:59
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface\getSourceFile
‪Resource File getSourceFile()
‪TYPO3\CMS\Core\Resource\Processing\LocalCropScaleMaskHelper
Definition: LocalCropScaleMaskHelper.php:30
‪TYPO3\CMS\Core\Resource\ProcessedFile\setName
‪setName($name)
Definition: ProcessedFile.php:254
‪TYPO3\CMS\Core\Resource\Processing\LocalImageProcessor\getGraphicalFunctionsObject
‪GraphicalFunctions getGraphicalFunctionsObject()
Definition: LocalImageProcessor.php:149
‪TYPO3\CMS\Core\Resource\Processing\LocalImageProcessor\checkForExistingTargetFile
‪bool checkForExistingTargetFile(TaskInterface $task)
Definition: LocalImageProcessor.php:95
‪TYPO3\CMS\Core\Resource\ProcessedFile\setUsesOriginalFile
‪setUsesOriginalFile()
Definition: ProcessedFile.php:419
‪TYPO3\CMS\Core\Imaging\GraphicalFunctions\getImageDimensions
‪array null getImageDimensions($imageFile)
Definition: GraphicalFunctions.php:2165
‪TYPO3\CMS\Core\Resource\Processing
Definition: AbstractGraphicalTask.php:16
‪TYPO3\CMS\Core\Resource\Processing\LocalImageProcessor\getHelperByTaskName
‪LocalCropScaleMaskHelper LocalPreviewHelper getHelperByTaskName($taskName)
Definition: LocalImageProcessor.php:130
‪TYPO3\CMS\Core\Resource\Processing\LocalImageProcessor
Definition: LocalImageProcessor.php:25
‪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:186
‪TYPO3\CMS\Core\Resource\Exception
Definition: Exception.php:22
‪TYPO3\CMS\Core\Resource\Processing\LocalImageProcessor\processTask
‪processTask(TaskInterface $task)
Definition: LocalImageProcessor.php:44
‪TYPO3\CMS\Core\Resource\Processing\LocalImageProcessor\canProcessTask
‪bool canProcessTask(TaskInterface $task)
Definition: LocalImageProcessor.php:32
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Resource\ProcessedFile\updateProperties
‪updateProperties(array $properties)
Definition: ProcessedFile.php:345
‪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:390