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!
38 protected $allowedAspectRatios;
43 protected $selectedRatio;
53 protected $coverAreas;
57 * @param string $title
58 * @param Area $cropArea
59 * @param Ratio[] $allowedAspectRatios
60 * @param string|null $selectedRatio
61 * @param Area|null $focusArea
62 * @param Area[]|null $coverAreas
63 * @throws InvalidConfigurationException
65 public function __construct(
69 array $allowedAspectRatios = null,
70 string $selectedRatio = null,
71 Area
$focusArea = null,
72 array $coverAreas = null
75 $this->title
= $title;
76 $this->cropArea
= $cropArea;
77 if ($allowedAspectRatios) {
78 $this->setAllowedAspectRatios(...$allowedAspectRatios);
80 $this->selectedRatio
= $selectedRatio;
81 $this->focusArea
= $focusArea;
82 if ($coverAreas !== null) {
83 $this->setCoverAreas(...$coverAreas);
89 * @param array $config
91 * @throws InvalidConfigurationException
93 public static function createFromConfiguration(string $id, array $config): CropVariant
98 $config['title'] ??
'',
99 Area
::createFromConfiguration($config['cropArea']),
100 isset($config['allowedAspectRatios']) ? Ratio
::createMultipleFromConfiguration($config['allowedAspectRatios']) : null,
101 $config['selectedRatio'] ??
null,
102 isset($config['focusArea']) ? Area
::createFromConfiguration($config['focusArea']) : null,
103 isset($config['coverAreas']) ? Area
::createMultipleFromConfiguration($config['coverAreas']) : null
105 } catch (\Throwable
$throwable) {
106 throw new InvalidConfigurationException(sprintf('Invalid type in configuration for crop variant: %s', $throwable->getMessage()), 1485278693, $throwable);
114 public function asArray(): array
116 $allowedAspectRatiosAsArray = [];
117 foreach ($this->allowedAspectRatios
as $id => $allowedAspectRatio) {
118 $allowedAspectRatiosAsArray[$id] = $allowedAspectRatio->asArray();
120 if ($this->coverAreas
!== null) {
121 $coverAreasAsArray = [];
122 foreach ($this->coverAreas
as $coverArea) {
123 $coverAreasAsArray[] = $coverArea->asArray();
128 'title' => $this->title
,
129 'cropArea' => $this->cropArea
->asArray(),
130 'allowedAspectRatios' => $allowedAspectRatiosAsArray,
131 'selectedRatio' => $this->selectedRatio
,
132 'focusArea' => $this->focusArea ?
$this->focusArea
->asArray() : null,
133 'coverAreas' => $coverAreasAsArray ??
null,
140 public function getId(): string
148 public function getCropArea(): Area
150 return $this->cropArea
;
156 public function getFocusArea()
158 return $this->focusArea
;
162 * @param Ratio[] $ratios
163 * @throws InvalidConfigurationException
165 protected function setAllowedAspectRatios(Ratio
...$ratios)
167 $this->allowedAspectRatios
= [];
168 foreach ($ratios as $ratio) {
169 $this->addAllowedAspectRatio($ratio);
174 * @param Ratio $ratio
175 * @throws InvalidConfigurationException
177 protected function addAllowedAspectRatio(Ratio
$ratio)
179 if (isset($this->allowedAspectRatios
[$ratio->getId()])) {
180 throw new InvalidConfigurationException(sprintf('Ratio with with duplicate ID (%s) is configured. Make sure all configured ratios have different ids.', $ratio->getId()), 1485274618);
182 $this->allowedAspectRatios
[$ratio->getId()] = $ratio;
186 * @param Area[] $areas
187 * @throws InvalidConfigurationException
189 protected function setCoverAreas(Area
...$areas)
191 $this->coverAreas
= [];
192 foreach ($areas as $area) {
193 $this->addCoverArea($area);
199 * @throws InvalidConfigurationException
201 protected function addCoverArea(Area
$area)
203 $this->coverAreas
[] = $area;