‪TYPO3CMS  ‪main
LocalCropScaleMaskHelper.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 
19 use TYPO3\CMS\Core\Imaging\GraphicalFunctions;
20 use TYPO3\CMS\Core\Imaging\ImageProcessingInstructions;
23 
28 {
48  public function ‪process(‪TaskInterface $task)
49  {
50  return $this->‪processWithLocalFile($task, $task->‪getSourceFile()->getForLocalProcessing(false));
51  }
52 
57  public function ‪processWithLocalFile(‪TaskInterface $task, string $originalFileName): ?array
58  {
59  $result = null;
60  $targetFile = $task->‪getTargetFile();
61  $targetFileExtension = $task->‪getTargetFileExtension();
62 
63  $imageOperations = GeneralUtility::makeInstance(GraphicalFunctions::class);
64 
65  $configuration = $targetFile->getProcessingConfiguration();
66  $configuration['additionalParameters'] ??= '';
67 
68  // Normal situation (no masking) - just scale the image
69  if (!is_array($configuration['maskImages'] ?? null)) {
70  // the result info is an array with 0=width,1=height,2=extension,3=filename
71  $result = $imageOperations->resize(
72  $originalFileName,
73  $targetFileExtension,
74  $configuration['width'] ?? '',
75  $configuration['height'] ?? '',
76  $configuration['additionalParameters'],
77  $configuration,
78  );
79  } else {
80  $temporaryFileName = $this->‪getFilenameForImageCropScaleMask($task);
81  $maskImage = $configuration['maskImages']['maskImage'] ?? null;
82  $maskBackgroundImage = $configuration['maskImages']['backgroundImage'];
83  if ($maskImage instanceof ‪FileInterface && $maskBackgroundImage instanceof ‪FileInterface) {
84  // This converts the original image to a temporary PNG file during all steps of the masking process
85  $tempFileInfo = $imageOperations->resize(
86  $originalFileName,
87  'png',
88  $configuration['width'] ?? '',
89  $configuration['height'] ?? '',
90  $configuration['additionalParameters'],
91  $configuration
92  );
93  if ($tempFileInfo !== null) {
94  // Scaling
95  $command = '-geometry ' . $tempFileInfo->getWidth() . 'x' . $tempFileInfo->getHeight() . '!';
96  $imageOperations->mask(
97  $tempFileInfo->getRealPath(),
98  $temporaryFileName,
99  $maskImage->getForLocalProcessing(),
100  $maskBackgroundImage->getForLocalProcessing(),
101  $command,
102  $configuration
103  );
104  $maskBottomImage = $configuration['maskImages']['maskBottomImage'] ?? null;
105  $maskBottomImageMask = $configuration['maskImages']['maskBottomImageMask'] ?? null;
106  if ($maskBottomImage instanceof ‪FileInterface && $maskBottomImageMask instanceof ‪FileInterface) {
107  // Uses the temporary PNG file from the previous step and applies another mask
108  $imageOperations->mask(
109  $temporaryFileName,
110  $temporaryFileName,
111  $maskBottomImage->getForLocalProcessing(),
112  $maskBottomImageMask->getForLocalProcessing(),
113  $command,
114  $configuration
115  );
116  }
117  }
118  $result = $tempFileInfo;
119  }
120  }
121 
122  // check if the processing really generated a new file (scaled and/or cropped)
123  if ($result !== null) {
124  if ($result->getRealPath() !== $originalFileName) {
125  $result = [
126  'width' => $result->getWidth(),
127  'height' => $result->getHeight(),
128  'filePath' => $result->getRealPath(),
129  ];
130  } else {
131  // No file was generated
132  $result = null;
133  }
134  }
135 
136  // If noScale option is applied, we need to reset the width and height to ensure the scaled values
137  // are used for the generated image tag even if the image itself is not scaled. This is needed, as
138  // the result is discarded due to the fact that the original image is used.
139  // @see https://forge.typo3.org/issues/100972
140  // Note: This should only happen if no image has been generated ($result === null).
141  if ($result === null && ($configuration['noScale'] ?? false)) {
142  $configuration = $task->‪getConfiguration();
143  $localProcessedFile = $task->‪getSourceFile()->getForLocalProcessing(false);
144  $imageDimensions = $imageOperations->getImageDimensions($localProcessedFile, true);
145  $imageScaleInfo = ImageProcessingInstructions::fromCropScaleValues(
146  $imageDimensions->getWidth(),
147  $imageDimensions->getHeight(),
148  $configuration['width'] ?? '',
149  $configuration['height'] ?? '',
150  $configuration
151  );
152  $targetFile->updateProperties([
153  'width' => $imageScaleInfo->width,
154  'height' => $imageScaleInfo->height,
155  ]);
156  }
157 
158  return $result;
159  }
160 
164  protected function ‪getFilenameForImageCropScaleMask(‪TaskInterface $task): string
165  {
166  $targetFileExtension = $task->‪getTargetFileExtension();
167  return ‪Environment::getPublicPath() . '/typo3temp/' . $task->‪getTargetFile()->generateProcessedFileNameWithoutExtension() . '.' . ltrim(trim($targetFileExtension), '.');
168  }
169 }
‪TYPO3\CMS\Core\Resource\Processing\LocalCropScaleMaskHelper\processWithLocalFile
‪processWithLocalFile(TaskInterface $task, string $originalFileName)
Definition: LocalCropScaleMaskHelper.php:57
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface\getTargetFileExtension
‪getTargetFileExtension()
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:26
‪TYPO3\CMS\Core\Resource\Processing\LocalCropScaleMaskHelper\getFilenameForImageCropScaleMask
‪getFilenameForImageCropScaleMask(TaskInterface $task)
Definition: LocalCropScaleMaskHelper.php:164
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface
Definition: TaskInterface.php:34
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface\getConfiguration
‪getConfiguration()
‪TYPO3\CMS\Core\Core\Environment\getPublicPath
‪static getPublicPath()
Definition: Environment.php:187
‪TYPO3\CMS\Core\Resource\Processing\LocalCropScaleMaskHelper
Definition: LocalCropScaleMaskHelper.php:28
‪TYPO3\CMS\Core\Resource\Processing
Definition: AbstractTask.php:18
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface\getTargetFile
‪getTargetFile()
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:41
‪TYPO3\CMS\Core\Resource\Processing\LocalCropScaleMaskHelper\process
‪array null process(TaskInterface $task)
Definition: LocalCropScaleMaskHelper.php:48
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface\getSourceFile
‪getSourceFile()