‪TYPO3CMS  ‪main
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 
66  public function ‪__construct(
67  string ‪$id,
68  string ‪$title,
70  array ‪$allowedAspectRatios = null,
71  string ‪$selectedRatio = null,
72  ‪Area ‪$focusArea = null,
73  array ‪$coverAreas = null
74  ) {
75  $this->id = ‪$id;
76  $this->title = ‪$title;
77  $this->cropArea = ‪$cropArea;
80  if (‪$selectedRatio && isset($this->allowedAspectRatios[‪$selectedRatio])) {
81  $this->selectedRatio = ‪$selectedRatio;
82  } else {
83  $this->selectedRatio = current($this->allowedAspectRatios)->getId();
84  }
85  }
86  $this->focusArea = ‪$focusArea;
87  if (‪$coverAreas !== null) {
89  }
90  }
91 
95  public static function ‪createFromConfiguration(string ‪$id, array $config): CropVariant
96  {
97  try {
98  return new self(
99  ‪$id,
100  $config['title'] ?? '',
101  ‪Area::createFromConfiguration($config['cropArea']),
102  isset($config['allowedAspectRatios']) ? Ratio::createMultipleFromConfiguration($config['allowedAspectRatios']) : null,
103  $config['selectedRatio'] ?? null,
104  isset($config['focusArea']) ? ‪Area::‪createFromConfiguration($config['focusArea']) : null,
105  isset($config['coverAreas']) ? ‪Area::createMultipleFromConfiguration($config['coverAreas']) : null
106  );
107  } catch (\Throwable $throwable) {
108  throw new ‪InvalidConfigurationException(sprintf('Invalid type in configuration for crop variant: %s', $throwable->getMessage()), 1485278693, $throwable);
109  }
110  }
111 
115  public function ‪asArray(): array
116  {
117  $coverAreasAsArray = null;
118  $allowedAspectRatiosAsArray = [];
119  foreach ($this->allowedAspectRatios ?? [] as ‪$id => $allowedAspectRatio) {
120  $allowedAspectRatiosAsArray[‪$id] = $allowedAspectRatio->asArray();
121  }
122  if ($this->coverAreas !== null) {
123  $coverAreasAsArray = [];
124  foreach ($this->coverAreas as $coverArea) {
125  $coverAreasAsArray[] = $coverArea->asArray();
126  }
127  }
128  return [
129  'id' => ‪$this->id,
130  'title' => ‪$this->title,
131  'cropArea' => $this->cropArea->asArray(),
132  'allowedAspectRatios' => $allowedAspectRatiosAsArray,
133  'selectedRatio' => ‪$this->selectedRatio,
134  'focusArea' => $this->focusArea ? $this->focusArea->asArray() : null,
135  'coverAreas' => $coverAreasAsArray ?? null,
136  ];
137  }
138 
139  public function ‪getId(): string
140  {
141  return ‪$this->id;
142  }
143 
144  public function ‪getCropArea(): ‪Area
145  {
146  return ‪$this->cropArea;
147  }
148 
152  public function ‪getFocusArea()
153  {
154  return ‪$this->focusArea;
155  }
156 
157  public function ‪applyRatioRestrictionToSelectedCropArea(FileInterface $file): CropVariant
158  {
159  if (!$this->selectedRatio) {
160  return $this;
161  }
162  $newVariant = clone $this;
163  $newArea = $this->cropArea->makeAbsoluteBasedOnFile($file);
164  $newArea = $newArea->applyRatioRestriction($this->allowedAspectRatios[$this->selectedRatio]);
165  $newVariant->cropArea = $newArea->makeRelativeBasedOnFile($file);
166  return $newVariant;
167  }
168 
172  protected function ‪setAllowedAspectRatios(Ratio ...$ratios)
173  {
174  $this->allowedAspectRatios = [];
175  foreach ($ratios as $ratio) {
176  $this->‪addAllowedAspectRatio($ratio);
177  }
178  }
179 
183  protected function ‪addAllowedAspectRatio(Ratio $ratio)
184  {
185  if (isset($this->allowedAspectRatios[$ratio->getId()])) {
186  throw new InvalidConfigurationException(sprintf('Ratio with with duplicate ID (%s) is configured. Make sure all configured ratios have different ids.', $ratio->getId()), 1485274618);
187  }
188  $this->allowedAspectRatios[$ratio->getId()] = $ratio;
189  }
190 
194  protected function ‪setCoverAreas(‪Area ...$areas)
195  {
196  $this->coverAreas = [];
197  foreach ($areas as $area) {
198  $this->‪addCoverArea($area);
199  }
200  }
201 
205  protected function ‪addCoverArea(‪Area $area)
206  {
207  $this->coverAreas[] = $area;
208  }
209 }
‪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:59
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area\createMultipleFromConfiguration
‪static Area[] createMultipleFromConfiguration(array $config)
Definition: Area.php:69
‪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:165
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area\createFromConfiguration
‪static createFromConfiguration(array $config)
Definition: Area.php:51
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant\$focusArea
‪Area null $focusArea
Definition: CropVariant.php:46
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:26
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant\getCropArea
‪getCropArea()
Definition: CropVariant.php:137
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant
Definition: CropVariant.php:23
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant\getFocusArea
‪Area null getFocusArea()
Definition: CropVariant.php:145
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant\addAllowedAspectRatio
‪addAllowedAspectRatio(Ratio $ratio)
Definition: CropVariant.php:176
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area
Definition: Area.php:23
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant\getId
‪getId()
Definition: CropVariant.php:132
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant\$cropArea
‪Area $cropArea
Definition: CropVariant.php:34
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant\createFromConfiguration
‪static createFromConfiguration(string $id, array $config)
Definition: CropVariant.php:88
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant\$title
‪string $title
Definition: CropVariant.php:30
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant\asArray
‪asArray()
Definition: CropVariant.php:108
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant\$id
‪string $id
Definition: CropVariant.php:26
‪TYPO3\CMS\Core\Imaging\ImageManipulation\InvalidConfigurationException
Definition: InvalidConfigurationException.php:23
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant\addCoverArea
‪addCoverArea(Area $area)
Definition: CropVariant.php:198
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant\setCoverAreas
‪setCoverAreas(Area ... $areas)
Definition: CropVariant.php:187
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant\$coverAreas
‪Area[] null $coverAreas
Definition: CropVariant.php:50
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant\applyRatioRestrictionToSelectedCropArea
‪applyRatioRestrictionToSelectedCropArea(FileInterface $file)
Definition: CropVariant.php:150
‪TYPO3\CMS\Core\Imaging\ImageManipulation
Definition: Area.php:18
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant\$selectedRatio
‪string $selectedRatio
Definition: CropVariant.php:42