fix: conversion HEIC — ajout fallback img+canvas pour Chrome macOS
This commit is contained in:
@@ -46,15 +46,43 @@ export async function convertHeicToJpeg(blob: Blob): Promise<Blob> {
|
||||
}
|
||||
|
||||
async function convertNative(blob: Blob): Promise<Blob> {
|
||||
// Force le type MIME pour que le navigateur reconnaisse le format
|
||||
const typed = blob.type ? blob : new Blob([blob], { type: 'image/heic' })
|
||||
const bitmap = await createImageBitmap(typed)
|
||||
const canvas = document.createElement('canvas')
|
||||
canvas.width = bitmap.width
|
||||
canvas.height = bitmap.height
|
||||
canvas.getContext('2d')!.drawImage(bitmap, 0, 0)
|
||||
bitmap.close()
|
||||
|
||||
// Tentative 1 : createImageBitmap (Safari 14+)
|
||||
try {
|
||||
const bitmap = await createImageBitmap(typed)
|
||||
const canvas = document.createElement('canvas')
|
||||
canvas.width = bitmap.width
|
||||
canvas.height = bitmap.height
|
||||
canvas.getContext('2d')!.drawImage(bitmap, 0, 0)
|
||||
bitmap.close()
|
||||
return await canvasToBlob(canvas)
|
||||
} catch {
|
||||
// Pas supporté — essai suivant
|
||||
}
|
||||
|
||||
// Tentative 2 : <img> + canvas (Chrome sur macOS — utilise le décodeur HEIC natif de macOS)
|
||||
return new Promise<Blob>((resolve, reject) => {
|
||||
const url = URL.createObjectURL(typed)
|
||||
const img = new Image()
|
||||
img.onload = () => {
|
||||
const canvas = document.createElement('canvas')
|
||||
canvas.width = img.naturalWidth
|
||||
canvas.height = img.naturalHeight
|
||||
canvas.getContext('2d')!.drawImage(img, 0, 0)
|
||||
URL.revokeObjectURL(url)
|
||||
canvasToBlob(canvas).then(resolve).catch(reject)
|
||||
}
|
||||
img.onerror = () => {
|
||||
URL.revokeObjectURL(url)
|
||||
reject(new Error('Décodage HEIC non supporté par ce navigateur'))
|
||||
}
|
||||
img.src = url
|
||||
})
|
||||
}
|
||||
|
||||
function canvasToBlob(canvas: HTMLCanvasElement): Promise<Blob> {
|
||||
return new Promise((resolve, reject) => {
|
||||
canvas.toBlob(
|
||||
b => (b ? resolve(b) : reject(new Error('canvas.toBlob a retourné null'))),
|
||||
'image/jpeg',
|
||||
|
||||
Reference in New Issue
Block a user