Skip to content

A self-hosted media server and file management system

feat: use blurhash for folder images

aminnausin27ba389489fa
diff --git a/package-lock.json b/package-lock.json
index acdab8db..7e3e94eb 100755
--- a/package-lock.json
+++ b/package-lock.json
@@ -11,6 +11,7 @@
"@vueuse/core": "^13.0.0",
"@vueuse/integrations": "^13.0.0",
"axios": "^1.9.0",
+ "blurhash": "^2.0.5",
"chart.js": "^4.4.7",
"focus-trap": "^7.5.4",
"laravel-echo": "^1.19.0",
@@ -3298,6 +3299,12 @@
"url": "https://github.com/sponsors/antfu"
}
},
+ "node_modules/blurhash": {
+ "version": "2.0.5",
+ "resolved": "https://registry.npmjs.org/blurhash/-/blurhash-2.0.5.tgz",
+ "integrity": "sha512-cRygWd7kGBQO3VEhPiTgq4Wc43ctsM+o46urrmPOiuAe+07fzlSB9OJVdpgDL0jPqXUVQ9ht7aq7kxOeJHRK+w==",
+ "license": "MIT"
+ },
"node_modules/boolbase": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/boolbase/-/boolbase-1.0.0.tgz",
diff --git a/package.json b/package.json
index fbb64871..3884c25e 100755
--- a/package.json
+++ b/package.json
@@ -96,6 +96,7 @@
"@vueuse/core": "^13.0.0",
"@vueuse/integrations": "^13.0.0",
"axios": "^1.9.0",
+ "blurhash": "^2.0.5",
"chart.js": "^4.4.7",
"focus-trap": "^7.5.4",
"laravel-echo": "^1.19.0",
diff --git a/resources/js/components/cards/data/explore/BrowseFolderCard.vue b/resources/js/components/cards/data/explore/BrowseFolderCard.vue
index 822b7bce..6eb59abc 100644
--- a/resources/js/components/cards/data/explore/BrowseFolderCard.vue
+++ b/resources/js/components/cards/data/explore/BrowseFolderCard.vue
@@ -6,6 +6,7 @@ import { FLAGS } from '@/config/featureFlags';
import { cn } from '@aminnausin/cedar-ui';
import PlayerOSDBase from '@/components/video/OSD/PlayerOSDBase.vue';
+import BlurhashImage from '@/components/lazy/BlurhashImage.vue';
import LazyImage from '@/components/lazy/LazyImage.vue';
import IconFolder from '@/components/icons/IconFolder.vue';
@@ -36,12 +37,14 @@ const scrollIntoView = (e: FocusEvent) => {
:to="`/${folder.category_id}/${folder.id}/details`"
:class="cn('relative overflow-clip rounded-t-md shadow-sm', { 'rounded-b-md': FLAGS.USE_TRANSPARENT_HOME_CARDS })"
>
- <LazyImage
+ <component
+ :is="folder.series?.poster_image?.blur_hash ? BlurhashImage : LazyImage"
:src="folder.series?.poster_image?.path ?? handleStorageURL(folder.series?.thumbnail_url) ?? '/storage/thumbnails/default.webp'"
:class="'aspect-2-3 w-full object-cover'"
:alt="folder.title"
:fetch-priority="eagerLoad ? 'high' : 'auto'"
:loading="eagerLoad ? 'eager' : 'lazy'"
+ :blurhash="folder.series?.poster_image?.blur_hash"
/>
<slot name="overlay">
<PlayerOSDBase
diff --git a/resources/js/components/home/HomeSpotlight.vue b/resources/js/components/home/HomeSpotlight.vue
index e7b9f841..06ecd0f2 100644
--- a/resources/js/components/home/HomeSpotlight.vue
+++ b/resources/js/components/home/HomeSpotlight.vue
@@ -8,7 +8,7 @@ import { cn, useSwipeHandler } from '@aminnausin/cedar-ui';
import { ButtonBase } from '@/components/cedar-ui/button';
import ButtonOverlay from '@/components/buttons/ButtonOverlay.vue';
-import LazyImage from '@/components/lazy/LazyImage.vue';
+import BlurhashImage from '@/components/lazy/BlurhashImage.vue';
import MediaTag from '@/components/labels/MediaTag.vue';
import ProiconsInfoSquare from '~icons/proicons/info-square';
@@ -136,7 +136,7 @@ onBeforeUnmount(() => timer && clearTimeout(timer));
<template>
<div
v-if="isLoading"
- class="ring-r-default/5 content-auto bg-foreground-3/50 dark:bg-surface-2 @container flex h-80 w-full animate-pulse items-end gap-4 rounded-xl p-3 ring-1 [contain-intrinsic-size:auto_300px] sm:h-[clamp(200px,28vw,380px)]"
+ class="ring-r-default/5 bg-foreground-3/50 dark:bg-surface-2 @container flex h-80 w-full animate-pulse items-end gap-4 rounded-xl p-3 ring-1 sm:h-[clamp(200px,28vw,380px)]"
>
<div class="mt-auto flex h-fit w-full flex-col flex-wrap items-center justify-center gap-x-4 gap-y-2 @md:flex-row @md:items-end @lg:flex-nowrap @lg:gap-x-12">
<div class="mt-auto flex h-fit flex-1 flex-col items-center gap-4 hover:text-white/90 @md:flex-row @md:items-end">
@@ -165,7 +165,15 @@ onBeforeUnmount(() => timer && clearTimeout(timer));
>
<Transition :name="hasMounted ? 'banner-fade' : ''">
<div :key="activeFolder?.id" class="absolute inset-0 -z-10">
- <LazyImage loading="eager" decoding="async" fetch-priority="high" class="size-full rounded-xl object-cover" :alt="activeFolder?.title" :src="bannerSrc" />
+ <BlurhashImage
+ loading="eager"
+ decoding="async"
+ fetch-priority="high"
+ class="size-full rounded-xl object-cover"
+ :alt="activeFolder?.title"
+ :src="bannerSrc"
+ :blurhash="activeFolder.series?.banner_image?.blur_hash ?? activeFolder.series?.poster_image?.blur_hash"
+ />
</div>
</Transition>
@@ -183,11 +191,12 @@ onBeforeUnmount(() => timer && clearTimeout(timer));
class="flex size-full flex-col items-center justify-end gap-3 @md:flex-row @md:items-end @md:justify-start"
>
<RouterLink :to="activeUrl" class="peer" title="View folder details">
- <LazyImage
+ <BlurhashImage
:alt="`${activeFolder.title} poster`"
:class="cn('aspect-2-3 duration-input w-full rounded-lg object-cover shadow-sm transition-shadow')"
:src="activeFolder?.series?.poster_image?.path ?? handleStorageURL(activeFolder?.series?.thumbnail_url) ?? '/storage/thumbnails/default.webp'"
:wrapper-class="cn('shrink-0 relative', 'w-32 sm:w-24 2xl:w-30 3xl:w-36 opacity-100 ease-in h-fit select-none ')"
+ :blurhash="activeFolder.series?.poster_image?.blur_hash"
/>
</RouterLink>
diff --git a/resources/js/components/lazy/BlurhashImage.vue b/resources/js/components/lazy/BlurhashImage.vue
new file mode 100644
index 00000000..0fce9d43
--- /dev/null
+++ b/resources/js/components/lazy/BlurhashImage.vue
@@ -0,0 +1,164 @@
+<script setup lang="ts">
+import type { HTMLAttributes, ImgHTMLAttributes } from 'vue';
+
+import { computed, nextTick, onBeforeUnmount, onMounted, ref, useAttrs, watch } from 'vue';
+import { SvgSpinners90RingWithBg } from '@/components/cedar-ui/icons';
+import { decode } from 'blurhash';
+import { cn } from '@aminnausin/cedar-ui';
+
+import ProIconsPhotoOff from '@/components/icons/ProIconsPhotoOff.vue';
+
+defineOptions({ inheritAttrs: false });
+
+const props = withDefaults(
+ defineProps<{
+ src?: string;
+ alt?: string;
+ fetchPriority?: ImgHTMLAttributes['fetchpriority'];
+ wrapperClass?: HTMLAttributes['class'];
+ animate?: boolean;
+ blurhash?: string;
+ rootMargin?: string;
+ }>(),
+ {
+ rootMargin: '144px',
+ animate: true,
+ },
+);
+
+const attrs = useAttrs();
+
+const containerRef = ref<HTMLDivElement>();
+const canvasRef = ref<HTMLCanvasElement>();
+
+const isLoading = ref(false);
+const isLoaded = ref(false);
+const isError = ref(false);
+const isVisible = ref(false);
+
+let observer: IntersectionObserver | null = null;
+
+const activeSrc = computed(() => (isVisible.value ? props.src : undefined));
+
+const blurhash = ref(props.blurhash);
+
+const drawBlurhash = () => {
+ if (!blurhash.value || !canvasRef.value) return;
+
+ try {
+ const w = 32;
+ const h = 32;
+ const pixels = decode(blurhash.value, w, h);
+ const ctx = canvasRef.value.getContext('2d');
+
+ if (!ctx) throw new Error('Blurhash canvas failed to load');
+
+ canvasRef.value.width = w;
+ canvasRef.value.height = h;
+
+ const imageData = ctx.createImageData(w, h);
+ imageData.data.set(pixels);
+ ctx.putImageData(imageData, 0, 0);
+ } catch {
+ // fall back to spinner
+ blurhash.value = undefined;
+ }
+};
+
+const startLoadingIfVisible = () => {
+ if (isVisible.value) isLoading.value = !!props.src;
+};
+
+const setupObserver = () => {
+ observer?.disconnect();
+ observer = null;
+
+ if (!containerRef.value || typeof IntersectionObserver === 'undefined') {
+ isVisible.value = true;
+ startLoadingIfVisible();
+ return;
+ }
+
+ // starts loading image when within {rootMargin} of the viewport and kills observer
+ observer = new IntersectionObserver(
+ ([entry]) => {
+ if (!entry.isIntersecting) return;
+
+ isVisible.value = true;
+ startLoadingIfVisible();
+ observer?.disconnect();
+ observer = null;
+ },
+ { rootMargin: props.rootMargin },
+ );
+
+ observer.observe(containerRef.value);
+};
+
+watch(
+ () => props.src,
+ () => {
+ isError.value = false;
+ isLoaded.value = false;
+ startLoadingIfVisible();
+ },
+);
+
+watch(
+ () => props.blurhash,
+ (val) => {
+ blurhash.value = val;
+ if (val) drawBlurhash();
+ },
+);
+
+onMounted(async () => {
+ if (blurhash.value) drawBlurhash();
+
+ await nextTick();
+ setupObserver();
+});
+
+onBeforeUnmount(() => {
+ observer?.disconnect();
+ observer = null;
+});
+</script>
+<template>
+ <div ref="containerRef" :class="cn('relative block h-full w-full', wrapperClass)">
+ <canvas v-if="blurhash" ref="canvasRef" v-show="!isError" :class="cn('absolute inset-0 z-0 h-full w-full object-cover', $attrs.class)" aria-hidden="true" />
+ <div v-else v-show="src && isLoading && !isError" class="absolute inset-0 flex items-center justify-center">
+ <SvgSpinners90RingWithBg class="size-4" />
+ </div>
+ <div v-show="isError && !isLoading" class="absolute inset-0 flex flex-wrap items-center justify-center gap-1 p-2 pb-6 text-center text-xs">
+ <ProIconsPhotoOff class="size-5" /><span> Image failed to load ({{ alt }}) </span>
+ </div>
+ <img
+ v-bind="attrs"
+ decoding="async"
+ :fetchpriority="fetchPriority"
+ :alt="isError || !isLoaded ? '' : alt"
+ :src="activeSrc"
+ :class="['lazy-image relative', { 'lazy-image transition-opacity duration-300 ease-in-out': animate }, { loaded: isLoaded }]"
+ @load="
+ isLoading = false;
+ isError = false;
+ isLoaded = true;
+ "
+ @error="
+ isError = true;
+ isLoading = false;
+ "
+ />
+ </div>
+</template>
+<style lang="css" scoped>
+.lazy-image {
+ opacity: 0;
+}
+
+.lazy-image.loaded {
+ opacity: 1;
+ z-index: 1;
+}
+</style>