PluginProbe
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization / 4.2.11
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization v4.2.11
4.2.13 4.2.12 4.2.11 4.2.10 4.2.9 4.2.8 4.2.7 4.2.6 4.2.5 2.5.5 2.5.6 2.5.7 3.0.0 3.0.1 3.1.0 3.1.1 3.1.2 3.1.3 3.10.0 3.11.0 3.11.1 3.11.2 3.11.3 3.12.0 3.12.1 All 134 releases
optimole-wp / vendor / codeinwp / optimole-sdk / src / Resource / ImageProperty / GravityProperty.php

GravityProperty.php in Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization 4.2.11, at vendor/codeinwp/optimole-sdk/src/Resource/ImageProperty/GravityProperty.php

81 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 /*
6 * This file is part of Optimole PHP SDK.
7 *
8 * (c) Optimole Team <friends@optimole.com>
9 *
10 * For the full copyright and license information, please view the LICENSE
11 * file that was distributed with this source code.
12 */
13
14 namespace Optimole\Sdk\Resource\ImageProperty;
15
16 use Optimole\Sdk\Exception\InvalidArgumentException;
17 use Optimole\Sdk\Resource\PropertyInterface;
18 use Optimole\Sdk\ValueObject\Position;
19
20 class GravityProperty implements PropertyInterface
21 {
22 /**
23 * Detects the most "interesting" section of the image and considers it as the center of the resulting image.
24 */
25 public const SMART = 'sm';
26
27 /**
28 * Sets the gravity to the focus point to a specific coordinate.
29 */
30 private const FOCUS_POINT = 'fp';
31
32 /**
33 * When using the focus point gravity, defines the coordinates of the resulting image on the X axis.
34 */
35 private float $focusPointX;
36
37 /**
38 * When using the focus point gravity, defines the coordinates of the resulting image on the Y axis.
39 */
40 private float $focusPointY;
41
42 /**
43 * The image gravity type.
44 */
45 private string $gravity;
46
47 /**
48 * Constructor.
49 */
50 public function __construct($gravity)
51 {
52 if (!is_string($gravity) && !is_array($gravity) && !$gravity instanceof Position) {
53 throw new InvalidArgumentException('Image gravity must be a string, an array or an instance of Position.');
54 } elseif (is_array($gravity) && !isset($gravity[0], $gravity[1])) {
55 throw new InvalidArgumentException('Focus point image gravity must be an array with two elements.');
56 }
57
58 if (is_string($gravity) && self::SMART !== $gravity) {
59 $gravity = new Position($gravity);
60 }
61
62 $this->focusPointX = is_array($gravity) ? (float) $gravity[0] : 0;
63 $this->focusPointY = is_array($gravity) ? (float) $gravity[1] : 0;
64 $this->gravity = is_array($gravity) ? self::FOCUS_POINT : (string) $gravity;
65 }
66
67 /**
68 * {@inheritdoc}
69 */
70 public function __toString(): string
71 {
72 $gravity = sprintf('g:%s', $this->gravity);
73
74 if (self::FOCUS_POINT === $this->gravity) {
75 $gravity .= sprintf(':%s:%s', $this->focusPointX, $this->focusPointY);
76 }
77
78 return $gravity;
79 }
80 }
81