Skip to content

Commit 320b290

Browse files
authored
Merge pull request #14 from micschwarz/master
Add code improvements and upgraded to PHP 7.0
2 parents b5824d3 + 8c3fec3 commit 320b290

File tree

3 files changed

+133
-87
lines changed

3 files changed

+133
-87
lines changed

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717
}
1818
],
1919
"require": {
20-
"php" : ">=5.5.0",
20+
"php" : ">=7.0.0",
2121
"ext-mbstring" : "*",
2222
"intervention/image": "^2.3"
2323
},
2424
"require-dev": {
25-
"phpunit/phpunit" : "4.*",
26-
"scrutinizer/ocular": "~1.1"
25+
"roave/security-advisories": "dev-master",
26+
"phpunit/phpunit" : "4.*"
2727
},
2828
"autoload": {
2929
"psr-4": {

example/index.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
require_once './../vendor/autoload.php';
3+
4+
use YoHang88\LetterAvatar\LetterAvatar;
5+
6+
$avatar = new LetterAvatar('Max Mustermann', 'circle', 64);
7+
?>
8+
9+
<!doctype html>
10+
<html>
11+
<head>
12+
<meta charset="UTF-8">
13+
<meta name="viewport"
14+
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
15+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
16+
<title>Example</title>
17+
</head>
18+
<body>
19+
<img src="<?php echo $avatar;?>" alt="">
20+
</body>
21+
</html>

src/LetterAvatar.php

Lines changed: 109 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -2,107 +2,94 @@
22

33
namespace YoHang88\LetterAvatar;
44

5+
use Intervention\Image\Gd\Font;
6+
use Intervention\Image\Gd\Shapes\CircleShape;
57
use Intervention\Image\ImageManager;
68

79
class LetterAvatar
810
{
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+
921
/**
1022
* @var string
1123
*/
12-
protected $name;
24+
private $name;
1325

1426

1527
/**
1628
* @var string
1729
*/
18-
protected $name_initials;
30+
private $nameInitials;
1931

2032

2133
/**
2234
* @var string
2335
*/
24-
protected $shape;
36+
private $shape;
2537

2638

2739
/**
2840
* @var int
2941
*/
30-
protected $size;
42+
private $size;
3143

3244
/**
3345
* @var ImageManager
3446
*/
35-
protected $image_manager;
36-
47+
private $imageManager;
3748

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)
3956
{
4057
$this->setName($name);
4158
$this->setImageManager(new ImageManager());
4259
$this->setShape($shape);
4360
$this->setSize($size);
4461
}
4562

46-
/**
47-
* @return string
48-
*/
49-
public function getName()
50-
{
51-
return $this->name;
52-
}
53-
5463
/**
5564
* @param string $name
5665
*/
57-
public function setName($name)
66+
private function setName(string $name)
5867
{
5968
$this->name = $name;
6069
}
6170

62-
/**
63-
* @return ImageManager
64-
*/
65-
public function getImageManager()
66-
{
67-
return $this->image_manager;
68-
}
6971

7072
/**
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
8074
*/
81-
public function getShape()
75+
private function setImageManager(ImageManager $imageManager)
8276
{
83-
return $this->shape;
77+
$this->imageManager = $imageManager;
8478
}
8579

8680
/**
8781
* @param string $shape
8882
*/
89-
public function setShape($shape)
83+
private function setShape(string $shape)
9084
{
9185
$this->shape = $shape;
9286
}
9387

94-
/**
95-
* @return int
96-
*/
97-
public function getSize()
98-
{
99-
return $this->size;
100-
}
10188

10289
/**
10390
* @param int $size
10491
*/
105-
public function setSize($size)
92+
private function setSize(int $size)
10693
{
10794
$this->size = $size;
10895
}
@@ -111,83 +98,121 @@ public function setSize($size)
11198
/**
11299
* @return \Intervention\Image\Image
113100
*/
114-
public function generate()
101+
private function generate(): \Intervention\Image\Image
115102
{
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';
129104

105+
$this->nameInitials = $this->getInitials($this->name);
130106
$color = $this->stringToColor($this->name);
131107

132-
if ($this->shape == 'circle') {
133-
$canvas = $this->image_manager->canvas(480, 480);
108+
$canvas = $this->imageManager->canvas(480, 480, $isCircle ? null : $color);
134109

135-
$canvas->circle(480, 240, 240, function ($draw) use ($color) {
110+
if ($isCircle) {
111+
$canvas->circle(480, 240, 240, function (CircleShape $draw) use ($color) {
136112
$draw->background($color);
137113
});
138114

139-
} else {
140-
141-
$canvas = $this->image_manager->canvas(480, 480, $color);
142115
}
143116

144-
$canvas->text($this->name_initials, 240, 240, function ($font) {
117+
$canvas->text($this->nameInitials, 240, 240, function (Font $font) {
145118
$font->file(__DIR__ . '/fonts/arial-bold.ttf');
146119
$font->size(220);
147-
$font->color('#ffffff');
120+
$font->color('#fafafa');
148121
$font->valign('middle');
149122
$font->align('center');
150123
});
151124

152125
return $canvas->resize($this->size, $this->size);
153126
}
154127

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
156133
{
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 '';
159138
}
160139

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+
162144
}
163145

164-
public function __toString()
146+
/**
147+
* @param string $word
148+
* @return string
149+
*/
150+
private function getFirstLetter(string $word): string
165151
{
166-
return (string) $this->generate()->encode('data-url');
152+
return mb_strtoupper(trim(mb_substr($word, 0, 1, 'UTF-8')));
167153
}
168154

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;
176172
}
177-
return $final_word_arr;
173+
174+
return \is_int(@file_put_contents($path, $this->generate()->encode($mimetype, $quality)));
178175
}
179176

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
181206
{
182207
// random color
183208
$rgb = substr(dechex(crc32($string)), 0, 6);
184209
// make it darker
185210
$darker = 2;
186211
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));
190215
return '#' . $R . $G . $B;
191216
}
192-
217+
193218
}

0 commit comments

Comments
 (0)