export async function getCroppedImg(imageSrc: string, pixelCrop: any): Promise { // SSR check - this function should only run on the client if (typeof document === 'undefined') { throw new Error('Image cropping is only available in the browser') } const image = await new Promise((resolve, reject) => { const img = new Image(); img.src = imageSrc; img.onload = () => resolve(img); img.onerror = reject; }); const canvas = document.createElement("canvas"); canvas.width = pixelCrop.width; canvas.height = pixelCrop.height; const ctx = canvas.getContext("2d"); ctx?.drawImage( image, pixelCrop.x, pixelCrop.y, pixelCrop.width, pixelCrop.height, 0, 0, pixelCrop.width, pixelCrop.height ); return new Promise((resolve) => { canvas.toBlob((blob) => { resolve(blob!); }, "image/png"); }); }