1const d={maxWidth:2e3,maxHeight:2e3,quality:.85,format:"webp"};function h(e){return e instanceof File&&e.name.toLowerCase().endsWith(".gif")||e.type==="image/gif"}async function L(e,a){const n={...d,...a},t=e.size;if(h(e)){const o=e instanceof File?e.name:"image.gif";return{blob:e,originalSize:t,optimizedSize:t,filename:o}}try{const o=await f(e),{width:i,height:r}=w(o.naturalWidth,o.naturalHeight,n.maxWidth,n.maxHeight),c=document.createElement("canvas");c.width=i,c.height=r;const s=c.getContext("2d");if(!s)throw new Error("Could not get canvas context");s.drawImage(o,0,0,i,r);const m=await b(c,n.format,n.quality),g=m.size<t?m:e,l=g.size,u=`${(e instanceof File?e.name:"image").replace(/\.[^/.]+$/,"")}.${n.format}`;return{blob:g,originalSize:t,optimizedSize:l,filename:u}}catch(o){console.error("Image optimization failed:",o);const i=e instanceof File?e.name:"image.jpg";return{blob:e,originalSize:t,optimizedSize:t,filename:i}}}function f(e){return new Promise((a,n)=>{const t=new Image;t.onload=()=>{URL.revokeObjectURL(t.src),a(t)},t.onerror=()=>{URL.revokeObjectURL(t.src),n(new Error("Failed to load image"))},t.src=URL.createObjectURL(e)})}function w(e,a,n,t){let o=e,i=a;return o>n&&(i=Math.round(i*n/o),o=n),i>t&&(o=Math.round(o*t/i),i=t),{width:o,height:i}}function b(e,a,n){return new Promise((t,o)=>{const i=`image/${a}`;e.toBlob(r=>{r?t(r):o(new Error("Failed to create blob"))},i,n)})}function U(e){if(e===0)return"0 Bytes";const a=1024,n=["Bytes","KB","MB","GB"],t=Math.floor(Math.log(e)/Math.log(a));return Math.round(e/Math.pow(a,t)*100)/100+" "+n[t]}function R(e,a){const n=(e-a)/e*100;return n>0?`${Math.round(n)}%`:"0%"}function v(e){return new Promise((a,n)=>{const t=new Image;t.onload=()=>{const o={width:t.naturalWidth,height:t.naturalHeight};URL.revokeObjectURL(t.src),a(o)},t.onerror=()=>{URL.revokeObjectURL(t.src),n(new Error("Failed to load image dimensions"))},t.src=URL.createObjectURL(e)})}export{R as calculateSavings,U as formatFileSize,h as isGif,v as loadImageDimensions,L as optimizeImage};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.