Implement libheif v1.19.3 instead of heic2any to prevent browser crashes with some iOS 18 heic image files

This commit is contained in:
schlagmichdoch 2024-11-11 20:39:46 +01:00
parent 00d2757fdc
commit fb6fe7ae61
9 changed files with 110 additions and 31 deletions

View file

@ -478,12 +478,7 @@ function getThumbnailAsDataUrl(file, width = undefined, height = undefined, qual
try {
if (file.type === "image/heif" || file.type === "image/heic") {
// browsers can't show heic files --> convert to jpeg before creating thumbnail
let blob = await fileToBlob(file);
file = await heic2any({
blob,
toType: "image/jpeg",
quality: quality
});
file = await heicToJpeg(file, 0.5);
}
let imageUrl = URL.createObjectURL(file);
@ -541,6 +536,32 @@ function getThumbnailAsDataUrl(file, width = undefined, height = undefined, qual
})
}
function initHeicConverter() {
return new Promise((resolve, reject) => {
fetch("libheif.wasm")
.then((res) => res.arrayBuffer())
.then(async (wasmBinary) => {
resolve(new HeifConvert(libheif({ wasmBinary: wasmBinary })));
})
.catch(reject);
});
}
async function heicToJpeg(file, quality) {
const heicConverter = await initHeicConverter();
console.log("Using libheif", heicConverter.libheif.heif_get_version());
const buffer = await file.arrayBuffer();
const canvas = await heicConverter.convert(buffer);
return new Promise(resolve => {
canvas.toBlob(blob => resolve(blob),
'image/jpeg',
quality
);
});
}
// Resolves returned promise when image is loaded and throws error if image cannot be shown
function waitUntilImageIsLoaded(imageUrl, timeout = 10000) {
return new Promise((resolve, reject) => {