@@ -46,6 +46,16 @@ class LetterAvatar
46
46
*/
47
47
private $ imageManager ;
48
48
49
+ /**
50
+ * @var string
51
+ */
52
+ private $ backgroundColor ;
53
+
54
+ /**
55
+ * @var string
56
+ */
57
+ private $ foregroundColor ;
58
+
49
59
/**
50
60
* LetterAvatar constructor.
51
61
* @param string $name
@@ -60,6 +70,19 @@ public function __construct(string $name, string $shape = 'circle', int $size =
60
70
$ this ->setSize ($ size );
61
71
}
62
72
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
+
63
86
/**
64
87
* @param string $name
65
88
*/
@@ -103,21 +126,22 @@ private function generate(): \Intervention\Image\Image
103
126
$ isCircle = $ this ->shape === 'circle ' ;
104
127
105
128
$ 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 ' ;
107
131
108
- $ canvas = $ this ->imageManager ->canvas (480 , 480 , $ isCircle ? null : $ color );
132
+ $ canvas = $ this ->imageManager ->canvas (480 , 480 , $ isCircle ? null : $ this -> backgroundColor );
109
133
110
134
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 );
113
137
});
114
138
115
139
}
116
140
117
141
$ canvas ->text ($ this ->nameInitials , 240 , 240 , function (Font $ font ) {
118
142
$ font ->file (__DIR__ . '/fonts/arial-bold.ttf ' );
119
143
$ font ->size (220 );
120
- $ font ->color (' #fafafa ' );
144
+ $ font ->color ($ this -> foregroundColor );
121
145
$ font ->valign ('middle ' );
122
146
$ font ->align ('center ' );
123
147
});
0 commit comments