1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace League\Glide\Manipulators; |
4
|
|
|
|
5
|
|
|
use Intervention\Image\Image; |
6
|
|
|
use League\Flysystem\FilesystemInterface; |
7
|
|
|
use League\Glide\Manipulators\Helpers\Dimension; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @property string $dpr |
11
|
|
|
* @property string $mark |
12
|
|
|
* @property string $markfit |
13
|
|
|
* @property string $markh |
14
|
|
|
* @property string $markpad |
15
|
|
|
* @property string $markpos |
16
|
|
|
* @property string $markw |
17
|
|
|
* @property string $markx |
18
|
|
|
* @property string $marky |
19
|
|
|
*/ |
20
|
|
|
class Watermark extends BaseManipulator |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* The watermarks file system. |
24
|
|
|
* @var FilesystemInterface|null |
25
|
|
|
*/ |
26
|
|
|
protected $watermarks; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The watermarks path prefix. |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $watermarksPathPrefix; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Create Watermark instance. |
36
|
|
|
* @param FilesystemInterface $watermarks The watermarks file system. |
37
|
|
|
*/ |
38
|
42 |
|
public function __construct(FilesystemInterface $watermarks = null, $watermarksPathPrefix = '') |
39
|
|
|
{ |
40
|
42 |
|
$this->setWatermarks($watermarks); |
41
|
42 |
|
$this->setWatermarksPathPrefix($watermarksPathPrefix); |
42
|
42 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Set the watermarks file system. |
46
|
|
|
* @param FilesystemInterface $watermarks The watermarks file system. |
47
|
|
|
*/ |
48
|
42 |
|
public function setWatermarks(FilesystemInterface $watermarks = null) |
49
|
|
|
{ |
50
|
42 |
|
$this->watermarks = $watermarks; |
51
|
42 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get the watermarks file system. |
55
|
|
|
* @return FilesystemInterface The watermarks file system. |
56
|
|
|
*/ |
57
|
9 |
|
public function getWatermarks() |
58
|
|
|
{ |
59
|
9 |
|
return $this->watermarks; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Set the watermarks path prefix. |
64
|
|
|
* @param string $watermarksPathPrefix The watermarks path prefix. |
65
|
|
|
*/ |
66
|
42 |
|
public function setWatermarksPathPrefix($watermarksPathPrefix = '') |
67
|
|
|
{ |
68
|
42 |
|
$this->watermarksPathPrefix = trim($watermarksPathPrefix, '/'); |
69
|
42 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get the watermarks path prefix. |
73
|
|
|
* @return string The watermarks path prefix. |
74
|
|
|
*/ |
75
|
6 |
|
public function getWatermarksPathPrefix() |
76
|
|
|
{ |
77
|
6 |
|
return $this->watermarksPathPrefix; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Perform watermark image manipulation. |
82
|
|
|
* @param Image $image The source image. |
83
|
|
|
* @return Image The manipulated image. |
84
|
|
|
*/ |
85
|
3 |
|
public function run(Image $image) |
86
|
|
|
{ |
87
|
3 |
|
if ($watermark = $this->getImage($image)) { |
88
|
3 |
|
$markw = $this->getDimension($image, 'markw'); |
89
|
3 |
|
$markh = $this->getDimension($image, 'markh'); |
90
|
3 |
|
$markx = $this->getDimension($image, 'markx'); |
91
|
3 |
|
$marky = $this->getDimension($image, 'marky'); |
92
|
3 |
|
$markpad = $this->getDimension($image, 'markpad'); |
93
|
3 |
|
$markfit = $this->getFit(); |
94
|
3 |
|
$markpos = $this->getPosition(); |
95
|
|
|
|
96
|
3 |
|
if ($markpad) { |
97
|
3 |
|
$markx = $marky = $markpad; |
98
|
2 |
|
} |
99
|
|
|
|
100
|
3 |
|
$size = new Size(); |
101
|
3 |
|
$size->setParams([ |
102
|
3 |
|
'w' => $markw, |
103
|
3 |
|
'h' => $markh, |
104
|
3 |
|
'fit' => $markfit, |
105
|
2 |
|
]); |
106
|
3 |
|
$watermark = $size->run($watermark); |
107
|
|
|
|
108
|
3 |
|
$image->insert($watermark, $markpos, intval($markx), intval($marky)); |
109
|
2 |
|
} |
110
|
|
|
|
111
|
3 |
|
return $image; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Get the watermark image. |
116
|
|
|
* @param Image $image The source image. |
117
|
|
|
* @return Image|null The watermark image. |
118
|
|
|
*/ |
119
|
6 |
|
public function getImage(Image $image) |
120
|
|
|
{ |
121
|
6 |
|
if (is_null($this->watermarks)) { |
122
|
|
|
return; |
123
|
|
|
} |
124
|
|
|
|
125
|
6 |
|
if (!is_string($this->mark)) { |
126
|
|
|
return; |
127
|
|
|
} |
128
|
|
|
|
129
|
6 |
|
if ($this->mark === '') { |
130
|
|
|
return; |
131
|
|
|
} |
132
|
|
|
|
133
|
6 |
|
$path = $this->mark; |
134
|
|
|
|
135
|
6 |
|
if ($this->watermarksPathPrefix) { |
136
|
|
|
$path = $this->watermarksPathPrefix.'/'.$path; |
137
|
|
|
} |
138
|
|
|
|
139
|
6 |
|
if ($this->watermarks->has($path)) { |
140
|
6 |
|
$source = $this->watermarks->read($path); |
141
|
|
|
|
142
|
6 |
|
if ($source === false) { |
143
|
|
|
throw new FilesystemException( |
144
|
|
|
'Could not read the image `'.$path.'`.' |
145
|
|
|
); |
146
|
|
|
} |
147
|
|
|
|
148
|
6 |
|
return $image->getDriver()->init($source); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Get a dimension. |
154
|
|
|
* @param Image $image The source image. |
155
|
|
|
* @param string $field The requested field. |
156
|
|
|
* @return double|null The dimension. |
157
|
|
|
*/ |
158
|
6 |
|
public function getDimension(Image $image, $field) |
159
|
|
|
{ |
160
|
6 |
|
if ($this->{$field}) { |
161
|
6 |
|
return (new Dimension($image, $this->getDpr()))->get($this->{$field}); |
162
|
|
|
} |
163
|
6 |
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Resolve the device pixel ratio. |
167
|
|
|
* @return double The device pixel ratio. |
168
|
|
|
*/ |
169
|
6 |
|
public function getDpr() |
170
|
|
|
{ |
171
|
6 |
|
if (!is_numeric($this->dpr)) { |
172
|
6 |
|
return 1.0; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
if ($this->dpr < 0 or $this->dpr > 8) { |
176
|
|
|
return 1.0; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
return (double) $this->dpr; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Get the fit. |
184
|
|
|
* @return string The fit. |
185
|
|
|
*/ |
186
|
6 |
|
public function getFit() |
187
|
|
|
{ |
188
|
|
|
$fitMethods = [ |
189
|
6 |
|
'contain', |
190
|
4 |
|
'max', |
191
|
4 |
|
'stretch', |
192
|
4 |
|
'crop', |
193
|
4 |
|
'crop-top-left', |
194
|
4 |
|
'crop-top', |
195
|
4 |
|
'crop-top-right', |
196
|
4 |
|
'crop-left', |
197
|
4 |
|
'crop-center', |
198
|
4 |
|
'crop-right', |
199
|
4 |
|
'crop-bottom-left', |
200
|
4 |
|
'crop-bottom', |
201
|
4 |
|
'crop-bottom-right', |
202
|
4 |
|
]; |
203
|
|
|
|
204
|
6 |
|
if (in_array($this->markfit, $fitMethods, true)) { |
205
|
3 |
|
return $this->markfit; |
206
|
|
|
} |
207
|
6 |
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Get the position. |
211
|
|
|
* @return string The position. |
212
|
|
|
*/ |
213
|
6 |
|
public function getPosition() |
214
|
|
|
{ |
215
|
|
|
$positions = [ |
216
|
6 |
|
'top-left', |
217
|
4 |
|
'top', |
218
|
4 |
|
'top-right', |
219
|
4 |
|
'left', |
220
|
4 |
|
'center', |
221
|
4 |
|
'right', |
222
|
4 |
|
'bottom-left', |
223
|
4 |
|
'bottom', |
224
|
4 |
|
'bottom-right', |
225
|
4 |
|
]; |
226
|
|
|
|
227
|
6 |
|
if (in_array($this->markpos, $positions, true)) { |
228
|
3 |
|
return $this->markpos; |
229
|
|
|
} |
230
|
|
|
|
231
|
6 |
|
return 'bottom-right'; |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|