Skip to content

Commit 1751e0e

Browse files
authored
Merge pull request #20 from habibillah/master
allow to add custom color
2 parents 522987e + 35f1ac2 commit 1751e0e

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,9 @@ $avatar->saveAs('path/to/filename', LetterAvatar::MIME_TYPE_JPEG);
3939
``` html
4040
<img src="<?php echo $avatar ?>" />
4141
```
42+
43+
To use static colour or custom colour use `->setColor($background, $foreground);`
44+
45+
``` html
46+
<img src="<?php echo $avatar->setColor('#000000', '#ffffff');?>" alt="">
47+
```

src/LetterAvatar.php

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ class LetterAvatar
4646
*/
4747
private $imageManager;
4848

49+
/**
50+
* @var string
51+
*/
52+
private $backgroundColor;
53+
54+
/**
55+
* @var string
56+
*/
57+
private $foregroundColor;
58+
4959
/**
5060
* LetterAvatar constructor.
5161
* @param string $name
@@ -60,6 +70,19 @@ public function __construct(string $name, string $shape = 'circle', int $size =
6070
$this->setSize($size);
6171
}
6272

73+
/**
74+
* color in RGB format (example: #FFFFFF)
75+
*
76+
* @param $backgroundColor
77+
* @param $foregroundColor
78+
*/
79+
public function setColor($backgroundColor, $foregroundColor)
80+
{
81+
$this->backgroundColor = $backgroundColor;
82+
$this->foregroundColor = $foregroundColor;
83+
return $this;
84+
}
85+
6386
/**
6487
* @param string $name
6588
*/
@@ -103,21 +126,22 @@ private function generate(): \Intervention\Image\Image
103126
$isCircle = $this->shape === 'circle';
104127

105128
$this->nameInitials = $this->getInitials($this->name);
106-
$color = $this->stringToColor($this->name);
129+
$this->backgroundColor = $this->backgroundColor ?: $this->stringToColor($this->name);
130+
$this->foregroundColor = $this->foregroundColor ?: '#fafafa';
107131

108-
$canvas = $this->imageManager->canvas(480, 480, $isCircle ? null : $color);
132+
$canvas = $this->imageManager->canvas(480, 480, $isCircle ? null : $this->backgroundColor);
109133

110134
if ($isCircle) {
111-
$canvas->circle(480, 240, 240, function (CircleShape $draw) use ($color) {
112-
$draw->background($color);
135+
$canvas->circle(480, 240, 240, function (CircleShape $draw) {
136+
$draw->background($this->backgroundColor);
113137
});
114138

115139
}
116140

117141
$canvas->text($this->nameInitials, 240, 240, function (Font $font) {
118142
$font->file(__DIR__ . '/fonts/arial-bold.ttf');
119143
$font->size(220);
120-
$font->color('#fafafa');
144+
$font->color($this->foregroundColor);
121145
$font->valign('middle');
122146
$font->align('center');
123147
});

0 commit comments

Comments
 (0)