optimole-wp
/
vendor
/
codeinwp
/
optimole-sdk
/
src
/
Resource
/
ImageProperty
/
WatermarkProperty.php
WatermarkProperty.php in Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization 4.2.11, at vendor/codeinwp/optimole-sdk/src/Resource/ImageProperty/WatermarkProperty.php
| 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 WatermarkProperty implements PropertyInterface |
| 21 | { |
| 22 | /** |
| 23 | * The watermark ID in Optimole. |
| 24 | */ |
| 25 | private int $id; |
| 26 | |
| 27 | /** |
| 28 | * The horizontal offset of the watermark. |
| 29 | */ |
| 30 | private float $offsetX; |
| 31 | |
| 32 | /** |
| 33 | * The vertical offset of the watermark. |
| 34 | */ |
| 35 | private float $offsetY; |
| 36 | |
| 37 | /** |
| 38 | * The opacity of the watermark. |
| 39 | */ |
| 40 | private float $opacity; |
| 41 | |
| 42 | /** |
| 43 | * The position of the watermark. |
| 44 | */ |
| 45 | private Position $position; |
| 46 | |
| 47 | /** |
| 48 | * Defines the watermark size relative to the resultant image size. |
| 49 | */ |
| 50 | private float $scale; |
| 51 | |
| 52 | /** |
| 53 | * Constructor. |
| 54 | */ |
| 55 | public function __construct(int $id, float $opacity, $position, float $offsetX = 0, float $offsetY = 0, float $scale = 0) |
| 56 | { |
| 57 | if (!is_string($position) && !$position instanceof Position) { |
| 58 | throw new InvalidArgumentException('Watermark position must be a string or an instance of Position.'); |
| 59 | } elseif (is_string($position)) { |
| 60 | $position = new Position($position); |
| 61 | } |
| 62 | |
| 63 | $this->id = $id; |
| 64 | $this->offsetX = $offsetX; |
| 65 | $this->offsetY = $offsetY; |
| 66 | $this->opacity = $opacity; |
| 67 | $this->position = $position; |
| 68 | $this->scale = $scale; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * {@inheritdoc} |
| 73 | */ |
| 74 | public function __toString(): string |
| 75 | { |
| 76 | $watermark = sprintf('wm:%s:%s:%s', $this->id, $this->opacity, $this->position); |
| 77 | |
| 78 | if (empty($this->offsetX)) { |
| 79 | return $watermark; |
| 80 | } |
| 81 | |
| 82 | $watermark .= sprintf(':%s', $this->offsetX); |
| 83 | |
| 84 | if (empty($this->offsetY)) { |
| 85 | return $watermark; |
| 86 | } |
| 87 | |
| 88 | $watermark .= sprintf(':%s', $this->offsetY); |
| 89 | |
| 90 | if (empty($this->scale)) { |
| 91 | return $watermark; |
| 92 | } |
| 93 | |
| 94 | $watermark .= sprintf(':%s', $this->scale); |
| 95 | |
| 96 | return $watermark; |
| 97 | } |
| 98 | } |
| 99 |