2
2
3
3
namespace YoHang88 \LetterAvatar ;
4
4
5
+ use Intervention \Image \Gd \Font ;
6
+ use Intervention \Image \Gd \Shapes \CircleShape ;
5
7
use Intervention \Image \ImageManager ;
6
8
7
9
class LetterAvatar
8
10
{
11
+ /**
12
+ * Image Type PNG
13
+ */
14
+ const MIME_TYPE_PNG = 'image/png ' ;
15
+
16
+ /**
17
+ * Image Type JPEG
18
+ */
19
+ const MIME_TYPE_JPEG = 'image/jpeg ' ;
20
+
9
21
/**
10
22
* @var string
11
23
*/
12
- protected $ name ;
24
+ private $ name ;
13
25
14
26
15
27
/**
16
28
* @var string
17
29
*/
18
- protected $ name_initials ;
30
+ private $ nameInitials ;
19
31
20
32
21
33
/**
22
34
* @var string
23
35
*/
24
- protected $ shape ;
36
+ private $ shape ;
25
37
26
38
27
39
/**
28
40
* @var int
29
41
*/
30
- protected $ size ;
42
+ private $ size ;
31
43
32
44
/**
33
45
* @var ImageManager
34
46
*/
35
- protected $ image_manager ;
36
-
47
+ private $ imageManager ;
37
48
38
- public function __construct ($ name , $ shape = 'circle ' , $ size = '48 ' )
49
+ /**
50
+ * LetterAvatar constructor.
51
+ * @param string $name
52
+ * @param string $shape
53
+ * @param int $size
54
+ */
55
+ public function __construct (string $ name , string $ shape = 'circle ' , int $ size = 48 )
39
56
{
40
57
$ this ->setName ($ name );
41
58
$ this ->setImageManager (new ImageManager ());
42
59
$ this ->setShape ($ shape );
43
60
$ this ->setSize ($ size );
44
61
}
45
62
46
- /**
47
- * @return string
48
- */
49
- public function getName ()
50
- {
51
- return $ this ->name ;
52
- }
53
-
54
63
/**
55
64
* @param string $name
56
65
*/
57
- public function setName ($ name )
66
+ private function setName (string $ name )
58
67
{
59
68
$ this ->name = $ name ;
60
69
}
61
70
62
- /**
63
- * @return ImageManager
64
- */
65
- public function getImageManager ()
66
- {
67
- return $ this ->image_manager ;
68
- }
69
71
70
72
/**
71
- * @param ImageManager $image_manager
72
- */
73
- public function setImageManager (ImageManager $ image_manager )
74
- {
75
- $ this ->image_manager = $ image_manager ;
76
- }
77
-
78
- /**
79
- * @return string
73
+ * @param ImageManager $imageManager
80
74
*/
81
- public function getShape ( )
75
+ private function setImageManager ( ImageManager $ imageManager )
82
76
{
83
- return $ this ->shape ;
77
+ $ this ->imageManager = $ imageManager ;
84
78
}
85
79
86
80
/**
87
81
* @param string $shape
88
82
*/
89
- public function setShape ($ shape )
83
+ private function setShape (string $ shape )
90
84
{
91
85
$ this ->shape = $ shape ;
92
86
}
93
87
94
- /**
95
- * @return int
96
- */
97
- public function getSize ()
98
- {
99
- return $ this ->size ;
100
- }
101
88
102
89
/**
103
90
* @param int $size
104
91
*/
105
- public function setSize ($ size )
92
+ private function setSize (int $ size )
106
93
{
107
94
$ this ->size = $ size ;
108
95
}
@@ -111,83 +98,121 @@ public function setSize($size)
111
98
/**
112
99
* @return \Intervention\Image\Image
113
100
*/
114
- public function generate ()
101
+ private function generate (): \ Intervention \ Image \ Image
115
102
{
116
- $ words = $ this ->break_words ($ this ->name );
117
-
118
- $ number_of_word = 1 ;
119
- $ this ->name_initials = '' ;
120
- foreach ($ words as $ word ) {
121
-
122
- if ($ number_of_word > 2 )
123
- break ;
124
-
125
- $ this ->name_initials .= mb_strtoupper (trim (mb_substr ($ word , 0 , 1 , 'UTF-8 ' )));
126
-
127
- $ number_of_word ++;
128
- }
103
+ $ isCircle = $ this ->shape === 'circle ' ;
129
104
105
+ $ this ->nameInitials = $ this ->getInitials ($ this ->name );
130
106
$ color = $ this ->stringToColor ($ this ->name );
131
107
132
- if ($ this ->shape == 'circle ' ) {
133
- $ canvas = $ this ->image_manager ->canvas (480 , 480 );
108
+ $ canvas = $ this ->imageManager ->canvas (480 , 480 , $ isCircle ? null : $ color );
134
109
135
- $ canvas ->circle (480 , 240 , 240 , function ($ draw ) use ($ color ) {
110
+ if ($ isCircle ) {
111
+ $ canvas ->circle (480 , 240 , 240 , function (CircleShape $ draw ) use ($ color ) {
136
112
$ draw ->background ($ color );
137
113
});
138
114
139
- } else {
140
-
141
- $ canvas = $ this ->image_manager ->canvas (480 , 480 , $ color );
142
115
}
143
116
144
- $ canvas ->text ($ this ->name_initials , 240 , 240 , function ($ font ) {
117
+ $ canvas ->text ($ this ->nameInitials , 240 , 240 , function (Font $ font ) {
145
118
$ font ->file (__DIR__ . '/fonts/arial-bold.ttf ' );
146
119
$ font ->size (220 );
147
- $ font ->color ('#ffffff ' );
120
+ $ font ->color ('#fafafa ' );
148
121
$ font ->valign ('middle ' );
149
122
$ font ->align ('center ' );
150
123
});
151
124
152
125
return $ canvas ->resize ($ this ->size , $ this ->size );
153
126
}
154
127
155
- public function saveAs ($ path , $ mimetype = 'image/png ' , $ quality = 90 )
128
+ /**
129
+ * @param string $name
130
+ * @return string
131
+ */
132
+ private function getInitials (string $ name ): string
156
133
{
157
- if (empty ($ path ) || empty ($ mimetype ) || $ mimetype != "image/png " && $ mimetype != 'image/jpeg ' ){
158
- return false ;
134
+ $ nameParts = $ this ->break_name ($ name );
135
+
136
+ if (!$ nameParts ) {
137
+ return '' ;
159
138
}
160
139
161
- return @file_put_contents ($ path , $ this ->generate ()->encode ($ mimetype , $ quality ));
140
+ $ secondLetter = $ nameParts [1 ] ? $ this ->getFirstLetter ($ nameParts [1 ]) : '' ;
141
+
142
+ return $ this ->getFirstLetter ($ nameParts [0 ]) . $ secondLetter ;
143
+
162
144
}
163
145
164
- public function __toString ()
146
+ /**
147
+ * @param string $word
148
+ * @return string
149
+ */
150
+ private function getFirstLetter (string $ word ): string
165
151
{
166
- return ( string ) $ this -> generate ()-> encode ( ' data-url ' );
152
+ return mb_strtoupper ( trim ( mb_substr ( $ word , 0 , 1 , ' UTF-8 ' )) );
167
153
}
168
154
169
- public function break_words ($ name ) {
170
- $ temp_word_arr = explode (' ' , $ name );
171
- $ final_word_arr = array ();
172
- foreach ($ temp_word_arr as $ key => $ word ) {
173
- if ( $ word != "" && $ word != ", " ) {
174
- $ final_word_arr [] = $ word ;
175
- }
155
+ /**
156
+ * Save the generated Letter-Avatar as a file
157
+ *
158
+ * @param $path
159
+ * @param string $mimetype
160
+ * @param int $quality
161
+ * @return bool
162
+ */
163
+ public function saveAs ($ path , $ mimetype = 'image/png ' , $ quality = 90 ): bool
164
+ {
165
+ $ allowedMimeTypes = [
166
+ 'image/png ' ,
167
+ 'image/jpeg '
168
+ ];
169
+
170
+ if (empty ($ path ) || empty ($ mimetype ) || \in_array ($ mimetype , $ allowedMimeTypes , true )) {
171
+ return false ;
176
172
}
177
- return $ final_word_arr ;
173
+
174
+ return \is_int (@file_put_contents ($ path , $ this ->generate ()->encode ($ mimetype , $ quality )));
178
175
}
179
176
180
- protected function stringToColor ($ string )
177
+ /**
178
+ * @return string
179
+ */
180
+ public function __toString (): string
181
+ {
182
+ return (string )$ this ->generate ()->encode ('data-url ' );
183
+ }
184
+
185
+ /**
186
+ * Explodes Name into an array.
187
+ * The function will check if a part is , or blank
188
+ *
189
+ * @param string $name Name to be broken up
190
+ * @return array Name broken up to an array
191
+ */
192
+ private function break_name (string $ name ): array
193
+ {
194
+ $ words = \explode (' ' , $ name );
195
+ $ words = array_filter ($ words , function ($ word ) {
196
+ return $ word !=='' && $ word !== ', ' ;
197
+ });
198
+ return array_values ($ words );
199
+ }
200
+
201
+ /**
202
+ * @param string $string
203
+ * @return string
204
+ */
205
+ private function stringToColor (string $ string ): string
181
206
{
182
207
// random color
183
208
$ rgb = substr (dechex (crc32 ($ string )), 0 , 6 );
184
209
// make it darker
185
210
$ darker = 2 ;
186
211
list ($ R16 , $ G16 , $ B16 ) = str_split ($ rgb , 2 );
187
- $ R = sprintf (" %02X " , floor (hexdec ($ R16 ) / $ darker ));
188
- $ G = sprintf (" %02X " , floor (hexdec ($ G16 ) / $ darker ));
189
- $ B = sprintf (" %02X " , floor (hexdec ($ B16 ) / $ darker ));
212
+ $ R = sprintf (' %02X ' , floor (hexdec ($ R16 ) / $ darker ));
213
+ $ G = sprintf (' %02X ' , floor (hexdec ($ G16 ) / $ darker ));
214
+ $ B = sprintf (' %02X ' , floor (hexdec ($ B16 ) / $ darker ));
190
215
return '# ' . $ R . $ G . $ B ;
191
216
}
192
-
217
+
193
218
}
0 commit comments