var D=(z,e={})=>new Promise((w,f)=>{let s=new FileReader;s.onload=u=>{var b;let n=new Image;n.src=(b=u.target)==null?void 0:b.result,n.onload=()=>{let i=n.width,g=n.height,c=i/g,r,a;e.width&&e.height?(r=e.width,a=e.height):e.width&&!e.height?(r=e.width,a=Math.round(e.width/c)):!e.width&&e.height?(a=e.height,r=Math.round(e.height*c)):(r=1200,a=630);let h=document.createElement("canvas");h.width=r,h.height=a;let R=h.getContext("2d");R.imageSmoothingQuality="high";let l,o,x=0,F=0;if(e.width&&e.height){let t=r/a;c>t?(o=g,l=g*t,x=(i-l)/2):(l=i,o=i/t,F=(g-o)/2)}else l=i,o=g;R.drawImage(n,x,F,l,o,0,0,r,a);let m=e.format||"jpeg",d=e.quality??(m==="jpeg"?.9:1);if(e.maxFileSizeKB){let t=h.toDataURL(`image/${m}`,d),I=Math.round(t.length*3/4/1024);for(;I>e.maxFileSizeKB&&d>.1;)d-=.1,t=h.toDataURL(`image/${m}`,d),I=Math.round(t.length*3/4/1024);w(t)}else{let t=h.toDataURL(`image/${m}`,d);w(t)}},n.onerror=i=>f(new Error("Image loading error: "+i))},s.onerror=u=>f(new Error("FileReader error: "+u)),s.readAsDataURL(z)});export{D as a};
