35 lines
1.3 KiB
TypeScript
35 lines
1.3 KiB
TypeScript
function isHeifConsoleMessage(msg: string): boolean {
|
|
return /HEIF|parse HEIF|Could not parse/i.test(msg);
|
|
}
|
|
|
|
/**
|
|
* Convert HEIC/HEIF blob to JPEG. Uses heic2any only when this function is called
|
|
* (dynamic import), so the receipt page can load even if heic2any fails to resolve.
|
|
* Suppresses the library's "Could not parse HEIF file" console output during conversion.
|
|
*/
|
|
export async function convertHeicBlobToJpeg(blob: Blob): Promise<Blob> {
|
|
const originalError = console.error;
|
|
const originalWarn = console.warn;
|
|
console.error = (...args: unknown[]) => {
|
|
const msg = args[0]?.toString?.() ?? "";
|
|
if (isHeifConsoleMessage(msg)) return;
|
|
originalError.apply(console, args);
|
|
};
|
|
console.warn = (...args: unknown[]) => {
|
|
const msg = args[0]?.toString?.() ?? "";
|
|
if (isHeifConsoleMessage(msg)) return;
|
|
originalWarn.apply(console, args);
|
|
};
|
|
try {
|
|
const mod = await import(/* @vite-ignore */ "heic2any");
|
|
const heic2any = (mod as unknown as {
|
|
default: (opts: { blob: Blob; toType: string; quality?: number }) => Promise<Blob | Blob[]>;
|
|
}).default;
|
|
const out = await heic2any({ blob, toType: "image/jpeg", quality: 0.92 });
|
|
return Array.isArray(out) ? out[0] : out;
|
|
} finally {
|
|
console.error = originalError;
|
|
console.warn = originalWarn;
|
|
}
|
|
}
|