‪TYPO3CMS  9.5
Area.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 
20 class ‪Area
21 {
25  protected ‪$x;
26 
30  protected ‪$y;
31 
35  protected ‪$width;
36 
40  protected ‪$height;
41 
48  public function ‪__construct(float ‪$x, float ‪$y, float ‪$width, float ‪$height)
49  {
50  $this->x = ‪$x;
51  $this->y = ‪$y;
52  $this->width = ‪$width;
53  $this->height = ‪$height;
54  }
55 
61  public static function ‪createFromConfiguration(array $config): ‪Area
62  {
63  try {
64  return new self(
65  (float)$config['x'],
66  (float)$config['y'],
67  (float)$config['width'],
68  (float)$config['height']
69  );
70  } catch (\Throwable $throwable) {
71  throw new InvalidConfigurationException(sprintf('Invalid type for area property given: %s', $throwable->getMessage()), 1485279226, $throwable);
72  }
73  }
74 
80  public static function ‪createMultipleFromConfiguration(array $config): array
81  {
82  $areas = [];
83  foreach ($config as $areaConfig) {
84  $areas[] = ‪self::createFromConfiguration($areaConfig);
85  }
86  return $areas;
87  }
88 
92  public static function ‪createEmpty()
93  {
94  return new self(0.0, 0.0, 1.0, 1.0);
95  }
96 
101  public function ‪asArray(): array
102  {
103  return [
104  'x' => ‪$this->x,
105  'y' => ‪$this->y,
106  'width' => ‪$this->width,
107  'height' => ‪$this->height,
108  ];
109  }
110 
115  public function ‪makeAbsoluteBasedOnFile(FileInterface $file)
116  {
117  return new self(
118  $this->x * $file->getProperty('width'),
119  $this->y * $file->getProperty('height'),
120  $this->width * $file->getProperty('width'),
121  $this->height * $file->getProperty('height')
122  );
123  }
124 
129  public function ‪makeRelativeBasedOnFile(FileInterface $file)
130  {
131  ‪$width = $file->getProperty('width');
132  ‪$height = $file->getProperty('height');
133 
134  if (empty(‪$width) || empty(‪$height)) {
135  return ‪self::createEmpty();
136  }
137 
138  return new self(
139  $this->x / ‪$width,
140  $this->y / ‪$height,
141  $this->width / ‪$width,
142  $this->height / ‪$height
143  );
144  }
145 
150  public function ‪applyRatioRestriction(Ratio $ratio): Area
151  {
152  if ($ratio->isFree()) {
153  return $this;
154  }
155  $expectedRatio = $ratio->getRatioValue();
156  $newArea = clone $this;
157  if ($newArea->height * $expectedRatio > $newArea->width) {
158  $newArea->height = $newArea->width / $expectedRatio;
159  $newArea->y += ($this->height - $newArea->height) / 2;
160  } else {
161  $newArea->width = $newArea->height * $expectedRatio;
162  $newArea->x += ($this->width - $newArea->width) / 2;
163  }
164  return $newArea;
165  }
166 
170  public function ‪isEmpty()
171  {
172  return $this->x === 0.0 && $this->y === 0.0 && $this->width === 1.0 && $this->height === 1.0;
173  }
174 
178  public function ‪__toString()
179  {
180  if ($this->‪isEmpty()) {
181  return '';
182  }
183  return json_encode($this->‪asArray());
184  }
185 }
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area\createMultipleFromConfiguration
‪static Area[] createMultipleFromConfiguration(array $config)
Definition: Area.php:76
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:21
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area\createFromConfiguration
‪static Area createFromConfiguration(array $config)
Definition: Area.php:57
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area\makeAbsoluteBasedOnFile
‪Area makeAbsoluteBasedOnFile(FileInterface $file)
Definition: Area.php:111
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area
Definition: Area.php:21
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Ratio\isFree
‪bool isFree()
Definition: Ratio.php:91
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Ratio\getRatioValue
‪float getRatioValue()
Definition: Ratio.php:83
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Ratio
Definition: Ratio.php:19
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area\applyRatioRestriction
‪Area applyRatioRestriction(Ratio $ratio)
Definition: Area.php:146
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area\makeRelativeBasedOnFile
‪Area makeRelativeBasedOnFile(FileInterface $file)
Definition: Area.php:125
‪TYPO3\CMS\Core\Resource\FileInterface\getProperty
‪string getProperty($key)
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area\$x
‪float $x
Definition: Area.php:24
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area\$width
‪float $width
Definition: Area.php:32
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area\$height
‪float $height
Definition: Area.php:36
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area\isEmpty
‪bool isEmpty()
Definition: Area.php:166
‪TYPO3\CMS\Core\Imaging\ImageManipulation\InvalidConfigurationException
Definition: InvalidConfigurationException.php:22
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area\__toString
‪string __toString()
Definition: Area.php:174
‪TYPO3\CMS\Core\Imaging\ImageManipulation
Definition: Area.php:3
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area\__construct
‪__construct(float $x, float $y, float $width, float $height)
Definition: Area.php:44
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area\asArray
‪array asArray()
Definition: Area.php:97
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area\$y
‪float $y
Definition: Area.php:28
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area\createEmpty
‪static Area createEmpty()
Definition: Area.php:88