import type { Area } from "react-easy-crop"; function loadImage(url: string): Promise { return new Promise((resolve, reject) => { const image = new Image(); image.addEventListener("load", () => resolve(image)); image.addEventListener("error", reject); image.crossOrigin = "anonymous"; image.src = url; }); } /** * Renders the cropped region of an image onto an off-screen canvas and * returns it as a Blob — the standard react-easy-crop output recipe. */ export async function getCroppedImageBlob( imageSrc: string, cropAreaPixels: Area, mimeType: string ): Promise { const image = await loadImage(imageSrc); const canvas = document.createElement("canvas"); canvas.width = cropAreaPixels.width; canvas.height = cropAreaPixels.height; const ctx = canvas.getContext("2d"); if (!ctx) { throw new Error("Canvas 2D context not available"); } ctx.drawImage( image, cropAreaPixels.x, cropAreaPixels.y, cropAreaPixels.width, cropAreaPixels.height, 0, 0, cropAreaPixels.width, cropAreaPixels.height ); return new Promise((resolve, reject) => { canvas.toBlob( (blob) => { if (blob) resolve(blob); else reject(new Error("Canvas produced an empty blob")); }, mimeType, 0.92 ); }); }