1 | <?php |
||
22 | class Watermark extends BaseManipulator |
||
23 | { |
||
24 | /** |
||
25 | * The watermarks file system. |
||
26 | * @var FilesystemInterface|null |
||
27 | */ |
||
28 | protected $watermarks; |
||
29 | |||
30 | /** |
||
31 | * The watermarks path prefix. |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $watermarksPathPrefix; |
||
35 | |||
36 | /** |
||
37 | * Create Watermark instance. |
||
38 | * @param FilesystemInterface $watermarks The watermarks file system. |
||
39 | */ |
||
40 | 40 | public function __construct(FilesystemInterface $watermarks = null, $watermarksPathPrefix = '') |
|
45 | |||
46 | /** |
||
47 | * Set the watermarks file system. |
||
48 | * @param FilesystemInterface $watermarks The watermarks file system. |
||
49 | */ |
||
50 | 40 | public function setWatermarks(FilesystemInterface $watermarks = null) |
|
54 | |||
55 | /** |
||
56 | * Get the watermarks file system. |
||
57 | * @return FilesystemInterface The watermarks file system. |
||
58 | */ |
||
59 | 8 | public function getWatermarks() |
|
63 | |||
64 | /** |
||
65 | * Set the watermarks path prefix. |
||
66 | * @param string $watermarksPathPrefix The watermarks path prefix. |
||
67 | */ |
||
68 | 40 | public function setWatermarksPathPrefix($watermarksPathPrefix = '') |
|
72 | |||
73 | /** |
||
74 | * Get the watermarks path prefix. |
||
75 | * @return string The watermarks path prefix. |
||
76 | */ |
||
77 | 4 | public function getWatermarksPathPrefix() |
|
81 | |||
82 | /** |
||
83 | * Perform watermark image manipulation. |
||
84 | * @param Image $image The source image. |
||
85 | * @return Image The manipulated image. |
||
86 | */ |
||
87 | 2 | public function run(Image $image) |
|
88 | { |
||
89 | 2 | if ($watermark = $this->getImage($image)) { |
|
90 | 2 | $markw = $this->getDimension($image, 'markw'); |
|
91 | 2 | $markh = $this->getDimension($image, 'markh'); |
|
92 | 2 | $markx = $this->getDimension($image, 'markx'); |
|
93 | 2 | $marky = $this->getDimension($image, 'marky'); |
|
94 | 2 | $markpad = $this->getDimension($image, 'markpad'); |
|
95 | 2 | $markfit = $this->getFit(); |
|
96 | 2 | $markpos = $this->getPosition(); |
|
97 | 2 | $markalpha = $this->getAlpha(); |
|
98 | |||
99 | 2 | if ($markpad) { |
|
100 | 2 | $markx = $marky = $markpad; |
|
101 | 2 | } |
|
102 | |||
103 | 2 | $size = new Size(); |
|
104 | 2 | $size->setParams([ |
|
105 | 2 | 'w' => $markw, |
|
106 | 2 | 'h' => $markh, |
|
107 | 2 | 'fit' => $markfit, |
|
108 | 2 | ]); |
|
109 | 2 | $watermark = $size->run($watermark); |
|
110 | |||
111 | 2 | if ($markalpha < 100) { |
|
112 | $watermark->opacity($markalpha); |
||
113 | } |
||
114 | |||
115 | 2 | $image->insert($watermark, $markpos, intval($markx), intval($marky)); |
|
116 | 2 | } |
|
117 | |||
118 | 2 | return $image; |
|
119 | } |
||
120 | |||
121 | /** |
||
122 | * Get the watermark image. |
||
123 | * @param Image $image The source image. |
||
124 | * @return Image|null The watermark image. |
||
125 | */ |
||
126 | 12 | public function getImage(Image $image) |
|
158 | |||
159 | /** |
||
160 | * Get a dimension. |
||
161 | * @param Image $image The source image. |
||
162 | * @param string $field The requested field. |
||
163 | * @return double|null The dimension. |
||
164 | */ |
||
165 | 4 | public function getDimension(Image $image, $field) |
|
171 | |||
172 | /** |
||
173 | * Resolve the device pixel ratio. |
||
174 | * @return double The device pixel ratio. |
||
175 | */ |
||
176 | 6 | public function getDpr() |
|
188 | |||
189 | /** |
||
190 | * Get the fit. |
||
191 | * @return string The fit. |
||
192 | */ |
||
193 | 4 | public function getFit() |
|
215 | |||
216 | /** |
||
217 | * Get the position. |
||
218 | * @return string The position. |
||
219 | */ |
||
220 | 4 | public function getPosition() |
|
240 | |||
241 | /** |
||
242 | * Get the alpha channel. |
||
243 | * @return int The alpha. |
||
244 | */ |
||
245 | 4 | public function getAlpha() |
|
257 | } |
||
258 |