‪TYPO3CMS  ‪main
SvgImageProcessor.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
23 use TYPO3\CMS\Core\Imaging\ImageProcessingInstructions;
26 use TYPO3\CMS\Core\Type\File\ImageInfo;
28 
35 {
36  private int ‪$defaultSvgDimension = 64;
37 
38  public function ‪canProcessTask(‪TaskInterface $task): bool
39  {
40  return $task->‪getType() === 'Image'
41  && in_array($task->‪getName(), ['Preview', 'CropScaleMask'], true)
42  && $task->‪getTargetFileExtension() === 'svg';
43  }
44 
50  public function ‪processTask(‪TaskInterface $task): void
51  {
52  try {
53  $processingInstructions = ImageProcessingInstructions::fromProcessingTask($task);
54  $imageDimension = new ‪ImageDimension($processingInstructions->width, $processingInstructions->height);
56  $processingInstructions = new ImageProcessingInstructions(
57  width: $this->defaultSvgDimension,
58  height: $this->defaultSvgDimension,
59  );
60  // To not fail image processing, we just assume an SVG image dimension here
61  $imageDimension = new ‪ImageDimension(
62  width: $this->defaultSvgDimension,
63  height: $this->defaultSvgDimension
64  );
65  }
66 
67  $task->‪getTargetFile()->updateProperties(
68  [
69  'width' => $imageDimension->getWidth(),
70  'height' => $imageDimension->getHeight(),
71  'size' => $task->‪getSourceFile()->getSize(),
72  'checksum' => $task->‪getConfigurationChecksum(),
73  ]
74  );
75 
76  if ($this->‪checkForExistingTargetFile($task)) {
77  return;
78  }
79 
80  $cropArea = $processingInstructions->cropArea;
81  if ($cropArea === null || $cropArea->makeRelativeBasedOnFile($task->‪getSourceFile())->isEmpty()) {
82  $task->‪setExecuted(true);
83  $task->‪getTargetFile()->setUsesOriginalFile();
84  return;
85  }
86 
87  $this->‪applyCropping($task, $cropArea, $imageDimension);
88  }
89 
101  protected function ‪applyCropping(‪TaskInterface $task, ‪Area $cropArea, ‪ImageDimension $imageDimension): void
102  {
103  $processedSvg = GeneralUtility::makeInstance(SvgManipulation::class)->cropScaleSvgString(
104  $task->‪getSourceFile()->getContents(),
105  $cropArea,
106  $imageDimension
107  );
108  // Save the output as a new processed file.
109  $temporaryFilename = $this->‪getFilenameForSvgCropScaleMask($task);
110  ‪GeneralUtility::writeFile($temporaryFilename, $processedSvg->saveXML());
111 
112  $task->‪setExecuted(true);
113  $imageInformation = GeneralUtility::makeInstance(ImageInfo::class, $temporaryFilename);
114 
115  $task->‪getTargetFile()->setName($task->‪getTargetFileName());
116 
117  $task->‪getTargetFile()->updateProperties([
118  // @todo: Use round() instead of int-cast to avoid an implicit floor()?
119  'width' => (string)$imageDimension->getWidth(),
120  'height' => (string)$imageDimension->getHeight(),
121  'size' => $imageInformation->getSize(),
122  'checksum' => $task->‪getConfigurationChecksum(),
123  ]);
124  $task->‪getTargetFile()->updateWithLocalFile($temporaryFilename);
125  // The temporary file is removed again
126  GeneralUtility::unlink_tempfile($temporaryFilename);
127  }
128 
136  protected function ‪checkForExistingTargetFile(‪TaskInterface $task): bool
137  {
138  // the storage of the processed file, not of the original file!
139  $storage = $task->‪getTargetFile()->getStorage();
140  $processingFolder = $storage->getProcessingFolder($task->‪getSourceFile());
141 
142  // explicitly check for the raw filename here, as we check for files that existed before we even started
143  // processing, i.e. that were processed earlier
144  if ($processingFolder->hasFile($task->‪getTargetFileName())) {
145  // When the processed file already exists set it as processed file
146  $task->‪getTargetFile()->setName($task->‪getTargetFileName());
147 
148  // If the processed file is stored on a remote server, we must fetch a local copy of the file, as we
149  // have no API for fetching file metadata from a remote file.
150  $localProcessedFile = $storage->getFileForLocalProcessing($task->‪getTargetFile(), false);
151  $task->‪setExecuted(true);
152  $imageInformation = GeneralUtility::makeInstance(ImageInfo::class, $localProcessedFile);
153  $properties = [
154  'width' => $imageInformation->getWidth(),
155  'height' => $imageInformation->getHeight(),
156  'size' => $imageInformation->getSize(),
157  'checksum' => $task->‪getConfigurationChecksum(),
158  ];
159  $task->‪getTargetFile()->updateProperties($properties);
160 
161  return true;
162  }
163  return false;
164  }
165 
170  protected function ‪getFilenameForSvgCropScaleMask(‪TaskInterface $task): string
171  {
172  $targetFileExtension = $task->‪getTargetFileExtension();
173  return GeneralUtility::tempnam($task->‪getTargetFile()->generateProcessedFileNameWithoutExtension(), '.' . ltrim(trim($targetFileExtension)));
174  }
175 
176 }
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface\getType
‪getType()
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface\getTargetFileExtension
‪getTargetFileExtension()
‪TYPO3\CMS\Core\Imaging\Exception\ZeroImageDimensionException
Definition: ZeroImageDimensionException.php:26
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface
Definition: TaskInterface.php:34
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area
Definition: Area.php:23
‪TYPO3\CMS\Core\Resource\Processing\SvgImageProcessor\checkForExistingTargetFile
‪checkForExistingTargetFile(TaskInterface $task)
Definition: SvgImageProcessor.php:136
‪TYPO3\CMS\Core\Resource\Processing\SvgImageProcessor
Definition: SvgImageProcessor.php:35
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface\getTargetFileName
‪getTargetFileName()
‪TYPO3\CMS\Core\Resource\Processing\SvgImageProcessor\canProcessTask
‪canProcessTask(TaskInterface $task)
Definition: SvgImageProcessor.php:38
‪TYPO3\CMS\Core\Resource\Processing\SvgImageProcessor\$defaultSvgDimension
‪int $defaultSvgDimension
Definition: SvgImageProcessor.php:36
‪TYPO3\CMS\Core\Resource\Processing
Definition: AbstractTask.php:18
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface\getTargetFile
‪getTargetFile()
‪TYPO3\CMS\Core\Resource\Processing\SvgImageProcessor\processTask
‪processTask(TaskInterface $task)
Definition: SvgImageProcessor.php:50
‪TYPO3\CMS\Core\Utility\GeneralUtility\writeFile
‪static bool writeFile(string $file, string $content, bool $changePermissions=false)
Definition: GeneralUtility.php:1469
‪TYPO3\CMS\Core\Resource\Processing\SvgImageProcessor\getFilenameForSvgCropScaleMask
‪getFilenameForSvgCropScaleMask(TaskInterface $task)
Definition: SvgImageProcessor.php:170
‪TYPO3\CMS\Core\Resource\Processing\SvgImageProcessor\applyCropping
‪applyCropping(TaskInterface $task, Area $cropArea, ImageDimension $imageDimension)
Definition: SvgImageProcessor.php:101
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface\getConfigurationChecksum
‪getConfigurationChecksum()
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface\setExecuted
‪setExecuted(bool $successful)
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Imaging\SvgManipulation
Definition: SvgManipulation.php:44
‪TYPO3\CMS\Core\Resource\Processing\ProcessorInterface
Definition: ProcessorInterface.php:22
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface\getSourceFile
‪getSourceFile()
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface\getName
‪getName()
‪TYPO3\CMS\Core\Resource\Exception\InsufficientFolderReadPermissionsException
Definition: InsufficientFolderReadPermissionsException.php:21
‪TYPO3\CMS\Core\Imaging\ImageDimension
Definition: ImageDimension.php:28