‪TYPO3CMS  9.5
LocalImageProcessor.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 
19 
24 {
31  public function ‪canProcessTask(‪TaskInterface $task)
32  {
33  $canProcessTask = $task->‪getType() === 'Image';
34  $canProcessTask = $canProcessTask & in_array($task->‪getName(), ['Preview', 'CropScaleMask']);
35  return $canProcessTask;
36  }
37 
44  public function ‪processTask(‪TaskInterface $task)
45  {
46  if (!$this->‪canProcessTask($task)) {
47  throw new \InvalidArgumentException('Cannot process task of type "' . $task->‪getType() . '.' . $task->‪getName() . '"', 1350570621);
48  }
49  if ($this->‪checkForExistingTargetFile($task)) {
50  return;
51  }
52  $helper = $this->‪getHelperByTaskName($task->‪getName());
53  try {
54  $result = $helper->process($task);
55  if ($result === null) {
56  $task->‪setExecuted(true);
58  } elseif (!empty($result['filePath']) && file_exists($result['filePath'])) {
59  $task->‪setExecuted(true);
60  $imageDimensions = $this->‪getGraphicalFunctionsObject()->‪getImageDimensions($result['filePath']);
63  ['width' => $imageDimensions[0], 'height' => $imageDimensions[1], 'size' => filesize($result['filePath']), 'checksum' => $task->‪getConfigurationChecksum()]
64  );
65  $task->‪getTargetFile()->‪updateWithLocalFile($result['filePath']);
66  } elseif (!empty($result['width']) && !empty($result['height']) && empty($result['filePath'])) {
67  // New dimensions + no new file (for instance svg)
68  $task->‪setExecuted(true);
71  ['width' => $result['width'], 'height' => $result['height'], 'size' => $task->‪getSourceFile()->‪getSize(), 'checksum' => $task->‪getConfigurationChecksum()]
72  );
73  } else {
74  // Seems we have no valid processing result
75  $task->‪setExecuted(false);
76  }
77  } catch (\‪Exception $e) {
78  $task->‪setExecuted(false);
79  }
80  }
81 
90  {
91  // the storage of the processed file, not of the original file!
92  $storage = $task->‪getTargetFile()->‪getStorage();
93  $processingFolder = $storage->‪getProcessingFolder($task->‪getSourceFile());
94 
95  // explicitly check for the raw filename here, as we check for files that existed before we even started
96  // processing, i.e. that were processed earlier
97  if ($processingFolder->hasFile($task->‪getTargetFileName())) {
98  // When the processed file already exists set it as processed file
100 
101  // If the processed file is stored on a remote server, we must fetch a local copy of the file, as we
102  // have no API for fetching file metadata from a remote file.
103  $localProcessedFile = $storage->getFileForLocalProcessing($task->‪getTargetFile(), false);
104  $task->‪setExecuted(true);
105  $imageDimensions = $this->‪getGraphicalFunctionsObject()->‪getImageDimensions($localProcessedFile);
106  $properties = [
107  'width' => $imageDimensions[0],
108  'height' => $imageDimensions[1],
109  'size' => filesize($localProcessedFile),
110  'checksum' => $task->‪getConfigurationChecksum()
111  ];
112  $task->‪getTargetFile()->‪updateProperties($properties);
113 
114  return true;
115  }
116  return false;
117  }
118 
124  protected function ‪getHelperByTaskName($taskName)
125  {
126  switch ($taskName) {
127  case 'Preview':
128  $helper = GeneralUtility::makeInstance(LocalPreviewHelper::class, $this);
129  break;
130  case 'CropScaleMask':
131  $helper = GeneralUtility::makeInstance(LocalCropScaleMaskHelper::class, $this);
132  break;
133  default:
134  throw new \InvalidArgumentException('Cannot find helper for task name: "' . $taskName . '"', 1353401352);
135  }
136 
137  return $helper;
138  }
139 
144  {
145  return GeneralUtility::makeInstance(GraphicalFunctions::class);
146  }
147 }
‪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:31
‪TYPO3\CMS\Core\Resource\ResourceStorage\getProcessingFolder
‪Folder getProcessingFolder(File $file=null)
Definition: ResourceStorage.php:2999
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface\getSourceFile
‪Resource File getSourceFile()
‪TYPO3\CMS\Core\Resource\Processing\LocalCropScaleMaskHelper
Definition: LocalCropScaleMaskHelper.php:27
‪TYPO3\CMS\Core\Resource\ProcessedFile\setName
‪setName($name)
Definition: ProcessedFile.php:244
‪TYPO3\CMS\Core\Resource\Processing\LocalImageProcessor\getGraphicalFunctionsObject
‪GraphicalFunctions getGraphicalFunctionsObject()
Definition: LocalImageProcessor.php:143
‪TYPO3\CMS\Core\Resource\Processing\LocalImageProcessor\checkForExistingTargetFile
‪bool checkForExistingTargetFile(TaskInterface $task)
Definition: LocalImageProcessor.php:89
‪TYPO3\CMS\Core\Resource\ProcessedFile\setUsesOriginalFile
‪setUsesOriginalFile()
Definition: ProcessedFile.php:402
‪TYPO3\CMS\Core\Imaging\GraphicalFunctions\getImageDimensions
‪array null getImageDimensions($imageFile)
Definition: GraphicalFunctions.php:2176
‪TYPO3\CMS\Core\Resource\Processing
Definition: AbstractGraphicalTask.php:2
‪TYPO3\CMS\Core\Resource\Processing\LocalImageProcessor\getHelperByTaskName
‪LocalCropScaleMaskHelper LocalPreviewHelper getHelperByTaskName($taskName)
Definition: LocalImageProcessor.php:124
‪TYPO3\CMS\Core\Resource\Processing\LocalImageProcessor
Definition: LocalImageProcessor.php:24
‪TYPO3\CMS\Core\Imaging\GraphicalFunctions
Definition: GraphicalFunctions.php:35
‪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:176
‪TYPO3\CMS\Core\Resource\Exception
Definition: Exception.php:21
‪TYPO3\CMS\Core\Resource\AbstractFile\getSize
‪int null getSize()
Definition: AbstractFile.php:182
‪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:31
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Resource\ProcessedFile\updateProperties
‪updateProperties(array $properties)
Definition: ProcessedFile.php:335
‪TYPO3\CMS\Core\Resource\Processing\ProcessorInterface
Definition: ProcessorInterface.php:21
‪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:361