Completed
Push — master ( fbcc48...388212 )
by Jonathan
02:22
created

Border::getMethod()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4286
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
namespace League\Glide\Manipulators;
4
5
use Intervention\Image\Image;
6
use League\Glide\Manipulators\Helpers\Color;
7
use League\Glide\Manipulators\Helpers\Dimension;
8
9
/**
10
 * @property string $border
11
 * @property string $dpr
12
 */
13
class Border extends BaseManipulator
14
{
15
    /**
16
     * Perform border image manipulation.
17
     * @param  Image $image The source image.
18
     * @return Image The manipulated image.
19
     */
20
    public function run(Image $image)
21
    {
22
        if ($border = $this->getBorder($image)) {
23
            list($width, $color, $method) = $border;
24
25
            if ($method === 'overlay') {
26
                return $this->runOverlay($image, $width, $color);
27
            }
28
29
            if ($method === 'shrink') {
30
                return $this->runShrink($image, $width, $color);
31
            }
32
33
            if ($method === 'expand') {
34
                return $this->runExpand($image, $width, $color);
35
            }
36
        }
37
38
        return $image;
39
    }
40
41
    /**
42
     * Resolve border amount.
43
     * @param  Image  $image The source image.
44
     * @return string The resolved border amount.
45
     */
46 3
    public function getBorder(Image $image)
47
    {
48 3
        if (!$this->border) {
49 3
            return;
50
        }
51
52 3
        $values = explode(',', $this->border);
53
54 3
        $width = $this->getWidth($image, $this->getDpr(), isset($values[0]) ? $values[0] : null);
55 3
        $color = $this->getColor(isset($values[1]) ? $values[1] : null);
56 3
        $method = $this->getMethod(isset($values[2]) ? $values[2] : null);
57
58 3
        if ($width) {
59 3
            return [$width, $color, $method];
60
        }
61
    }
62
63
    /**
64
     * Get border width.
65
     * @param  Image  $image The source image.
66
     * @param  double $dpr   The device pixel ratio.
67
     * @param  string $width The border width.
68
     * @return double The resolved border width.
69
     */
70 6
    public function getWidth(Image $image, $dpr, $width)
71
    {
72 6
        return (new Dimension($image, $dpr))->get($width);
73
    }
74
75
    /**
76
     * Get formatted color.
77
     * @param  string $color The color.
78
     * @return string The formatted color.
79
     */
80 6
    public function getColor($color)
81
    {
82 6
        return (new Color($color))->formatted();
83
    }
84
85
    /**
86
     * Resolve the border method.
87
     * @param  string $method The raw border method.
88
     * @return string The resolved border method.
89
     */
90 6
    public function getMethod($method)
91
    {
92 6
        if (!in_array($method, ['expand', 'shrink', 'overlay'], true)) {
93 6
            return 'overlay';
94
        }
95
96 3
        return $method;
97
    }
98
99
    /**
100
     * Resolve the device pixel ratio.
101
     * @return double The device pixel ratio.
102
     */
103 6
    public function getDpr()
104
    {
105 6
        if (!is_numeric($this->dpr)) {
106 6
            return 1.0;
107
        }
108
109 3
        if ($this->dpr < 0 or $this->dpr > 8) {
110 3
            return 1.0;
111
        }
112
113 3
        return (double) $this->dpr;
114
    }
115
116
    /**
117
     * Run the overlay border method.
118
     * @param  Image  $image The source image.
119
     * @param  double $width The border width.
120
     * @param  string $color The border color.
121
     * @return Image  The manipulated image.
122
     */
123
    public function runOverlay(Image $image, $width, $color)
124
    {
125
        return $image->rectangle(
126
            $width / 2,
127
            $width / 2,
128
            $image->width() - ($width / 2),
129
            $image->height() - ($width / 2),
130
            function ($draw) use ($width, $color) {
131
                $draw->border($width, $color);
132
            }
133
        );
134
    }
135
136
    /**
137
     * Run the shrink border method.
138
     * @param  Image  $image The source image.
139
     * @param  double $width The border width.
140
     * @param  string $color The border color.
141
     * @return Image  The manipulated image.
142
     */
143
    public function runShrink(Image $image, $width, $color)
144
    {
145
        return $image
146
            ->resize(
147
                $image->width() - ($width * 2),
148
                $image->height() - ($width * 2)
149
            )
150
            ->resizeCanvas(
151
                $width * 2,
152
                $width * 2,
153
                'center',
154
                true,
155
                $color
156
            );
157
    }
158
159
    /**
160
     * Run the expand border method.
161
     * @param  Image  $image The source image.
162
     * @param  double $width The border width.
163
     * @param  string $color The border color.
164
     * @return Image  The manipulated image.
165
     */
166
    public function runExpand(Image $image, $width, $color)
167
    {
168
        return $image->resizeCanvas(
169
            $width * 2,
170
            $width * 2,
171
            'center',
172
            true,
173
            $color
174
        );
175
    }
176
}
177