Watermark   A
last analyzed

↳ Parent: Project

Coupling/Cohesion

Components 1
Dependencies 7

Complexity

Total Complexity 30

Size/Duplication

Total Lines 236
Duplicated Lines 0 %

Test Coverage

Coverage 97.2%

Importance

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

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