‪TYPO3CMS  10.4
CropVariant.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 
21 
23 {
27  protected ‪$id;
28 
32  protected ‪$title;
33 
37  protected ‪$cropArea;
38 
43 
47  protected ‪$selectedRatio;
48 
52  protected ‪$focusArea;
53 
57  protected ‪$coverAreas;
58 
69  public function ‪__construct(
70  string ‪$id,
71  string ‪$title,
73  array ‪$allowedAspectRatios = null,
74  string ‪$selectedRatio = null,
75  ‪Area ‪$focusArea = null,
76  array ‪$coverAreas = null
77  ) {
78  $this->id = ‪$id;
79  $this->title = ‪$title;
80  $this->cropArea = ‪$cropArea;
83  if (‪$selectedRatio && isset($this->allowedAspectRatios[‪$selectedRatio])) {
84  $this->selectedRatio = ‪$selectedRatio;
85  } else {
86  $this->selectedRatio = current($this->allowedAspectRatios)->getId();
87  }
88  }
89  $this->focusArea = ‪$focusArea;
90  if (‪$coverAreas !== null) {
92  }
93  }
94 
101  public static function ‪createFromConfiguration(string ‪$id, array $config): ‪CropVariant
102  {
103  try {
104  return new self(
105  ‪$id,
106  $config['title'] ?? '',
107  ‪Area::createFromConfiguration($config['cropArea']),
108  isset($config['allowedAspectRatios']) ? ‪Ratio::createMultipleFromConfiguration($config['allowedAspectRatios']) : null,
109  $config['selectedRatio'] ?? null,
110  isset($config['focusArea']) ? ‪Area::createFromConfiguration($config['focusArea']) : null,
111  isset($config['coverAreas']) ? ‪Area::createMultipleFromConfiguration($config['coverAreas']) : null
112  );
113  } catch (\Throwable $throwable) {
114  throw new ‪InvalidConfigurationException(sprintf('Invalid type in configuration for crop variant: %s', $throwable->getMessage()), 1485278693, $throwable);
115  }
116  }
117 
122  public function ‪asArray(): array
123  {
124  $coverAreasAsArray = null;
125  $allowedAspectRatiosAsArray = [];
126  foreach ($this->allowedAspectRatios ?? [] as ‪$id => $allowedAspectRatio) {
127  $allowedAspectRatiosAsArray[‪$id] = $allowedAspectRatio->asArray();
128  }
129  if ($this->coverAreas !== null) {
130  $coverAreasAsArray = [];
131  foreach ($this->coverAreas as $coverArea) {
132  $coverAreasAsArray[] = $coverArea->asArray();
133  }
134  }
135  return [
136  'id' => ‪$this->id,
137  'title' => ‪$this->title,
138  'cropArea' => $this->cropArea->asArray(),
139  'allowedAspectRatios' => $allowedAspectRatiosAsArray,
140  'selectedRatio' => ‪$this->selectedRatio,
141  'focusArea' => $this->focusArea ? $this->focusArea->asArray() : null,
142  'coverAreas' => $coverAreasAsArray ?? null,
143  ];
144  }
145 
149  public function ‪getId(): string
150  {
151  return ‪$this->id;
152  }
153 
157  public function ‪getCropArea(): ‪Area
158  {
159  return ‪$this->cropArea;
160  }
161 
165  public function ‪getFocusArea()
166  {
168  }
169 
175  {
176  if (!$this->selectedRatio) {
177  return $this;
178  }
179  $newVariant = clone $this;
180  $newArea = $this->cropArea->makeAbsoluteBasedOnFile($file);
181  $newArea = $newArea->applyRatioRestriction($this->allowedAspectRatios[$this->selectedRatio]);
182  $newVariant->cropArea = $newArea->makeRelativeBasedOnFile($file);
183  return $newVariant;
184  }
185 
190  protected function ‪setAllowedAspectRatios(‪Ratio ...$ratios)
191  {
192  $this->allowedAspectRatios = [];
193  foreach ($ratios as $ratio) {
194  $this->‪addAllowedAspectRatio($ratio);
195  }
196  }
197 
202  protected function ‪addAllowedAspectRatio(Ratio $ratio)
203  {
204  if (isset($this->allowedAspectRatios[$ratio->getId()])) {
205  throw new InvalidConfigurationException(sprintf('Ratio with with duplicate ID (%s) is configured. Make sure all configured ratios have different ids.', $ratio->getId()), 1485274618);
206  }
207  $this->allowedAspectRatios[$ratio->getId()] = $ratio;
208  }
209 
214  protected function ‪setCoverAreas(‪Area ...$areas)
215  {
216  $this->coverAreas = [];
217  foreach ($areas as $area) {
218  $this->‪addCoverArea($area);
219  }
220  }
221 
226  protected function ‪addCoverArea(‪Area $area)
227  {
228  $this->coverAreas[] = $area;
229  }
230 }
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant\__construct
‪__construct(string $id, string $title, Area $cropArea, array $allowedAspectRatios=null, string $selectedRatio=null, Area $focusArea=null, array $coverAreas=null)
Definition: CropVariant.php:62
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area\createMultipleFromConfiguration
‪static Area[] createMultipleFromConfiguration(array $config)
Definition: Area.php:78
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant\$allowedAspectRatios
‪Ratio[] $allowedAspectRatios
Definition: CropVariant.php:38
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant\setAllowedAspectRatios
‪setAllowedAspectRatios(Ratio ... $ratios)
Definition: CropVariant.php:183
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant\$focusArea
‪Area null $focusArea
Definition: CropVariant.php:46
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:22
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant
Definition: CropVariant.php:23
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant\getFocusArea
‪Area null getFocusArea()
Definition: CropVariant.php:158
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant\addAllowedAspectRatio
‪addAllowedAspectRatio(Ratio $ratio)
Definition: CropVariant.php:195
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant\getId
‪string getId()
Definition: CropVariant.php:142
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area\createFromConfiguration
‪static Area createFromConfiguration(array $config)
Definition: Area.php:59
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area
Definition: Area.php:23
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant\$cropArea
‪Area $cropArea
Definition: CropVariant.php:34
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant\$title
‪string $title
Definition: CropVariant.php:30
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Ratio\getId
‪string getId()
Definition: Ratio.php:42
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Ratio
Definition: Ratio.php:21
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant\$id
‪string $id
Definition: CropVariant.php:26
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant\createFromConfiguration
‪static CropVariant createFromConfiguration(string $id, array $config)
Definition: CropVariant.php:94
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant\getCropArea
‪Area getCropArea()
Definition: CropVariant.php:150
‪TYPO3\CMS\Core\Imaging\ImageManipulation\InvalidConfigurationException
Definition: InvalidConfigurationException.php:24
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant\asArray
‪array asArray()
Definition: CropVariant.php:115
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant\applyRatioRestrictionToSelectedCropArea
‪CropVariant applyRatioRestrictionToSelectedCropArea(FileInterface $file)
Definition: CropVariant.php:167
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant\addCoverArea
‪addCoverArea(Area $area)
Definition: CropVariant.php:219
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant\setCoverAreas
‪setCoverAreas(Area ... $areas)
Definition: CropVariant.php:207
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant\$coverAreas
‪Area[] null $coverAreas
Definition: CropVariant.php:50
‪TYPO3\CMS\Core\Imaging\ImageManipulation
Definition: Area.php:18
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Ratio\createMultipleFromConfiguration
‪static Ratio[] createMultipleFromConfiguration(array $config)
Definition: Ratio.php:52
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant\$selectedRatio
‪string $selectedRatio
Definition: CropVariant.php:42