Spaces:
Running
Running
File size: 1,523 Bytes
dd87944 | 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 36 37 38 39 40 41 42 43 44 45 46 47 48 | /** Résout une image (data URL ou https) en Blob pour copie / téléchargement. */
export async function fetchImageBlob(src) {
if (!src) throw new Error("Aucune source image");
const res = await fetch(src);
if (!res.ok) throw new Error("Impossible de récupérer l'image");
return res.blob();
}
export function imageDownloadFilename(blob, prefix = "emo-image") {
const ext = (blob?.type || "image/png").split("/")[1]?.replace("jpeg", "jpg") || "png";
return `${prefix}-${Date.now()}.${ext}`;
}
export async function copyImageFromSrc(src) {
const blob = await fetchImageBlob(src);
const type = blob.type || "image/png";
if (!navigator.clipboard?.write || typeof ClipboardItem === "undefined") {
throw new Error("Presse-papiers indisponible");
}
await navigator.clipboard.write([new ClipboardItem({ [type]: blob })]);
}
export function downloadImageBlob(blob, filename) {
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
a.remove();
URL.revokeObjectURL(url);
}
export async function downloadImageFromSrc(src, filename) {
try {
const blob = await fetchImageBlob(src);
downloadImageBlob(blob, filename || imageDownloadFilename(blob));
} catch {
const a = document.createElement("a");
a.href = src;
a.download = filename || `emo-image-${Date.now()}.png`;
a.rel = "noopener";
document.body.appendChild(a);
a.click();
a.remove();
}
}
|