Completed
Push — master ( 72430f...dafba8 )
by Jonathan
04:56
created

Watermark   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 236
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 74.82%

Importance

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