‪TYPO3CMS  ‪main
CropVariantCollection.php
Go to the documentation of this file.
1 <?php
2 
3 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 
23 {
27  protected ‪$cropVariants;
28 
33  public function ‪__construct(array ‪$cropVariants)
34  {
35  $this->‪setCropVariants(...$cropVariants);
36  }
37 
38  public static function ‪create(string $jsonString, array $tcaConfig = []): ‪CropVariantCollection
39  {
40  $persistedCollectionConfig = empty($jsonString) ? [] : json_decode($jsonString, true);
41  if (empty($persistedCollectionConfig) && empty($tcaConfig)) {
42  return ‪self::createEmpty();
43  }
44  try {
45  if ($tcaConfig === []) {
46  $tcaConfig = (array)$persistedCollectionConfig;
47  } else {
48  if (!is_array($persistedCollectionConfig)) {
49  $persistedCollectionConfig = [];
50  }
51  // Merge selected areas with crop tool configuration
52  reset($persistedCollectionConfig);
53  foreach ($tcaConfig as $id => &$cropVariantConfig) {
54  if (!isset($persistedCollectionConfig[$id])) {
55  $id = key($persistedCollectionConfig);
56  next($persistedCollectionConfig);
57  }
58  if (isset($persistedCollectionConfig[$id]['cropArea'])) {
59  $cropVariantConfig['cropArea'] = $persistedCollectionConfig[$id]['cropArea'];
60  }
61  if (isset($persistedCollectionConfig[$id]['focusArea'], $cropVariantConfig['focusArea'])) {
62  $cropVariantConfig['focusArea'] = $persistedCollectionConfig[$id]['focusArea'];
63  }
64  if (isset($persistedCollectionConfig[$id]['selectedRatio'], $cropVariantConfig['allowedAspectRatios'][$persistedCollectionConfig[$id]['selectedRatio']])) {
65  $cropVariantConfig['selectedRatio'] = $persistedCollectionConfig[$id]['selectedRatio'];
66  }
67  }
68  unset($cropVariantConfig);
69  }
70  ‪$cropVariants = [];
71  foreach ($tcaConfig as $id => $cropVariantConfig) {
72  ‪$cropVariants[] = ‪CropVariant::createFromConfiguration($id, $cropVariantConfig);
73  }
74  return new self(‪$cropVariants);
75  } catch (\Throwable $throwable) {
76  return ‪self::createEmpty();
77  }
78  }
79 
83  public function ‪asArray(): array
84  {
85  $cropVariantsAsArray = [];
86  foreach ($this->cropVariants as $id => $cropVariant) {
87  $cropVariantsAsArray[$id] = $cropVariant->asArray();
88  }
89  return $cropVariantsAsArray;
90  }
91 
93  {
94  $newCollection = clone $this;
95  foreach ($this->cropVariants as $id => $cropVariant) {
96  $newCollection->cropVariants[$id] = $cropVariant->‪applyRatioRestrictionToSelectedCropArea($file);
97  }
98  return $newCollection;
99  }
100 
101  public function ‪__toString()
102  {
103  $filterNonPersistentKeys = static function ($key) {
104  if (in_array($key, ['id', 'title', 'allowedAspectRatios', 'coverAreas'], true)) {
105  return false;
106  }
107  return true;
108  };
109  $cropVariantsAsArray = [];
110  foreach ($this->cropVariants as $id => $cropVariant) {
111  $cropVariantsAsArray[$id] = array_filter($cropVariant->asArray(), $filterNonPersistentKeys, ARRAY_FILTER_USE_KEY);
112  }
113  return json_encode($cropVariantsAsArray) ?: '[]';
114  }
115 
116  public function ‪getCropArea(string $id = 'default'): ‪Area
117  {
118  if (isset($this->cropVariants[$id])) {
119  return $this->cropVariants[$id]->getCropArea();
120  }
121  return ‪Area::createEmpty();
122  }
123 
124  public function ‪getFocusArea(string $id = 'default'): ‪Area
125  {
126  if (isset($this->cropVariants[$id]) && $this->cropVariants[$id]->‪getFocusArea() !== null) {
127  return $this->cropVariants[$id]->getFocusArea();
128  }
129  return ‪Area::createEmpty();
130  }
131 
132  protected static function ‪createEmpty(): ‪CropVariantCollection
133  {
134  return new self([]);
135  }
136 
140  protected function ‪setCropVariants(‪CropVariant ...‪$cropVariants)
141  {
142  $this->cropVariants = [];
143  foreach (‪$cropVariants as $cropVariant) {
144  $this->‪addCropVariant($cropVariant);
145  }
146  }
147 
151  protected function ‪addCropVariant(CropVariant $cropVariant)
152  {
153  if (isset($this->cropVariants[$cropVariant->getId()])) {
154  throw new InvalidConfigurationException(sprintf('Crop variant with with duplicate ID (%s) is configured. Make sure all configured cropVariants have different ids.', $cropVariant->getId()), 1485284352);
155  }
156  $this->cropVariants[$cropVariant->getId()] = $cropVariant;
157  }
158 }
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection\create
‪static create(string $jsonString, array $tcaConfig=[])
Definition: CropVariantCollection.php:37
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:26
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant
Definition: CropVariant.php:23
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection\setCropVariants
‪setCropVariants(CropVariant ... $cropVariants)
Definition: CropVariantCollection.php:139
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area
Definition: Area.php:23
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant\getId
‪getId()
Definition: CropVariant.php:132
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariant\createFromConfiguration
‪static createFromConfiguration(string $id, array $config)
Definition: CropVariant.php:88
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection\createEmpty
‪static createEmpty()
Definition: CropVariantCollection.php:131
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection\getCropArea
‪getCropArea(string $id='default')
Definition: CropVariantCollection.php:115
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection\getFocusArea
‪getFocusArea(string $id='default')
Definition: CropVariantCollection.php:123
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection\addCropVariant
‪addCropVariant(CropVariant $cropVariant)
Definition: CropVariantCollection.php:150
‪TYPO3\CMS\Core\Imaging\ImageManipulation\InvalidConfigurationException
Definition: InvalidConfigurationException.php:23
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection\__toString
‪__toString()
Definition: CropVariantCollection.php:100
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection\applyRatioRestrictionToSelectedCropArea
‪applyRatioRestrictionToSelectedCropArea(FileInterface $file)
Definition: CropVariantCollection.php:91
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection
Definition: CropVariantCollection.php:23
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection\asArray
‪asArray()
Definition: CropVariantCollection.php:82
‪TYPO3\CMS\Core\Imaging\ImageManipulation
Definition: Area.php:18
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection\$cropVariants
‪CropVariant[] $cropVariants
Definition: CropVariantCollection.php:26
‪TYPO3\CMS\Core\Imaging\ImageManipulation\CropVariantCollection\__construct
‪__construct(array $cropVariants)
Definition: CropVariantCollection.php:32
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area\createEmpty
‪static Area createEmpty()
Definition: Area.php:81