‪TYPO3CMS  ‪main
GalleryProcessor.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 
23 
97 {
103  protected ‪$contentObjectRenderer;
104 
110  protected ‪$processorConfiguration;
111 
117  protected ‪$availableGalleryPositions = [
118  'horizontal' => [
119  'center' => [0, 8],
120  'right' => [1, 9, 17, 25],
121  'left' => [2, 10, 18, 26],
122  ],
123  'vertical' => [
124  'above' => [0, 1, 2],
125  'intext' => [17, 18, 25, 26],
126  'below' => [8, 9, 10],
127  ],
128  ];
129 
135  protected ‪$galleryData = [
136  'position' => [
137  'horizontal' => '',
138  'vertical' => '',
139  'noWrap' => false,
140  ],
141  'width' => 0,
142  'count' => [
143  'files' => 0,
144  'columns' => 0,
145  'rows' => 0,
146  ],
147  'columnSpacing' => 0,
148  'border' => [
149  'enabled' => false,
150  'width' => 0,
151  'padding' => 0,
152  ],
153  'rows' => [],
154  ];
155 
159  protected ‪$numberOfColumns;
160 
164  protected ‪$mediaOrientation;
165 
169  protected ‪$maxGalleryWidth;
170 
175 
179  protected ‪$equalMediaHeight;
180 
184  protected ‪$equalMediaWidth;
185 
189  protected ‪$columnSpacing;
190 
194  protected ‪$borderEnabled;
195 
199  protected ‪$borderWidth;
200 
204  protected ‪$borderPadding;
205 
209  protected ‪$cropVariant = 'default';
210 
216  protected ‪$fileObjects = [];
217 
223  protected ‪$mediaDimensions = [];
224 
235  public function ‪process(
237  array $contentObjectConfiguration,
239  array $processedData
240  ) {
241  if (isset(‪$processorConfiguration['if.']) && !$cObj->‪checkIf(‪$processorConfiguration['if.'])) {
242  return $processedData;
243  }
244 
245  $this->contentObjectRenderer = $cObj;
246  $this->processorConfiguration = ‪$processorConfiguration;
247 
248  $filesProcessedDataKey = (string)$cObj->‪stdWrapValue(
249  'filesProcessedDataKey',
251  'files'
252  );
253  if (isset($processedData[$filesProcessedDataKey]) && is_array($processedData[$filesProcessedDataKey])) {
254  $this->fileObjects = $processedData[$filesProcessedDataKey];
255  $this->galleryData['count']['files'] = count($this->fileObjects);
256  } else {
257  throw new ‪ContentRenderingException('No files found for key ' . $filesProcessedDataKey . ' in $processedData.', 1436809789);
258  }
259 
260  $this->numberOfColumns = (int)$this->‪getConfigurationValue('numberOfColumns', 'imagecols');
261  $this->mediaOrientation = (int)$this->‪getConfigurationValue('mediaOrientation', 'imageorient');
262  $this->maxGalleryWidth = (int)$this->‪getConfigurationValue('maxGalleryWidth') ?: 600;
263  $this->maxGalleryWidthInText = (int)$this->‪getConfigurationValue('maxGalleryWidthInText') ?: 300;
264  $this->equalMediaHeight = (int)$this->‪getConfigurationValue('equalMediaHeight', 'imageheight');
265  $this->equalMediaWidth = (int)$this->‪getConfigurationValue('equalMediaWidth', 'imagewidth');
266  $this->columnSpacing = (int)$this->‪getConfigurationValue('columnSpacing');
267  $this->borderEnabled = (bool)$this->‪getConfigurationValue('borderEnabled', 'imageborder');
268  $this->borderWidth = (int)$this->‪getConfigurationValue('borderWidth');
269  $this->borderPadding = (int)$this->‪getConfigurationValue('borderPadding');
270  $this->cropVariant = $this->‪getConfigurationValue('cropVariant') ?: 'default';
271 
274 
277 
278  $this->‪prepareGalleryData();
279 
280  $targetFieldName = (string)$cObj->‪stdWrapValue(
281  'as',
283  'gallery'
284  );
285 
286  $processedData[$targetFieldName] = ‪$this->galleryData;
287 
288  return $processedData;
289  }
290 
299  protected function ‪getConfigurationValue($key, $dataArrayKey = null)
300  {
301  $defaultValue = '';
302  if ($dataArrayKey && isset($this->contentObjectRenderer->data[$dataArrayKey])) {
303  $defaultValue = $this->contentObjectRenderer->data[$dataArrayKey];
304  }
305  return $this->contentObjectRenderer->stdWrapValue(
306  $key,
307  $this->processorConfiguration,
308  $defaultValue
309  );
310  }
311 
318  protected function ‪determineGalleryPosition()
319  {
320  foreach ($this->availableGalleryPositions as $positionDirectionKey => $positionDirectionValue) {
321  foreach ($positionDirectionValue as $positionKey => $positionArray) {
322  if (in_array($this->mediaOrientation, $positionArray, true)) {
323  $this->galleryData['position'][$positionDirectionKey] = $positionKey;
324  }
325  }
326  }
327 
328  if ($this->mediaOrientation === 25 || $this->mediaOrientation === 26) {
329  $this->galleryData['position']['noWrap'] = true;
330  }
331  }
332 
336  protected function ‪determineMaximumGalleryWidth()
337  {
338  if ($this->galleryData['position']['vertical'] === 'intext') {
339  $this->galleryData['width'] = ‪$this->maxGalleryWidthInText;
340  } else {
341  $this->galleryData['width'] = ‪$this->maxGalleryWidth;
342  }
343  }
344 
348  protected function ‪calculateRowsAndColumns()
349  {
350  // If no columns defined, set it to 1
351  $columns = max((int)$this->numberOfColumns, 1);
352 
353  // When more columns than media elements, set the columns to the amount of media elements
354  if ($columns > $this->galleryData['count']['files']) {
355  $columns = $this->galleryData['count']['files'];
356  }
357 
358  if ($columns === 0) {
359  $columns = 1;
360  }
361 
362  // Calculate the rows from the amount of files and the columns
363  $rows = ceil($this->galleryData['count']['files'] / $columns);
364 
365  $this->galleryData['count']['columns'] = $columns;
366  $this->galleryData['count']['rows'] = (int)$rows;
367  }
368 
377  protected function ‪calculateMediaWidthsAndHeights()
378  {
379  $columnSpacingTotal = ($this->galleryData['count']['columns'] - 1) * $this->columnSpacing;
380 
381  $galleryWidthMinusBorderAndSpacing = max($this->galleryData['width'] - $columnSpacingTotal, 1);
382 
383  if ($this->borderEnabled) {
384  $borderPaddingTotal = ($this->galleryData['count']['columns'] * 2) * $this->borderPadding;
385  $borderWidthTotal = ($this->galleryData['count']['columns'] * 2) * $this->borderWidth;
386  $galleryWidthMinusBorderAndSpacing = $galleryWidthMinusBorderAndSpacing - $borderPaddingTotal - $borderWidthTotal;
387  }
388 
389  // User entered a predefined height
390  if ($this->equalMediaHeight) {
391  $mediaScalingCorrection = 1;
392  $maximumRowWidth = 0;
393 
394  // Calculate the scaling correction when the total of media elements is wider than the gallery width
395  for ($row = 1; $row <= $this->galleryData['count']['rows']; $row++) {
396  $totalRowWidth = 0;
397  for ($column = 1; $column <= $this->galleryData['count']['columns']; $column++) {
398  $fileKey = (($row - 1) * $this->galleryData['count']['columns']) + $column - 1;
399  if ($fileKey > $this->galleryData['count']['files'] - 1) {
400  break 2;
401  }
402  $currentMediaScaling = $this->equalMediaHeight / max($this->‪getCroppedDimensionalProperty($this->fileObjects[$fileKey], 'height'), 1);
403  $totalRowWidth += $this->‪getCroppedDimensionalProperty($this->fileObjects[$fileKey], 'width') * $currentMediaScaling;
404  }
405  $maximumRowWidth = max($totalRowWidth, $maximumRowWidth);
406  $mediaInRowScaling = $totalRowWidth / $galleryWidthMinusBorderAndSpacing;
407  $mediaScalingCorrection = max($mediaInRowScaling, $mediaScalingCorrection);
408  }
409 
410  // Set the corrected dimensions for each media element
411  foreach ($this->fileObjects as $key => $fileObject) {
412  $mediaHeight = floor($this->equalMediaHeight / $mediaScalingCorrection);
413  $mediaWidth = floor(
414  $this->‪getCroppedDimensionalProperty($fileObject, 'width') * ($mediaHeight / max($this->‪getCroppedDimensionalProperty($fileObject, 'height'), 1))
415  );
416  $this->mediaDimensions[$key] = [
417  'width' => $mediaWidth,
418  'height' => $mediaHeight,
419  ];
420  }
421 
422  // Recalculate gallery width
423  $this->galleryData['width'] = floor($maximumRowWidth / $mediaScalingCorrection);
424 
425  // User entered a predefined width
426  } elseif ($this->equalMediaWidth) {
427  $mediaScalingCorrection = 1;
428 
429  // Calculate the scaling correction when the total of media elements is wider than the gallery width
430  $totalRowWidth = $this->galleryData['count']['columns'] * ‪$this->equalMediaWidth;
431  $mediaInRowScaling = $totalRowWidth / $galleryWidthMinusBorderAndSpacing;
432  $mediaScalingCorrection = max($mediaInRowScaling, $mediaScalingCorrection);
433 
434  // Set the corrected dimensions for each media element
435  foreach ($this->fileObjects as $key => $fileObject) {
436  $mediaWidth = floor($this->equalMediaWidth / $mediaScalingCorrection);
437  $mediaHeight = floor(
438  $this->‪getCroppedDimensionalProperty($fileObject, 'height') * ($mediaWidth / max($this->‪getCroppedDimensionalProperty($fileObject, 'width'), 1))
439  );
440  $this->mediaDimensions[$key] = [
441  'width' => $mediaWidth,
442  'height' => $mediaHeight,
443  ];
444  }
445 
446  // Recalculate gallery width
447  $this->galleryData['width'] = floor($totalRowWidth / $mediaScalingCorrection);
448 
449  // Automatic setting of width and height
450  } else {
451  $maxMediaWidth = (int)($galleryWidthMinusBorderAndSpacing / $this->galleryData['count']['columns']);
452  foreach ($this->fileObjects as $key => $fileObject) {
453  $croppedWidth = $this->‪getCroppedDimensionalProperty($fileObject, 'width');
454  $mediaWidth = $croppedWidth > 0 ? min($maxMediaWidth, $croppedWidth) : $maxMediaWidth;
455  $mediaHeight = floor(
456  $this->‪getCroppedDimensionalProperty($fileObject, 'height') * ($mediaWidth / max($this->‪getCroppedDimensionalProperty($fileObject, 'width'), 1))
457  );
458  $this->mediaDimensions[$key] = [
459  'width' => $mediaWidth,
460  'height' => $mediaHeight,
461  ];
462  }
463  }
464  }
465 
473  protected function ‪getCroppedDimensionalProperty(FileInterface $fileObject, $dimensionalProperty)
474  {
475  if (!$fileObject->hasProperty('crop') || empty($fileObject->getProperty('crop'))) {
476  return $fileObject->getProperty($dimensionalProperty);
477  }
478 
479  $croppingConfiguration = $fileObject->getProperty('crop');
480  $cropVariantCollection = ‪CropVariantCollection::create((string)$croppingConfiguration);
481  return (int)$cropVariantCollection->getCropArea($this->cropVariant)->makeAbsoluteBasedOnFile($fileObject)->asArray()[$dimensionalProperty];
482  }
483 
489  protected function ‪prepareGalleryData()
490  {
491  for ($row = 1; $row <= $this->galleryData['count']['rows']; $row++) {
492  for ($column = 1; $column <= $this->galleryData['count']['columns']; $column++) {
493  $fileKey = (($row - 1) * $this->galleryData['count']['columns']) + $column - 1;
494 
495  $this->galleryData['rows'][$row]['columns'][$column] = [
496  'media' => $this->fileObjects[$fileKey] ?? null,
497  'dimensions' => [
498  'width' => $this->mediaDimensions[$fileKey]['width'] ?? null,
499  'height' => $this->mediaDimensions[$fileKey]['height'] ?? null,
500  ],
501  ];
502  }
503  }
504 
505  $this->galleryData['columnSpacing'] = ‪$this->columnSpacing;
506  $this->galleryData['border']['enabled'] = ‪$this->borderEnabled;
507  $this->galleryData['border']['width'] = ‪$this->borderWidth;
508  $this->galleryData['border']['padding'] = ‪$this->borderPadding;
509  }
510 }
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection\create
‪static create(string $jsonString, array $tcaConfig=[])
Definition: CropVariantCollection.php:37
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:26
‪TYPO3\CMS\Frontend\DataProcessing
Definition: CommaSeparatedValueProcessor.php:16
‪TYPO3\CMS\Core\Resource\FileInterface\hasProperty
‪hasProperty(string $key)
‪TYPO3\CMS\Frontend\ContentObject\Exception\ContentRenderingException
Definition: ContentRenderingException.php:24
‪TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer\checkIf
‪checkIf($conf)
Definition: ContentObjectRenderer.php:2443
‪TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer\stdWrapValue
‪string int bool null stdWrapValue($key, array $config, $defaultValue='')
Definition: ContentObjectRenderer.php:1139
‪TYPO3\CMS\Frontend\ContentObject\DataProcessorInterface
Definition: DataProcessorInterface.php:23
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection
Definition: CropVariantCollection.php:23
‪TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
Definition: ContentObjectRenderer.php:102
‪TYPO3\CMS\Core\Resource\FileInterface\getProperty
‪getProperty(string $key)