‪TYPO3CMS  9.5
CropVariantCollection.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 ‪$cropVariants;
26 
31  public function ‪__construct(array ‪$cropVariants)
32  {
33  $this->‪setCropVariants(...$cropVariants);
34  }
35 
41  public static function ‪create(string $jsonString, array $tcaConfig = []): ‪CropVariantCollection
42  {
43  $persistedCollectionConfig = empty($jsonString) ? [] : json_decode($jsonString, true);
44  if (empty($persistedCollectionConfig) && empty($tcaConfig)) {
45  return ‪self::createEmpty();
46  }
47  try {
48  if ($tcaConfig === []) {
49  $tcaConfig = (array)$persistedCollectionConfig;
50  } else {
51  if (!is_array($persistedCollectionConfig)) {
52  $persistedCollectionConfig = [];
53  }
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);
60  }
61  if (isset($persistedCollectionConfig[$id]['cropArea'])) {
62  $cropVariantConfig['cropArea'] = $persistedCollectionConfig[$id]['cropArea'];
63  }
64  if (isset($persistedCollectionConfig[$id]['focusArea'], $cropVariantConfig['focusArea'])) {
65  $cropVariantConfig['focusArea'] = $persistedCollectionConfig[$id]['focusArea'];
66  }
67  if (isset($persistedCollectionConfig[$id]['selectedRatio'], $cropVariantConfig['allowedAspectRatios'][$persistedCollectionConfig[$id]['selectedRatio']])) {
68  $cropVariantConfig['selectedRatio'] = $persistedCollectionConfig[$id]['selectedRatio'];
69  }
70  }
71  unset($cropVariantConfig);
72  }
73  ‪$cropVariants = [];
74  foreach ($tcaConfig as $id => $cropVariantConfig) {
75  ‪$cropVariants[] = ‪CropVariant::createFromConfiguration($id, $cropVariantConfig);
76  }
77  return new self(‪$cropVariants);
78  } catch (\Throwable $throwable) {
79  return ‪self::createEmpty();
80  }
81  }
82 
87  public function ‪asArray(): array
88  {
89  $cropVariantsAsArray = [];
90  foreach ($this->cropVariants as $id => $cropVariant) {
91  $cropVariantsAsArray[$id] = $cropVariant->asArray();
92  }
93  return $cropVariantsAsArray;
94  }
95 
101  {
102  $newCollection = clone $this;
103  foreach ($this->cropVariants as $id => $cropVariant) {
104  $newCollection->cropVariants[$id] = $cropVariant->‪applyRatioRestrictionToSelectedCropArea($file);
105  }
106  return $newCollection;
107  }
108 
109  public function ‪__toString()
110  {
111  $filterNonPersistentKeys = function ($key) {
112  if (in_array($key, ['id', 'title', 'allowedAspectRatios', 'coverAreas'], true)) {
113  return false;
114  }
115  return true;
116  };
117  $cropVariantsAsArray = [];
118  foreach ($this->cropVariants as $id => $cropVariant) {
119  $cropVariantsAsArray[$id] = array_filter($cropVariant->asArray(), $filterNonPersistentKeys, ARRAY_FILTER_USE_KEY);
120  }
121  return json_encode($cropVariantsAsArray) ?: '[]';
122  }
123 
128  public function ‪getCropArea(string $id = 'default'): ‪Area
129  {
130  if (isset($this->cropVariants[$id])) {
131  return $this->cropVariants[$id]->getCropArea();
132  }
133  return ‪Area::createEmpty();
134  }
135 
140  public function ‪getFocusArea(string $id = 'default'): Area
141  {
142  if (isset($this->cropVariants[$id]) && $this->cropVariants[$id]->‪getFocusArea() !== null) {
143  return $this->cropVariants[$id]->getFocusArea();
144  }
145  return ‪Area::createEmpty();
146  }
147 
151  protected static function ‪createEmpty(): CropVariantCollection
152  {
153  return new self([]);
154  }
155 
160  protected function ‪setCropVariants(CropVariant ...‪$cropVariants)
161  {
162  $this->cropVariants = [];
163  foreach (‪$cropVariants as $cropVariant) {
164  $this->‪addCropVariant($cropVariant);
165  }
166  }
167 
172  protected function ‪addCropVariant(CropVariant $cropVariant)
173  {
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);
176  }
177  $this->cropVariants[$cropVariant->getId()] = $cropVariant;
178  }
179 }
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection\createEmpty
‪static CropVariantCollection createEmpty()
Definition: CropVariantCollection.php:150
‪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\getId
‪string getId()
Definition: CropVariant.php:139
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection\setCropVariants
‪setCropVariants(CropVariant ... $cropVariants)
Definition: CropVariantCollection.php:159
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area
Definition: Area.php:21
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection\addCropVariant
‪addCropVariant(CropVariant $cropVariant)
Definition: CropVariantCollection.php:171
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection\asArray
‪array asArray()
Definition: CropVariantCollection.php:86
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection\getFocusArea
‪Area getFocusArea(string $id='default')
Definition: CropVariantCollection.php:139
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant\createFromConfiguration
‪static CropVariant createFromConfiguration(string $id, array $config)
Definition: CropVariant.php:92
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection\getCropArea
‪Area getCropArea(string $id='default')
Definition: CropVariantCollection.php:127
‪TYPO3\CMS\Core\Imaging\ImageManipulation\InvalidConfigurationException
Definition: InvalidConfigurationException.php:22
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection\__toString
‪__toString()
Definition: CropVariantCollection.php:108
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection
Definition: CropVariantCollection.php:21
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection\applyRatioRestrictionToSelectedCropArea
‪CropVariantCollection applyRatioRestrictionToSelectedCropArea(FileInterface $file)
Definition: CropVariantCollection.php:99
‪TYPO3\CMS\Core\Imaging\ImageManipulation
Definition: Area.php:3
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection\$cropVariants
‪CropVariant[] $cropVariants
Definition: CropVariantCollection.php:24
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection\create
‪static CropVariantCollection create(string $jsonString, array $tcaConfig=[])
Definition: CropVariantCollection.php:40
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection\__construct
‪__construct(array $cropVariants)
Definition: CropVariantCollection.php:30
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area\createEmpty
‪static Area createEmpty()
Definition: Area.php:88