2 declare(strict_types
=1);
3 namespace TYPO3\CMS\Core\Imaging\ImageManipulation
;
6 * This file is part of the TYPO3 CMS project.
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.
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
15 * The TYPO3 project - inspiring people to share!
18 use TYPO3\CMS\Core\
Resource\FileInterface
;
40 protected $allowedAspectRatios;
45 protected $selectedRatio;
55 protected $coverAreas;
59 * @param string $title
60 * @param Area $cropArea
61 * @param Ratio[] $allowedAspectRatios
62 * @param string|null $selectedRatio
63 * @param Area|null $focusArea
64 * @param Area[]|null $coverAreas
65 * @throws InvalidConfigurationException
67 public function __construct(
71 array $allowedAspectRatios = null,
72 string $selectedRatio = null,
73 Area
$focusArea = null,
74 array $coverAreas = null
77 $this->title
= $title;
78 $this->cropArea
= $cropArea;
79 if ($allowedAspectRatios) {
80 $this->setAllowedAspectRatios(...$allowedAspectRatios);
81 if ($selectedRatio && isset($this->allowedAspectRatios
[$selectedRatio])) {
82 $this->selectedRatio
= $selectedRatio;
84 $this->selectedRatio
= current($this->allowedAspectRatios
)->getId();
87 $this->focusArea
= $focusArea;
88 if ($coverAreas !== null) {
89 $this->setCoverAreas(...$coverAreas);
95 * @param array $config
97 * @throws InvalidConfigurationException
99 public static function createFromConfiguration(string $id, array $config): CropVariant
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
111 } catch (\Throwable
$throwable) {
112 throw new InvalidConfigurationException(sprintf('Invalid type in configuration for crop variant: %s', $throwable->getMessage()), 1485278693, $throwable);
120 public function asArray(): array
122 $allowedAspectRatiosAsArray = [];
123 foreach ($this->allowedAspectRatios
as $id => $allowedAspectRatio) {
124 $allowedAspectRatiosAsArray[$id] = $allowedAspectRatio->asArray();
126 if ($this->coverAreas
!== null) {
127 $coverAreasAsArray = [];
128 foreach ($this->coverAreas
as $coverArea) {
129 $coverAreasAsArray[] = $coverArea->asArray();
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,
146 public function getId(): string
154 public function getCropArea(): Area
156 return $this->cropArea
;
162 public function getFocusArea()
164 return $this->focusArea
;
168 * @param FileInterface $file
169 * @return CropVariant
171 public function applyRatioRestrictionToSelectedCropArea(FileInterface
$file): CropVariant
173 if (!$this->selectedRatio
) {
176 $newVariant = clone $this;
177 $newArea = $this->cropArea
->makeAbsoluteBasedOnFile($file);
178 $newArea = $newArea->applyRatioRestriction($this->allowedAspectRatios
[$this->selectedRatio
]);
179 $newVariant->cropArea
= $newArea->makeRelativeBasedOnFile($file);
184 * @param Ratio[] $ratios
185 * @throws InvalidConfigurationException
187 protected function setAllowedAspectRatios(Ratio
...$ratios)
189 $this->allowedAspectRatios
= [];
190 foreach ($ratios as $ratio) {
191 $this->addAllowedAspectRatio($ratio);
196 * @param Ratio $ratio
197 * @throws InvalidConfigurationException
199 protected function addAllowedAspectRatio(Ratio
$ratio)
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);
204 $this->allowedAspectRatios
[$ratio->getId()] = $ratio;
208 * @param Area[] $areas
209 * @throws InvalidConfigurationException
211 protected function setCoverAreas(Area
...$areas)
213 $this->coverAreas
= [];
214 foreach ($areas as $area) {
215 $this->addCoverArea($area);
221 * @throws InvalidConfigurationException
223 protected function addCoverArea(Area
$area)
225 $this->coverAreas
[] = $area;