// A famous person's photo: one stored version, served by the backend through // the /api/v1 proxy; next/image makes the sized versions from it (and // Cloudflare caches those), so no sizes are stored. import type { CelebrityPhoto } from "./types"; /** The stored photo's address on this site (also the source of next/image). */ export function photoSrc(photo: Pick): string { return `/api/v1/public/photos/${photo.key.split("/").map(encodeURIComponent).join("/")}`; } /** CSS `object-position` that keeps the face point in view when the photo is cropped. */ export function objectPosition(photo: Pick): string { return `${(photo.focus_x * 100).toFixed(1)}% ${(photo.focus_y * 100).toFixed(1)}%`; } /** * Where a `width`×`height` photo goes in a `boxW`×`boxH` frame so that it * covers it with the face point as near the middle as the edges allow (for * the preview image, whose renderer has no object-position). */ export function coverBox( photo: Pick, boxW: number, boxH: number, ): { width: number; height: number; left: number; top: number } { const scale = Math.max(boxW / photo.width, boxH / photo.height); const width = Math.ceil(photo.width * scale); const height = Math.ceil(photo.height * scale); const clamp = (v: number, min: number) => Math.round(Math.min(0, Math.max(min, v))); return { width, height, left: clamp(boxW / 2 - photo.focus_x * width, boxW - width), top: clamp(boxH / 2 - photo.focus_y * height, boxH - height), }; }