PluginProbe
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell / 3.13.1
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell v3.13.1
3.13.1 3.13.0 3.12.13 3.12.12 3.12.11 3.12.10 3.12.9 3.12.8 3.12.7 3.12.6 3.12.5 3.12.4 3.12.3 3.12.1 3.12.2 3.12.0 3.11.1 3.11.0 3.10.9 3.10.8 3.10.7 3.10.6 2.8.16 2.8.17 2.8.18 All 259 releases
wpfunnels / vendor / intervention / image / src / Intervention / Image / Size.php

Size.php in WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell 3.13.1, at vendor/intervention/image/src/Intervention/Image/Size.php

375 lines 9.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Intervention\Image;
4
5 use Closure;
6 use Intervention\Image\Exception\InvalidArgumentException;
7
8 class Size
9 {
10 /**
11 * Width
12 *
13 * @var int
14 */
15 public $width;
16
17 /**
18 * Height
19 *
20 * @var int
21 */
22 public $height;
23
24 /**
25 * Pivot point
26 *
27 * @var Point
28 */
29 public $pivot;
30
31 /**
32 * Creates a new Size instance
33 *
34 * @param int $width
35 * @param int $height
36 * @param Point $pivot
37 */
38 public function __construct($width = null, $height = null, Point $pivot = null)
39 {
40 $this->width = is_numeric($width) ? intval($width) : 1;
41 $this->height = is_numeric($height) ? intval($height) : 1;
42 $this->pivot = $pivot ? $pivot : new Point;
43 }
44
45 /**
46 * Set the width and height absolutely
47 *
48 * @param int $width
49 * @param int $height
50 */
51 public function set($width, $height)
52 {
53 $this->width = $width;
54 $this->height = $height;
55 }
56
57 /**
58 * Set current pivot point
59 *
60 * @param Point $point
61 */
62 public function setPivot(Point $point)
63 {
64 $this->pivot = $point;
65 }
66
67 /**
68 * Get the current width
69 *
70 * @return int
71 */
72 public function getWidth()
73 {
74 return intval($this->width);
75 }
76
77 /**
78 * Get the current height
79 *
80 * @return int
81 */
82 public function getHeight()
83 {
84 return intval($this->height);
85 }
86
87 /**
88 * Calculate the current aspect ratio
89 *
90 * @return float
91 */
92 public function getRatio()
93 {
94 return $this->width / $this->height;
95 }
96
97 /**
98 * Resize to desired width and/or height
99 *
100 * @param int $width
101 * @param int $height
102 * @param Closure $callback
103 * @return Size
104 */
105 public function resize($width, $height, Closure $callback = null)
106 {
107 if (is_null($width) && is_null($height)) {
108 throw new InvalidArgumentException(
109 "Width or height needs to be defined."
110 );
111 }
112
113 // new size with dominant width
114 $dominant_w_size = clone $this;
115 $dominant_w_size->resizeHeight($height, $callback);
116 $dominant_w_size->resizeWidth($width, $callback);
117
118 // new size with dominant height
119 $dominant_h_size = clone $this;
120 $dominant_h_size->resizeWidth($width, $callback);
121 $dominant_h_size->resizeHeight($height, $callback);
122
123 // decide which size to use
124 if ($dominant_h_size->fitsInto(new self($width, $height))) {
125 $this->set($dominant_h_size->width, $dominant_h_size->height);
126 } else {
127 $this->set($dominant_w_size->width, $dominant_w_size->height);
128 }
129
130 return $this;
131 }
132
133 /**
134 * Scale size according to given constraints
135 *
136 * @param int $width
137 * @param Closure $callback
138 * @return Size
139 */
140 private function resizeWidth($width, Closure $callback = null)
141 {
142 $constraint = $this->getConstraint($callback);
143
144 if ($constraint->isFixed(Constraint::UPSIZE)) {
145 $max_width = $constraint->getSize()->getWidth();
146 $max_height = $constraint->getSize()->getHeight();
147 }
148
149 if (is_numeric($width)) {
150
151 if ($constraint->isFixed(Constraint::UPSIZE)) {
152 $this->width = ($width > $max_width) ? $max_width : $width;
153 } else {
154 $this->width = $width;
155 }
156
157 if ($constraint->isFixed(Constraint::ASPECTRATIO)) {
158 $h = max(1, intval(round($this->width / $constraint->getSize()->getRatio())));
159
160 if ($constraint->isFixed(Constraint::UPSIZE)) {
161 $this->height = ($h > $max_height) ? $max_height : $h;
162 } else {
163 $this->height = $h;
164 }
165 }
166 }
167 }
168
169 /**
170 * Scale size according to given constraints
171 *
172 * @param int $height
173 * @param Closure $callback
174 * @return Size
175 */
176 private function resizeHeight($height, Closure $callback = null)
177 {
178 $constraint = $this->getConstraint($callback);
179
180 if ($constraint->isFixed(Constraint::UPSIZE)) {
181 $max_width = $constraint->getSize()->getWidth();
182 $max_height = $constraint->getSize()->getHeight();
183 }
184
185 if (is_numeric($height)) {
186
187 if ($constraint->isFixed(Constraint::UPSIZE)) {
188 $this->height = ($height > $max_height) ? $max_height : $height;
189 } else {
190 $this->height = $height;
191 }
192
193 if ($constraint->isFixed(Constraint::ASPECTRATIO)) {
194 $w = max(1, intval(round($this->height * $constraint->getSize()->getRatio())));
195
196 if ($constraint->isFixed(Constraint::UPSIZE)) {
197 $this->width = ($w > $max_width) ? $max_width : $w;
198 } else {
199 $this->width = $w;
200 }
201 }
202 }
203 }
204
205 /**
206 * Calculate the relative position to another Size
207 * based on the pivot point settings of both sizes.
208 *
209 * @param Size $size
210 * @return \Intervention\Image\Point
211 */
212 public function relativePosition(Size $size)
213 {
214 $x = $this->pivot->x - $size->pivot->x;
215 $y = $this->pivot->y - $size->pivot->y;
216
217 return new Point($x, $y);
218 }
219
220 /**
221 * Resize given Size to best fitting size of current size.
222 *
223 * @param Size $size
224 * @return \Intervention\Image\Size
225 */
226 public function fit(Size $size, $position = 'center')
227 {
228 // create size with auto height
229 $auto_height = clone $size;
230
231 $auto_height->resize($this->width, null, function ($constraint) {
232 $constraint->aspectRatio();
233 });
234
235 // decide which version to use
236 if ($auto_height->fitsInto($this)) {
237
238 $size = $auto_height;
239
240 } else {
241
242 // create size with auto width
243 $auto_width = clone $size;
244
245 $auto_width->resize(null, $this->height, function ($constraint) {
246 $constraint->aspectRatio();
247 });
248
249 $size = $auto_width;
250 }
251
252 $this->align($position);
253 $size->align($position);
254 $size->setPivot($this->relativePosition($size));
255
256 return $size;
257 }
258
259 /**
260 * Checks if given size fits into current size
261 *
262 * @param Size $size
263 * @return boolean
264 */
265 public function fitsInto(Size $size)
266 {
267 return ($this->width <= $size->width) && ($this->height <= $size->height);
268 }
269
270 /**
271 * Aligns current size's pivot point to given position
272 * and moves point automatically by offset.
273 *
274 * @param string $position
275 * @param int $offset_x
276 * @param int $offset_y
277 * @return \Intervention\Image\Size
278 */
279 public function align($position, $offset_x = 0, $offset_y = 0)
280 {
281 switch (strtolower($position)) {
282
283 case 'top':
284 case 'top-center':
285 case 'top-middle':
286 case 'center-top':
287 case 'middle-top':
288 $x = intval($this->width / 2);
289 $y = 0 + $offset_y;
290 break;
291
292 case 'top-right':
293 case 'right-top':
294 $x = $this->width - $offset_x;
295 $y = 0 + $offset_y;
296 break;
297
298 case 'left':
299 case 'left-center':
300 case 'left-middle':
301 case 'center-left':
302 case 'middle-left':
303 $x = 0 + $offset_x;
304 $y = intval($this->height / 2);
305 break;
306
307 case 'right':
308 case 'right-center':
309 case 'right-middle':
310 case 'center-right':
311 case 'middle-right':
312 $x = $this->width - $offset_x;
313 $y = intval($this->height / 2);
314 break;
315
316 case 'bottom-left':
317 case 'left-bottom':
318 $x = 0 + $offset_x;
319 $y = $this->height - $offset_y;
320 break;
321
322 case 'bottom':
323 case 'bottom-center':
324 case 'bottom-middle':
325 case 'center-bottom':
326 case 'middle-bottom':
327 $x = intval($this->width / 2);
328 $y = $this->height - $offset_y;
329 break;
330
331 case 'bottom-right':
332 case 'right-bottom':
333 $x = $this->width - $offset_x;
334 $y = $this->height - $offset_y;
335 break;
336
337 case 'center':
338 case 'middle':
339 case 'center-center':
340 case 'middle-middle':
341 $x = intval($this->width / 2) + $offset_x;
342 $y = intval($this->height / 2) + $offset_y;
343 break;
344
345 default:
346 case 'top-left':
347 case 'left-top':
348 $x = 0 + $offset_x;
349 $y = 0 + $offset_y;
350 break;
351 }
352
353 $this->pivot->setPosition($x, $y);
354
355 return $this;
356 }
357
358 /**
359 * Runs constraints on current size
360 *
361 * @param Closure $callback
362 * @return \Intervention\Image\Constraint
363 */
364 private function getConstraint(Closure $callback = null)
365 {
366 $constraint = new Constraint(clone $this);
367
368 if (is_callable($callback)) {
369 $callback($constraint);
370 }
371
372 return $constraint;
373 }
374 }
375