TYPO3 CMS  TYPO3_8-7
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 
20 
25 {
29  protected $logger;
30 
34  public function __construct()
35  {
37  $logManager = GeneralUtility::makeInstance(LogManager::class);
38  $this->logger = $logManager->getLogger(__CLASS__);
39  }
40 
47  public function canProcessTask(TaskInterface $task)
48  {
49  $canProcessTask = $task->getType() === 'Image';
50  $canProcessTask = $canProcessTask & in_array($task->getName(), ['Preview', 'CropScaleMask']);
51  return $canProcessTask;
52  }
53 
60  public function processTask(TaskInterface $task)
61  {
62  if (!$this->canProcessTask($task)) {
63  throw new \InvalidArgumentException('Cannot process task of type "' . $task->getType() . '.' . $task->getName() . '"', 1350570621);
64  }
65  if ($this->checkForExistingTargetFile($task)) {
66  return;
67  }
68  $helper = $this->getHelperByTaskName($task->getName());
69  try {
70  $result = $helper->process($task);
71  if ($result === null) {
72  $task->setExecuted(true);
73  $task->getTargetFile()->setUsesOriginalFile();
74  } elseif (!empty($result['filePath']) && file_exists($result['filePath'])) {
75  $task->setExecuted(true);
76  $imageDimensions = $this->getGraphicalFunctionsObject()->getImageDimensions($result['filePath']);
77  $task->getTargetFile()->setName($task->getTargetFileName());
78  $task->getTargetFile()->updateProperties(
79  ['width' => $imageDimensions[0], 'height' => $imageDimensions[1], 'size' => filesize($result['filePath']), 'checksum' => $task->getConfigurationChecksum()]
80  );
81  $task->getTargetFile()->updateWithLocalFile($result['filePath']);
82  } elseif (!empty($result['width']) && !empty($result['height']) && empty($result['filePath'])) {
83  // New dimensions + no new file (for instance svg)
84  $task->setExecuted(true);
85  $task->getTargetFile()->setUsesOriginalFile();
86  $task->getTargetFile()->updateProperties(
87  ['width' => $result['width'], 'height' => $result['height'], 'size' => $task->getSourceFile()->getSize(), 'checksum' => $task->getConfigurationChecksum()]
88  );
89  } else {
90  // Seems we have no valid processing result
91  $task->setExecuted(false);
92  }
93  } catch (\Exception $e) {
94  $task->setExecuted(false);
95  }
96  }
97 
105  protected function checkForExistingTargetFile(TaskInterface $task)
106  {
107  // the storage of the processed file, not of the original file!
108  $storage = $task->getTargetFile()->getStorage();
109  $processingFolder = $storage->getProcessingFolder($task->getSourceFile());
110 
111  // explicitly check for the raw filename here, as we check for files that existed before we even started
112  // processing, i.e. that were processed earlier
113  if ($processingFolder->hasFile($task->getTargetFileName())) {
114  // When the processed file already exists set it as processed file
115  $task->getTargetFile()->setName($task->getTargetFileName());
116 
117  // If the processed file is stored on a remote server, we must fetch a local copy of the file, as we
118  // have no API for fetching file metadata from a remote file.
119  $localProcessedFile = $storage->getFileForLocalProcessing($task->getTargetFile(), false);
120  $task->setExecuted(true);
121  $imageDimensions = $this->getGraphicalFunctionsObject()->getImageDimensions($localProcessedFile);
122  $properties = [
123  'width' => $imageDimensions[0],
124  'height' => $imageDimensions[1],
125  'size' => filesize($localProcessedFile),
126  'checksum' => $task->getConfigurationChecksum()
127  ];
128  $task->getTargetFile()->updateProperties($properties);
129 
130  return true;
131  }
132  return false;
133  }
134 
140  protected function getHelperByTaskName($taskName)
141  {
142  switch ($taskName) {
143  case 'Preview':
144  $helper = GeneralUtility::makeInstance(LocalPreviewHelper::class, $this);
145  break;
146  case 'CropScaleMask':
147  $helper = GeneralUtility::makeInstance(LocalCropScaleMaskHelper::class, $this);
148  break;
149  default:
150  throw new \InvalidArgumentException('Cannot find helper for task name: "' . $taskName . '"', 1353401352);
151  }
152 
153  return $helper;
154  }
155 
159  protected function getGraphicalFunctionsObject()
160  {
161  static $graphicalFunctionsObject = null;
162 
163  if ($graphicalFunctionsObject === null) {
164  $graphicalFunctionsObject = GeneralUtility::makeInstance(GraphicalFunctions::class);
165  $graphicalFunctionsObject->init();
166  }
167 
168  return $graphicalFunctionsObject;
169  }
170 }
static makeInstance($className,... $constructorArguments)