Convert and resize avatars to fit the displayed images

This commit is contained in:
2023-05-02 17:31:16 +02:00
parent fbaedaf45b
commit 736b8498af
10 changed files with 797 additions and 84 deletions

View File

@ -3,10 +3,29 @@
export let account: Account;
let avatarDescription: string;
let sourceSetHtml: string;
$: avatarDescription = `Avatar for ${account.acct}`;
$: {
const m = new Map<string, string[]>();
for (const resizedAvatar of account.resizedAvatars ?? []) {
const extension = resizedAvatar.file.split('.').pop();
const mime = extension ? `image/${extension}` : 'application/octet-stream';
const sourceSetEntry = `${resizedAvatar.file} ${resizedAvatar.sizeDescriptor}`;
m.set(mime, [...(m.get(mime) || []), sourceSetEntry]);
}
let html = '';
for (const entry of m.entries()) {
const srcset = entry[1].join(', ');
html += `<source srcset="${srcset}" type="${entry[0]}" />`;
}
sourceSetHtml = html;
}
</script>
<img src={account.avatar} alt={avatarDescription} />
<picture>
{@html sourceSetHtml}
<img src={account.avatar} alt={avatarDescription} loading="lazy" />
</picture>
<style>
img {