‪TYPO3CMS  10.4
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 
25 
30 {
50  public function ‪process(‪TaskInterface $task)
51  {
52  return $this->‪processWithLocalFile($task, $task->‪getSourceFile()->‪getForLocalProcessing(false));
53  }
54 
63  public function ‪processWithLocalFile(‪TaskInterface $task, string $originalFileName): ?array
64  {
65  $result = null;
66  $targetFile = $task->‪getTargetFile();
67 
68  $gifBuilder = GeneralUtility::makeInstance(GifBuilder::class);
69 
70  $configuration = $targetFile->getProcessingConfiguration();
71  $configuration['additionalParameters'] = $this->‪modifyImageMagickStripProfileParameters($configuration['additionalParameters'], $configuration);
72 
73  if (empty($configuration['fileExtension'])) {
74  $configuration['fileExtension'] = $task->‪getTargetFileExtension();
75  }
76 
77  $options = $this->‪getConfigurationForImageCropScaleMask($targetFile, $gifBuilder);
78 
79  $croppedImage = null;
80  if (!empty($configuration['crop'])) {
81 
82  // check if it is a json object
83  $cropData = json_decode($configuration['crop']);
84  if ($cropData) {
85  $crop = implode(',', [(int)$cropData->x, (int)$cropData->y, (int)$cropData->width, (int)$cropData->height]);
86  } else {
87  $crop = $configuration['crop'];
88  }
89 
90  [$offsetLeft, $offsetTop, $newWidth, $newHeight] = explode(',', $crop, 4);
91 
92  $backupPrefix = $gifBuilder->filenamePrefix;
93  $gifBuilder->filenamePrefix = 'crop_';
94 
95  $jpegQuality = ‪MathUtility::forceIntegerInRange(‪$GLOBALS['TYPO3_CONF_VARS']['GFX']['jpg_quality'], 10, 100, 85);
96 
97  // the result info is an array with 0=width,1=height,2=extension,3=filename
98  $result = $gifBuilder->imageMagickConvert(
99  $originalFileName,
100  $configuration['fileExtension'],
101  '',
102  '',
103  sprintf('-crop %dx%d+%d+%d +repage -quality %d', $newWidth, $newHeight, $offsetLeft, $offsetTop, $jpegQuality),
104  '',
105  ['noScale' => true],
106  true
107  );
108  $gifBuilder->filenamePrefix = $backupPrefix;
109 
110  if ($result !== null) {
111  $originalFileName = $croppedImage = $result[3];
112  }
113  }
114 
115  // Normal situation (no masking)
116  if (!(is_array($configuration['maskImages']) && ‪$GLOBALS['TYPO3_CONF_VARS']['GFX']['processor_enabled'])) {
117  // the result info is an array with 0=width,1=height,2=extension,3=filename
118  $result = $gifBuilder->imageMagickConvert(
119  $originalFileName,
120  $configuration['fileExtension'],
121  $configuration['width'],
122  $configuration['height'],
123  $configuration['additionalParameters'],
124  $configuration['frame'],
125  $options
126  );
127  } else {
128  $targetFileName = $this->‪getFilenameForImageCropScaleMask($task);
129  $temporaryFileName = ‪Environment::getPublicPath() . '/typo3temp/' . $targetFileName;
130  $maskImage = $configuration['maskImages']['maskImage'];
131  $maskBackgroundImage = $configuration['maskImages']['backgroundImage'];
132  if ($maskImage instanceof ‪FileInterface && $maskBackgroundImage instanceof ‪FileInterface) {
133  $temporaryExtension = 'png';
134  if (!‪$GLOBALS['TYPO3_CONF_VARS']['GFX']['processor_allowTemporaryMasksAsPng']) {
135  // If ImageMagick version 5+
136  $temporaryExtension = $gifBuilder->gifExtension;
137  }
138  $tempFileInfo = $gifBuilder->imageMagickConvert(
139  $originalFileName,
140  $temporaryExtension,
141  $configuration['width'],
142  $configuration['height'],
143  $configuration['additionalParameters'],
144  $configuration['frame'],
145  $options
146  );
147  if (is_array($tempFileInfo)) {
148  $maskBottomImage = $configuration['maskImages']['maskBottomImage'];
149  if ($maskBottomImage instanceof ‪FileInterface) {
150  $maskBottomImageMask = $configuration['maskImages']['maskBottomImageMask'];
151  } else {
152  $maskBottomImageMask = null;
153  }
154 
155  // Scaling: ****
156  $tempScale = [];
157  $command = '-geometry ' . $tempFileInfo[0] . 'x' . $tempFileInfo[1] . '!';
158  $command = $this->‪modifyImageMagickStripProfileParameters($command, $configuration);
159  $tmpStr = $gifBuilder->randomName();
160  // m_mask
161  $tempScale['m_mask'] = $tmpStr . '_mask.' . $temporaryExtension;
162  $gifBuilder->imageMagickExec($maskImage->getForLocalProcessing(true), $tempScale['m_mask'], $command);
163  // m_bgImg
164  $tempScale['m_bgImg'] = $tmpStr . '_bgImg.miff';
165  $gifBuilder->imageMagickExec($maskBackgroundImage->getForLocalProcessing(), $tempScale['m_bgImg'], $command);
166  // m_bottomImg / m_bottomImg_mask
167  if ($maskBottomImage instanceof ‪FileInterface && $maskBottomImageMask instanceof ‪FileInterface) {
168  $tempScale['m_bottomImg'] = $tmpStr . '_bottomImg.' . $temporaryExtension;
169  $gifBuilder->imageMagickExec($maskBottomImage->getForLocalProcessing(), $tempScale['m_bottomImg'], $command);
170  $tempScale['m_bottomImg_mask'] = ($tmpStr . '_bottomImg_mask.') . $temporaryExtension;
171  $gifBuilder->imageMagickExec($maskBottomImageMask->getForLocalProcessing(), $tempScale['m_bottomImg_mask'], $command);
172  // BEGIN combining:
173  // The image onto the background
174  $gifBuilder->combineExec($tempScale['m_bgImg'], $tempScale['m_bottomImg'], $tempScale['m_bottomImg_mask'], $tempScale['m_bgImg']);
175  }
176  // The image onto the background
177  $gifBuilder->combineExec($tempScale['m_bgImg'], $tempFileInfo[3], $tempScale['m_mask'], $temporaryFileName);
178  $tempFileInfo[3] = $temporaryFileName;
179  // Unlink the temp-images...
180  foreach ($tempScale as $tempFile) {
181  if (@is_file($tempFile)) {
182  unlink($tempFile);
183  }
184  }
185  }
186  $result = $tempFileInfo;
187  }
188  }
189 
190  // check if the processing really generated a new file (scaled and/or cropped)
191  if ($result !== null) {
192  if ($result[3] !== $originalFileName || $originalFileName === $croppedImage) {
193  $result = [
194  'width' => $result[0],
195  'height' => $result[1],
196  'filePath' => $result[3],
197  ];
198  } else {
199  // No file was generated
200  $result = null;
201  }
202  }
203 
204  // Cleanup temp file if it isn't used as result
205  if ($croppedImage && ($result === null || $croppedImage !== $result['filePath'])) {
206  GeneralUtility::unlink_tempfile($croppedImage);
207  }
208 
209  return $result;
210  }
211 
218  protected function ‪getConfigurationForImageCropScaleMask(‪ProcessedFile $processedFile, ‪GifBuilder $gifBuilder)
219  {
220  $configuration = $processedFile->‪getProcessingConfiguration();
221 
222  if ($configuration['useSample']) {
223  $gifBuilder->scalecmd = '-sample';
224  }
225  $options = [];
226  if ($configuration['maxWidth']) {
227  $options['maxW'] = $configuration['maxWidth'];
228  }
229  if ($configuration['maxHeight']) {
230  $options['maxH'] = $configuration['maxHeight'];
231  }
232  if ($configuration['minWidth']) {
233  $options['minW'] = $configuration['minWidth'];
234  }
235  if ($configuration['minHeight']) {
236  $options['minH'] = $configuration['minHeight'];
237  }
238 
239  $options['noScale'] = $configuration['noScale'];
240 
241  return $options;
242  }
243 
251  {
252  $configuration = $task->‪getTargetFile()->‪getProcessingConfiguration();
253  $targetFileExtension = $task->‪getSourceFile()->‪getExtension();
254  $processedFileExtension = ‪$GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_png'] ? 'png' : 'gif';
255  if (is_array($configuration['maskImages']) && ‪$GLOBALS['TYPO3_CONF_VARS']['GFX']['processor_enabled'] && $task->‪getSourceFile()->‪getExtension() != $processedFileExtension) {
256  $targetFileExtension = 'jpg';
257  } elseif ($configuration['fileExtension']) {
258  $targetFileExtension = $configuration['fileExtension'];
259  }
260 
261  return $task->‪getTargetFile()->‪generateProcessedFileNameWithoutExtension() . '.' . ltrim(trim($targetFileExtension), '.');
262  }
263 
271  protected function ‪modifyImageMagickStripProfileParameters($parameters, array $configuration)
272  {
273  // Strips profile information of image to save some space:
274  if (isset($configuration['stripProfile'])) {
275  if (
276  $configuration['stripProfile']
277  && ‪$GLOBALS['TYPO3_CONF_VARS']['GFX']['processor_stripColorProfileCommand'] !== ''
278  ) {
279  $parameters = ‪$GLOBALS['TYPO3_CONF_VARS']['GFX']['processor_stripColorProfileCommand'] . $parameters;
280  } else {
281  $parameters .= '###SkipStripProfile###';
282  }
283  }
284  return $parameters;
285  }
286 }
‪TYPO3\CMS\Core\Core\Environment\getPublicPath
‪static string getPublicPath()
Definition: Environment.php:180
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:22
‪TYPO3\CMS\Core\Resource\AbstractFile\getForLocalProcessing
‪string getForLocalProcessing($writable=true)
Definition: AbstractFile.php:577
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface
Definition: TaskInterface.php:33
‪TYPO3\CMS\Core\Resource\Processing\LocalCropScaleMaskHelper\processWithLocalFile
‪array null processWithLocalFile(TaskInterface $task, string $originalFileName)
Definition: LocalCropScaleMaskHelper.php:63
‪TYPO3\CMS\Core\Resource\Processing\LocalCropScaleMaskHelper\getFilenameForImageCropScaleMask
‪string getFilenameForImageCropScaleMask(TaskInterface $task)
Definition: LocalCropScaleMaskHelper.php:250
‪TYPO3\CMS\Core\Utility\MathUtility\forceIntegerInRange
‪static int forceIntegerInRange($theInt, $min, $max=2000000000, $defaultValue=0)
Definition: MathUtility.php:32
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface\getSourceFile
‪Resource File getSourceFile()
‪TYPO3\CMS\Core\Resource\Processing\LocalCropScaleMaskHelper\getConfigurationForImageCropScaleMask
‪array getConfigurationForImageCropScaleMask(ProcessedFile $processedFile, GifBuilder $gifBuilder)
Definition: LocalCropScaleMaskHelper.php:218
‪TYPO3\CMS\Core\Resource\Processing\LocalCropScaleMaskHelper
Definition: LocalCropScaleMaskHelper.php:30
‪TYPO3\CMS\Core\Resource\Processing
Definition: AbstractGraphicalTask.php:16
‪TYPO3\CMS\Core\Resource\ProcessedFile\getProcessingConfiguration
‪array getProcessingConfiguration()
Definition: ProcessedFile.php:541
‪TYPO3\CMS\Core\Resource\ProcessedFile\generateProcessedFileNameWithoutExtension
‪string generateProcessedFileNameWithoutExtension()
Definition: ProcessedFile.php:576
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface\getTargetFile
‪Resource ProcessedFile getTargetFile()
‪TYPO3\CMS\Frontend\Imaging\GifBuilder
Definition: GifBuilder.php:56
‪TYPO3\CMS\Core\Resource
Definition: generateMimeTypes.php:52
‪TYPO3\CMS\Core\Resource\ProcessedFile
Definition: ProcessedFile.php:44
‪TYPO3\CMS\Core\Resource\AbstractFile\getExtension
‪string getExtension()
Definition: AbstractFile.php:254
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:40
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:22
‪TYPO3\CMS\Core\Resource\Processing\LocalCropScaleMaskHelper\process
‪array null process(TaskInterface $task)
Definition: LocalCropScaleMaskHelper.php:50
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Resource\Processing\LocalCropScaleMaskHelper\modifyImageMagickStripProfileParameters
‪string modifyImageMagickStripProfileParameters($parameters, array $configuration)
Definition: LocalCropScaleMaskHelper.php:271
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface\getTargetFileExtension
‪string getTargetFileExtension()