function e(e){let t=e.indexOf(`,`),n=e.length-(t+1);return Math.floor(n*.75)}function t(t,n,r=1920){return new Promise((i,a)=>{let o=new FileReader;o.onload=t=>{let o=new Image;o.onload=()=>{let t=document.createElement(`canvas`),{width:s,height:c}=o,l=Math.min(r/s,r/c,1);s=Math.floor(s*l),c=Math.floor(c*l),t.width=s,t.height=c;let u=t.getContext(`2d`);if(!u){a(Error(`Canvas not supported`));return}u.fillStyle=`#fff`,u.fillRect(0,0,s,c),u.drawImage(o,0,0,s,c);let d=.92,f=t.toDataURL(`image/jpeg`,d);for(;e(f)>n&&d>.1;)d-=.08,f=t.toDataURL(`image/jpeg`,d);i(f)},o.onerror=a,o.src=t.target?.result},o.onerror=a,o.readAsDataURL(t)})}function n(t,n){return new Promise((r,i)=>{let a=new Image;a.crossOrigin=`anonymous`,a.onload=()=>{let t=document.createElement(`canvas`),{width:o,height:s}=a,c=Math.min(n.maxDim/o,n.maxDim/s,1);o=Math.floor(o*c),s=Math.floor(s*c),t.width=o,t.height=s;let l=t.getContext(`2d`);if(!l){i(Error(`Canvas not supported`));return}l.fillStyle=`#fff`,l.fillRect(0,0,o,s),l.drawImage(a,0,0,o,s);let u=.92,d=t.toDataURL(`image/jpeg`,u),f=Math.max(.02,(.92-n.minQuality)/20);for(;e(d)>n.maxSize&&u>n.minQuality+f/2;)u-=f,d=t.toDataURL(`image/jpeg`,u);r(d)},a.onerror=()=>i(Error(`图片加载失败`)),a.src=t})}export{n,e as r,t};