import { checkImage } from "check-image-type" import { toUnit8Array } from "../../bit" /** * 检查一个 Blob 或 File 的类型是否为图片,并返回具体类型 * @returns 如果是图片,返回 { ext: string, mime: string },否则返回 undefined */ export async function checkImageType(blob: Blob | File): Promise<{ ext: string mime: string } | void> { // 优先根据 blob.type 判断 if (blob.type) { for (const key in IMAGE_TYPES) { if (IMAGE_TYPES[key].mime === blob.type) { return IMAGE_TYPES[key] } } } // 否则用 checkImage 检查 return checkImage(await toUnit8Array(blob)) } /** * 所有图片类型定义,Key 为类型(ext) */ const IMAGE_TYPES: any = { webp: { ext: "webp", mime: "image/webp" }, jpg: { ext: "jpg", mime: "image/jpeg" }, png: { ext: "png", mime: "image/png" }, bmp: { ext: "bmp", mime: "image/bmp" }, gif: { ext: "gif", mime: "image/gif" }, avif: { ext: "avif", mime: "image/avif" }, heic: { ext: "heic", mime: "image/heic" }, bpg: { ext: "bpg", mime: "image/bpg" }, ico: { ext: "ico", mime: "image/ico" }, psd: { ext: "psd", mime: "image/vnd.adobe.photoshop" }, jxr: { ext: "jxr", mime: "image/vnd.ms-photo" }, flif: { ext: "flif", mime: "image/flif" }, icns: { ext: "icns", mime: "image/icns" }, orf: { ext: "orf", mime: "image/x-olympus-orf" }, xcf: { ext: "xcf", mime: "image/x-xcf" }, rw2: { ext: "rw2", mime: "image/x-panasonic-rw2" }, ktx: { ext: "ktx", mime: "image/ktx" }, jxl: { ext: "jxl", mime: "image/jxl" }, cur: { ext: "cur", mime: "image/x-icon" }, raf: { ext: "raf", mime: "image/x-fujifilm-raf" }, } as const