‪TYPO3CMS  ‪main
Area.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 
22 class ‪Area
23 {
27  protected ‪$x;
28 
32  protected ‪$y;
33 
37  protected ‪$width;
38 
42  protected ‪$height;
43 
44  public function ‪__construct(float ‪$x, float ‪$y, float ‪$width, float ‪$height)
45  {
46  $this->x = ‪$x;
47  $this->y = ‪$y;
48  $this->width = ‪$width;
49  $this->height = ‪$height;
50  }
51 
55  public static function ‪createFromConfiguration(array $config): ‪Area
56  {
57  try {
58  return new self(
59  (float)$config['x'],
60  (float)$config['y'],
61  (float)$config['width'],
62  (float)$config['height']
63  );
64  } catch (\Throwable $throwable) {
65  throw new ‪InvalidConfigurationException(sprintf('Invalid type for area property given: %s', $throwable->getMessage()), 1485279226, $throwable);
66  }
67  }
68 
73  public static function ‪createMultipleFromConfiguration(array $config): array
74  {
75  $areas = [];
76  foreach ($config as $areaConfig) {
77  $areas[] = ‪self::createFromConfiguration($areaConfig);
78  }
79  return $areas;
80  }
81 
85  public static function ‪createEmpty()
86  {
87  return new self(0.0, 0.0, 1.0, 1.0);
88  }
89 
90  public function ‪getWidth(): float
91  {
92  return ‪$this->width;
93  }
94 
95  public function ‪getHeight(): float
96  {
97  return ‪$this->height;
98  }
99 
100  public function ‪getOffsetLeft(): float
101  {
102  return ‪$this->x;
103  }
104 
105  public function ‪getOffsetTop(): float
106  {
107  return ‪$this->y;
108  }
109 
113  public function ‪asArray(): array
114  {
115  return [
116  'x' => ‪$this->x,
117  'y' => ‪$this->y,
118  'width' => ‪$this->width,
119  'height' => ‪$this->height,
120  ];
121  }
122 
126  public function ‪makeAbsoluteBasedOnFile(‪FileInterface $file)
127  {
128  return new self(
129  $this->x * $file->‪getProperty('width'),
130  $this->y * $file->‪getProperty('height'),
131  $this->width * $file->‪getProperty('width'),
132  $this->height * $file->‪getProperty('height')
133  );
134  }
135 
139  public function ‪makeRelativeBasedOnFile(‪FileInterface $file)
140  {
141  ‪$width = $file->‪getProperty('width');
142  ‪$height = $file->‪getProperty('height');
143 
144  if (empty(‪$width) || empty(‪$height)) {
145  return ‪self::createEmpty();
146  }
147 
148  return new self(
149  $this->x / ‪$width,
150  $this->y / ‪$height,
151  $this->width / ‪$width,
152  $this->height / ‪$height
153  );
154  }
155 
156  public function ‪applyRatioRestriction(Ratio $ratio): ‪Area
157  {
158  if ($ratio->isFree()) {
159  return $this;
160  }
161  $expectedRatio = $ratio->getRatioValue();
162  $newArea = clone $this;
163  if ($newArea->height * $expectedRatio > $newArea->width) {
164  $newArea->height = $newArea->width / $expectedRatio;
165  $newArea->y += ($this->height - $newArea->height) / 2;
166  } else {
167  $newArea->width = $newArea->height * $expectedRatio;
168  $newArea->x += ($this->width - $newArea->width) / 2;
169  }
170  return $newArea;
171  }
172 
176  public function ‪isEmpty()
177  {
178  return $this->x === 0.0 && $this->y === 0.0 && $this->width === 1.0 && $this->height === 1.0;
179  }
180 
184  public function ‪__toString()
185  {
186  if ($this->‪isEmpty()) {
187  return '';
188  }
189  return json_encode($this->‪asArray());
190  }
191 }
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area\createMultipleFromConfiguration
‪static Area[] createMultipleFromConfiguration(array $config)
Definition: Area.php:69
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area\getWidth
‪getWidth()
Definition: Area.php:86
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area\createFromConfiguration
‪static createFromConfiguration(array $config)
Definition: Area.php:51
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:26
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area\makeAbsoluteBasedOnFile
‪Area makeAbsoluteBasedOnFile(FileInterface $file)
Definition: Area.php:122
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area
Definition: Area.php:23
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area\getHeight
‪getHeight()
Definition: Area.php:91
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area\asArray
‪asArray()
Definition: Area.php:109
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area\makeRelativeBasedOnFile
‪Area makeRelativeBasedOnFile(FileInterface $file)
Definition: Area.php:135
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area\$x
‪float $x
Definition: Area.php:26
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area\$width
‪float $width
Definition: Area.php:34
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area\$height
‪float $height
Definition: Area.php:38
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area\isEmpty
‪bool isEmpty()
Definition: Area.php:172
‪TYPO3\CMS\Core\Imaging\ImageManipulation\InvalidConfigurationException
Definition: InvalidConfigurationException.php:23
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area\applyRatioRestriction
‪applyRatioRestriction(Ratio $ratio)
Definition: Area.php:152
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area\__toString
‪string __toString()
Definition: Area.php:180
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area\getOffsetTop
‪getOffsetTop()
Definition: Area.php:101
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area\getOffsetLeft
‪getOffsetLeft()
Definition: Area.php:96
‪TYPO3\CMS\Core\Imaging\ImageManipulation
Definition: Area.php:18
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area\__construct
‪__construct(float $x, float $y, float $width, float $height)
Definition: Area.php:40
‪TYPO3\CMS\Core\Resource\FileInterface\getProperty
‪getProperty(string $key)
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area\$y
‪float $y
Definition: Area.php:30
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area\createEmpty
‪static Area createEmpty()
Definition: Area.php:81