UNPKG

@awesome-compressor/browser-compress-image

Version:

🚀 A powerful, lightweight browser image compression library with TypeScript support. Compress JPEG, PNG, GIF images with multiple output formats (Blob, File, Base64, ArrayBuffer) and zero dependencies.

1 lines • 3.03 kB
import{__esmMin as e,__export as t}from"./chunk-BFNP_Rck.js";import{init_logger as n,logger_default as r}from"./logger-C7b05YKe.js";import{LRUCache as i,init_lruCache as a}from"./lruCache-B8iebN31.js";var o={};t(o,{clearTinyPngCache:()=>l,compressWithTinyPng:()=>c,configureTinyPngCache:()=>f,getTinyPngCacheInfo:()=>d,getTinyPngCacheSize:()=>u});function s(e,t){let n={targetWidth:t.targetWidth,targetHeight:t.targetHeight,maxWidth:t.maxWidth,maxHeight:t.maxHeight,mode:t.mode===`keepQuality`?t.mode:void 0};return`${e.name}_${e.size}_${e.lastModified}_${JSON.stringify(n)}`}function c(e,t){return new Promise(async(n,i)=>{try{let a=s(e,t);if(p.has(a)){r.log(`TinyPNG: Using cached result for file:`,e.name),n(p.get(a));return}let o=t.key;if(!o){i(`TinyPNG API key is required. Please set TINYPNG_API_KEY environment variable or window.TINYPNG_API_KEY`);return}let c=[`image/jpeg`,`image/jpg`,`image/png`,`image/webp`];if(!c.includes(e.type)){i(`Unsupported file type: ${e.type}. TinyPNG supports JPEG, PNG, and WebP images.`);return}let l=await fetch(`https://api.tinify.com/shrink`,{method:`POST`,headers:{Authorization:`Basic ${btoa(`api:${o}`)}`,"Content-Type":e.type},body:e});if(!l.ok){let e=await l.text();i(`TinyPNG upload failed: ${l.status} - ${e}`);return}let u=l.headers.get(`Location`);if(!u){i(`No output URL received from TinyPNG`);return}let d=null;t.mode===`keepQuality`&&(t.targetWidth||t.targetHeight||t.maxWidth||t.maxHeight)&&(d={},t.targetWidth&&t.targetHeight?(d.method=`fit`,d.width=t.targetWidth,d.height=t.targetHeight):t.maxWidth&&t.maxHeight?(d.method=`scale`,d.width=t.maxWidth,d.height=t.maxHeight):t.targetWidth?(d.method=`scale`,d.width=t.targetWidth):t.targetHeight&&(d.method=`scale`,d.height=t.targetHeight));let f=u;if(d){let e=await fetch(u,{method:`POST`,headers:{Authorization:`Basic ${btoa(`api:${o}`)}`,"Content-Type":`application/json`},body:JSON.stringify({resize:d})});if(!e.ok){let t=await e.text();i(`TinyPNG resize failed: ${e.status} - ${t}`);return}f=e.headers.get(`Location`)||u}let m=await fetch(f,{headers:{Authorization:`Basic ${btoa(`api:${o}`)}`}});if(!m.ok){i(`Failed to download compressed image: ${m.status}`);return}let h=await m.blob();if(h.size>=e.size*.98)r.warn(`TinyPNG compression did not significantly reduce file size, returning original file`),p.set(a,e),n(e);else{let t=new Blob([h],{type:e.type});p.set(a,t),n(t)}}catch(e){r.error(`TinyPNG compression error:`,e),i(e)}})}function l(){p.clear(),r.log(`TinyPNG cache cleared`)}function u(){return p.size}function d(){let e=Array.from(p.entries()).map(([e,t])=>({key:e,size:t.size,type:t.type})),t=p.getStats();return{totalEntries:t.size,maxSize:t.maxSize,usageRate:t.usageRate,entries:e}}function f(e=50){p.setMaxSize(e),r.log(`TinyPNG cache reconfigured with max size: ${e}`)}var p,m=e(()=>{a(),n(),p=new i(50)});export{l as clearTinyPngCache,c as compressWithTinyPng,o as compressWithTinyPng_exports,f as configureTinyPngCache,d as getTinyPngCacheInfo,u as getTinyPngCacheSize,m as init_compressWithTinyPng};