Skip to content

Commit 829ee44

Browse files
committed
User initial avatars
#13492
1 parent b2a3c1f commit 829ee44

File tree

1 file changed

+60
-1
lines changed

1 file changed

+60
-1
lines changed

src/elements/User.php

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,26 @@ class User extends Element implements IdentityInterface
9999

100100
public const IMPERSONATE_KEY = 'Craft.UserSessionService.prevImpersonateUserId';
101101

102+
private static array $photoColors = [
103+
'red-100',
104+
'orange-200',
105+
'amber-200',
106+
'yellow-200',
107+
'lime-200',
108+
'green-200',
109+
'emerald-200',
110+
'teal-200',
111+
'cyan-200',
112+
'sky-200',
113+
'blue-200',
114+
'indigo-200',
115+
'violet-200',
116+
'purple-200',
117+
'fuchsia-200',
118+
'pink-100',
119+
'rose-200',
120+
];
121+
102122
// User statuses
103123
// -------------------------------------------------------------------------
104124

@@ -1266,7 +1286,46 @@ public function getThumbUrl(int $size): ?string
12661286
*/
12671287
protected function thumbSvg(): ?string
12681288
{
1269-
return file_get_contents(Craft::getAlias('@appicons/user.svg'));
1289+
$name = $this->getName();
1290+
$words = StringHelper::splitOnWords($name);
1291+
if (count($words) > 2) {
1292+
$words = [$words[0], end($words)];
1293+
}
1294+
$initials = implode('', array_map(fn($word) => mb_strtoupper(mb_substr($word, 0, 1)), $words));
1295+
1296+
// Choose a color based on the UUID
1297+
$uid = strtolower($this->uid ?? '00ff');
1298+
$color1Index = (int)base_convert(substr($uid, 0, 2), 16, 10);
1299+
$color2Index = (int)base_convert(substr($uid, 2, 2), 16, 10);
1300+
if ($color2Index >= $color1Index - 1 && $color2Index <= $color1Index + 1) {
1301+
$color2Index = $color1Index + 2;
1302+
}
1303+
$totalColors = count(self::$photoColors);
1304+
$color1 = self::$photoColors[$color1Index % $totalColors];
1305+
$color2 = self::$photoColors[$color2Index % $totalColors];
1306+
1307+
$gradientId = sprintf('gradient-%s', StringHelper::randomString(10));
1308+
1309+
return <<<XML
1310+
<svg version="1.1" baseProfile="full" width="100" height="100" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
1311+
<defs>
1312+
<linearGradient id="$gradientId" x1="0" y1="1" x2="1" y2="0">
1313+
<stop class="stop1" offset="0%" />
1314+
<stop class="stop2" offset="100%" />
1315+
</linearGradient>
1316+
</defs>
1317+
<style>
1318+
<![CDATA[
1319+
.stop1 { stop-color: var(--$color1); }
1320+
.stop2 { stop-color: var(--$color2); }
1321+
]]>
1322+
</style>
1323+
</defs>
1324+
<circle cx="50" cy="50" r="50" fill="url(#$gradientId)"/>
1325+
<text x="50" y="69" font-size="46" font-family="sans-serif" text-anchor="middle" fill="var(--white)" fill-opacity="0.4">$initials</text>
1326+
<text x="50" y="66" font-size="46" font-family="sans-serif" text-anchor="middle" fill="var(--black)" fill-opacity="0.65">$initials</text>
1327+
</svg>
1328+
XML;
12701329
}
12711330

12721331
/**

0 commit comments

Comments
 (0)