Add onerror fallback for avatars and show a large one on Mein Profil

The Discord CDN image is now paired with a hidden letter-placeholder
sibling; if the <img> fails to load (stale avatar hash, a tracking
blocker on cdn.discordapp.com, etc.) it swaps to the placeholder
instead of leaving a broken image icon. Also adds the same avatar,
at a larger size, next to the heading on the Mein Profil page.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Maaxxs 2026-09-19 07:20:33 +02:00
parent fa05cf3ef0
commit 4eba8d1fef
3 changed files with 41 additions and 14 deletions

View file

@ -173,7 +173,10 @@ a:hover { color: var(--gold-hover); text-decoration: underline; }
border: 1px solid rgba(255, 255, 255, 0.15);
}
.hero .avatar { margin-bottom: 1rem; }
.avatar-wrap { display: inline-flex; }
.hero .avatar,
.hero .avatar-wrap { margin-bottom: 1rem; }
.app-content {
flex: 1;
@ -520,6 +523,12 @@ input:focus, select:focus, textarea:focus {
color: var(--gold);
}
.profile-header {
display: flex;
align-items: center;
gap: 1.25rem;
}
.profile-grid {
display: grid;
grid-template-columns: 1fr 1fr;

View file

@ -135,17 +135,12 @@ function discord_logo(int $size = 24): string
// Profilbild: Discord-Avatar, falls verknuepft und gesetzt, sonst ein
// Platzhalter mit dem Anfangsbuchstaben des Anzeigenamens auf einem
// nutzerfest zugeordneten Hintergrund aus einer zur Lumina-Optik passenden Palette.
// Der Platzhalter wird zusaetzlich immer mitgerendert (nur unsichtbar), damit
// bei einem fehlschlagenden Discord-Bild (abgelaufener Hash, Netzwerk-/
// Trackingblocker o.ae.) per onerror automatisch darauf zurueckgefallen wird,
// statt ein kaputtes Bild-Icon anzuzeigen.
function avatar_html(array $user, int $size = 40): string
{
if (!empty($user['discord_id']) && !empty($user['discord_avatar'])) {
$ext = str_starts_with((string) $user['discord_avatar'], 'a_') ? 'gif' : 'png';
$url = 'https://cdn.discordapp.com/avatars/' . rawurlencode((string) $user['discord_id'])
. '/' . rawurlencode((string) $user['discord_avatar']) . '.' . $ext . '?size=' . ($size * 2);
return '<img class="avatar" src="' . e($url) . '" alt="" width="' . $size . '" height="' . $size . '" '
. 'style="width:' . $size . 'px;height:' . $size . 'px;">';
}
static $palette = ['#4f6f52', '#6a4c93', '#1b6ca8', '#b5533c', '#8a6d3b', '#3f6c86', '#4a5568', '#a4443a'];
$name = trim((string) ($user['display_name'] ?? '')) !== '' ? $user['display_name'] : (string) ($user['username'] ?? '');
@ -158,8 +153,26 @@ function avatar_html(array $user, int $size = 40): string
$color = $palette[crc32($seed) % count($palette)];
$fontSize = round($size * 0.42, 1);
return '<span class="avatar avatar-placeholder" style="width:' . $size . 'px;height:' . $size . 'px;'
. 'background:' . e($color) . ';font-size:' . $fontSize . 'px;">' . e($letter) . '</span>';
$hasDiscordAvatar = !empty($user['discord_id']) && !empty($user['discord_avatar']);
$placeholderStyle = 'width:' . $size . 'px;height:' . $size . 'px;background:' . e($color) . ';font-size:' . $fontSize . 'px;'
. ($hasDiscordAvatar ? 'display:none;' : '');
$placeholder = '<span class="avatar avatar-placeholder" style="' . $placeholderStyle . '">' . e($letter) . '</span>';
if (!$hasDiscordAvatar) {
return $placeholder;
}
$ext = str_starts_with((string) $user['discord_avatar'], 'a_') ? 'gif' : 'png';
$url = 'https://cdn.discordapp.com/avatars/' . rawurlencode((string) $user['discord_id'])
. '/' . rawurlencode((string) $user['discord_avatar']) . '.' . $ext . '?size=' . ($size * 2);
return '<span class="avatar-wrap">'
. '<img class="avatar" src="' . e($url) . '" alt="" width="' . $size . '" height="' . $size . '" '
. 'style="width:' . $size . 'px;height:' . $size . 'px;" '
. "onerror=\"this.style.display='none';this.nextElementSibling.style.display='inline-flex';\">"
. $placeholder
. '</span>';
}
// Ordnet die (teils deutschen) Klassennamen aus dem Raid-Bot den Icon-Dateien

View file

@ -41,8 +41,13 @@ $pageTitle = 'Mein Profil';
include __DIR__ . '/includes/header.php';
?>
<div class="panel">
<h1>Mein Profil</h1>
<p class="text-muted">Benutzername: <strong><?= e($user['username']) ?></strong></p>
<div class="profile-header">
<?= avatar_html($user, 72) ?>
<div>
<h1>Mein Profil</h1>
<p class="text-muted">Benutzername: <strong><?= e($user['username']) ?></strong></p>
</div>
</div>
</div>
<div class="profile-grid">