Skip to content

Commit f324950

Browse files
authored
Add support to use foreground and background border colors for svgs (#118)
* Add support to use foreground and background border colors for svgs * Fix codestyle issue
1 parent 94d9bfd commit f324950

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

src/Avatar.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,15 +215,15 @@ public function toSvg()
215215
$svg .= '<rect x="'.$x
216216
.'" y="'.$y
217217
.'" width="'.$width.'" height="'.$height
218-
.'" stroke="'.$this->borderColor
218+
.'" stroke="'.$this->getBorderColor()
219219
.'" stroke-width="'.$this->borderSize
220220
.'" rx="'.$this->borderRadius
221221
.'" fill="'.$this->background.'" />';
222222
} elseif ($this->shape == 'circle') {
223223
$svg .= '<circle cx="'.$center
224224
.'" cy="'.$center
225225
.'" r="'.$radius
226-
.'" stroke="'.$this->borderColor
226+
.'" stroke="'.$this->getBorderColor()
227227
.'" stroke-width="'.$this->borderSize
228228
.'" fill="'.$this->background.'" />';
229229
}

tests/AvatarPhpTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,50 @@ public function it_can_generate_svg_with_custom_font_family()
308308
$this->assertEquals($expected, $svg);
309309
}
310310

311+
/**
312+
* @test
313+
*/
314+
public function it_can_use_the_foreground_color_for_the_svg_border()
315+
{
316+
$expected = '<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 100 100">';
317+
$expected .= '<circle cx="50" cy="50" r="45" stroke="green" stroke-width="10" fill="red" />';
318+
$expected .= '<text x="50" y="50" font-size="24" fill="green" alignment-baseline="middle" text-anchor="middle" dominant-baseline="central">AB</text>';
319+
$expected .= '</svg>';
320+
321+
$avatar = new \Laravolt\Avatar\Avatar(['border' => ['size' => 10, 'color' => 'foreground']]);
322+
$svg = $avatar->create('Andi Budiman')
323+
->setShape('circle')
324+
->setFontSize(24)
325+
->setDimension(100, 100)
326+
->setForeground('green')
327+
->setBackground('red')
328+
->toSvg();
329+
330+
$this->assertEquals($expected, $svg);
331+
}
332+
333+
/**
334+
* @test
335+
*/
336+
public function it_can_use_the_background_color_for_the_svg_border()
337+
{
338+
$expected = '<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 100 100">';
339+
$expected .= '<circle cx="50" cy="50" r="45" stroke="red" stroke-width="10" fill="red" />';
340+
$expected .= '<text x="50" y="50" font-size="24" fill="green" alignment-baseline="middle" text-anchor="middle" dominant-baseline="central">AB</text>';
341+
$expected .= '</svg>';
342+
343+
$avatar = new \Laravolt\Avatar\Avatar(['border' => ['size' => 10, 'color' => 'background']]);
344+
$svg = $avatar->create('Andi Budiman')
345+
->setShape('circle')
346+
->setFontSize(24)
347+
->setDimension(100, 100)
348+
->setForeground('green')
349+
->setBackground('red')
350+
->toSvg();
351+
352+
$this->assertEquals($expected, $svg);
353+
}
354+
311355
/**
312356
* @test
313357
*/

0 commit comments

Comments
 (0)