File size: 815 Bytes
83ae5d2 3e3eb0a 83ae5d2 3e3eb0a 83ae5d2 9ec41c1 83ae5d2 3e3eb0a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | import { Jimp } from "jimp";
const SUPPORTED_MIME_TYPES = new Set([
"image/jpeg",
"image/png",
"image/gif",
"image/bmp",
"image/tiff",
"image/webp",
]);
export async function loadImageFromUrl(url: string) {
const response = await fetch(url, {
headers: {
Accept: "image/jpeg, image/png, image/gif, image/bmp, image/tiff, image/webp, */*",
},
});
if (!response.ok) {
throw new Error(`Failed to fetch: ${response.status}`);
}
const contentType = response.headers.get("Content-Type") ?? "";
const mime = contentType.split(";")[0].trim().toLowerCase();
if (mime && !SUPPORTED_MIME_TYPES.has(mime)) {
throw new Error("Failed to fetch");
}
const arrayBuffer = await response.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
return Jimp.read(buffer);
}
|