Files
llink/js/src/lib/image-thumbnail.ts
T
Arjun PatelandGitHub d6280439d0 feat: support file and image attachments (#98)
* upload & view attachments to particles

* allow download of attachments
2026-03-30 10:31:24 -07:00

25 lines
834 B
TypeScript

/**
* Downscale an image file to a thumbnail and return an object URL.
* Returns undefined for non-image files.
* Caller is responsible for revoking the URL via URL.revokeObjectURL().
*/
export async function createImageThumbnail(
file: File,
maxDim = 200,
): Promise<string | undefined> {
if (!file.type.startsWith("image/")) return undefined;
const bitmap = await createImageBitmap(file);
const scale = Math.min(1, maxDim / Math.max(bitmap.width, bitmap.height));
const w = Math.round(bitmap.width * scale);
const h = Math.round(bitmap.height * scale);
const canvas = new OffscreenCanvas(w, h);
const ctx = canvas.getContext("2d")!;
ctx.drawImage(bitmap, 0, 0, w, h);
bitmap.close();
const blob = await canvas.convertToBlob({ type: "image/jpeg", quality: 0.7 });
return URL.createObjectURL(blob);
}