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
;
20 class CropVariantCollection
25 protected $cropVariants;
28 * @param CropVariant[] cropVariants
29 * @throws \TYPO3\CMS\Core\Imaging\ImageManipulation\InvalidConfigurationException
31 public function __construct(array $cropVariants)
33 $this->setCropVariants(...$cropVariants);
37 * @param string $jsonString
38 * @param array $tcaConfig
39 * @return CropVariantCollection
41 public static function create(string $jsonString, array $tcaConfig = []): CropVariantCollection
43 $persistedCollectionConfig = empty($jsonString) ?
[] : json_decode($jsonString, true
);
44 if (empty($persistedCollectionConfig) && empty($tcaConfig)) {
45 return self
::createEmpty();
48 if ($tcaConfig === []) {
49 $tcaConfig = (array)$persistedCollectionConfig;
51 if (!is_array($persistedCollectionConfig)) {
52 $persistedCollectionConfig = [];
54 // Merge selected areas with crop tool configuration
55 reset($persistedCollectionConfig);
56 foreach ($tcaConfig as $id => &$cropVariantConfig) {
57 if (!isset($persistedCollectionConfig[$id])) {
58 $id = key($persistedCollectionConfig);
59 next($persistedCollectionConfig);
61 if (isset($persistedCollectionConfig[$id]['cropArea'])) {
62 $cropVariantConfig['cropArea'] = $persistedCollectionConfig[$id]['cropArea'];
64 if (isset($persistedCollectionConfig[$id]['focusArea'], $cropVariantConfig['focusArea'])) {
65 $cropVariantConfig['focusArea'] = $persistedCollectionConfig[$id]['focusArea'];
67 if (isset($persistedCollectionConfig[$id]['selectedRatio'], $cropVariantConfig['allowedAspectRatios'][$persistedCollectionConfig[$id]['selectedRatio']])) {
68 $cropVariantConfig['selectedRatio'] = $persistedCollectionConfig[$id]['selectedRatio'];
71 unset($cropVariantConfig);
74 foreach ($tcaConfig as $id => $cropVariantConfig) {
75 $cropVariants[] = CropVariant
::createFromConfiguration($id, $cropVariantConfig);
77 return new self($cropVariants);
78 } catch (\Throwable
$throwable) {
79 return self
::createEmpty();
87 public function asArray(): array
89 $cropVariantsAsArray = [];
90 foreach ($this->cropVariants
as $id => $cropVariant) {
91 $cropVariantsAsArray[$id] = $cropVariant->asArray();
93 return $cropVariantsAsArray;
97 * @param FileInterface $file
98 * @return CropVariantCollection
100 public function applyRatioRestrictionToSelectedCropArea(FileInterface
$file): CropVariantCollection
102 $newCollection = clone $this;
103 foreach ($this->cropVariants
as $id => $cropVariant) {
104 $newCollection->cropVariants
[$id] = $cropVariant->applyRatioRestrictionToSelectedCropArea($file);
106 return $newCollection;
109 public function __toString()
111 $filterNonPersistentKeys = function ($key) {
112 if (in_array($key, ['id', 'title', 'allowedAspectRatios', 'coverAreas'], true
)) {
117 $cropVariantsAsArray = [];
118 foreach ($this->cropVariants
as $id => $cropVariant) {
119 $cropVariantsAsArray[$id] = array_filter($cropVariant->asArray(), $filterNonPersistentKeys, ARRAY_FILTER_USE_KEY
);
121 return json_encode($cropVariantsAsArray);
128 public function getCropArea(string $id = 'default'): Area
130 if (isset($this->cropVariants
[$id])) {
131 return $this->cropVariants
[$id]->getCropArea();
133 return Area
::createEmpty();
140 public function getFocusArea(string $id = 'default'): Area
142 if (isset($this->cropVariants
[$id]) && $this->cropVariants
[$id]->getFocusArea() !== null
) {
143 return $this->cropVariants
[$id]->getFocusArea();
145 return Area
::createEmpty();
149 * @return CropVariantCollection
151 protected static function createEmpty(): CropVariantCollection
157 * @param CropVariant[] $cropVariants
158 * @throws \TYPO3\CMS\Core\Imaging\ImageManipulation\InvalidConfigurationException
160 protected function setCropVariants(CropVariant
...$cropVariants)
162 $this->cropVariants
= [];
163 foreach ($cropVariants as $cropVariant) {
164 $this->addCropVariant($cropVariant);
169 * @param CropVariant $cropVariant
170 * @throws InvalidConfigurationException
172 protected function addCropVariant(CropVariant
$cropVariant)
174 if (isset($this->cropVariants
[$cropVariant->getId()])) {
175 throw new InvalidConfigurationException(sprintf('Crop variant with with duplicate ID (%s) is configured. Make sure all configured cropVariants have different ids.', $cropVariant->getId()), 1485284352);
177 $this->cropVariants
[$cropVariant->getId()] = $cropVariant;