Completed
Push — master ( d3f7ad...af44be )
by Jonathan
11:37
created

Watermark   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 214
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 96.91%

Importance

Changes 10
Bugs 2 Features 3
Metric Value
wmc 25
c 10
b 2
f 3
lcom 1
cbo 7
dl 0
loc 214
ccs 94
cts 97
cp 0.9691
rs 10

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