‪TYPO3CMS  11.5
ImageDimension.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 
26 
32 {
36  private ‪$width;
37 
41  private ‪$height;
42 
43  public function ‪__construct(int ‪$width, int ‪$height)
44  {
45  $this->width = ‪$width;
46  $this->height = ‪$height;
47  }
48 
49  public function ‪getWidth(): int
50  {
51  return ‪$this->width;
52  }
53 
54  public function ‪getHeight(): int
55  {
56  return ‪$this->height;
57  }
58 
59  public static function ‪fromProcessingTask(‪TaskInterface $task): self
60  {
62  $processedFile = $task->‪getTargetFile();
63  $isCropped = false;
64  if (($config['crop'] ?? null) instanceof ‪Area) {
65  $isCropped = true;
66  $imageDimension = new self(
67  (int)round($config['crop']->‪getWidth()),
68  (int)round($config['crop']->‪getHeight())
69  );
70  } else {
71  $imageDimension = new self(
72  (int)$processedFile->getOriginalFile()->getProperty('width'),
73  (int)$processedFile->getOriginalFile()->getProperty('height')
74  );
75  }
76  if ($imageDimension->width <= 0 || $imageDimension->height <= 0) {
77  throw new ZeroImageDimensionException('Width and height of the image must be greater than zero.', 1597310560);
78  }
79  $result = GeneralUtility::makeInstance(GraphicalFunctions::class)->getImageScale(
80  [
81  $imageDimension->width,
82  $imageDimension->height,
83  $processedFile->getExtension(),
84  ],
85  (string)($config['width'] ?? ''),
86  (string)($config['height'] ?? ''),
87  $config
88  );
89  $imageWidth = $geometryWidth = (int)$result[0];
90  $imageHeight = $geometryHeight = (int)$result[1];
91  $isCropScaled = $result['crs'];
92 
93  if ($isCropScaled) {
94  $cropWidth = (int)$result['origW'];
95  $cropHeight = (int)$result['origH'];
96  // If the image is crop scaled, use the dimension of the crop
97  // unless crop area exceeds the dimension of the scaled image
98  if ($cropWidth <= $geometryWidth && $cropHeight <= $geometryHeight) {
99  $imageWidth = $cropWidth;
100  $imageHeight = $cropHeight;
101  }
102  if (!$isCropped && $task->‪getTargetFileExtension() === 'svg') {
103  // Keep aspect ratio of SVG files, when crop-scaling is requested
104  // but no crop is applied
105  if ($geometryWidth > $geometryHeight) {
106  $imageHeight = (int)round($imageWidth * $geometryHeight / $geometryWidth);
107  } else {
108  $imageWidth = (int)round($imageHeight * $geometryWidth / $geometryHeight);
109  }
110  }
111  }
112  $imageDimension->width = $imageWidth;
113  $imageDimension->height = $imageHeight;
114 
115  return $imageDimension;
116  }
117 
118  private static function ‪getConfigurationForImageCropScaleMask(‪TaskInterface $task): array
119  {
120  $configuration = $task->‪getConfiguration();
121 
123  $configuration = ‪LocalPreviewHelper::preProcessConfiguration($configuration);
124  $configuration['maxWidth'] = $configuration['width'];
125  unset($configuration['width']);
126  $configuration['maxHeight'] = $configuration['height'];
127  unset($configuration['height']);
128  }
129 
130  $options = $configuration;
131  if ($configuration['maxWidth'] ?? null) {
132  $options['maxW'] = $configuration['maxWidth'];
133  }
134  if ($configuration['maxHeight'] ?? null) {
135  $options['maxH'] = $configuration['maxHeight'];
136  }
137  if ($configuration['minWidth'] ?? null) {
138  $options['minW'] = $configuration['minWidth'];
139  }
140  if ($configuration['minHeight'] ?? null) {
141  $options['minH'] = $configuration['minHeight'];
142  }
143  if ($configuration['crop'] ?? null) {
144  $options['crop'] = $configuration['crop'];
145  if (is_string($configuration['crop'])) {
146  // check if it is a json object
147  $cropData = json_decode($configuration['crop']);
148  if ($cropData) {
149  $options['crop'] = new Area((float)$cropData->x, (float)$cropData->y, (float)$cropData->width, (float)$cropData->height);
150  } else {
151  [$offsetLeft, $offsetTop, $newWidth, $newHeight] = explode(',', $configuration['crop'], 4);
152  $options['crop'] = new Area((float)$offsetLeft, (float)$offsetTop, (float)$newWidth, (float)$newHeight);
153  }
154  if ($options['crop']->isEmpty()) {
155  unset($options['crop']);
156  }
157  }
158  }
159  if ($configuration['noScale'] ?? null) {
160  $options['noScale'] = $configuration['noScale'];
161  }
162 
163  return $options;
164  }
165 }
‪TYPO3\CMS\Core\Resource\ProcessedFile\CONTEXT_IMAGEPREVIEW
‪const CONTEXT_IMAGEPREVIEW
Definition: ProcessedFile.php:53
‪TYPO3\CMS\Core\Resource\Processing\LocalPreviewHelper
Definition: LocalPreviewHelper.php:29
‪TYPO3\CMS\Core\Imaging\ImageDimension\__construct
‪__construct(int $width, int $height)
Definition: ImageDimension.php:41
‪TYPO3\CMS\Core\Imaging
Definition: Dimension.php:16
‪TYPO3\CMS\Core\Imaging\ImageDimension\getConfigurationForImageCropScaleMask
‪static getConfigurationForImageCropScaleMask(TaskInterface $task)
Definition: ImageDimension.php:116
‪TYPO3\CMS\Core\Imaging\Exception\ZeroImageDimensionException
Definition: ZeroImageDimensionException.php:26
‪TYPO3\CMS\Core\Imaging\ImageDimension\getHeight
‪getHeight()
Definition: ImageDimension.php:52
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface
Definition: TaskInterface.php:33
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area
Definition: Area.php:23
‪TYPO3\CMS\Core\Imaging\ImageDimension\fromProcessingTask
‪static fromProcessingTask(TaskInterface $task)
Definition: ImageDimension.php:57
‪TYPO3\CMS\Core\Resource\ProcessedFile\getTaskIdentifier
‪string getTaskIdentifier()
Definition: ProcessedFile.php:553
‪TYPO3\CMS\Core\Imaging\ImageDimension\getWidth
‪getWidth()
Definition: ImageDimension.php:47
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface\getTargetFile
‪Resource ProcessedFile getTargetFile()
‪TYPO3\CMS\Core\Imaging\ImageDimension\$height
‪int $height
Definition: ImageDimension.php:39
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface\getConfiguration
‪array getConfiguration()
‪TYPO3\CMS\Core\Imaging\ImageDimension\$width
‪int $width
Definition: ImageDimension.php:35
‪TYPO3\CMS\Core\Resource\Processing\LocalPreviewHelper\preProcessConfiguration
‪static array preProcessConfiguration(array $configuration)
Definition: LocalPreviewHelper.php:45
‪TYPO3\CMS\Core\Resource\ProcessedFile
Definition: ProcessedFile.php:45
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Resource\Processing\TaskInterface\getTargetFileExtension
‪string getTargetFileExtension()
‪TYPO3\CMS\Core\Imaging\ImageDimension
Definition: ImageDimension.php:32