|
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
|
12 |
|
public function run(Image $image) |
|
21
|
|
|
{ |
|
22
|
12 |
|
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
|
3 |
|
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
|
18 |
|
public function getBorder(Image $image) |
|
47
|
|
|
{ |
|
48
|
18 |
|
if (!$this->border) { |
|
49
|
6 |
|
return; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
15 |
|
$values = explode(',', $this->border); |
|
53
|
|
|
|
|
54
|
15 |
|
$width = $this->getWidth($image, $this->getDpr(), isset($values[0]) ? $values[0] : null); |
|
55
|
15 |
|
$color = $this->getColor(isset($values[1]) ? $values[1] : null); |
|
56
|
15 |
|
$method = $this->getMethod(isset($values[2]) ? $values[2] : null); |
|
57
|
|
|
|
|
58
|
15 |
|
if ($width) { |
|
59
|
12 |
|
return [$width, $color, $method]; |
|
60
|
|
|
} |
|
61
|
3 |
|
} |
|
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
|
18 |
|
public function getWidth(Image $image, $dpr, $width) |
|
71
|
|
|
{ |
|
72
|
18 |
|
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
|
18 |
|
public function getColor($color) |
|
81
|
|
|
{ |
|
82
|
18 |
|
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
|
18 |
|
public function getMethod($method) |
|
91
|
|
|
{ |
|
92
|
18 |
|
if (!in_array($method, ['expand', 'shrink', 'overlay'], true)) { |
|
93
|
9 |
|
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
|
18 |
|
public function getDpr() |
|
104
|
|
|
{ |
|
105
|
18 |
|
if (!is_numeric($this->dpr)) { |
|
106
|
18 |
|
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
|
3 |
|
} |
|
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
|
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
|
|
|
) |
|
150
|
3 |
|
->resizeCanvas( |
|
151
|
3 |
|
$width * 2, |
|
152
|
3 |
|
$width * 2, |
|
153
|
3 |
|
'center', |
|
154
|
3 |
|
true, |
|
155
|
2 |
|
$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
|
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
|
2 |
|
$color |
|
174
|
|
|
); |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|