Watermark   A
last analyzed

Coupling/Cohesion

Components 1
Dependencies 7

Complexity

Total Complexity 30

Size/Duplication

Total Lines 236
Duplicated Lines 0 %

Test Coverage

Coverage 74.82%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 30
lcom 1
cbo 7
dl 0
loc 236
ccs 104
cts 139
cp 0.7482
rs 10
c 3
b 0
f 0

12 Methods

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