@@ -10,7 +10,9 @@ class InitialAvatar
1010 private $ image ;
1111
1212 private $ parameter_cacheTime = 0 ;
13- private $ parameter_name = 'JD ' ;
13+ private $ parameter_length = 2 ;
14+ private $ parameter_initials = 'JD ' ;
15+ private $ parameter_name = 'John Doe ' ;
1416 private $ parameter_size = 48 ;
1517 private $ parameter_bgColor = '#000 ' ;
1618 private $ parameter_fontColor = '#fff ' ;
@@ -23,7 +25,16 @@ public function __construct()
2325
2426 public function name ( string $ nameOrInitials ): self
2527 {
26- $ this ->parameter_name = $ this ->generateInitials ( $ nameOrInitials );
28+ $ this ->parameter_name = $ nameOrInitials ;
29+ $ this ->parameter_initials = $ this ->generateInitials ();
30+
31+ return $ this ;
32+ }
33+
34+ public function length ( int $ length = 2 ): self
35+ {
36+ $ this ->parameter_length = (int ) $ length ;
37+ $ this ->parameter_initials = $ this ->generateInitials ();
2738
2839 return $ this ;
2940 }
@@ -74,14 +85,14 @@ public function generate( $name = null ): Image
7485 {
7586 if ( $ name !== null )
7687 {
77- $ this ->parameter_name = $ this ->generateInitials ( $ name );
88+ $ this ->parameter_initials = $ this ->generateInitials ( $ this -> parameter_name );
7889 }
7990
8091 $ fontFile = $ this ->parameter_fontFile ;
8192 $ size = $ this ->parameter_size ;
8293 $ color = $ this ->parameter_fontColor ;
8394 $ bgColor = $ this ->parameter_bgColor ;
84- $ name = $ this ->parameter_name ;
95+ $ name = $ this ->parameter_initials ;
8596
8697 $ img = $ this ->image ->cache ( function ( ImageCache $ image ) use ( $ size , $ bgColor , $ color , $ fontFile , $ name )
8798 {
@@ -103,9 +114,9 @@ public function generate( $name = null ): Image
103114 *
104115 * @return string
105116 */
106- public function getParameterName ()
117+ public function getInitials ()
107118 {
108- return $ this ->parameter_name ;
119+ return $ this ->parameter_initials ;
109120 }
110121
111122 /**
@@ -114,26 +125,41 @@ public function getParameterName()
114125 * For safety, we limit it to two characters,
115126 * in case its a single, but long, name.
116127 *
117- * @param string $nameOrInitials
118- *
119128 * @return string
120129 */
121- private function generateInitials ( string $ nameOrInitials = ' John Doe ' ): string
130+ private function generateInitials (): string
122131 {
123- $ nameOrInitials = mb_strtoupper ( trim ( $ nameOrInitials ) );
124-
125- $ names = explode ( ' ' , $ nameOrInitials );
132+ $ nameOrInitials = mb_strtoupper ( trim ( $ this ->parameter_name ) );
133+ $ names = explode ( ' ' , $ nameOrInitials );
134+ $ initials = $ nameOrInitials ;
135+ $ assignedNames = 0 ;
126136
127137 if ( count ( $ names ) > 1 )
128138 {
129- $ firstNameLetter = mb_substr ( $ names [0 ], 0 , 1 );
130- $ lastNameLetter = mb_substr ( $ names [ count ( $ names ) - 1 ], 0 , 1 );
139+ $ initials = '' ;
140+ $ start = 0 ;
141+
142+ for ( $ i = 0 ; $ i < $ this ->parameter_length ; $ i ++ )
143+ {
144+ $ index = $ i ;
145+
146+ if ( ( $ index === ( $ this ->parameter_length - 1 ) && $ index > 0 ) || ( $ index > ( count ( $ names ) - 1 ) ) )
147+ {
148+ $ index = count ( $ names ) - 1 ;
149+ }
150+
151+ if ( $ assignedNames >= count ( $ names ) )
152+ {
153+ $ start ++;
154+ }
131155
132- $ nameOrInitials = "{$ firstNameLetter }{$ lastNameLetter }" ;
156+ $ initials .= mb_substr ( $ names [ $ index ], $ start , 1 );
157+ $ assignedNames ++;
158+ }
133159 }
134160
135- $ nameOrInitials = mb_substr ( $ nameOrInitials , 0 , 2 );
161+ $ initials = mb_substr ( $ initials , 0 , $ this -> parameter_length );
136162
137- return $ nameOrInitials ;
163+ return $ initials ;
138164 }
139165}
0 commit comments