‪TYPO3CMS  10.4
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 
50  public function ‪__construct(float ‪$x, float ‪$y, float ‪$width, float ‪$height)
51  {
52  $this->x = ‪$x;
53  $this->y = ‪$y;
54  $this->width = ‪$width;
55  $this->height = ‪$height;
56  }
57 
63  public static function ‪createFromConfiguration(array $config): ‪Area
64  {
65  try {
66  return new self(
67  (float)$config['x'],
68  (float)$config['y'],
69  (float)$config['width'],
70  (float)$config['height']
71  );
72  } catch (\Throwable $throwable) {
73  throw new InvalidConfigurationException(sprintf('Invalid type for area property given: %s', $throwable->getMessage()), 1485279226, $throwable);
74  }
75  }
76 
82  public static function ‪createMultipleFromConfiguration(array $config): array
83  {
84  $areas = [];
85  foreach ($config as $areaConfig) {
86  $areas[] = ‪self::createFromConfiguration($areaConfig);
87  }
88  return $areas;
89  }
90 
94  public static function ‪createEmpty()
95  {
96  return new self(0.0, 0.0, 1.0, 1.0);
97  }
98 
99  public function ‪getWidth(): float
100  {
101  return ‪$this->width;
102  }
103 
104  public function ‪getHeight(): float
105  {
106  return ‪$this->height;
107  }
108 
109  public function ‪getOffsetLeft(): float
110  {
111  return ‪$this->x;
112  }
113 
114  public function ‪getOffsetTop(): float
115  {
116  return ‪$this->y;
117  }
118 
123  public function ‪asArray(): array
124  {
125  return [
126  'x' => ‪$this->x,
127  'y' => ‪$this->y,
128  'width' => ‪$this->width,
129  'height' => ‪$this->height,
130  ];
131  }
132 
137  public function ‪makeAbsoluteBasedOnFile(FileInterface $file)
138  {
139  return new self(
140  $this->x * $file->getProperty('width'),
141  $this->y * $file->getProperty('height'),
142  $this->width * $file->getProperty('width'),
143  $this->height * $file->getProperty('height')
144  );
145  }
146 
151  public function ‪makeRelativeBasedOnFile(FileInterface $file)
152  {
153  ‪$width = $file->getProperty('width');
154  ‪$height = $file->getProperty('height');
155 
156  if (empty(‪$width) || empty(‪$height)) {
157  return ‪self::createEmpty();
158  }
159 
160  return new self(
161  $this->x / ‪$width,
162  $this->y / ‪$height,
163  $this->width / ‪$width,
164  $this->height / ‪$height
165  );
166  }
167 
172  public function ‪applyRatioRestriction(Ratio $ratio): Area
173  {
174  if ($ratio->isFree()) {
175  return $this;
176  }
177  $expectedRatio = $ratio->getRatioValue();
178  $newArea = clone $this;
179  if ($newArea->height * $expectedRatio > $newArea->width) {
180  $newArea->height = $newArea->width / $expectedRatio;
181  $newArea->y += ($this->height - $newArea->height) / 2;
182  } else {
183  $newArea->width = $newArea->height * $expectedRatio;
184  $newArea->x += ($this->width - $newArea->width) / 2;
185  }
186  return $newArea;
187  }
188 
192  public function ‪isEmpty()
193  {
194  return $this->x === 0.0 && $this->y === 0.0 && $this->width === 1.0 && $this->height === 1.0;
195  }
196 
200  public function ‪__toString()
201  {
202  if ($this->‪isEmpty()) {
203  return '';
204  }
205  return json_encode($this->‪asArray());
206  }
207 }
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area\createMultipleFromConfiguration
‪static Area[] createMultipleFromConfiguration(array $config)
Definition: Area.php:78
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area\getWidth
‪getWidth()
Definition: Area.php:95
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:22
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area\createFromConfiguration
‪static Area createFromConfiguration(array $config)
Definition: Area.php:59
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area\makeAbsoluteBasedOnFile
‪Area makeAbsoluteBasedOnFile(FileInterface $file)
Definition: Area.php:133
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area
Definition: Area.php:23
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area\getHeight
‪getHeight()
Definition: Area.php:100
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Ratio\isFree
‪bool isFree()
Definition: Ratio.php:93
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Ratio\getRatioValue
‪float getRatioValue()
Definition: Ratio.php:85
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Ratio
Definition: Ratio.php:21
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area\applyRatioRestriction
‪Area applyRatioRestriction(Ratio $ratio)
Definition: Area.php:168
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area\makeRelativeBasedOnFile
‪Area makeRelativeBasedOnFile(FileInterface $file)
Definition: Area.php:147
‪TYPO3\CMS\Core\Resource\FileInterface\getProperty
‪string getProperty($key)
‪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:188
‪TYPO3\CMS\Core\Imaging\ImageManipulation\InvalidConfigurationException
Definition: InvalidConfigurationException.php:24
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area\__toString
‪string __toString()
Definition: Area.php:196
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area\getOffsetTop
‪getOffsetTop()
Definition: Area.php:110
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area\getOffsetLeft
‪getOffsetLeft()
Definition: Area.php:105
‪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:46
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area\asArray
‪array asArray()
Definition: Area.php:119
‪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:90