pic-compressor 0.1.0 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1,4 +1,4 @@
1
- /*! pic-compressor v0.1.0 | MIT License | https://github.com/chandq/pic-compressor */
1
+ /*! pic-compressor v0.1.3 | MIT License | https://github.com/chandq/pic-compressor */
2
2
  Object.defineProperties(exports, {
3
3
  __esModule: { value: true },
4
4
  [Symbol.toStringTag]: { value: "Module" }
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- /*! pic-compressor v0.1.0 | MIT License | https://github.com/chandq/pic-compressor */
1
+ /*! pic-compressor v0.1.3 | MIT License | https://github.com/chandq/pic-compressor */
2
2
  //#region src/index.ts
3
3
  /**
4
4
  * Browser image compression utilities.
package/dist/index.umd.js CHANGED
@@ -1,2 +1,2 @@
1
- /*! pic-compressor v0.1.0 | MIT License | https://github.com/chandq/pic-compressor */
1
+ /*! pic-compressor v0.1.3 | MIT License | https://github.com/chandq/pic-compressor */
2
2
  (function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports):typeof define==`function`&&define.amd?define([`exports`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.PicCompressor={}))})(this,function(e){Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:`Module`}});function t(e){return typeof e==`object`&&!!e}let n=[`image/jpeg`,`image/png`,`image/webp`,`image/avif`],r=2560,i=8192;function a(){if(typeof document>`u`||typeof document.createElement!=`function`)return!1;try{let e=document.createElement(`canvas`);return typeof e.getContext==`function`&&e.getContext(`2d`)!==null}catch{return!1}}let o={balanced:{quality:.82,minQuality:.6,maxWidth:1920,maxHeight:1920,maxPixels:6291456,targetFileSizeKB:500,preserveLongImage:!0},social:{quality:.82,minQuality:.62,maxWidth:1280,maxHeight:1280,maxPixels:4194304,targetFileSizeKB:300,preserveLongImage:!0},"high-quality":{quality:.88,minQuality:.72,maxWidth:2560,maxHeight:2560,maxPixels:8388608,targetFileSizeKB:1024,preserveLongImage:!0},thumbnail:{quality:.78,minQuality:.58,maxWidth:400,maxHeight:400,maxPixels:16e4,targetFileSizeKB:30},"long-image":{quality:.82,minQuality:.65,maxWidth:1080,maxHeight:i,maxPixels:12582912,preserveLongImage:!0}};function s(e){return typeof File<`u`&&e instanceof File||Object.prototype.toString.call(e)===`[object File]`}function c(e){return typeof FileList<`u`&&e instanceof FileList||Object.prototype.toString.call(e)===`[object FileList]`}function l(e){return typeof Blob<`u`&&e instanceof Blob||Object.prototype.toString.call(e)===`[object Blob]`}function u(){if(typeof DOMException<`u`)return new DOMException(`Image compression aborted`,`AbortError`);let e=/* @__PURE__ */ Error(`Image compression aborted`);return e.name=`AbortError`,e}function d(e){if(e?.aborted)throw u()}function f(e,t,n,r=1/0){if(typeof e!=`number`||!Number.isFinite(e)||e<n||e>r)throw RangeError(`${t} must be a finite number between ${n} and ${r}`)}function p(e){let{preset:r=`balanced`,quality:a,mime:s=`image/jpeg`,maxWidth:c,maxHeight:l,maxSize:u,minFileSizeKB:d=50,maxPixels:p,maxCanvasDimension:m=i,preserveLongImage:h,targetFileSizeKB:g,minQuality:_,maxIterations:v=8,concurrency:y=2,outputMode:b=`legacy`,keepOriginalIfLarger:x=!0,backgroundColor:S=`#fff`,strictMime:C=!1,onProgress:w,signal:T,fileName:E=`image`}=t(e)?e:{},D=o[r];if(!D)throw TypeError(`Unsupported image compression preset: ${String(r)}`);let O=a??D.quality,k=_??Math.min(D.minQuality,O),A=p??D.maxPixels,j=g===null?void 0:g??D.targetFileSizeKB,M=c!==void 0||l!==void 0||u!==void 0,N=M?c??u:D.maxWidth,P=M?l??u:D.maxHeight,F=h??(!M&&!!D.preserveLongImage);if(f(O,`quality`,0,1),f(k,`minQuality`,0,1),k>O)throw RangeError(`minQuality must not be greater than quality`);if(!n.includes(s))throw TypeError(`Unsupported image mime type: ${String(s)}`);if(N!==void 0&&f(N,`maxWidth`,1),P!==void 0&&f(P,`maxHeight`,1),u!==void 0&&f(u,`maxSize`,1),f(d,`minFileSizeKB`,0),f(A,`maxPixels`,1),f(m,`maxCanvasDimension`,1),j!==void 0&&f(j,`targetFileSizeKB`,1),f(v,`maxIterations`,1),f(y,`concurrency`,1),typeof x!=`boolean`)throw TypeError(`keepOriginalIfLarger must be a boolean`);if(typeof S!=`string`)throw TypeError(`backgroundColor must be a string`);if(typeof C!=`boolean`)throw TypeError(`strictMime must be a boolean`);if(typeof F!=`boolean`)throw TypeError(`preserveLongImage must be a boolean`);if(w!==void 0&&typeof w!=`function`)throw TypeError(`onProgress must be a function`);if(typeof E!=`string`||E.length===0)throw TypeError(`fileName must be a non-empty string`);if(b!==`legacy`&&b!==`compact`)throw TypeError(`outputMode must be "legacy" or "compact"`);return{preset:r,quality:O,mime:s,maxWidth:N===void 0?void 0:Math.floor(N),maxHeight:P===void 0?void 0:Math.floor(P),minFileSizeKB:d,maxPixels:Math.floor(A),maxCanvasDimension:Math.floor(m),preserveLongImage:F,targetFileSizeKB:j,minQuality:k,maxIterations:Math.max(1,Math.floor(v)),concurrency:Math.max(1,Math.floor(y)),outputMode:b,keepOriginalIfLarger:x,backgroundColor:S,strictMime:C,onProgress:w,signal:T,fileName:E}}function m(e,t){return new File([e],t,{type:e.type,lastModified:Date.now()})}function h(e,t,n,i,a,o,s){let c=e,l=t;c===void 0&&l===void 0&&(c=r,l=r);let u=Math.max(o/s,s/o);a&&u>=3&&(o>=s?c=i:l=i),c=Math.min(c??i,i),l=Math.min(l??i,i);let d=Math.min(1,c/o,l/s),f=Math.min(1,Math.sqrt(n/(o*s))),p=Math.min(d,f);return{width:Math.max(1,Math.floor(o*p)),height:Math.max(1,Math.floor(s*p))}}function g(e,t,n){return new Promise((r,i)=>{d(n);let a=new FileReader,o=!1,s=()=>{a.onerror=null,a.onabort=null,a.onload=null,n?.removeEventListener(`abort`,l)},c=e=>{o||(o=!0,s(),i(e))},l=()=>{try{a.abort()}finally{c(u())}};a.onerror=()=>c(a.error??/* @__PURE__ */ Error(`Failed to read image data`)),a.onabort=()=>c(u()),a.onload=()=>{if(o)return;let e=a.result;t===`dataURL`&&typeof e==`string`||t===`arrayBuffer`&&e instanceof ArrayBuffer?(o=!0,s(),r(e)):c(/* @__PURE__ */ Error(`Unexpected FileReader result for ${t}`))},n?.addEventListener(`abort`,l,{once:!0}),t===`dataURL`?a.readAsDataURL(e):a.readAsArrayBuffer(e)})}async function _(e,t){if(d(t),typeof createImageBitmap==`function`)try{let n=await createImageBitmap(e);try{if(d(t),!n.width||!n.height)throw Error(`Decoded image has invalid dimensions`)}catch(e){throw n.close(),e}return{source:n,width:n.width,height:n.height,release:()=>n.close()}}catch(e){if(e.name===`AbortError`)throw e}return new Promise((n,r)=>{let i=new Image,a,o=!1,s=()=>{i.onload=null,i.onerror=null,t?.removeEventListener(`abort`,l),a&&typeof URL.revokeObjectURL==`function`&&URL.revokeObjectURL(a)},c=e=>{o||(o=!0,s(),r(e))},l=()=>c(u());i.onload=()=>{if(o)return;o=!0;let e=i.naturalWidth||i.width,t=i.naturalHeight||i.height;s(),!e||!t?r(/* @__PURE__ */ Error(`Decoded image has invalid dimensions`)):n({source:i,width:e,height:t,release:()=>void 0})},i.onerror=()=>c(/* @__PURE__ */ Error(`Failed to decode image: ${e.name}`)),t?.addEventListener(`abort`,l,{once:!0});try{typeof URL<`u`&&typeof URL.createObjectURL==`function`?(a=URL.createObjectURL(e),i.src=a):g(e,`dataURL`,t).then(e=>i.src=e,c)}catch(e){c(e instanceof Error?e:Error(String(e)))}})}function v(e,t,n){if(typeof e.toBlob==`function`)return new Promise((r,i)=>{e.toBlob(e=>e?r(e):i(/* @__PURE__ */ Error(`Canvas image encoding failed`)),t,n)});try{let[r,i=``]=e.toDataURL(t,n).split(`,`),a=/^data:([^;]+)/.exec(r??``)?.[1]||t,o=atob(i),s=new Uint8Array(o.length);for(let e=0;e<o.length;e++)s[e]=o.charCodeAt(e);return Promise.resolve(new Blob([s],{type:a}))}catch(e){return Promise.reject(e)}}function y(e){return e===`image/jpg`?`image/jpeg`:n.includes(e)?e:void 0}function b(e,t){let n={"image/jpeg":`jpg`,"image/png":`png`,"image/webp":`webp`,"image/avif":`avif`},r=e.lastIndexOf(`.`);return`${r>0?e.slice(0,r):e||`image`}.${n[t]}`}function x(e,t){if(e.onProgress)try{e.onProgress(Math.max(0,Math.min(100,Math.round(t))))}catch{}}function S(e,t){let n=e.size/1024;return n>=t.minFileSizeKB||t.targetFileSizeKB!==void 0&&n>t.targetFileSizeKB}function C(e){return e===`image/jpeg`||e===`image/webp`||e===`image/avif`}function w(e,t,n){let r=y(e.type);if(!r)throw Error(`Canvas returned unsupported image mime type: ${e.type||`unknown`}`);if(n&&r!==t)throw Error(`Current runtime does not support encoding ${t}; received ${r} instead`);return r}async function T(e,t,n,r,i){let a=0,o=async n=>{d(t.signal);let r=await v(e,t.mime,n);return a++,x(t,10+(i+a)/t.maxIterations*80),{blob:r,mime:w(r,t.mime,t.strictMime),quality:n}},s=await o(t.quality);if(n===void 0||s.blob.size<=n||!C(s.mime)||r<=1||t.quality<=t.minQuality)return{...s,iterations:a};let c=await o(t.minQuality);if(c.blob.size>n)return{...c,iterations:a};let l=c,u=t.minQuality,f=t.quality,p=r-a;for(let e=0;e<p&&f-u>.01;e++){let e=(u+f)/2,t=await o(e);t.blob.size<=n?(l=t,u=e):f=e}return{...l,iterations:a}}async function E(e,t,n,r,i){let a=t,o=n,s=0,c;for(;s<r.maxIterations;){let t=document.createElement(`canvas`);t.width=a,t.height=o;let n=t.getContext(`2d`);if(!n)throw Error(`Failed to create Canvas 2D context`);n.imageSmoothingEnabled=!0,n.imageSmoothingQuality=`high`,r.mime===`image/jpeg`&&(n.fillStyle=r.backgroundColor,n.fillRect(0,0,a,o)),n.drawImage(e.source,0,0,a,o),d(r.signal);try{c=await T(t,r,i,r.maxIterations-s,s),s+=c.iterations}finally{t.width=1,t.height=1}if(i===void 0||c.blob.size<=i||s>=r.maxIterations)return{...c,width:a,height:o,iterations:s};let l=Math.min(.9,Math.sqrt(i/c.blob.size)*.95),u=Math.max(1,Math.floor(a*l)),f=Math.max(1,Math.floor(o*l));if(u===a&&f===o||a===1&&o===1)return{...c,width:a,height:o,iterations:s};a=u,o=f}if(!c)throw Error(`Image encoding did not produce a result`);return{...c,width:a,height:o,iterations:s}}async function D(e,t,n){if(x(t,0),d(t.signal),e.type&&!e.type.startsWith(`image/`))throw TypeError(`${e.name} is not an image file`);let r=e.size/1024;if(!S(e,t))return x(t,100),{file:e};if(!n)throw Error(`Current runtime environment not support Canvas`);let i=await _(e,t.signal);try{x(t,8),d(t.signal);let n=h(t.maxWidth,t.maxHeight,t.maxPixels,t.maxCanvasDimension,t.preserveLongImage,i.width,i.height),a=t.targetFileSizeKB===void 0?void 0:t.targetFileSizeKB*1024,o=y(e.type),s=n.width===i.width&&n.height===i.height,c=t.keepOriginalIfLarger&&s&&o===t.mime?Math.max(0,e.size-1):void 0,l=a===void 0?c:c===void 0?a:Math.min(a,c),u=await E(i,n.width,n.height,t,l);d(t.signal);let{blob:f,mime:p,width:m,height:_,quality:v,iterations:S}=u;if(t.keepOriginalIfLarger&&s&&o===p&&f.size>=e.size)return x(t,100),{file:e};let C=new File([f],b(e.name,p),{type:p,lastModified:e.lastModified}),w={file:C,beforeKB:Number(r.toFixed(2)),afterKB:Number((C.size/1024).toFixed(2)),width:m,height:_,mime:p,compressed:!0,quality:v,iterations:S,...t.targetFileSizeKB===void 0?{}:{targetAchieved:f.size<=t.targetFileSizeKB*1024}};if(t.outputMode===`legacy`){let[n,r,i]=await Promise.all([g(e,`dataURL`,t.signal),g(f,`dataURL`,t.signal),g(f,`arrayBuffer`,t.signal)]);d(t.signal),w.origin=e,w.beforeSrc=n,w.afterSrc=r,w.bufferArray=new Uint8Array(i)}return x(t,100),w}finally{i.release()}}async function O(e,t,n){let r=[],i=0,a=!1;return await Promise.all(Array.from({length:Math.min(t,e.length)},async()=>{for(;!a&&i<e.length;){let t=i++;try{r[t]=await n(e[t],t)}catch(e){throw a=!0,e}}})),r}function k(e,t={}){let n=s(e),r=!n&&l(e),i=c(e);if(!n&&!r&&!i)throw TypeError(`${String(e)} require be File or FileList`);let o=p(t);if(d(o.signal),n||r){let t=n?e:m(e,o.fileName);return D(t,o,!S(t,o)||a())}let u=Array.from(e),f=!u.some(e=>S(e,o))||a(),h=Array.from({length:u.length},()=>0);return O(u,o.concurrency,(e,t)=>D(e,o.onProgress?{...o,onProgress:e=>{h[t]=e,x(o,h.reduce((e,t)=>e+t,0)/u.length)}}:o,f))}var A={IMAGE_COMPRESSION_PRESETS:o,supportCanvas:a,compressImage:k};e.IMAGE_COMPRESSION_PRESETS=o,e.compressImage=k,e.default=A,e.supportCanvas=a});
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "pic-compressor",
3
- "version": "0.1.0",
4
- "description": "Zero-dependency browser image compressor with presets, target-size iteration, long-image support, and batch processing.",
3
+ "version": "0.1.3",
4
+ "description": "🦅 Zero-dependency browser image compressor with presets, target-size iteration, long-image support, and batch processing.",
5
5
  "keywords": [
6
6
  "avif",
7
7
  "compress",