feat(hero): pick the best Jellyfin artwork for the home hero

Jellyfin stores several images per item. Ask /Items/{id}/Images for their real
sizes and use the biggest wide image (Backdrop, then Thumb) for the 16:9 hero.
Poster-only items are no longer stretched: the poster is blurred into an ambient
background with the sharp poster displayed beside the text. The slide element is
kept mounted across changes so the previous artwork stays until the next loads.
This commit is contained in:
1xDed committed 2026-11-27 09:20:26 +03:00
1 parent 3131c1f905
commit 4c9dd86962
4 files changed
+235 -33

No files matched your search

+158
View File
@@ -0,0 +1,158 @@
import type { Api } from "@jellyfin/sdk";
import type {
BaseItemDto,
ImageInfo,
ImageType,
} from "@jellyfin/sdk/lib/generated-client";
import { getImageApi } from "@jellyfin/sdk/lib/utils/api/image-api";
import { queryOptions } from "@tanstack/react-query";
export type HeroArtwork = {
url: string;
/** Primary (poster) is portrait; it must not be cropped into a 16:9 hero. */
isPortrait: boolean;
/** BlurHash for the chosen image, used as an instant placeholder. */
blurhash?: string;
};
/** All images Jellyfin actually stores for an item (with real resolutions). */
export const getItemImagesQueryOptions = (
api: Api | null | undefined,
itemId: string | undefined | null,
) =>
queryOptions({
queryKey: ["item", "images", itemId],
queryFn: async () => {
if (!api || !itemId) return [];
const result = await getImageApi(api).getItemImageInfos({ itemId });
return result.data;
},
enabled: Boolean(api && itemId),
staleTime: 30 * 60_000,
});
/** Prefer the biggest wide image Jellyfin has; Backdrop wins ties over Thumb. */
const pickWideImage = (images: ImageInfo[]): ImageInfo | undefined => {
const wide = images.filter((image) => {
if (image.ImageType !== "Backdrop" && image.ImageType !== "Thumb") {
return false;
}
const width = image.Width ?? 0;
const height = image.Height ?? 0;
return height > 0 && width / height >= 1.3;
});
wide.sort((a, b) => {
const area = (i: ImageInfo) => (i.Width ?? 0) * (i.Height ?? 0);
const diff = area(b) - area(a);
if (diff !== 0) return diff;
return a.ImageType === "Backdrop" ? -1 : 1;
});
return wide[0];
};
const imageUrl = (
api: Api,
itemId: string,
type: ImageType,
tag: string | null | undefined,
params: { maxWidth?: number; maxHeight?: number },
) => {
const query = new URLSearchParams();
if (tag) query.set("tag", tag);
if (params.maxWidth) query.set("maxWidth", String(params.maxWidth));
if (params.maxHeight) query.set("maxHeight", String(params.maxHeight));
query.set("quality", "90");
return `${api.basePath}/Items/${itemId}/Images/${type}?${query.toString()}`;
};
const blurhashFor = (
item: BaseItemDto,
type: ImageType,
tag: string | null | undefined,
) => (tag ? item.ImageBlurHashes?.[type]?.[tag] ?? undefined : undefined);
/**
* Best artwork for the home hero (a wide 16:9 area).
*
* Jellyfin usually stores several images per item; the player/hero should use
* a wide one: Backdrop, then Thumb (often a 4K still). A Primary poster is 2:3,
* so it is only used when nothing wide exists, and the caller renders it as a
* poster on a blurred background instead of stretching it.
*/
export const resolveHeroArtwork = (
api: Api | undefined,
item: BaseItemDto | undefined | null,
images: ImageInfo[] | undefined,
): HeroArtwork | undefined => {
if (!api || !item?.Id) return undefined;
const wide = images?.length ? pickWideImage(images) : undefined;
if (wide) {
return {
url: imageUrl(api, item.Id, wide.ImageType as ImageType, wide.ImageTag, {
maxWidth: 1920,
}),
isPortrait: false,
blurhash: blurhashFor(item, wide.ImageType as ImageType, wide.ImageTag),
};
}
// Fall back to tags while /Images is still loading (or for servers that
// don't return image infos).
if (item.BackdropImageTags?.length) {
return {
url: imageUrl(api, item.Id, "Backdrop", item.BackdropImageTags[0], {
maxWidth: 1920,
}),
isPortrait: false,
blurhash: blurhashFor(item, "Backdrop", item.BackdropImageTags[0]),
};
}
if (item.ImageTags?.Thumb) {
return {
url: imageUrl(api, item.Id, "Thumb", item.ImageTags.Thumb, {
maxWidth: 1920,
}),
isPortrait: false,
blurhash: blurhashFor(item, "Thumb", item.ImageTags.Thumb),
};
}
if (item.ImageTags?.Primary) {
return {
url: imageUrl(api, item.Id, "Primary", item.ImageTags.Primary, {
maxHeight: 1400,
}),
isPortrait: true,
blurhash: blurhashFor(item, "Primary", item.ImageTags.Primary),
};
}
if (item.ParentBackdropItemId && item.ParentBackdropImageTags?.length) {
return {
url: imageUrl(
api,
item.ParentBackdropItemId,
"Backdrop",
item.ParentBackdropImageTags[0],
{ maxWidth: 1920 },
),
isPortrait: false,
blurhash: blurhashFor(
item,
"Backdrop",
item.ParentBackdropImageTags[0],
),
};
}
return undefined;
};
/** Sharp poster URL for the poster-on-blurred-background hero treatment. */
export const getHeroPosterUrl = (
api: Api | undefined,
item: BaseItemDto | undefined | null,
) => {
if (!api || !item?.Id || !item.ImageTags?.Primary) return undefined;
return imageUrl(api, item.Id, "Primary", item.ImageTags.Primary, {
maxHeight: 900,
});
};