1 | <?php |
||
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) |
||
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) |
||
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) |
||
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) |
||
176 | } |
||
177 |