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 class CropVariantCollection
23 protected $cropVariants;
26 * @param CropVariant[] cropVariants
27 * @throws \TYPO3\CMS\Core\Imaging\ImageManipulation\InvalidConfigurationException
29 public function __construct(array $cropVariants)
31 $this->setCropVariants(...$cropVariants);
35 * @param string $jsonString
36 * @param array $tcaConfig
37 * @return CropVariantCollection
39 public static function create(string $jsonString, array $tcaConfig = []): CropVariantCollection
41 if (empty($jsonString) && empty($tcaConfig)) {
42 return self
::createEmpty();
44 $persistedCollectionConfig = json_decode($jsonString, true);
45 if (!is_array($persistedCollectionConfig)) {
46 $persistedCollectionConfig = [];
49 if ($tcaConfig === []) {
50 $tcaConfig = $persistedCollectionConfig;
52 // Merge selected areas with crop tool configuration
53 reset($persistedCollectionConfig);
54 foreach ($tcaConfig as $id => &$cropVariantConfig) {
55 if (!isset($persistedCollectionConfig[$id])) {
56 $id = key($persistedCollectionConfig);
57 next($persistedCollectionConfig);
59 if (isset($persistedCollectionConfig[$id]['cropArea'], $cropVariantConfig['cropArea'])) {
60 $cropVariantConfig['cropArea'] = $persistedCollectionConfig[$id]['cropArea'];
62 if (isset($persistedCollectionConfig[$id]['focusArea'], $cropVariantConfig['focusArea'])) {
63 $cropVariantConfig['focusArea'] = $persistedCollectionConfig[$id]['focusArea'];
65 if (isset($persistedCollectionConfig[$id]['selectedRatio'], $cropVariantConfig['selectedRatio'])) {
66 $cropVariantConfig['selectedRatio'] = $persistedCollectionConfig[$id]['selectedRatio'];
69 unset($cropVariantConfig);
72 foreach ($tcaConfig as $id => $cropVariantConfig) {
73 $cropVariants[] = CropVariant
::createFromConfiguration($id, $cropVariantConfig);
75 return new self($cropVariants);
76 } catch (\Throwable
$throwable) {
77 return self
::createEmpty();
85 public function asArray(): array
87 $cropVariantsAsArray = [];
88 foreach ($this->cropVariants
as $id => $cropVariant) {
89 $cropVariantsAsArray[$id] = $cropVariant->asArray();
91 return $cropVariantsAsArray;
98 public function getCropArea(string $id = 'default'): Area
100 if (isset($this->cropVariants
[$id])) {
101 return $this->cropVariants
[$id]->getCropArea();
103 return Area
::createEmpty();
111 public function getFocusArea(string $id = 'default'): Area
113 if (isset($this->cropVariants
[$id]) && $this->cropVariants
[$id]->getFocusArea() !== null) {
114 return $this->cropVariants
[$id]->getFocusArea();
116 return Area
::createEmpty();
121 * @return CropVariantCollection
123 protected static function createEmpty(): CropVariantCollection
129 * @param CropVariant[] ...$cropVariants
130 * @throws \TYPO3\CMS\Core\Imaging\ImageManipulation\InvalidConfigurationException
132 protected function setCropVariants(CropVariant
...$cropVariants)
134 $this->cropVariants
= [];
135 foreach ($cropVariants as $cropVariant) {
136 $this->addCropVariant($cropVariant);
141 * @param CropVariant $cropVariant
142 * @throws InvalidConfigurationException
144 protected function addCropVariant(CropVariant
$cropVariant)
146 if (isset($this->cropVariants
[$cropVariant->getId()])) {
147 throw new InvalidConfigurationException(sprintf('Crop variant with with duplicate ID (%s) is configured. Make sure all configured cropVariants have different ids.', $cropVariant->getId()), 1485284352);
149 $this->cropVariants
[$cropVariant->getId()] = $cropVariant;