60 lines
1.5 KiB
Svelte
60 lines
1.5 KiB
Svelte
<script lang="ts">
|
|
import { findClosestNumber } from '$lib/util.js'
|
|
import { createAvatar } from '@dicebear/core'
|
|
import * as initials from '@dicebear/initials'
|
|
|
|
const sizes = [16, 32, 48, 64, 128, 256, 512]
|
|
|
|
export let url: string | undefined
|
|
export let alt: string = ''
|
|
export let title: string = ''
|
|
export let circle: boolean = true
|
|
|
|
export let width: number
|
|
export let res: number | undefined = undefined
|
|
|
|
const optimizeUrl = (url: string | undefined): string | undefined => {
|
|
if (url === undefined) return
|
|
|
|
try {
|
|
const urlObj = new URL(url)
|
|
urlObj.searchParams.append('format', 'webp')
|
|
urlObj.searchParams.append(
|
|
'thumbnail',
|
|
findClosestNumber(sizes, res || width).toString()
|
|
)
|
|
|
|
return urlObj.toString()
|
|
} catch (e) {
|
|
return undefined
|
|
}
|
|
}
|
|
|
|
$: optimizedURL = optimizeUrl(url)
|
|
</script>
|
|
|
|
{#if optimizedURL}
|
|
<img
|
|
src={optimizedURL}
|
|
{alt}
|
|
{width}
|
|
{title}
|
|
loading="lazy"
|
|
class="aspect-square object-cover overflow-hidden {$$props.class} flex-shrink-0"
|
|
style="width: {width}px; height: {width}px;"
|
|
class:rounded-full={circle}
|
|
class:rounded-md={!circle}
|
|
/>
|
|
{:else}
|
|
<div
|
|
style="width: {width}px; height: {width}px;"
|
|
class="aspect-square object-cover overflow-hidden {$$props.class} flex-shrink-0"
|
|
class:rounded-full={circle}
|
|
class:rounded-md={!circle}
|
|
>
|
|
{@html createAvatar(initials, {
|
|
seed: alt,
|
|
}).toString()}
|
|
</div>
|
|
{/if}
|