Watermark::getWatermarksPathPrefix()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace League\Glide\Manipulators;
4
5
use Intervention\Image\Image;
6
use League\Flysystem\FilesystemInterface;
7
use League\Glide\Filesystem\FilesystemException;
8
use League\Glide\Manipulators\Helpers\Dimension;
9
10
/**
11
 * @property string $dpr
12
 * @property string $mark
13
 * @property string $markfit
14
 * @property string $markh
15
 * @property string $markpad
16
 * @property string $markpos
17
 * @property string $markw
18
 * @property string $markx
19
 * @property string $marky
20
 * @property string $markalpha
21
 */
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
    public function __construct(FilesystemInterface $watermarks = null, $watermarksPathPrefix = '')
41
    {
42
        $this->setWatermarks($watermarks);
43
        $this->setWatermarksPathPrefix($watermarksPathPrefix);
44
    }
45
46
    /**
47
     * Set the watermarks file system.
48
     * @param FilesystemInterface $watermarks The watermarks file system.
49
     */
50
    public function setWatermarks(FilesystemInterface $watermarks = null)
51
    {
52
        $this->watermarks = $watermarks;
53
    }
54
55
    /**
56
     * Get the watermarks file system.
57
     * @return FilesystemInterface The watermarks file system.
58
     */
59
    public function getWatermarks()
60
    {
61
        return $this->watermarks;
62
    }
63
64
    /**
65
     * Set the watermarks path prefix.
66
     * @param string $watermarksPathPrefix The watermarks path prefix.
67
     */
68
    public function setWatermarksPathPrefix($watermarksPathPrefix = '')
69
    {
70
        $this->watermarksPathPrefix = trim($watermarksPathPrefix, '/');
71
    }
72
73
    /**
74
     * Get the watermarks path prefix.
75
     * @return string The watermarks path prefix.
76
     */
77
    public function getWatermarksPathPrefix()
78
    {
79
        return $this->watermarksPathPrefix;
80
    }
81
82
    /**
83
     * Perform watermark image manipulation.
84
     * @param  Image $image The source image.
85
     * @return Image The manipulated image.
86
     */
87
    public function run(Image $image)
88
    {
89
        if ($watermark = $this->getImage($image)) {
90
            $markw = $this->getDimension($image, 'markw');
91
            $markh = $this->getDimension($image, 'markh');
92
            $markx = $this->getDimension($image, 'markx');
93
            $marky = $this->getDimension($image, 'marky');
94
            $markpad = $this->getDimension($image, 'markpad');
95
            $markfit = $this->getFit();
96
            $markpos = $this->getPosition();
97
            $markalpha = $this->getAlpha();
98
99
            if ($markpad) {
100
                $markx = $marky = $markpad;
101
            }
102
103
            $size = new Size();
104
            $size->setParams([
105
                'w' => $markw,
106
                'h' => $markh,
107
                'fit' => $markfit,
108
            ]);
109
            $watermark = $size->run($watermark);
110
111
            if ($markalpha < 100) {
112
                $watermark->opacity($markalpha);
113
            }
114
115
            $image->insert($watermark, $markpos, intval($markx), intval($marky));
116
        }
117
118
        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
    public function getImage(Image $image)
127
    {
128
        if (is_null($this->watermarks)) {
129
            return;
130
        }
131
132
        if (!is_string($this->mark)) {
133
            return;
134
        }
135
136
        if ($this->mark === '') {
137
            return;
138
        }
139
140
        $path = $this->mark;
141
142
        if ($this->watermarksPathPrefix) {
143
            $path = $this->watermarksPathPrefix.'/'.$path;
144
        }
145
146
        if ($this->watermarks->has($path)) {
147
            $source = $this->watermarks->read($path);
148
149
            if ($source === false) {
150
                throw new FilesystemException(
151
                    'Could not read the image `'.$path.'`.'
152
                );
153
            }
154
155
            return $image->getDriver()->init($source);
156
        }
157
    }
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
    public function getDimension(Image $image, $field)
166
    {
167
        if ($this->{$field}) {
168
            return (new Dimension($image, $this->getDpr()))->get($this->{$field});
169
        }
170
    }
171
172
    /**
173
     * Resolve the device pixel ratio.
174
     * @return double The device pixel ratio.
175
     */
176
    public function getDpr()
177
    {
178
        if (!is_numeric($this->dpr)) {
179
            return 1.0;
180
        }
181
182
        if ($this->dpr < 0 or $this->dpr > 8) {
183
            return 1.0;
184
        }
185
186
        return (double) $this->dpr;
187
    }
188
189
    /**
190
     * Get the fit.
191
     * @return string The fit.
192
     */
193
    public function getFit()
194
    {
195
        $fitMethods = [
196
            'contain',
197
            'max',
198
            'stretch',
199
            'crop',
200
            'crop-top-left',
201
            'crop-top',
202
            'crop-top-right',
203
            'crop-left',
204
            'crop-center',
205
            'crop-right',
206
            'crop-bottom-left',
207
            'crop-bottom',
208
            'crop-bottom-right',
209
        ];
210
211
        if (in_array($this->markfit, $fitMethods, true)) {
212
            return $this->markfit;
213
        }
214
    }
215
216
    /**
217
     * Get the position.
218
     * @return string The position.
219
     */
220
    public function getPosition()
221
    {
222
        $positions = [
223
            'top-left',
224
            'top',
225
            'top-right',
226
            'left',
227
            'center',
228
            'right',
229
            'bottom-left',
230
            'bottom',
231
            'bottom-right',
232
        ];
233
234
        if (in_array($this->markpos, $positions, true)) {
235
            return $this->markpos;
236
        }
237
238
        return 'bottom-right';
239
    }
240
241
    /**
242
     * Get the alpha channel.
243
     * @return int The alpha.
244
     */
245
    public function getAlpha()
246
    {
247
        if (!is_numeric($this->markalpha)) {
248
            return 100;
249
        }
250
251
        if ($this->markalpha < 0 or $this->markalpha > 100) {
252
            return 100;
253
        }
254
255
        return (int) $this->markalpha;
256
    }
257
}
258