TYPO3  7.6
LocalImageProcessor.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Core\Resource\Processing;
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 
18 
23 {
27  protected $logger;
28 
32  public function __construct()
33  {
35  $logManager = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Log\LogManager::class);
36  $this->logger = $logManager->getLogger(__CLASS__);
37  }
38 
45  public function canProcessTask(TaskInterface $task)
46  {
47  $canProcessTask = $task->getType() === 'Image';
48  $canProcessTask = $canProcessTask & in_array($task->getName(), array('Preview', 'CropScaleMask'));
49  return $canProcessTask;
50  }
51 
58  public function processTask(TaskInterface $task)
59  {
60  if (!$this->canProcessTask($task)) {
61  throw new \InvalidArgumentException('Cannot process task of type "' . $task->getType() . '.' . $task->getName() . '"', 1350570621);
62  }
63  if ($this->checkForExistingTargetFile($task)) {
64  return;
65  }
66  $helper = $this->getHelperByTaskName($task->getName());
67  try {
68  $result = $helper->process($task);
69  if ($result === null) {
70  $task->setExecuted(true);
71  $task->getTargetFile()->setUsesOriginalFile();
72  } elseif (!empty($result['filePath']) && file_exists($result['filePath'])) {
73  $task->setExecuted(true);
74  $imageDimensions = $this->getGraphicalFunctionsObject()->getImageDimensions($result['filePath']);
75  $task->getTargetFile()->setName($task->getTargetFileName());
76  $task->getTargetFile()->updateProperties(
77  array('width' => $imageDimensions[0], 'height' => $imageDimensions[1], 'size' => filesize($result['filePath']), 'checksum' => $task->getConfigurationChecksum())
78  );
79  $task->getTargetFile()->updateWithLocalFile($result['filePath']);
80 
81  // New dimensions + no new file (for instance svg)
82  } elseif (!empty($result['width']) && !empty($result['height']) && empty($result['filePath'])) {
83  $task->setExecuted(true);
84  $task->getTargetFile()->setUsesOriginalFile();
85  $task->getTargetFile()->updateProperties(
86  array('width' => $result['width'], 'height' => $result['height'], 'size' => $task->getSourceFile()->getSize(), 'checksum' => $task->getConfigurationChecksum())
87  );
88 
89  // Seems we have no valid processing result
90  } else {
91  $task->setExecuted(false);
92  }
93  } catch (\Exception $e) {
94  $task->setExecuted(false);
95  }
96  }
97 
105  protected function checkForExistingTargetFile(TaskInterface $task)
106  {
107  $processingFolder = $task->getTargetFile()->getStorage()->getProcessingFolder();
108  $storage = $task->getTargetFile()->getStorage();
109  // @todo: make proper use of the FAL API, see https://forge.typo3.org/issues/67126
110  if ($processingFolder->hasFile($task->getTargetFileName()) && $storage->getDriverType() === 'Local') {
111  $processedFileIdentifier = rtrim($processingFolder->getIdentifier(), '/') . '/' . $task->getTargetFileName();
112  $configuration = $storage->getConfiguration();
113  if ($configuration['pathType'] === 'relative') {
114  $absoluteBasePath = PATH_site . $configuration['basePath'];
115  } else {
116  $absoluteBasePath = $configuration['basePath'];
117  }
118  $targetFile = $absoluteBasePath . ltrim($processedFileIdentifier, '/');
119 
120  $task->setExecuted(true);
121  $imageDimensions = $this->getGraphicalFunctionsObject()->getImageDimensions($targetFile);
122  $task->getTargetFile()->setName($task->getTargetFileName());
123  $properties = array(
124  'width' => $imageDimensions[0],
125  'height' => $imageDimensions[1],
126  'size' => filesize($targetFile),
127  'checksum' => $task->getConfigurationChecksum()
128  );
129  $task->getTargetFile()->updateProperties($properties);
130 
131  return true;
132  } else {
133  return false;
134  }
135  }
136 
142  protected function getHelperByTaskName($taskName)
143  {
144  switch ($taskName) {
145  case 'Preview':
146  $helper = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\Processing\LocalPreviewHelper::class, $this);
147  break;
148  case 'CropScaleMask':
149  $helper = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\Processing\LocalCropScaleMaskHelper::class, $this);
150  break;
151  default:
152  throw new \InvalidArgumentException('Cannot find helper for task name: "' . $taskName . '"', 1353401352);
153  }
154 
155  return $helper;
156  }
157 
172  public function getTemporaryImageWithText($filename, $textline1, $textline2, $textline3)
173  {
175  $graphicalFunctions = $this->getGraphicalFunctionsObject();
176  $graphicalFunctions->getTemporaryImageWithText($filename, $textline1, $textline2, $textline3);
177  }
178 
182  protected function getGraphicalFunctionsObject()
183  {
184  static $graphicalFunctionsObject = null;
185 
186  if ($graphicalFunctionsObject === null) {
187  $graphicalFunctionsObject = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Imaging\GraphicalFunctions::class);
188  }
189 
190  return $graphicalFunctionsObject;
191  }
192 }