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

Border::runOverlay()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1.001

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
ccs 9
cts 10
cp 0.9
rs 9.4286
cc 1
eloc 8
nc 1
nop 3
crap 1.001
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 9
    public function run(Image $image)
21
    {
22 9
        if ($border = $this->getBorder($image)) {
23 9
            list($width, $color, $method) = $border;
24
25 9
            if ($method === 'overlay') {
26 3
                return $this->runOverlay($image, $width, $color);
27
            }
28
29 6
            if ($method === 'shrink') {
30 3
                return $this->runShrink($image, $width, $color);
31
            }
32
33 3
            if ($method === 'expand') {
34 3
                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 12
    public function getBorder(Image $image)
47
    {
48 12
        if (!$this->border) {
49 3
            return;
50
        }
51
52 12
        $values = explode(',', $this->border);
53
54 12
        $width = $this->getWidth($image, $this->getDpr(), isset($values[0]) ? $values[0] : null);
55 12
        $color = $this->getColor(isset($values[1]) ? $values[1] : null);
56 12
        $method = $this->getMethod(isset($values[2]) ? $values[2] : null);
57
58 12
        if ($width) {
59 12
            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 15
    public function getWidth(Image $image, $dpr, $width)
71
    {
72 15
        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 15
    public function getColor($color)
81
    {
82 15
        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 15
    public function getMethod($method)
91
    {
92 15
        if (!in_array($method, ['expand', 'shrink', 'overlay'], true)) {
93 6
            return 'overlay';
94
        }
95
96 12
        return $method;
97
    }
98
99
    /**
100
     * Resolve the device pixel ratio.
101
     * @return double The device pixel ratio.
102
     */
103 15
    public function getDpr()
104
    {
105 15
        if (!is_numeric($this->dpr)) {
106 15
            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 3
    public function runOverlay(Image $image, $width, $color)
124
    {
125 3
        return $image->rectangle(
126 3
            $width / 2,
127 3
            $width / 2,
128 3
            $image->width() - ($width / 2),
129 3
            $image->height() - ($width / 2),
130 1
            function ($draw) use ($width, $color) {
131
                $draw->border($width, $color);
132 1
            }
133 2
        );
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 3
    public function runShrink(Image $image, $width, $color)
144
    {
145
        return $image
146 3
            ->resize(
147 3
                $image->width() - ($width * 2),
148 3
                $image->height() - ($width * 2)
149 2
            )
150 3
            ->resizeCanvas(
151 3
                $width * 2,
152 3
                $width * 2,
153 3
                'center',
154 3
                true,
155
                $color
156 2
            );
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 3
    public function runExpand(Image $image, $width, $color)
167
    {
168 3
        return $image->resizeCanvas(
169 3
            $width * 2,
170 3
            $width * 2,
171 3
            'center',
172 3
            true,
173
            $color
174 2
        );
175
    }
176
}
177