Completed
Pushf44f0e...93bfc8
passed — Build
created

Watermark::getAlpha()   A

↳ Parent: Watermark

Complexity

Conditions 4
Paths 3

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 12
Code Lines 6

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
ccs 0
cts 0
cp 0
rs 9.2
cc 4
eloc 6
nc 3
nop 0
crap 20
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 57
     */
40
    public function __construct(FilesystemInterface $watermarks = null, $watermarksPathPrefix = '')
41 57
    {
42 57
        $this->setWatermarks($watermarks);
43 57
        $this->setWatermarksPathPrefix($watermarksPathPrefix);
44
    }
45
46
    /**
47
     * Set the watermarks file system.
48
     * @param FilesystemInterface $watermarks The watermarks file system.
49 57
     */
50
    public function setWatermarks(FilesystemInterface $watermarks = null)
51 57
    {
52 57
        $this->watermarks = $watermarks;
53
    }
54
55
    /**
56
     * Get the watermarks file system.
57
     * @return FilesystemInterface The watermarks file system.
58 12
     */
59
    public function getWatermarks()
60 12
    {
61
        return $this->watermarks;
62
    }
63
64
    /**
65
     * Set the watermarks path prefix.
66
     * @param string $watermarksPathPrefix The watermarks path prefix.
67 57
     */
68
    public function setWatermarksPathPrefix($watermarksPathPrefix = '')
69 57
    {
70 57
        $this->watermarksPathPrefix = trim($watermarksPathPrefix, '/');
71
    }
72
73
    /**
74
     * Get the watermarks path prefix.
75
     * @return string The watermarks path prefix.
76 6
     */
77
    public function getWatermarksPathPrefix()
78 6
    {
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 3
     */
87
    public function run(Image $image)
88 3
    {
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
            $markpos = $this->getPosition();
97 3
            $markalpha = $this->getAlpha();
98 3
99 2
            if ($markpad) {
100
                $markx = $marky = $markpad;
101 3
            }
102 3
103 3
            $size = new Size();
104 3
            $size->setParams([
105 3
                'w' => $markw,
106 2
                'h' => $markh,
107 3
                'fit' => $markfit,
108
            ]);
109 3
            $watermark = $size->run($watermark);
110 2
111
            if ($markalpha < 100) {
112 3
                $watermark->opacity($markalpha);
113
            }
114
115
            $image->insert($watermark, $markpos, intval($markx), intval($marky));
116
        }
117
118
        return $image;
119
    }
120 18
121
    /**
122 18
     * Get the watermark image.
123 3
     * @param  Image      $image The source image.
124
     * @return Image|null The watermark image.
125
     */
126 15
    public function getImage(Image $image)
127 3
    {
128
        if (is_null($this->watermarks)) {
129
            return;
130 12
        }
131 3
132
        if (!is_string($this->mark)) {
133
            return;
134 9
        }
135
136 9
        if ($this->mark === '') {
137 3
            return;
138 2
        }
139
140 9
        $path = $this->mark;
141 9
142
        if ($this->watermarksPathPrefix) {
143 9
            $path = $this->watermarksPathPrefix.'/'.$path;
144 3
        }
145 3
146 2
        if ($this->watermarks->has($path)) {
147
            $source = $this->watermarks->read($path);
148
149 6
            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 6
    /**
160
     * Get a dimension.
161 6
     * @param  Image       $image The source image.
162 6
     * @param  string      $field The requested field.
163
     * @return double|null The dimension.
164 6
     */
165
    public function getDimension(Image $image, $field)
166
    {
167
        if ($this->{$field}) {
168
            return (new Dimension($image, $this->getDpr()))->get($this->{$field});
169
        }
170 9
    }
171
172 9
    /**
173 9
     * Resolve the device pixel ratio.
174
     * @return double The device pixel ratio.
175
     */
176 3
    public function getDpr()
177 3
    {
178
        if (!is_numeric($this->dpr)) {
179
            return 1.0;
180 3
        }
181
182
        if ($this->dpr < 0 or $this->dpr > 8) {
183
            return 1.0;
184
        }
185
186
        return (double) $this->dpr;
187 6
    }
188
189
    /**
190 6
     * Get the fit.
191 4
     * @return string The fit.
192 4
     */
193 4
    public function getFit()
194 4
    {
195 4
        $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
            'crop-center',
205 6
            'crop-right',
206 3
            'crop-bottom-left',
207
            'crop-bottom',
208 6
            'crop-bottom-right',
209
        ];
210
211
        if (in_array($this->markfit, $fitMethods, true)) {
212
            return $this->markfit;
213
        }
214 6
    }
215
216
    /**
217 6
     * Get the position.
218 4
     * @return string The position.
219 4
     */
220 4
    public function getPosition()
221 4
    {
222 4
        $positions = [
223 4
            'top-left',
224 4
            'top',
225 4
            'top-right',
226 4
            'left',
227
            'center',
228 6
            'right',
229 3
            'bottom-left',
230
            'bottom',
231
            'bottom-right',
232 6
        ];
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