-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathRectangle.php
More file actions
335 lines (291 loc) · 8.33 KB
/
Rectangle.php
File metadata and controls
335 lines (291 loc) · 8.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
<?php
declare(strict_types=1);
namespace Intervention\Image\Geometry;
use Intervention\Image\Exceptions\GeometryException;
use Intervention\Image\Geometry\Tools\RectangleResizer;
use Intervention\Image\Interfaces\PointInterface;
use Intervention\Image\Interfaces\SizeInterface;
class Rectangle extends Polygon implements SizeInterface
{
/**
* Create new rectangle instance
*
* @return void
*/
public function __construct(
int $width,
int $height,
protected PointInterface $pivot = new Point()
) {
$this->addPoint(new Point($this->pivot->x(), $this->pivot->y()));
$this->addPoint(new Point($this->pivot->x() + $width, $this->pivot->y()));
$this->addPoint(new Point($this->pivot->x() + $width, $this->pivot->y() - $height));
$this->addPoint(new Point($this->pivot->x(), $this->pivot->y() - $height));
}
/**
* Set size of rectangle
*/
public function setSize(int $width, int $height): self
{
return $this->setWidth($width)->setHeight($height);
}
/**
* Set width of rectangle
*/
public function setWidth(int $width): self
{
$this[1]->setX($this[0]->x() + $width);
$this[2]->setX($this[3]->x() + $width);
return $this;
}
/**
* Set height of rectangle
*/
public function setHeight(int $height): self
{
$this[2]->setY($this[1]->y() + $height);
$this[3]->setY($this[0]->y() + $height);
return $this;
}
/**
* Return pivot point of rectangle
*/
public function pivot(): PointInterface
{
return $this->pivot;
}
/**
* Set pivot point of rectangle
*/
public function setPivot(PointInterface $pivot): self
{
$this->pivot = $pivot;
return $this;
}
/**
* Move pivot to the given position in the rectangle and adjust the new
* position by given offset values.
*/
public function movePivot(string $position, int $offset_x = 0, int $offset_y = 0): self
{
switch (strtolower($position)) {
case 'top':
case 'top-center':
case 'top-middle':
case 'center-top':
case 'middle-top':
$x = intval(round($this->width() / 2)) + $offset_x;
$y = $offset_y;
break;
case 'top-right':
case 'right-top':
$x = $this->width() - $offset_x;
$y = $offset_y;
break;
case 'left':
case 'left-center':
case 'left-middle':
case 'center-left':
case 'middle-left':
$x = $offset_x;
$y = intval(round($this->height() / 2)) + $offset_y;
break;
case 'right':
case 'right-center':
case 'right-middle':
case 'center-right':
case 'middle-right':
$x = $this->width() - $offset_x;
$y = intval(round($this->height() / 2)) + $offset_y;
break;
case 'bottom-left':
case 'left-bottom':
$x = $offset_x;
$y = $this->height() - $offset_y;
break;
case 'bottom':
case 'bottom-center':
case 'bottom-middle':
case 'center-bottom':
case 'middle-bottom':
$x = intval(round($this->width() / 2)) + $offset_x;
$y = $this->height() - $offset_y;
break;
case 'bottom-right':
case 'right-bottom':
$x = $this->width() - $offset_x;
$y = $this->height() - $offset_y;
break;
case 'center':
case 'middle':
case 'center-center':
case 'middle-middle':
$x = intval(round($this->width() / 2)) + $offset_x;
$y = intval(round($this->height() / 2)) + $offset_y;
break;
default:
case 'top-left':
case 'left-top':
$x = $offset_x;
$y = $offset_y;
break;
}
$this->pivot->setPosition($x, $y);
return $this;
}
/**
* Align pivot relative to given size at given position
*/
public function alignPivotTo(SizeInterface $size, string $position): self
{
$reference = new self($size->width(), $size->height());
$reference->movePivot($position);
$this->movePivot($position)->setPivot(
$reference->relativePositionTo($this)
);
return $this;
}
/**
* Return relative position to given rectangle
*/
public function relativePositionTo(SizeInterface $rectangle): PointInterface
{
return new Point(
$this->pivot()->x() - $rectangle->pivot()->x(),
$this->pivot()->y() - $rectangle->pivot()->y()
);
}
/**
* Return aspect ration of rectangle
*/
public function aspectRatio(): float
{
return $this->width() / $this->height();
}
/**
* Determine if rectangle fits into given rectangle
*/
public function fitsInto(SizeInterface $size): bool
{
if ($this->width() > $size->width()) {
return false;
}
if ($this->height() > $size->height()) {
return false;
}
return true;
}
/**
* Determine if rectangle has landscape format
*/
public function isLandscape(): bool
{
return $this->width() > $this->height();
}
/**
* Determine if rectangle has landscape format
*/
public function isPortrait(): bool
{
return $this->width() < $this->height();
}
/**
* Return most top left point of rectangle
*/
public function topLeftPoint(): PointInterface
{
return $this->points[0];
}
/**
* Return bottom right point of rectangle
*/
public function bottomRightPoint(): PointInterface
{
return $this->points[2];
}
/**
* @see SizeInterface::resize()
*
* @throws GeometryException
*/
public function resize(?int $width = null, ?int $height = null): SizeInterface
{
return $this->resizer($width, $height)->resize($this);
}
/**
* @see SizeInterface::resizeDown()
*
* @throws GeometryException
*/
public function resizeDown(?int $width = null, ?int $height = null): SizeInterface
{
return $this->resizer($width, $height)->resizeDown($this);
}
/**
* @see SizeInterface::scale()
*
* @throws GeometryException
*/
public function scale(?int $width = null, ?int $height = null): SizeInterface
{
return $this->resizer($width, $height)->scale($this);
}
/**
* @see SizeInterface::scaleDown()
*
* @throws GeometryException
*/
public function scaleDown(?int $width = null, ?int $height = null): SizeInterface
{
return $this->resizer($width, $height)->scaleDown($this);
}
/**
* @see SizeInterface::cover()
*
* @throws GeometryException
*/
public function cover(int $width, int $height): SizeInterface
{
return $this->resizer($width, $height)->cover($this);
}
/**
* @see SizeInterface::contain()
*
* @throws GeometryException
*/
public function contain(int $width, int $height): SizeInterface
{
return $this->resizer($width, $height)->contain($this);
}
/**
* @see SizeInterface::containMax()
*
* @throws GeometryException
*/
public function containMax(int $width, int $height): SizeInterface
{
return $this->resizer($width, $height)->containDown($this);
}
/**
* Create resizer instance with given target size
*
* @throws GeometryException
*/
protected function resizer(?int $width = null, ?int $height = null): RectangleResizer
{
return new RectangleResizer($width, $height);
}
/**
* Show debug info for the current rectangle
*
* @return array<string, int|object>
*/
public function __debugInfo(): array
{
return [
'width' => $this->width(),
'height' => $this->height(),
'pivot' => $this->pivot,
];
}
}