Skip to content

Commit e2c552a

Browse files
authored
Add responsive property to help with SVG generation (#163)
* Add responsive property to help with SVG generation * Add missing Laravel code
1 parent e7717f1 commit e2c552a

File tree

6 files changed

+51
-1
lines changed

6 files changed

+51
-1
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ You may specify custom font-family for your SVG text.
108108
Avatar::create('Susilo Bambang Yudhoyono')->setFontFamily('Laravolt')->toSvg();
109109
```
110110

111+
You may make the SVG responsive. This excludes the height and width attributes.
112+
```php
113+
Avatar::create('Susilo Bambang Yudhoyono')->setResponsive()->toSvg();
114+
```
115+
111116
## Get underlying Intervention image object
112117
```php
113118
Avatar::create('Abdul Somad')->getImageObject();

config/config.php

+3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
// Image height, in pixel
3535
'height' => 100,
3636

37+
// Responsive SVG, height and width attributes are not added when true
38+
'responsive' => false,
39+
3740
// Number of characters used as initials. If name consists of single word, the first N character will be used
3841
'chars' => 2,
3942

src/Avatar.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class Avatar
3030

3131
protected int $height;
3232

33+
protected bool $responsive = false;
34+
3335
protected array $availableBackgrounds = [];
3436

3537
protected array $availableForegrounds = [];
@@ -134,6 +136,7 @@ public function applyTheme(array $config): void
134136
$this->fontSize = $config['fontSize'];
135137
$this->width = $config['width'];
136138
$this->height = $config['height'];
139+
$this->responsive = $config['responsive'];
137140
$this->ascii = $config['ascii'];
138141
$this->uppercase = $config['uppercase'];
139142
$this->rtl = $config['rtl'];
@@ -210,7 +213,11 @@ public function toSvg(): string
210213
$radius = ($this->width - $this->borderSize) / 2;
211214
$center = $this->width / 2;
212215

213-
$svg = '<svg xmlns="http://www.w3.org/2000/svg" width="'.$this->width.'" height="'.$this->height.'" viewBox="0 0 '.$this->width.' '.$this->height.'">';
216+
$svg = '<svg xmlns="http://www.w3.org/2000/svg"';
217+
if (! $this->responsive) {
218+
$svg .= ' width="'.$this->width.'" height="'.$this->height.'"';
219+
}
220+
$svg .= ' viewBox="0 0 '.$this->width.' '.$this->height.'">';
214221

215222
if ($this->shape === 'square') {
216223
$svg .= '<rect x="'.$x
@@ -455,6 +462,7 @@ protected function validateConfig(array $config): array
455462
'fontSize' => 48,
456463
'width' => 100,
457464
'height' => 100,
465+
'responsive' => false,
458466
'ascii' => false,
459467
'uppercase' => false,
460468
'rtl' => false,

src/Concerns/AttributeSetter.php

+7
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ public function setDimension($width, $height = null): static
4545

4646
return $this;
4747
}
48+
49+
public function setResponsive($responsive): static
50+
{
51+
$this->responsive = $responsive;
52+
53+
return $this;
54+
}
4855

4956
public function setFontSize($size): static
5057
{

tests/AvatarLaravelTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public function it_can_override_attributes_when_instantiated()
1616
'shape' => 'circle',
1717
'width' => 100,
1818
'height' => 100,
19+
'responsive' => true,
1920
'chars' => 2,
2021
'fontSize' => 48,
2122
'fonts' => ['arial.ttf'],
@@ -37,6 +38,7 @@ public function it_can_override_attributes_when_instantiated()
3738
$this->assertEquals('circle', $avatar->getAttribute('shape'));
3839
$this->assertEquals(100, $avatar->getAttribute('width'));
3940
$this->assertEquals(100, $avatar->getAttribute('height'));
41+
$this->assertEquals(true, $avatar->getAttribute('responsive'));
4042
$this->assertEquals(['#000000'], $avatar->getAttribute('availableBackgrounds'));
4143
$this->assertEquals(['#FFFFFF'], $avatar->getAttribute('availableForegrounds'));
4244
$this->assertEquals(['arial.ttf'], $avatar->getAttribute('fonts'));

tests/AvatarPhpTest.php

+25
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,31 @@ public function it_can_generate_rectangle_svg()
290290
$this->assertEquals($expected, $svg);
291291
}
292292

293+
/**
294+
* @test
295+
*/
296+
public function it_can_generate_responsive_svg()
297+
{
298+
$expected = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">';
299+
$expected .= '<rect x="5" y="5" width="90" height="90" stroke="yellow" stroke-width="10" rx="15" fill="red" />';
300+
$expected .= '<text font-size="24" fill="white" x="50%" y="50%" dy=".1em" style="line-height:1" alignment-baseline="middle" text-anchor="middle" dominant-baseline="central">AB</text>';
301+
$expected .= '</svg>';
302+
303+
$avatar = new \Laravolt\Avatar\Avatar();
304+
$svg = $avatar->create('Andi Budiman')
305+
->setShape('square')
306+
->setFontSize(24)
307+
->setDimension(100, 100)
308+
->setForeground('white')
309+
->setBorder(10, 'yellow')
310+
->setBorderRadius(15)
311+
->setBackground('red')
312+
->setResponsive(true)
313+
->toSvg();
314+
315+
$this->assertEquals($expected, $svg);
316+
}
317+
293318
/**
294319
* @test
295320
*/

0 commit comments

Comments
 (0)