[FEATURE] Add new imageManipulation supporting multiple crop variants
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Imaging / ImageManipulation / Ratio.php
1 <?php
2 declare(strict_types=1);
3 namespace TYPO3\CMS\Core\Imaging\ImageManipulation;
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
18 class Ratio
19 {
20 /**
21 * @var string
22 */
23 protected $id;
24 /**
25 * @var string
26 */
27 protected $title;
28 /**
29 * @var float
30 */
31 protected $value;
32
33 public function __construct(string $id, string $title, float $value)
34 {
35 $this->id = $id;
36 $this->title = $title;
37 $this->value = $value;
38 }
39
40 /**
41 * @return string
42 */
43 public function getId(): string
44 {
45 return $this->id;
46 }
47
48 /**
49 * @param array $config
50 * @return Ratio[]
51 * @throws \TYPO3\CMS\Core\Imaging\ImageManipulation\InvalidConfigurationException
52 */
53 public static function createMultipleFromConfiguration(array $config): array
54 {
55 $areas = [];
56 try {
57 foreach ($config as $id => $ratioConfig) {
58 $areas[] = new self(
59 $id,
60 $ratioConfig['title'],
61 $ratioConfig['value']
62 );
63 }
64 } catch (\Throwable $throwable) {
65 throw new InvalidConfigurationException(sprintf('Invalid type for ratio id given: %s', $throwable->getMessage()), 1486313971, $throwable);
66 }
67 return $areas;
68 }
69
70 /**
71 * @return array
72 * @internal
73 */
74 public function asArray(): array
75 {
76 return [
77 'id' => $this->id,
78 'title' => $this->title,
79 'value' => $this->value,
80 ];
81 }
82 }