‪TYPO3CMS  9.5
CropVariant.php
Go to the documentation of this file.
1 <?php
2 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 {
25  protected ‪$id;
26 
30  protected ‪$title;
31 
35  protected ‪$cropArea;
36 
41 
45  protected ‪$selectedRatio;
46 
50  protected ‪$focusArea;
51 
55  protected ‪$coverAreas;
56 
67  public function ‪__construct(
68  string ‪$id,
69  string ‪$title,
71  array ‪$allowedAspectRatios = null,
72  string ‪$selectedRatio = null,
73  ‪Area ‪$focusArea = null,
74  array ‪$coverAreas = null
75  ) {
76  $this->id = ‪$id;
77  $this->title = ‪$title;
78  $this->cropArea = ‪$cropArea;
81  if (‪$selectedRatio && isset($this->allowedAspectRatios[‪$selectedRatio])) {
82  $this->selectedRatio = ‪$selectedRatio;
83  } else {
84  $this->selectedRatio = current($this->allowedAspectRatios)->getId();
85  }
86  }
87  $this->focusArea = ‪$focusArea;
88  if (‪$coverAreas !== null) {
90  }
91  }
92 
99  public static function ‪createFromConfiguration(string ‪$id, array $config): ‪CropVariant
100  {
101  try {
102  return new self(
103  ‪$id,
104  $config['title'] ?? '',
105  ‪Area::createFromConfiguration($config['cropArea']),
106  isset($config['allowedAspectRatios']) ? ‪Ratio::createMultipleFromConfiguration($config['allowedAspectRatios']) : null,
107  $config['selectedRatio'] ?? null,
108  isset($config['focusArea']) ? ‪Area::createFromConfiguration($config['focusArea']) : null,
109  isset($config['coverAreas']) ? ‪Area::createMultipleFromConfiguration($config['coverAreas']) : null
110  );
111  } catch (\Throwable $throwable) {
112  throw new ‪InvalidConfigurationException(sprintf('Invalid type in configuration for crop variant: %s', $throwable->getMessage()), 1485278693, $throwable);
113  }
114  }
115 
120  public function ‪asArray(): array
121  {
122  $allowedAspectRatiosAsArray = [];
123  foreach ($this->allowedAspectRatios as ‪$id => $allowedAspectRatio) {
124  $allowedAspectRatiosAsArray[‪$id] = $allowedAspectRatio->asArray();
125  }
126  if ($this->coverAreas !== null) {
127  $coverAreasAsArray = [];
128  foreach ($this->coverAreas as $coverArea) {
129  $coverAreasAsArray[] = $coverArea->asArray();
130  }
131  }
132  return [
133  'id' => ‪$this->id,
134  'title' => ‪$this->title,
135  'cropArea' => $this->cropArea->asArray(),
136  'allowedAspectRatios' => $allowedAspectRatiosAsArray,
137  'selectedRatio' => ‪$this->selectedRatio,
138  'focusArea' => $this->focusArea ? $this->focusArea->asArray() : null,
139  'coverAreas' => $coverAreasAsArray ?? null,
140  ];
141  }
142 
146  public function ‪getId(): string
147  {
148  return ‪$this->id;
149  }
150 
154  public function ‪getCropArea(): ‪Area
155  {
156  return ‪$this->cropArea;
157  }
158 
162  public function ‪getFocusArea()
163  {
165  }
166 
172  {
173  if (!$this->selectedRatio) {
174  return $this;
175  }
176  $newVariant = clone $this;
177  $newArea = $this->cropArea->makeAbsoluteBasedOnFile($file);
178  $newArea = $newArea->applyRatioRestriction($this->allowedAspectRatios[$this->selectedRatio]);
179  $newVariant->cropArea = $newArea->makeRelativeBasedOnFile($file);
180  return $newVariant;
181  }
182 
187  protected function ‪setAllowedAspectRatios(‪Ratio ...$ratios)
188  {
189  $this->allowedAspectRatios = [];
190  foreach ($ratios as $ratio) {
191  $this->‪addAllowedAspectRatio($ratio);
192  }
193  }
194 
199  protected function ‪addAllowedAspectRatio(Ratio $ratio)
200  {
201  if (isset($this->allowedAspectRatios[$ratio->getId()])) {
202  throw new InvalidConfigurationException(sprintf('Ratio with with duplicate ID (%s) is configured. Make sure all configured ratios have different ids.', $ratio->getId()), 1485274618);
203  }
204  $this->allowedAspectRatios[$ratio->getId()] = $ratio;
205  }
206 
211  protected function ‪setCoverAreas(‪Area ...$areas)
212  {
213  $this->coverAreas = [];
214  foreach ($areas as $area) {
215  $this->‪addCoverArea($area);
216  }
217  }
218 
223  protected function ‪addCoverArea(Area $area)
224  {
225  $this->coverAreas[] = $area;
226  }
227 }
‪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:60
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area\createMultipleFromConfiguration
‪static Area[] createMultipleFromConfiguration(array $config)
Definition: Area.php:76
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant\$allowedAspectRatios
‪Ratio[] $allowedAspectRatios
Definition: CropVariant.php:36
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant\setAllowedAspectRatios
‪setAllowedAspectRatios(Ratio ... $ratios)
Definition: CropVariant.php:180
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant\$focusArea
‪Area null $focusArea
Definition: CropVariant.php:44
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:21
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant
Definition: CropVariant.php:21
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant\getFocusArea
‪Area null getFocusArea()
Definition: CropVariant.php:155
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant\addAllowedAspectRatio
‪addAllowedAspectRatio(Ratio $ratio)
Definition: CropVariant.php:192
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant\getId
‪string getId()
Definition: CropVariant.php:139
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area\createFromConfiguration
‪static Area createFromConfiguration(array $config)
Definition: Area.php:57
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area
Definition: Area.php:21
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant\$cropArea
‪Area $cropArea
Definition: CropVariant.php:32
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant\$title
‪string $title
Definition: CropVariant.php:28
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Ratio\getId
‪string getId()
Definition: Ratio.php:40
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Ratio
Definition: Ratio.php:19
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant\$id
‪string $id
Definition: CropVariant.php:24
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant\createFromConfiguration
‪static CropVariant createFromConfiguration(string $id, array $config)
Definition: CropVariant.php:92
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant\getCropArea
‪Area getCropArea()
Definition: CropVariant.php:147
‪TYPO3\CMS\Core\Imaging\ImageManipulation\InvalidConfigurationException
Definition: InvalidConfigurationException.php:22
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant\asArray
‪array asArray()
Definition: CropVariant.php:113
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant\applyRatioRestrictionToSelectedCropArea
‪CropVariant applyRatioRestrictionToSelectedCropArea(FileInterface $file)
Definition: CropVariant.php:164
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant\addCoverArea
‪addCoverArea(Area $area)
Definition: CropVariant.php:216
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant\setCoverAreas
‪setCoverAreas(Area ... $areas)
Definition: CropVariant.php:204
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant\$coverAreas
‪Area[] null $coverAreas
Definition: CropVariant.php:48
‪TYPO3\CMS\Core\Imaging\ImageManipulation
Definition: Area.php:3
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Ratio\createMultipleFromConfiguration
‪static Ratio[] createMultipleFromConfiguration(array $config)
Definition: Ratio.php:50
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant\$selectedRatio
‪string $selectedRatio
Definition: CropVariant.php:40