From 7d1f4f51079f8fdff0a84dae71ef343a7f13601a Mon Sep 17 00:00:00 2001 From: Donald Date: Wed, 25 Mar 2020 23:49:36 +0800 Subject: [PATCH] Release 1.0.9 --- CHANGELOG.md | 4 + README.md | 4 +- coverage/badge.svg | 2 +- coverage/coverage-final.json | 6 +- coverage/coverage-summary.json | 4 +- coverage/image-compression.js.html | 71 +++++---- coverage/index.html | 28 ++-- coverage/index.js.html | 4 +- .../lcov-report/image-compression.js.html | 71 +++++---- coverage/lcov-report/index.html | 28 ++-- coverage/lcov-report/index.js.html | 4 +- coverage/lcov-report/utils.js.html | 54 +++---- coverage/lcov.info | 146 ++++++++++-------- coverage/utils.js.html | 54 +++---- dist/browser-image-compression.js | 4 +- dist/browser-image-compression.js.map | 2 +- dist/browser-image-compression.mjs | 4 +- dist/browser-image-compression.mjs.map | 2 +- example/React App/package.json | 2 +- example/basic.html | 2 +- lib/index.d.ts | 25 +-- lib/index.js | 2 +- package-lock.json | 2 +- package.json | 2 +- 24 files changed, 288 insertions(+), 239 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eed70fa..7eac75d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## v1.0.9 (25 Mar 2020) +* updated: compression becomes less aggressive, output file is closer to the 'maxWidthOrHeight' and/or 'maxSizeMB' in config +* fixed: file size increased in specific situation + ## v1.0.8 (16 Mar 2020) * added: support for Server Side Rendering (SSR) * updated: ts type file diff --git a/README.md b/README.md index b496074..9f3ca05 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ yarn add browser-image-compression ``` or use a CDN like [delivrjs]: ``` -https://cdn.jsdelivr.net/npm/browser-image-compression@1.0.8/dist/browser-image-compression.js +https://cdn.jsdelivr.net/npm/browser-image-compression@1.0.9/dist/browser-image-compression.js or https://cdn.jsdelivr.net/npm/browser-image-compression@latest/dist/browser-image-compression.js ``` @@ -80,7 +80,7 @@ async function handleImageUpload(event) { console.log('originalFile instanceof Blob', imageFile instanceof Blob); // true console.log(`originalFile size ${imageFile.size / 1024 / 1024} MB`); - var options = { + const options = { maxSizeMB: 1, maxWidthOrHeight: 1920, useWebWorker: true diff --git a/coverage/badge.svg b/coverage/badge.svg index 0444008..8362212 100644 --- a/coverage/badge.svg +++ b/coverage/badge.svg @@ -1 +1 @@ - CoverageCoverage74.52%74.52% \ No newline at end of file + CoverageCoverage74.88%74.88% \ No newline at end of file diff --git a/coverage/coverage-final.json b/coverage/coverage-final.json index 47bb03e..70fd1b7 100644 --- a/coverage/coverage-final.json +++ b/coverage/coverage-final.json @@ -1,4 +1,4 @@ -{"/Users/donald/Project/browser-image-compression/lib/image-compression.js": {"path":"/Users/donald/Project/browser-image-compression/lib/image-compression.js","statementMap":{"0":{"start":{"line":25,"column":17},"end":{"line":25,"column":18}},"1":{"start":{"line":28,"column":4},"end":{"line":28,"column":19}},"2":{"start":{"line":29,"column":4},"end":{"line":31,"column":5}},"3":{"start":{"line":30,"column":6},"end":{"line":30,"column":49}},"4":{"start":{"line":35,"column":4},"end":{"line":35,"column":51}},"5":{"start":{"line":36,"column":4},"end":{"line":38,"column":5}},"6":{"start":{"line":37,"column":6},"end":{"line":37,"column":34}},"7":{"start":{"line":41,"column":24},"end":{"line":41,"column":50}},"8":{"start":{"line":43,"column":22},"end":{"line":43,"column":53}},"9":{"start":{"line":45,"column":2},"end":{"line":45,"column":15}},"10":{"start":{"line":48,"column":26},"end":{"line":48,"column":54}},"11":{"start":{"line":50,"column":2},"end":{"line":50,"column":15}},"12":{"start":{"line":53,"column":38},"end":{"line":53,"column":81}},"13":{"start":{"line":55,"column":2},"end":{"line":55,"column":15}},"14":{"start":{"line":58,"column":2},"end":{"line":58,"column":85}},"15":{"start":{"line":59,"column":2},"end":{"line":59,"column":15}},"16":{"start":{"line":60,"column":33},"end":{"line":60,"column":108}},"17":{"start":{"line":61,"column":2},"end":{"line":61,"column":15}},"18":{"start":{"line":63,"column":16},"end":{"line":63,"column":17}},"19":{"start":{"line":65,"column":17},"end":{"line":65,"column":129}},"20":{"start":{"line":66,"column":2},"end":{"line":66,"column":15}},"21":{"start":{"line":68,"column":2},"end":{"line":72,"column":3}},"22":{"start":{"line":70,"column":4},"end":{"line":70,"column":20}},"23":{"start":{"line":71,"column":4},"end":{"line":71,"column":19}},"24":{"start":{"line":74,"column":23},"end":{"line":74,"column":36}},"25":{"start":{"line":75,"column":20},"end":{"line":75,"column":32}},"26":{"start":{"line":78,"column":15},"end":{"line":78,"column":37}},"27":{"start":{"line":79,"column":2},"end":{"line":97,"column":3}},"28":{"start":{"line":80,"column":21},"end":{"line":80,"column":39}},"29":{"start":{"line":81,"column":22},"end":{"line":81,"column":41}},"30":{"start":{"line":82,"column":4},"end":{"line":82,"column":62}},"31":{"start":{"line":84,"column":4},"end":{"line":84,"column":52}},"32":{"start":{"line":86,"column":4},"end":{"line":88,"column":5}},"33":{"start":{"line":87,"column":6},"end":{"line":87,"column":20}},"34":{"start":{"line":89,"column":4},"end":{"line":89,"column":120}},"35":{"start":{"line":91,"column":4},"end":{"line":91,"column":31}},"36":{"start":{"line":93,"column":4},"end":{"line":93,"column":22}},"37":{"start":{"line":95,"column":4},"end":{"line":95,"column":37}},"38":{"start":{"line":96,"column":4},"end":{"line":96,"column":108}},"39":{"start":{"line":101,"column":2},"end":{"line":101,"column":29}},"40":{"start":{"line":102,"column":2},"end":{"line":102,"column":32}},"41":{"start":{"line":103,"column":2},"end":{"line":103,"column":50}},"42":{"start":{"line":104,"column":2},"end":{"line":104,"column":45}},"43":{"start":{"line":105,"column":2},"end":{"line":105,"column":33}},"44":{"start":{"line":107,"column":2},"end":{"line":107,"column":18}},"45":{"start":{"line":108,"column":2},"end":{"line":108,"column":23}}},"fnMap":{"0":{"name":"compress","decl":{"start":{"line":24,"column":30},"end":{"line":24,"column":38}},"loc":{"start":{"line":24,"column":55},"end":{"line":109,"column":1}},"line":24},"1":{"name":"incProgress","decl":{"start":{"line":27,"column":11},"end":{"line":27,"column":22}},"loc":{"start":{"line":27,"column":33},"end":{"line":32,"column":3}},"line":27},"2":{"name":"setProgress","decl":{"start":{"line":34,"column":11},"end":{"line":34,"column":22}},"loc":{"start":{"line":34,"column":27},"end":{"line":39,"column":3}},"line":34}},"branchMap":{"0":{"loc":{"start":{"line":27,"column":24},"end":{"line":27,"column":31}},"type":"default-arg","locations":[{"start":{"line":27,"column":30},"end":{"line":27,"column":31}}],"line":27},"1":{"loc":{"start":{"line":29,"column":4},"end":{"line":31,"column":5}},"type":"if","locations":[{"start":{"line":29,"column":4},"end":{"line":31,"column":5}},{"start":{"line":29,"column":4},"end":{"line":31,"column":5}}],"line":29},"2":{"loc":{"start":{"line":36,"column":4},"end":{"line":38,"column":5}},"type":"if","locations":[{"start":{"line":36,"column":4},"end":{"line":38,"column":5}},{"start":{"line":36,"column":4},"end":{"line":38,"column":5}}],"line":36},"3":{"loc":{"start":{"line":41,"column":24},"end":{"line":41,"column":50}},"type":"binary-expr","locations":[{"start":{"line":41,"column":24},"end":{"line":41,"column":44}},{"start":{"line":41,"column":48},"end":{"line":41,"column":50}}],"line":41},"4":{"loc":{"start":{"line":58,"column":28},"end":{"line":58,"column":85}},"type":"binary-expr","locations":[{"start":{"line":58,"column":28},"end":{"line":58,"column":51}},{"start":{"line":58,"column":55},"end":{"line":58,"column":85}}],"line":58},"5":{"loc":{"start":{"line":65,"column":60},"end":{"line":65,"column":89}},"type":"binary-expr","locations":[{"start":{"line":65,"column":60},"end":{"line":65,"column":76}},{"start":{"line":65,"column":80},"end":{"line":65,"column":89}}],"line":65},"6":{"loc":{"start":{"line":68,"column":2},"end":{"line":72,"column":3}},"type":"if","locations":[{"start":{"line":68,"column":2},"end":{"line":72,"column":3}},{"start":{"line":68,"column":2},"end":{"line":72,"column":3}}],"line":68},"7":{"loc":{"start":{"line":79,"column":9},"end":{"line":79,"column":55}},"type":"binary-expr","locations":[{"start":{"line":79,"column":9},"end":{"line":79,"column":26}},{"start":{"line":79,"column":30},"end":{"line":79,"column":55}}],"line":79},"8":{"loc":{"start":{"line":86,"column":4},"end":{"line":88,"column":5}},"type":"if","locations":[{"start":{"line":86,"column":4},"end":{"line":88,"column":5}},{"start":{"line":86,"column":4},"end":{"line":88,"column":5}}],"line":86},"9":{"loc":{"start":{"line":89,"column":51},"end":{"line":89,"column":80}},"type":"binary-expr","locations":[{"start":{"line":89,"column":51},"end":{"line":89,"column":67}},{"start":{"line":89,"column":71},"end":{"line":89,"column":80}}],"line":89}},"s":{"0":6,"1":36,"2":36,"3":0,"4":12,"5":12,"6":0,"7":6,"8":6,"9":6,"10":6,"11":6,"12":6,"13":6,"14":6,"15":6,"16":6,"17":6,"18":6,"19":6,"20":6,"21":6,"22":4,"23":4,"24":2,"25":2,"26":2,"27":2,"28":6,"29":6,"30":6,"31":6,"32":6,"33":2,"34":6,"35":6,"36":6,"37":6,"38":6,"39":2,"40":2,"41":2,"42":2,"43":2,"44":2,"45":2},"f":{"0":6,"1":36,"2":12},"b":{"0":[36],"1":[0,36],"2":[0,12],"3":[6,6],"4":[6,0],"5":[6,6],"6":[4,2],"7":[8,8],"8":[2,4],"9":[6,6]},"_coverageSchema":"1a1c01bbd47fc00a2c39e90264f33305004495a9","hash":"764e45f5b544d62d6cc1a266f120657020460cc0","contentHash":"7c585b6ebcf9bb99c6fd1fb767a978965f1cc44dfd3b7e34148c7006363d7071"} -,"/Users/donald/Project/browser-image-compression/lib/index.js": {"path":"/Users/donald/Project/browser-image-compression/lib/index.js","statementMap":{"0":{"start":{"line":35,"column":2},"end":{"line":35,"column":67}},"1":{"start":{"line":36,"column":23},"end":{"line":36,"column":95}},"2":{"start":{"line":37,"column":2},"end":{"line":37,"column":29}},"3":{"start":{"line":39,"column":2},"end":{"line":43,"column":3}},"4":{"start":{"line":40,"column":4},"end":{"line":40,"column":72}},"5":{"start":{"line":41,"column":9},"end":{"line":43,"column":3}},"6":{"start":{"line":42,"column":4},"end":{"line":42,"column":53}},"7":{"start":{"line":46,"column":22},"end":{"line":46,"column":99}},"8":{"start":{"line":54,"column":2},"end":{"line":67,"column":3}},"9":{"start":{"line":55,"column":4},"end":{"line":63,"column":5}},"10":{"start":{"line":58,"column":6},"end":{"line":58,"column":63}},"11":{"start":{"line":62,"column":6},"end":{"line":62,"column":52}},"12":{"start":{"line":66,"column":4},"end":{"line":66,"column":50}},"13":{"start":{"line":69,"column":2},"end":{"line":72,"column":16}},"14":{"start":{"line":70,"column":4},"end":{"line":70,"column":35}},"15":{"start":{"line":71,"column":4},"end":{"line":71,"column":51}},"16":{"start":{"line":74,"column":2},"end":{"line":74,"column":23}},"17":{"start":{"line":77,"column":0},"end":{"line":77,"column":56}},"18":{"start":{"line":78,"column":0},"end":{"line":78,"column":56}},"19":{"start":{"line":79,"column":0},"end":{"line":79,"column":38}},"20":{"start":{"line":80,"column":0},"end":{"line":80,"column":54}},"21":{"start":{"line":81,"column":0},"end":{"line":81,"column":52}},"22":{"start":{"line":82,"column":0},"end":{"line":82,"column":44}},"23":{"start":{"line":83,"column":0},"end":{"line":83,"column":56}},"24":{"start":{"line":84,"column":0},"end":{"line":84,"column":64}},"25":{"start":{"line":85,"column":0},"end":{"line":85,"column":62}},"26":{"start":{"line":86,"column":0},"end":{"line":86,"column":52}},"27":{"start":{"line":87,"column":0},"end":{"line":87,"column":34}}},"fnMap":{"0":{"name":"imageCompression","decl":{"start":{"line":31,"column":15},"end":{"line":31,"column":31}},"loc":{"start":{"line":31,"column":48},"end":{"line":75,"column":1}},"line":31}},"branchMap":{"0":{"loc":{"start":{"line":35,"column":22},"end":{"line":35,"column":67}},"type":"binary-expr","locations":[{"start":{"line":35,"column":22},"end":{"line":35,"column":39}},{"start":{"line":35,"column":43},"end":{"line":35,"column":67}}],"line":35},"1":{"loc":{"start":{"line":36,"column":23},"end":{"line":36,"column":95}},"type":"cond-expr","locations":[{"start":{"line":36,"column":67},"end":{"line":36,"column":87}},{"start":{"line":36,"column":90},"end":{"line":36,"column":95}}],"line":36},"2":{"loc":{"start":{"line":39,"column":2},"end":{"line":43,"column":3}},"type":"if","locations":[{"start":{"line":39,"column":2},"end":{"line":43,"column":3}},{"start":{"line":39,"column":2},"end":{"line":43,"column":3}}],"line":39},"3":{"loc":{"start":{"line":39,"column":8},"end":{"line":39,"column":58}},"type":"binary-expr","locations":[{"start":{"line":39,"column":8},"end":{"line":39,"column":28}},{"start":{"line":39,"column":32},"end":{"line":39,"column":58}}],"line":39},"4":{"loc":{"start":{"line":41,"column":9},"end":{"line":43,"column":3}},"type":"if","locations":[{"start":{"line":41,"column":9},"end":{"line":43,"column":3}},{"start":{"line":41,"column":9},"end":{"line":43,"column":3}}],"line":41},"5":{"loc":{"start":{"line":46,"column":22},"end":{"line":46,"column":99}},"type":"binary-expr","locations":[{"start":{"line":46,"column":22},"end":{"line":46,"column":62}},{"start":{"line":46,"column":66},"end":{"line":46,"column":99}}],"line":46},"6":{"loc":{"start":{"line":54,"column":2},"end":{"line":67,"column":3}},"type":"if","locations":[{"start":{"line":54,"column":2},"end":{"line":67,"column":3}},{"start":{"line":54,"column":2},"end":{"line":67,"column":3}}],"line":54},"7":{"loc":{"start":{"line":54,"column":6},"end":{"line":54,"column":66}},"type":"binary-expr","locations":[{"start":{"line":54,"column":6},"end":{"line":54,"column":18}},{"start":{"line":54,"column":22},"end":{"line":54,"column":50}},{"start":{"line":54,"column":54},"end":{"line":54,"column":66}}],"line":54}},"s":{"0":9,"1":9,"2":9,"3":9,"4":2,"5":7,"6":1,"7":6,"8":6,"9":0,"10":0,"11":0,"12":6,"13":6,"14":6,"15":6,"16":6,"17":1,"18":1,"19":1,"20":1,"21":1,"22":1,"23":1,"24":1,"25":1,"26":1,"27":1},"f":{"0":9},"b":{"0":[9,2],"1":[9,0],"2":[2,7],"3":[9,2],"4":[1,6],"5":[6,0],"6":[0,6],"7":[6,0,0]},"_coverageSchema":"1a1c01bbd47fc00a2c39e90264f33305004495a9","hash":"08dc0f3d3d0afdd647f3e3d6a70e3768e6eb4404","contentHash":"457886a7cc0ef0e7de4be9da78478e48d51bd514ce41f873e18c2619cad9bdc7"} -,"/Users/donald/Project/browser-image-compression/lib/utils.js": {"path":"/Users/donald/Project/browser-image-compression/lib/utils.js","statementMap":{"0":{"start":{"line":1,"column":18},"end":{"line":1,"column":47}},"1":{"start":{"line":4,"column":21},"end":{"line":4,"column":124}},"2":{"start":{"line":5,"column":26},"end":{"line":5,"column":113}},"3":{"start":{"line":6,"column":32},"end":{"line":6,"column":131}},"4":{"start":{"line":15,"column":2},"end":{"line":20,"column":4}},"5":{"start":{"line":16,"column":19},"end":{"line":16,"column":41}},"6":{"start":{"line":17,"column":4},"end":{"line":17,"column":48}},"7":{"start":{"line":17,"column":26},"end":{"line":17,"column":48}},"8":{"start":{"line":18,"column":4},"end":{"line":18,"column":37}},"9":{"start":{"line":18,"column":28},"end":{"line":18,"column":37}},"10":{"start":{"line":19,"column":4},"end":{"line":19,"column":30}},"11":{"start":{"line":32,"column":2},"end":{"line":58,"column":4}},"12":{"start":{"line":33,"column":16},"end":{"line":33,"column":34}},"13":{"start":{"line":34,"column":17},"end":{"line":34,"column":43}},"14":{"start":{"line":35,"column":17},"end":{"line":35,"column":29}},"15":{"start":{"line":36,"column":12},"end":{"line":36,"column":23}},"16":{"start":{"line":37,"column":18},"end":{"line":37,"column":35}},"17":{"start":{"line":38,"column":4},"end":{"line":40,"column":5}},"18":{"start":{"line":39,"column":6},"end":{"line":39,"column":35}},"19":{"start":{"line":41,"column":17},"end":{"line":41,"column":50}},"20":{"start":{"line":42,"column":4},"end":{"line":42,"column":24}},"21":{"start":{"line":43,"column":4},"end":{"line":43,"column":36}},"22":{"start":{"line":44,"column":4},"end":{"line":44,"column":17}},"23":{"start":{"line":68,"column":2},"end":{"line":73,"column":4}},"24":{"start":{"line":69,"column":16},"end":{"line":69,"column":27}},"25":{"start":{"line":70,"column":4},"end":{"line":70,"column":35}},"26":{"start":{"line":70,"column":23},"end":{"line":70,"column":35}},"27":{"start":{"line":71,"column":4},"end":{"line":71,"column":34}},"28":{"start":{"line":71,"column":25},"end":{"line":71,"column":34}},"29":{"start":{"line":72,"column":4},"end":{"line":72,"column":17}},"30":{"start":{"line":83,"column":24},"end":{"line":83,"column":65}},"31":{"start":{"line":84,"column":2},"end":{"line":84,"column":55}},"32":{"start":{"line":85,"column":2},"end":{"line":85,"column":15}},"33":{"start":{"line":96,"column":2},"end":{"line":101,"column":3}},"34":{"start":{"line":97,"column":4},"end":{"line":97,"column":39}},"35":{"start":{"line":99,"column":20},"end":{"line":99,"column":50}},"36":{"start":{"line":100,"column":4},"end":{"line":100,"column":34}},"37":{"start":{"line":102,"column":17},"end":{"line":102,"column":39}},"38":{"start":{"line":103,"column":2},"end":{"line":103,"column":22}},"39":{"start":{"line":118,"column":2},"end":{"line":125,"column":3}},"40":{"start":{"line":119,"column":4},"end":{"line":119,"column":66}},"41":{"start":{"line":120,"column":4},"end":{"line":120,"column":24}},"42":{"start":{"line":121,"column":4},"end":{"line":121,"column":40}},"43":{"start":{"line":123,"column":20},"end":{"line":123,"column":55}},"44":{"start":{"line":124,"column":4},"end":{"line":124,"column":72}},"45":{"start":{"line":126,"column":2},"end":{"line":126,"column":13}},"46":{"start":{"line":138,"column":2},"end":{"line":175,"column":4}},"47":{"start":{"line":139,"column":19},"end":{"line":139,"column":41}},"48":{"start":{"line":140,"column":4},"end":{"line":172,"column":5}},"49":{"start":{"line":141,"column":19},"end":{"line":141,"column":48}},"50":{"start":{"line":142,"column":6},"end":{"line":144,"column":7}},"51":{"start":{"line":143,"column":8},"end":{"line":143,"column":26}},"52":{"start":{"line":145,"column":21},"end":{"line":145,"column":36}},"53":{"start":{"line":146,"column":19},"end":{"line":146,"column":20}},"54":{"start":{"line":147,"column":6},"end":{"line":170,"column":7}},"55":{"start":{"line":148,"column":8},"end":{"line":148,"column":70}},"56":{"start":{"line":148,"column":52},"end":{"line":148,"column":70}},"57":{"start":{"line":149,"column":23},"end":{"line":149,"column":52}},"58":{"start":{"line":150,"column":8},"end":{"line":150,"column":19}},"59":{"start":{"line":151,"column":8},"end":{"line":169,"column":9}},"60":{"start":{"line":152,"column":10},"end":{"line":154,"column":11}},"61":{"start":{"line":153,"column":12},"end":{"line":153,"column":30}},"62":{"start":{"line":156,"column":23},"end":{"line":156,"column":67}},"63":{"start":{"line":157,"column":10},"end":{"line":157,"column":54}},"64":{"start":{"line":158,"column":21},"end":{"line":158,"column":51}},"65":{"start":{"line":159,"column":10},"end":{"line":159,"column":21}},"66":{"start":{"line":160,"column":10},"end":{"line":164,"column":11}},"67":{"start":{"line":160,"column":23},"end":{"line":160,"column":24}},"68":{"start":{"line":161,"column":12},"end":{"line":163,"column":13}},"69":{"start":{"line":162,"column":14},"end":{"line":162,"column":75}},"70":{"start":{"line":165,"column":15},"end":{"line":169,"column":9}},"71":{"start":{"line":166,"column":10},"end":{"line":166,"column":15}},"72":{"start":{"line":168,"column":10},"end":{"line":168,"column":49}},"73":{"start":{"line":171,"column":6},"end":{"line":171,"column":24}},"74":{"start":{"line":173,"column":4},"end":{"line":173,"column":37}},"75":{"start":{"line":173,"column":28},"end":{"line":173,"column":37}},"76":{"start":{"line":174,"column":4},"end":{"line":174,"column":34}},"77":{"start":{"line":185,"column":16},"end":{"line":185,"column":28}},"78":{"start":{"line":186,"column":17},"end":{"line":186,"column":30}},"79":{"start":{"line":187,"column":27},"end":{"line":187,"column":51}},"80":{"start":{"line":189,"column":23},"end":{"line":189,"column":116}},"81":{"start":{"line":191,"column":18},"end":{"line":191,"column":24}},"82":{"start":{"line":194,"column":2},"end":{"line":206,"column":3}},"83":{"start":{"line":195,"column":4},"end":{"line":195,"column":56}},"84":{"start":{"line":196,"column":4},"end":{"line":202,"column":5}},"85":{"start":{"line":197,"column":6},"end":{"line":197,"column":40}},"86":{"start":{"line":198,"column":6},"end":{"line":198,"column":60}},"87":{"start":{"line":200,"column":6},"end":{"line":200,"column":59}},"88":{"start":{"line":201,"column":6},"end":{"line":201,"column":41}},"89":{"start":{"line":203,"column":4},"end":{"line":203,"column":66}},"90":{"start":{"line":205,"column":4},"end":{"line":205,"column":31}},"91":{"start":{"line":208,"column":2},"end":{"line":208,"column":18}},"92":{"start":{"line":220,"column":16},"end":{"line":220,"column":28}},"93":{"start":{"line":221,"column":17},"end":{"line":221,"column":30}},"94":{"start":{"line":223,"column":27},"end":{"line":223,"column":60}},"95":{"start":{"line":226,"column":2},"end":{"line":232,"column":3}},"96":{"start":{"line":227,"column":4},"end":{"line":227,"column":28}},"97":{"start":{"line":228,"column":4},"end":{"line":228,"column":28}},"98":{"start":{"line":230,"column":4},"end":{"line":230,"column":27}},"99":{"start":{"line":231,"column":4},"end":{"line":231,"column":29}},"100":{"start":{"line":235,"column":2},"end":{"line":244,"column":3}},"101":{"start":{"line":236,"column":12},"end":{"line":236,"column":49}},"102":{"start":{"line":236,"column":50},"end":{"line":236,"column":56}},"103":{"start":{"line":237,"column":12},"end":{"line":237,"column":55}},"104":{"start":{"line":237,"column":56},"end":{"line":237,"column":62}},"105":{"start":{"line":238,"column":12},"end":{"line":238,"column":50}},"106":{"start":{"line":238,"column":51},"end":{"line":238,"column":57}},"107":{"start":{"line":239,"column":12},"end":{"line":239,"column":44}},"108":{"start":{"line":239,"column":45},"end":{"line":239,"column":51}},"109":{"start":{"line":240,"column":12},"end":{"line":240,"column":50}},"110":{"start":{"line":240,"column":51},"end":{"line":240,"column":57}},"111":{"start":{"line":241,"column":12},"end":{"line":241,"column":55}},"112":{"start":{"line":241,"column":56},"end":{"line":241,"column":62}},"113":{"start":{"line":242,"column":12},"end":{"line":242,"column":49}},"114":{"start":{"line":242,"column":50},"end":{"line":242,"column":56}},"115":{"start":{"line":243,"column":13},"end":{"line":243,"column":19}},"116":{"start":{"line":246,"column":2},"end":{"line":246,"column":44}},"117":{"start":{"line":248,"column":2},"end":{"line":248,"column":29}},"118":{"start":{"line":250,"column":2},"end":{"line":250,"column":18}},"119":{"start":{"line":262,"column":2},"end":{"line":271,"column":3}},"120":{"start":{"line":263,"column":4},"end":{"line":263,"column":47}},"121":{"start":{"line":264,"column":4},"end":{"line":264,"column":33}},"122":{"start":{"line":265,"column":4},"end":{"line":267,"column":5}},"123":{"start":{"line":266,"column":6},"end":{"line":266,"column":67}},"124":{"start":{"line":269,"column":4},"end":{"line":269,"column":45}},"125":{"start":{"line":270,"column":4},"end":{"line":270,"column":33}},"126":{"start":{"line":272,"column":2},"end":{"line":272,"column":22}},"127":{"start":{"line":273,"column":2},"end":{"line":273,"column":24}},"128":{"start":{"line":274,"column":2},"end":{"line":274,"column":22}},"129":{"start":{"line":283,"column":2},"end":{"line":283,"column":18}},"130":{"start":{"line":284,"column":2},"end":{"line":284,"column":19}},"131":{"start":{"line":287,"column":0},"end":{"line":294,"column":1}},"132":{"start":{"line":289,"column":2},"end":{"line":293,"column":3}},"133":{"start":{"line":290,"column":4},"end":{"line":292,"column":33}}},"fnMap":{"0":{"name":"getDataUrlFromFile","decl":{"start":{"line":14,"column":16},"end":{"line":14,"column":34}},"loc":{"start":{"line":14,"column":42},"end":{"line":21,"column":1}},"line":14},"1":{"name":"(anonymous_1)","decl":{"start":{"line":15,"column":21},"end":{"line":15,"column":22}},"loc":{"start":{"line":15,"column":42},"end":{"line":20,"column":3}},"line":15},"2":{"name":"(anonymous_2)","decl":{"start":{"line":17,"column":20},"end":{"line":17,"column":21}},"loc":{"start":{"line":17,"column":26},"end":{"line":17,"column":48}},"line":17},"3":{"name":"(anonymous_3)","decl":{"start":{"line":18,"column":21},"end":{"line":18,"column":22}},"loc":{"start":{"line":18,"column":28},"end":{"line":18,"column":37}},"line":18},"4":{"name":"getFilefromDataUrl","decl":{"start":{"line":31,"column":16},"end":{"line":31,"column":34}},"loc":{"start":{"line":31,"column":82},"end":{"line":59,"column":1}},"line":31},"5":{"name":"(anonymous_5)","decl":{"start":{"line":32,"column":21},"end":{"line":32,"column":22}},"loc":{"start":{"line":32,"column":34},"end":{"line":58,"column":3}},"line":32},"6":{"name":"loadImage","decl":{"start":{"line":67,"column":16},"end":{"line":67,"column":25}},"loc":{"start":{"line":67,"column":32},"end":{"line":74,"column":1}},"line":67},"7":{"name":"(anonymous_7)","decl":{"start":{"line":68,"column":21},"end":{"line":68,"column":22}},"loc":{"start":{"line":68,"column":42},"end":{"line":73,"column":3}},"line":68},"8":{"name":"(anonymous_8)","decl":{"start":{"line":70,"column":17},"end":{"line":70,"column":18}},"loc":{"start":{"line":70,"column":23},"end":{"line":70,"column":35}},"line":70},"9":{"name":"(anonymous_9)","decl":{"start":{"line":71,"column":18},"end":{"line":71,"column":19}},"loc":{"start":{"line":71,"column":25},"end":{"line":71,"column":34}},"line":71},"10":{"name":"drawImageInCanvas","decl":{"start":{"line":82,"column":16},"end":{"line":82,"column":33}},"loc":{"start":{"line":82,"column":40},"end":{"line":86,"column":1}},"line":82},"11":{"name":"drawFileInCanvas","decl":{"start":{"line":94,"column":22},"end":{"line":94,"column":38}},"loc":{"start":{"line":94,"column":46},"end":{"line":104,"column":1}},"line":94},"12":{"name":"canvasToFile","decl":{"start":{"line":116,"column":22},"end":{"line":116,"column":34}},"loc":{"start":{"line":116,"column":95},"end":{"line":127,"column":1}},"line":116},"13":{"name":"getExifOrientation","decl":{"start":{"line":137,"column":16},"end":{"line":137,"column":34}},"loc":{"start":{"line":137,"column":42},"end":{"line":176,"column":1}},"line":137},"14":{"name":"(anonymous_14)","decl":{"start":{"line":138,"column":21},"end":{"line":138,"column":22}},"loc":{"start":{"line":138,"column":42},"end":{"line":175,"column":3}},"line":138},"15":{"name":"(anonymous_15)","decl":{"start":{"line":140,"column":20},"end":{"line":140,"column":21}},"loc":{"start":{"line":140,"column":27},"end":{"line":172,"column":5}},"line":140},"16":{"name":"(anonymous_16)","decl":{"start":{"line":173,"column":21},"end":{"line":173,"column":22}},"loc":{"start":{"line":173,"column":28},"end":{"line":173,"column":37}},"line":173},"17":{"name":"handleMaxWidthOrHeight","decl":{"start":{"line":184,"column":16},"end":{"line":184,"column":38}},"loc":{"start":{"line":184,"column":57},"end":{"line":209,"column":1}},"line":184},"18":{"name":"followExifOrientation","decl":{"start":{"line":219,"column":16},"end":{"line":219,"column":37}},"loc":{"start":{"line":219,"column":64},"end":{"line":251,"column":1}},"line":219},"19":{"name":"getNewCanvasAndCtx","decl":{"start":{"line":259,"column":16},"end":{"line":259,"column":34}},"loc":{"start":{"line":259,"column":51},"end":{"line":275,"column":1}},"line":259},"20":{"name":"cleanupCanvasMemory","decl":{"start":{"line":282,"column":16},"end":{"line":282,"column":35}},"loc":{"start":{"line":282,"column":45},"end":{"line":285,"column":1}},"line":282},"21":{"name":"(anonymous_21)","decl":{"start":{"line":289,"column":41},"end":{"line":289,"column":42}},"loc":{"start":{"line":289,"column":58},"end":{"line":293,"column":3}},"line":289}},"branchMap":{"0":{"loc":{"start":{"line":4,"column":21},"end":{"line":4,"column":124}},"type":"binary-expr","locations":[{"start":{"line":4,"column":21},"end":{"line":4,"column":30}},{"start":{"line":4,"column":34},"end":{"line":4,"column":48}},{"start":{"line":4,"column":52},"end":{"line":4,"column":74}},{"start":{"line":4,"column":78},"end":{"line":4,"column":124}}],"line":4},"1":{"loc":{"start":{"line":5,"column":26},"end":{"line":5,"column":113}},"type":"binary-expr","locations":[{"start":{"line":5,"column":26},"end":{"line":5,"column":35}},{"start":{"line":5,"column":41},"end":{"line":5,"column":53}},{"start":{"line":5,"column":57},"end":{"line":5,"column":103}},{"start":{"line":5,"column":108},"end":{"line":5,"column":112}}],"line":5},"2":{"loc":{"start":{"line":6,"column":32},"end":{"line":6,"column":131}},"type":"binary-expr","locations":[{"start":{"line":6,"column":32},"end":{"line":6,"column":41}},{"start":{"line":6,"column":47},"end":{"line":6,"column":59}},{"start":{"line":6,"column":63},"end":{"line":6,"column":115}},{"start":{"line":6,"column":120},"end":{"line":6,"column":130}}],"line":6},"3":{"loc":{"start":{"line":31,"column":55},"end":{"line":31,"column":80}},"type":"default-arg","locations":[{"start":{"line":31,"column":70},"end":{"line":31,"column":80}}],"line":31},"4":{"loc":{"start":{"line":116,"column":82},"end":{"line":116,"column":93}},"type":"default-arg","locations":[{"start":{"line":116,"column":92},"end":{"line":116,"column":93}}],"line":116},"5":{"loc":{"start":{"line":118,"column":2},"end":{"line":125,"column":3}},"type":"if","locations":[{"start":{"line":118,"column":2},"end":{"line":125,"column":3}},{"start":{"line":118,"column":2},"end":{"line":125,"column":3}}],"line":118},"6":{"loc":{"start":{"line":118,"column":6},"end":{"line":118,"column":80}},"type":"binary-expr","locations":[{"start":{"line":118,"column":6},"end":{"line":118,"column":43}},{"start":{"line":118,"column":47},"end":{"line":118,"column":80}}],"line":118},"7":{"loc":{"start":{"line":142,"column":6},"end":{"line":144,"column":7}},"type":"if","locations":[{"start":{"line":142,"column":6},"end":{"line":144,"column":7}},{"start":{"line":142,"column":6},"end":{"line":144,"column":7}}],"line":142},"8":{"loc":{"start":{"line":148,"column":8},"end":{"line":148,"column":70}},"type":"if","locations":[{"start":{"line":148,"column":8},"end":{"line":148,"column":70}},{"start":{"line":148,"column":8},"end":{"line":148,"column":70}}],"line":148},"9":{"loc":{"start":{"line":151,"column":8},"end":{"line":169,"column":9}},"type":"if","locations":[{"start":{"line":151,"column":8},"end":{"line":169,"column":9}},{"start":{"line":151,"column":8},"end":{"line":169,"column":9}}],"line":151},"10":{"loc":{"start":{"line":152,"column":10},"end":{"line":154,"column":11}},"type":"if","locations":[{"start":{"line":152,"column":10},"end":{"line":154,"column":11}},{"start":{"line":152,"column":10},"end":{"line":154,"column":11}}],"line":152},"11":{"loc":{"start":{"line":161,"column":12},"end":{"line":163,"column":13}},"type":"if","locations":[{"start":{"line":161,"column":12},"end":{"line":163,"column":13}},{"start":{"line":161,"column":12},"end":{"line":163,"column":13}}],"line":161},"12":{"loc":{"start":{"line":165,"column":15},"end":{"line":169,"column":9}},"type":"if","locations":[{"start":{"line":165,"column":15},"end":{"line":169,"column":9}},{"start":{"line":165,"column":15},"end":{"line":169,"column":9}}],"line":165},"13":{"loc":{"start":{"line":189,"column":23},"end":{"line":189,"column":116}},"type":"binary-expr","locations":[{"start":{"line":189,"column":23},"end":{"line":189,"column":57}},{"start":{"line":189,"column":62},"end":{"line":189,"column":86}},{"start":{"line":189,"column":90},"end":{"line":189,"column":115}}],"line":189},"14":{"loc":{"start":{"line":194,"column":2},"end":{"line":206,"column":3}},"type":"if","locations":[{"start":{"line":194,"column":2},"end":{"line":206,"column":3}},{"start":{"line":194,"column":2},"end":{"line":206,"column":3}}],"line":194},"15":{"loc":{"start":{"line":196,"column":4},"end":{"line":202,"column":5}},"type":"if","locations":[{"start":{"line":196,"column":4},"end":{"line":202,"column":5}},{"start":{"line":196,"column":4},"end":{"line":202,"column":5}}],"line":196},"16":{"loc":{"start":{"line":226,"column":2},"end":{"line":232,"column":3}},"type":"if","locations":[{"start":{"line":226,"column":2},"end":{"line":232,"column":3}},{"start":{"line":226,"column":2},"end":{"line":232,"column":3}}],"line":226},"17":{"loc":{"start":{"line":226,"column":6},"end":{"line":226,"column":48}},"type":"binary-expr","locations":[{"start":{"line":226,"column":6},"end":{"line":226,"column":25}},{"start":{"line":226,"column":29},"end":{"line":226,"column":48}}],"line":226},"18":{"loc":{"start":{"line":235,"column":2},"end":{"line":244,"column":3}},"type":"switch","locations":[{"start":{"line":236,"column":4},"end":{"line":236,"column":56}},{"start":{"line":237,"column":4},"end":{"line":237,"column":62}},{"start":{"line":238,"column":4},"end":{"line":238,"column":57}},{"start":{"line":239,"column":4},"end":{"line":239,"column":51}},{"start":{"line":240,"column":4},"end":{"line":240,"column":57}},{"start":{"line":241,"column":4},"end":{"line":241,"column":62}},{"start":{"line":242,"column":4},"end":{"line":242,"column":56}},{"start":{"line":243,"column":4},"end":{"line":243,"column":19}}],"line":235},"19":{"loc":{"start":{"line":265,"column":4},"end":{"line":267,"column":5}},"type":"if","locations":[{"start":{"line":265,"column":4},"end":{"line":267,"column":5}},{"start":{"line":265,"column":4},"end":{"line":267,"column":5}}],"line":265},"20":{"loc":{"start":{"line":287,"column":0},"end":{"line":294,"column":1}},"type":"if","locations":[{"start":{"line":287,"column":0},"end":{"line":294,"column":1}},{"start":{"line":287,"column":0},"end":{"line":294,"column":1}}],"line":287},"21":{"loc":{"start":{"line":289,"column":21},"end":{"line":293,"column":3}},"type":"binary-expr","locations":[{"start":{"line":289,"column":21},"end":{"line":289,"column":37}},{"start":{"line":289,"column":41},"end":{"line":293,"column":3}}],"line":289},"22":{"loc":{"start":{"line":290,"column":11},"end":{"line":292,"column":33}},"type":"binary-expr","locations":[{"start":{"line":290,"column":11},"end":{"line":290,"column":36}},{"start":{"line":291,"column":6},"end":{"line":291,"column":21}},{"start":{"line":292,"column":6},"end":{"line":292,"column":33}}],"line":290}},"s":{"0":1,"1":1,"2":1,"3":1,"4":13,"5":13,"6":13,"7":13,"8":13,"9":0,"10":13,"11":14,"12":14,"13":14,"14":14,"15":14,"16":14,"17":14,"18":18315383,"19":14,"20":14,"21":14,"22":14,"23":13,"24":13,"25":13,"26":13,"27":13,"28":0,"29":13,"30":12,"31":12,"32":12,"33":11,"34":11,"35":11,"36":11,"37":11,"38":11,"39":12,"40":0,"41":0,"42":0,"43":12,"44":12,"45":12,"46":1,"47":1,"48":1,"49":1,"50":1,"51":1,"52":0,"53":0,"54":0,"55":0,"56":0,"57":0,"58":0,"59":0,"60":0,"61":0,"62":0,"63":0,"64":0,"65":0,"66":0,"67":0,"68":0,"69":0,"70":0,"71":0,"72":0,"73":0,"74":1,"75":0,"76":1,"77":6,"78":6,"79":6,"80":6,"81":6,"82":6,"83":4,"84":4,"85":2,"86":2,"87":2,"88":2,"89":4,"90":4,"91":6,"92":6,"93":6,"94":6,"95":6,"96":0,"97":0,"98":6,"99":6,"100":6,"101":0,"102":0,"103":0,"104":0,"105":0,"106":0,"107":0,"108":0,"109":0,"110":0,"111":0,"112":0,"113":0,"114":0,"115":6,"116":6,"117":6,"118":6,"119":28,"120":28,"121":0,"122":0,"123":0,"124":28,"125":28,"126":28,"127":28,"128":28,"129":26,"130":26,"131":1,"132":1,"133":0},"f":{"0":13,"1":13,"2":13,"3":0,"4":14,"5":14,"6":13,"7":13,"8":13,"9":0,"10":12,"11":11,"12":12,"13":1,"14":1,"15":1,"16":0,"17":6,"18":6,"19":28,"20":26,"21":0},"b":{"0":[1,1,0,0],"1":[1,1,0,1],"2":[1,1,0,1],"3":[2],"4":[0],"5":[0,12],"6":[12,0],"7":[1,0],"8":[0,0],"9":[0,0],"10":[0,0],"11":[0,0],"12":[0,0],"13":[6,4,0],"14":[4,2],"15":[2,2],"16":[0,6],"17":[6,0],"18":[0,0,0,0,0,0,0,6],"19":[0,0],"20":[1,0],"21":[1,0],"22":[0,0,0]},"_coverageSchema":"1a1c01bbd47fc00a2c39e90264f33305004495a9","hash":"45ca086857cf37c95607f20968601dd7e4d7948c","contentHash":"42935f859f92718ac15bd882b452c4be43bb0fee5c221b86076013e7382344a9"} +{"/Users/donald/Project/browser-image-compression/lib/image-compression.js": {"path":"/Users/donald/Project/browser-image-compression/lib/image-compression.js","statementMap":{"0":{"start":{"line":25,"column":17},"end":{"line":25,"column":18}},"1":{"start":{"line":28,"column":4},"end":{"line":28,"column":19}},"2":{"start":{"line":29,"column":4},"end":{"line":31,"column":5}},"3":{"start":{"line":30,"column":6},"end":{"line":30,"column":49}},"4":{"start":{"line":35,"column":4},"end":{"line":35,"column":51}},"5":{"start":{"line":36,"column":4},"end":{"line":38,"column":5}},"6":{"start":{"line":37,"column":6},"end":{"line":37,"column":34}},"7":{"start":{"line":41,"column":24},"end":{"line":41,"column":50}},"8":{"start":{"line":43,"column":22},"end":{"line":43,"column":53}},"9":{"start":{"line":45,"column":2},"end":{"line":45,"column":15}},"10":{"start":{"line":48,"column":26},"end":{"line":48,"column":54}},"11":{"start":{"line":50,"column":2},"end":{"line":50,"column":15}},"12":{"start":{"line":53,"column":38},"end":{"line":53,"column":81}},"13":{"start":{"line":55,"column":2},"end":{"line":55,"column":15}},"14":{"start":{"line":58,"column":2},"end":{"line":58,"column":85}},"15":{"start":{"line":59,"column":2},"end":{"line":59,"column":15}},"16":{"start":{"line":60,"column":33},"end":{"line":60,"column":108}},"17":{"start":{"line":61,"column":2},"end":{"line":61,"column":15}},"18":{"start":{"line":63,"column":16},"end":{"line":63,"column":17}},"19":{"start":{"line":65,"column":17},"end":{"line":65,"column":129}},"20":{"start":{"line":66,"column":2},"end":{"line":66,"column":15}},"21":{"start":{"line":68,"column":28},"end":{"line":68,"column":55}},"22":{"start":{"line":69,"column":27},"end":{"line":69,"column":52}},"23":{"start":{"line":72,"column":2},"end":{"line":76,"column":3}},"24":{"start":{"line":74,"column":4},"end":{"line":74,"column":20}},"25":{"start":{"line":75,"column":4},"end":{"line":75,"column":19}},"26":{"start":{"line":78,"column":21},"end":{"line":78,"column":30}},"27":{"start":{"line":79,"column":23},"end":{"line":79,"column":36}},"28":{"start":{"line":80,"column":20},"end":{"line":80,"column":32}},"29":{"start":{"line":83,"column":15},"end":{"line":83,"column":37}},"30":{"start":{"line":84,"column":2},"end":{"line":102,"column":3}},"31":{"start":{"line":85,"column":21},"end":{"line":85,"column":75}},"32":{"start":{"line":86,"column":22},"end":{"line":86,"column":78}},"33":{"start":{"line":87,"column":4},"end":{"line":87,"column":62}},"34":{"start":{"line":89,"column":4},"end":{"line":89,"column":52}},"35":{"start":{"line":91,"column":4},"end":{"line":93,"column":5}},"36":{"start":{"line":92,"column":6},"end":{"line":92,"column":21}},"37":{"start":{"line":94,"column":4},"end":{"line":94,"column":120}},"38":{"start":{"line":96,"column":4},"end":{"line":96,"column":31}},"39":{"start":{"line":98,"column":4},"end":{"line":98,"column":22}},"40":{"start":{"line":100,"column":4},"end":{"line":100,"column":37}},"41":{"start":{"line":101,"column":4},"end":{"line":101,"column":108}},"42":{"start":{"line":106,"column":2},"end":{"line":106,"column":29}},"43":{"start":{"line":107,"column":2},"end":{"line":107,"column":32}},"44":{"start":{"line":108,"column":2},"end":{"line":108,"column":50}},"45":{"start":{"line":109,"column":2},"end":{"line":109,"column":45}},"46":{"start":{"line":110,"column":2},"end":{"line":110,"column":33}},"47":{"start":{"line":112,"column":2},"end":{"line":112,"column":18}},"48":{"start":{"line":113,"column":2},"end":{"line":113,"column":23}}},"fnMap":{"0":{"name":"compress","decl":{"start":{"line":24,"column":30},"end":{"line":24,"column":38}},"loc":{"start":{"line":24,"column":55},"end":{"line":114,"column":1}},"line":24},"1":{"name":"incProgress","decl":{"start":{"line":27,"column":11},"end":{"line":27,"column":22}},"loc":{"start":{"line":27,"column":33},"end":{"line":32,"column":3}},"line":27},"2":{"name":"setProgress","decl":{"start":{"line":34,"column":11},"end":{"line":34,"column":22}},"loc":{"start":{"line":34,"column":27},"end":{"line":39,"column":3}},"line":34}},"branchMap":{"0":{"loc":{"start":{"line":27,"column":24},"end":{"line":27,"column":31}},"type":"default-arg","locations":[{"start":{"line":27,"column":30},"end":{"line":27,"column":31}}],"line":27},"1":{"loc":{"start":{"line":29,"column":4},"end":{"line":31,"column":5}},"type":"if","locations":[{"start":{"line":29,"column":4},"end":{"line":31,"column":5}},{"start":{"line":29,"column":4},"end":{"line":31,"column":5}}],"line":29},"2":{"loc":{"start":{"line":36,"column":4},"end":{"line":38,"column":5}},"type":"if","locations":[{"start":{"line":36,"column":4},"end":{"line":38,"column":5}},{"start":{"line":36,"column":4},"end":{"line":38,"column":5}}],"line":36},"3":{"loc":{"start":{"line":41,"column":24},"end":{"line":41,"column":50}},"type":"binary-expr","locations":[{"start":{"line":41,"column":24},"end":{"line":41,"column":44}},{"start":{"line":41,"column":48},"end":{"line":41,"column":50}}],"line":41},"4":{"loc":{"start":{"line":58,"column":28},"end":{"line":58,"column":85}},"type":"binary-expr","locations":[{"start":{"line":58,"column":28},"end":{"line":58,"column":51}},{"start":{"line":58,"column":55},"end":{"line":58,"column":85}}],"line":58},"5":{"loc":{"start":{"line":65,"column":60},"end":{"line":65,"column":89}},"type":"binary-expr","locations":[{"start":{"line":65,"column":60},"end":{"line":65,"column":76}},{"start":{"line":65,"column":80},"end":{"line":65,"column":89}}],"line":65},"6":{"loc":{"start":{"line":72,"column":2},"end":{"line":76,"column":3}},"type":"if","locations":[{"start":{"line":72,"column":2},"end":{"line":76,"column":3}},{"start":{"line":72,"column":2},"end":{"line":76,"column":3}}],"line":72},"7":{"loc":{"start":{"line":72,"column":6},"end":{"line":72,"column":45}},"type":"binary-expr","locations":[{"start":{"line":72,"column":6},"end":{"line":72,"column":24}},{"start":{"line":72,"column":28},"end":{"line":72,"column":45}}],"line":72},"8":{"loc":{"start":{"line":84,"column":9},"end":{"line":84,"column":85}},"type":"binary-expr","locations":[{"start":{"line":84,"column":9},"end":{"line":84,"column":26}},{"start":{"line":84,"column":31},"end":{"line":84,"column":56}},{"start":{"line":84,"column":60},"end":{"line":84,"column":84}}],"line":84},"9":{"loc":{"start":{"line":85,"column":21},"end":{"line":85,"column":75}},"type":"cond-expr","locations":[{"start":{"line":85,"column":41},"end":{"line":85,"column":60}},{"start":{"line":85,"column":63},"end":{"line":85,"column":75}}],"line":85},"10":{"loc":{"start":{"line":86,"column":22},"end":{"line":86,"column":78}},"type":"cond-expr","locations":[{"start":{"line":86,"column":42},"end":{"line":86,"column":62}},{"start":{"line":86,"column":65},"end":{"line":86,"column":78}}],"line":86},"11":{"loc":{"start":{"line":91,"column":4},"end":{"line":93,"column":5}},"type":"if","locations":[{"start":{"line":91,"column":4},"end":{"line":93,"column":5}},{"start":{"line":91,"column":4},"end":{"line":93,"column":5}}],"line":91},"12":{"loc":{"start":{"line":94,"column":51},"end":{"line":94,"column":80}},"type":"binary-expr","locations":[{"start":{"line":94,"column":51},"end":{"line":94,"column":67}},{"start":{"line":94,"column":71},"end":{"line":94,"column":80}}],"line":94}},"s":{"0":6,"1":36,"2":36,"3":0,"4":15,"5":15,"6":0,"7":6,"8":6,"9":6,"10":6,"11":6,"12":6,"13":6,"14":6,"15":6,"16":6,"17":6,"18":6,"19":6,"20":6,"21":6,"22":6,"23":6,"24":4,"25":4,"26":2,"27":2,"28":2,"29":2,"30":2,"31":9,"32":9,"33":9,"34":9,"35":9,"36":2,"37":9,"38":9,"39":9,"40":9,"41":9,"42":2,"43":2,"44":2,"45":2,"46":2,"47":2,"48":2},"f":{"0":6,"1":36,"2":15},"b":{"0":[36],"1":[0,36],"2":[0,15],"3":[6,6],"4":[6,0],"5":[6,6],"6":[4,2],"7":[6,4],"8":[11,11,2],"9":[9,0],"10":[9,0],"11":[2,7],"12":[9,9]},"_coverageSchema":"1a1c01bbd47fc00a2c39e90264f33305004495a9","hash":"bd5eeeae6dbdf83bc9da09df5676d00c9864b600","contentHash":"c857a120b527908caec9085701b6621ee34fc215abea20588cfc4ec61b3189de"} +,"/Users/donald/Project/browser-image-compression/lib/index.js": {"path":"/Users/donald/Project/browser-image-compression/lib/index.js","statementMap":{"0":{"start":{"line":35,"column":2},"end":{"line":35,"column":67}},"1":{"start":{"line":36,"column":23},"end":{"line":36,"column":95}},"2":{"start":{"line":37,"column":2},"end":{"line":37,"column":29}},"3":{"start":{"line":39,"column":2},"end":{"line":43,"column":3}},"4":{"start":{"line":40,"column":4},"end":{"line":40,"column":72}},"5":{"start":{"line":41,"column":9},"end":{"line":43,"column":3}},"6":{"start":{"line":42,"column":4},"end":{"line":42,"column":53}},"7":{"start":{"line":46,"column":22},"end":{"line":46,"column":99}},"8":{"start":{"line":54,"column":2},"end":{"line":67,"column":3}},"9":{"start":{"line":55,"column":4},"end":{"line":63,"column":5}},"10":{"start":{"line":58,"column":6},"end":{"line":58,"column":63}},"11":{"start":{"line":62,"column":6},"end":{"line":62,"column":52}},"12":{"start":{"line":66,"column":4},"end":{"line":66,"column":50}},"13":{"start":{"line":69,"column":2},"end":{"line":72,"column":16}},"14":{"start":{"line":70,"column":4},"end":{"line":70,"column":35}},"15":{"start":{"line":71,"column":4},"end":{"line":71,"column":51}},"16":{"start":{"line":74,"column":2},"end":{"line":74,"column":23}},"17":{"start":{"line":77,"column":0},"end":{"line":77,"column":56}},"18":{"start":{"line":78,"column":0},"end":{"line":78,"column":56}},"19":{"start":{"line":79,"column":0},"end":{"line":79,"column":38}},"20":{"start":{"line":80,"column":0},"end":{"line":80,"column":54}},"21":{"start":{"line":81,"column":0},"end":{"line":81,"column":52}},"22":{"start":{"line":82,"column":0},"end":{"line":82,"column":44}},"23":{"start":{"line":83,"column":0},"end":{"line":83,"column":56}},"24":{"start":{"line":84,"column":0},"end":{"line":84,"column":64}},"25":{"start":{"line":85,"column":0},"end":{"line":85,"column":62}},"26":{"start":{"line":86,"column":0},"end":{"line":86,"column":52}},"27":{"start":{"line":87,"column":0},"end":{"line":87,"column":34}}},"fnMap":{"0":{"name":"imageCompression","decl":{"start":{"line":31,"column":15},"end":{"line":31,"column":31}},"loc":{"start":{"line":31,"column":48},"end":{"line":75,"column":1}},"line":31}},"branchMap":{"0":{"loc":{"start":{"line":35,"column":22},"end":{"line":35,"column":67}},"type":"binary-expr","locations":[{"start":{"line":35,"column":22},"end":{"line":35,"column":39}},{"start":{"line":35,"column":43},"end":{"line":35,"column":67}}],"line":35},"1":{"loc":{"start":{"line":36,"column":23},"end":{"line":36,"column":95}},"type":"cond-expr","locations":[{"start":{"line":36,"column":67},"end":{"line":36,"column":87}},{"start":{"line":36,"column":90},"end":{"line":36,"column":95}}],"line":36},"2":{"loc":{"start":{"line":39,"column":2},"end":{"line":43,"column":3}},"type":"if","locations":[{"start":{"line":39,"column":2},"end":{"line":43,"column":3}},{"start":{"line":39,"column":2},"end":{"line":43,"column":3}}],"line":39},"3":{"loc":{"start":{"line":39,"column":8},"end":{"line":39,"column":58}},"type":"binary-expr","locations":[{"start":{"line":39,"column":8},"end":{"line":39,"column":28}},{"start":{"line":39,"column":32},"end":{"line":39,"column":58}}],"line":39},"4":{"loc":{"start":{"line":41,"column":9},"end":{"line":43,"column":3}},"type":"if","locations":[{"start":{"line":41,"column":9},"end":{"line":43,"column":3}},{"start":{"line":41,"column":9},"end":{"line":43,"column":3}}],"line":41},"5":{"loc":{"start":{"line":46,"column":22},"end":{"line":46,"column":99}},"type":"binary-expr","locations":[{"start":{"line":46,"column":22},"end":{"line":46,"column":62}},{"start":{"line":46,"column":66},"end":{"line":46,"column":99}}],"line":46},"6":{"loc":{"start":{"line":54,"column":2},"end":{"line":67,"column":3}},"type":"if","locations":[{"start":{"line":54,"column":2},"end":{"line":67,"column":3}},{"start":{"line":54,"column":2},"end":{"line":67,"column":3}}],"line":54},"7":{"loc":{"start":{"line":54,"column":6},"end":{"line":54,"column":66}},"type":"binary-expr","locations":[{"start":{"line":54,"column":6},"end":{"line":54,"column":18}},{"start":{"line":54,"column":22},"end":{"line":54,"column":50}},{"start":{"line":54,"column":54},"end":{"line":54,"column":66}}],"line":54}},"s":{"0":9,"1":9,"2":9,"3":9,"4":2,"5":7,"6":1,"7":6,"8":6,"9":0,"10":0,"11":0,"12":6,"13":6,"14":6,"15":6,"16":6,"17":1,"18":1,"19":1,"20":1,"21":1,"22":1,"23":1,"24":1,"25":1,"26":1,"27":1},"f":{"0":9},"b":{"0":[9,2],"1":[9,0],"2":[2,7],"3":[9,2],"4":[1,6],"5":[6,0],"6":[0,6],"7":[6,0,0]},"_coverageSchema":"1a1c01bbd47fc00a2c39e90264f33305004495a9","hash":"08dc0f3d3d0afdd647f3e3d6a70e3768e6eb4404","contentHash":"e023fdfd410ac396140f8c11560d0bdef75e4aa358b4ea746e13e411ba04f6f1"} +,"/Users/donald/Project/browser-image-compression/lib/utils.js": {"path":"/Users/donald/Project/browser-image-compression/lib/utils.js","statementMap":{"0":{"start":{"line":1,"column":18},"end":{"line":1,"column":47}},"1":{"start":{"line":4,"column":21},"end":{"line":4,"column":124}},"2":{"start":{"line":5,"column":26},"end":{"line":5,"column":113}},"3":{"start":{"line":6,"column":32},"end":{"line":6,"column":131}},"4":{"start":{"line":15,"column":2},"end":{"line":20,"column":4}},"5":{"start":{"line":16,"column":19},"end":{"line":16,"column":41}},"6":{"start":{"line":17,"column":4},"end":{"line":17,"column":48}},"7":{"start":{"line":17,"column":26},"end":{"line":17,"column":48}},"8":{"start":{"line":18,"column":4},"end":{"line":18,"column":37}},"9":{"start":{"line":18,"column":28},"end":{"line":18,"column":37}},"10":{"start":{"line":19,"column":4},"end":{"line":19,"column":30}},"11":{"start":{"line":32,"column":2},"end":{"line":58,"column":4}},"12":{"start":{"line":33,"column":16},"end":{"line":33,"column":34}},"13":{"start":{"line":34,"column":17},"end":{"line":34,"column":43}},"14":{"start":{"line":35,"column":17},"end":{"line":35,"column":29}},"15":{"start":{"line":36,"column":12},"end":{"line":36,"column":23}},"16":{"start":{"line":37,"column":18},"end":{"line":37,"column":35}},"17":{"start":{"line":38,"column":4},"end":{"line":40,"column":5}},"18":{"start":{"line":39,"column":6},"end":{"line":39,"column":35}},"19":{"start":{"line":41,"column":17},"end":{"line":41,"column":50}},"20":{"start":{"line":42,"column":4},"end":{"line":42,"column":24}},"21":{"start":{"line":43,"column":4},"end":{"line":43,"column":36}},"22":{"start":{"line":44,"column":4},"end":{"line":44,"column":17}},"23":{"start":{"line":68,"column":2},"end":{"line":73,"column":4}},"24":{"start":{"line":69,"column":16},"end":{"line":69,"column":27}},"25":{"start":{"line":70,"column":4},"end":{"line":70,"column":35}},"26":{"start":{"line":70,"column":23},"end":{"line":70,"column":35}},"27":{"start":{"line":71,"column":4},"end":{"line":71,"column":34}},"28":{"start":{"line":71,"column":25},"end":{"line":71,"column":34}},"29":{"start":{"line":72,"column":4},"end":{"line":72,"column":17}},"30":{"start":{"line":83,"column":24},"end":{"line":83,"column":65}},"31":{"start":{"line":84,"column":2},"end":{"line":84,"column":55}},"32":{"start":{"line":85,"column":2},"end":{"line":85,"column":15}},"33":{"start":{"line":96,"column":2},"end":{"line":101,"column":3}},"34":{"start":{"line":97,"column":4},"end":{"line":97,"column":39}},"35":{"start":{"line":99,"column":20},"end":{"line":99,"column":50}},"36":{"start":{"line":100,"column":4},"end":{"line":100,"column":34}},"37":{"start":{"line":102,"column":17},"end":{"line":102,"column":39}},"38":{"start":{"line":103,"column":2},"end":{"line":103,"column":22}},"39":{"start":{"line":118,"column":2},"end":{"line":125,"column":3}},"40":{"start":{"line":119,"column":4},"end":{"line":119,"column":66}},"41":{"start":{"line":120,"column":4},"end":{"line":120,"column":24}},"42":{"start":{"line":121,"column":4},"end":{"line":121,"column":40}},"43":{"start":{"line":123,"column":20},"end":{"line":123,"column":55}},"44":{"start":{"line":124,"column":4},"end":{"line":124,"column":72}},"45":{"start":{"line":126,"column":2},"end":{"line":126,"column":13}},"46":{"start":{"line":138,"column":2},"end":{"line":175,"column":4}},"47":{"start":{"line":139,"column":19},"end":{"line":139,"column":41}},"48":{"start":{"line":140,"column":4},"end":{"line":172,"column":5}},"49":{"start":{"line":141,"column":19},"end":{"line":141,"column":48}},"50":{"start":{"line":142,"column":6},"end":{"line":144,"column":7}},"51":{"start":{"line":143,"column":8},"end":{"line":143,"column":26}},"52":{"start":{"line":145,"column":21},"end":{"line":145,"column":36}},"53":{"start":{"line":146,"column":19},"end":{"line":146,"column":20}},"54":{"start":{"line":147,"column":6},"end":{"line":170,"column":7}},"55":{"start":{"line":148,"column":8},"end":{"line":148,"column":70}},"56":{"start":{"line":148,"column":52},"end":{"line":148,"column":70}},"57":{"start":{"line":149,"column":23},"end":{"line":149,"column":52}},"58":{"start":{"line":150,"column":8},"end":{"line":150,"column":19}},"59":{"start":{"line":151,"column":8},"end":{"line":169,"column":9}},"60":{"start":{"line":152,"column":10},"end":{"line":154,"column":11}},"61":{"start":{"line":153,"column":12},"end":{"line":153,"column":30}},"62":{"start":{"line":156,"column":23},"end":{"line":156,"column":67}},"63":{"start":{"line":157,"column":10},"end":{"line":157,"column":54}},"64":{"start":{"line":158,"column":21},"end":{"line":158,"column":51}},"65":{"start":{"line":159,"column":10},"end":{"line":159,"column":21}},"66":{"start":{"line":160,"column":10},"end":{"line":164,"column":11}},"67":{"start":{"line":160,"column":23},"end":{"line":160,"column":24}},"68":{"start":{"line":161,"column":12},"end":{"line":163,"column":13}},"69":{"start":{"line":162,"column":14},"end":{"line":162,"column":75}},"70":{"start":{"line":165,"column":15},"end":{"line":169,"column":9}},"71":{"start":{"line":166,"column":10},"end":{"line":166,"column":15}},"72":{"start":{"line":168,"column":10},"end":{"line":168,"column":49}},"73":{"start":{"line":171,"column":6},"end":{"line":171,"column":24}},"74":{"start":{"line":173,"column":4},"end":{"line":173,"column":37}},"75":{"start":{"line":173,"column":28},"end":{"line":173,"column":37}},"76":{"start":{"line":174,"column":4},"end":{"line":174,"column":34}},"77":{"start":{"line":185,"column":16},"end":{"line":185,"column":28}},"78":{"start":{"line":186,"column":17},"end":{"line":186,"column":30}},"79":{"start":{"line":187,"column":27},"end":{"line":187,"column":51}},"80":{"start":{"line":189,"column":23},"end":{"line":189,"column":115}},"81":{"start":{"line":191,"column":18},"end":{"line":191,"column":24}},"82":{"start":{"line":194,"column":2},"end":{"line":206,"column":3}},"83":{"start":{"line":195,"column":4},"end":{"line":195,"column":56}},"84":{"start":{"line":196,"column":4},"end":{"line":202,"column":5}},"85":{"start":{"line":197,"column":6},"end":{"line":197,"column":40}},"86":{"start":{"line":198,"column":6},"end":{"line":198,"column":60}},"87":{"start":{"line":200,"column":6},"end":{"line":200,"column":59}},"88":{"start":{"line":201,"column":6},"end":{"line":201,"column":41}},"89":{"start":{"line":203,"column":4},"end":{"line":203,"column":66}},"90":{"start":{"line":205,"column":4},"end":{"line":205,"column":31}},"91":{"start":{"line":208,"column":2},"end":{"line":208,"column":18}},"92":{"start":{"line":220,"column":16},"end":{"line":220,"column":28}},"93":{"start":{"line":221,"column":17},"end":{"line":221,"column":30}},"94":{"start":{"line":223,"column":27},"end":{"line":223,"column":60}},"95":{"start":{"line":226,"column":2},"end":{"line":232,"column":3}},"96":{"start":{"line":227,"column":4},"end":{"line":227,"column":28}},"97":{"start":{"line":228,"column":4},"end":{"line":228,"column":28}},"98":{"start":{"line":230,"column":4},"end":{"line":230,"column":27}},"99":{"start":{"line":231,"column":4},"end":{"line":231,"column":29}},"100":{"start":{"line":235,"column":2},"end":{"line":244,"column":3}},"101":{"start":{"line":236,"column":12},"end":{"line":236,"column":49}},"102":{"start":{"line":236,"column":50},"end":{"line":236,"column":56}},"103":{"start":{"line":237,"column":12},"end":{"line":237,"column":55}},"104":{"start":{"line":237,"column":56},"end":{"line":237,"column":62}},"105":{"start":{"line":238,"column":12},"end":{"line":238,"column":50}},"106":{"start":{"line":238,"column":51},"end":{"line":238,"column":57}},"107":{"start":{"line":239,"column":12},"end":{"line":239,"column":44}},"108":{"start":{"line":239,"column":45},"end":{"line":239,"column":51}},"109":{"start":{"line":240,"column":12},"end":{"line":240,"column":50}},"110":{"start":{"line":240,"column":51},"end":{"line":240,"column":57}},"111":{"start":{"line":241,"column":12},"end":{"line":241,"column":55}},"112":{"start":{"line":241,"column":56},"end":{"line":241,"column":62}},"113":{"start":{"line":242,"column":12},"end":{"line":242,"column":49}},"114":{"start":{"line":242,"column":50},"end":{"line":242,"column":56}},"115":{"start":{"line":243,"column":13},"end":{"line":243,"column":19}},"116":{"start":{"line":246,"column":2},"end":{"line":246,"column":44}},"117":{"start":{"line":248,"column":2},"end":{"line":248,"column":29}},"118":{"start":{"line":250,"column":2},"end":{"line":250,"column":18}},"119":{"start":{"line":262,"column":2},"end":{"line":271,"column":3}},"120":{"start":{"line":263,"column":4},"end":{"line":263,"column":47}},"121":{"start":{"line":264,"column":4},"end":{"line":264,"column":33}},"122":{"start":{"line":265,"column":4},"end":{"line":267,"column":5}},"123":{"start":{"line":266,"column":6},"end":{"line":266,"column":67}},"124":{"start":{"line":269,"column":4},"end":{"line":269,"column":45}},"125":{"start":{"line":270,"column":4},"end":{"line":270,"column":33}},"126":{"start":{"line":272,"column":2},"end":{"line":272,"column":22}},"127":{"start":{"line":273,"column":2},"end":{"line":273,"column":24}},"128":{"start":{"line":274,"column":2},"end":{"line":274,"column":22}},"129":{"start":{"line":283,"column":2},"end":{"line":283,"column":18}},"130":{"start":{"line":284,"column":2},"end":{"line":284,"column":19}},"131":{"start":{"line":287,"column":0},"end":{"line":294,"column":1}},"132":{"start":{"line":289,"column":2},"end":{"line":293,"column":3}},"133":{"start":{"line":290,"column":4},"end":{"line":292,"column":33}}},"fnMap":{"0":{"name":"getDataUrlFromFile","decl":{"start":{"line":14,"column":16},"end":{"line":14,"column":34}},"loc":{"start":{"line":14,"column":42},"end":{"line":21,"column":1}},"line":14},"1":{"name":"(anonymous_1)","decl":{"start":{"line":15,"column":21},"end":{"line":15,"column":22}},"loc":{"start":{"line":15,"column":42},"end":{"line":20,"column":3}},"line":15},"2":{"name":"(anonymous_2)","decl":{"start":{"line":17,"column":20},"end":{"line":17,"column":21}},"loc":{"start":{"line":17,"column":26},"end":{"line":17,"column":48}},"line":17},"3":{"name":"(anonymous_3)","decl":{"start":{"line":18,"column":21},"end":{"line":18,"column":22}},"loc":{"start":{"line":18,"column":28},"end":{"line":18,"column":37}},"line":18},"4":{"name":"getFilefromDataUrl","decl":{"start":{"line":31,"column":16},"end":{"line":31,"column":34}},"loc":{"start":{"line":31,"column":82},"end":{"line":59,"column":1}},"line":31},"5":{"name":"(anonymous_5)","decl":{"start":{"line":32,"column":21},"end":{"line":32,"column":22}},"loc":{"start":{"line":32,"column":34},"end":{"line":58,"column":3}},"line":32},"6":{"name":"loadImage","decl":{"start":{"line":67,"column":16},"end":{"line":67,"column":25}},"loc":{"start":{"line":67,"column":32},"end":{"line":74,"column":1}},"line":67},"7":{"name":"(anonymous_7)","decl":{"start":{"line":68,"column":21},"end":{"line":68,"column":22}},"loc":{"start":{"line":68,"column":42},"end":{"line":73,"column":3}},"line":68},"8":{"name":"(anonymous_8)","decl":{"start":{"line":70,"column":17},"end":{"line":70,"column":18}},"loc":{"start":{"line":70,"column":23},"end":{"line":70,"column":35}},"line":70},"9":{"name":"(anonymous_9)","decl":{"start":{"line":71,"column":18},"end":{"line":71,"column":19}},"loc":{"start":{"line":71,"column":25},"end":{"line":71,"column":34}},"line":71},"10":{"name":"drawImageInCanvas","decl":{"start":{"line":82,"column":16},"end":{"line":82,"column":33}},"loc":{"start":{"line":82,"column":40},"end":{"line":86,"column":1}},"line":82},"11":{"name":"drawFileInCanvas","decl":{"start":{"line":94,"column":22},"end":{"line":94,"column":38}},"loc":{"start":{"line":94,"column":46},"end":{"line":104,"column":1}},"line":94},"12":{"name":"canvasToFile","decl":{"start":{"line":116,"column":22},"end":{"line":116,"column":34}},"loc":{"start":{"line":116,"column":95},"end":{"line":127,"column":1}},"line":116},"13":{"name":"getExifOrientation","decl":{"start":{"line":137,"column":16},"end":{"line":137,"column":34}},"loc":{"start":{"line":137,"column":42},"end":{"line":176,"column":1}},"line":137},"14":{"name":"(anonymous_14)","decl":{"start":{"line":138,"column":21},"end":{"line":138,"column":22}},"loc":{"start":{"line":138,"column":42},"end":{"line":175,"column":3}},"line":138},"15":{"name":"(anonymous_15)","decl":{"start":{"line":140,"column":20},"end":{"line":140,"column":21}},"loc":{"start":{"line":140,"column":27},"end":{"line":172,"column":5}},"line":140},"16":{"name":"(anonymous_16)","decl":{"start":{"line":173,"column":21},"end":{"line":173,"column":22}},"loc":{"start":{"line":173,"column":28},"end":{"line":173,"column":37}},"line":173},"17":{"name":"handleMaxWidthOrHeight","decl":{"start":{"line":184,"column":16},"end":{"line":184,"column":38}},"loc":{"start":{"line":184,"column":57},"end":{"line":209,"column":1}},"line":184},"18":{"name":"followExifOrientation","decl":{"start":{"line":219,"column":16},"end":{"line":219,"column":37}},"loc":{"start":{"line":219,"column":64},"end":{"line":251,"column":1}},"line":219},"19":{"name":"getNewCanvasAndCtx","decl":{"start":{"line":259,"column":16},"end":{"line":259,"column":34}},"loc":{"start":{"line":259,"column":51},"end":{"line":275,"column":1}},"line":259},"20":{"name":"cleanupCanvasMemory","decl":{"start":{"line":282,"column":16},"end":{"line":282,"column":35}},"loc":{"start":{"line":282,"column":45},"end":{"line":285,"column":1}},"line":282},"21":{"name":"(anonymous_21)","decl":{"start":{"line":289,"column":41},"end":{"line":289,"column":42}},"loc":{"start":{"line":289,"column":58},"end":{"line":293,"column":3}},"line":289}},"branchMap":{"0":{"loc":{"start":{"line":4,"column":21},"end":{"line":4,"column":124}},"type":"binary-expr","locations":[{"start":{"line":4,"column":21},"end":{"line":4,"column":30}},{"start":{"line":4,"column":34},"end":{"line":4,"column":48}},{"start":{"line":4,"column":52},"end":{"line":4,"column":74}},{"start":{"line":4,"column":78},"end":{"line":4,"column":124}}],"line":4},"1":{"loc":{"start":{"line":5,"column":26},"end":{"line":5,"column":113}},"type":"binary-expr","locations":[{"start":{"line":5,"column":26},"end":{"line":5,"column":35}},{"start":{"line":5,"column":41},"end":{"line":5,"column":53}},{"start":{"line":5,"column":57},"end":{"line":5,"column":103}},{"start":{"line":5,"column":108},"end":{"line":5,"column":112}}],"line":5},"2":{"loc":{"start":{"line":6,"column":32},"end":{"line":6,"column":131}},"type":"binary-expr","locations":[{"start":{"line":6,"column":32},"end":{"line":6,"column":41}},{"start":{"line":6,"column":47},"end":{"line":6,"column":59}},{"start":{"line":6,"column":63},"end":{"line":6,"column":115}},{"start":{"line":6,"column":120},"end":{"line":6,"column":130}}],"line":6},"3":{"loc":{"start":{"line":31,"column":55},"end":{"line":31,"column":80}},"type":"default-arg","locations":[{"start":{"line":31,"column":70},"end":{"line":31,"column":80}}],"line":31},"4":{"loc":{"start":{"line":116,"column":82},"end":{"line":116,"column":93}},"type":"default-arg","locations":[{"start":{"line":116,"column":92},"end":{"line":116,"column":93}}],"line":116},"5":{"loc":{"start":{"line":118,"column":2},"end":{"line":125,"column":3}},"type":"if","locations":[{"start":{"line":118,"column":2},"end":{"line":125,"column":3}},{"start":{"line":118,"column":2},"end":{"line":125,"column":3}}],"line":118},"6":{"loc":{"start":{"line":118,"column":6},"end":{"line":118,"column":80}},"type":"binary-expr","locations":[{"start":{"line":118,"column":6},"end":{"line":118,"column":43}},{"start":{"line":118,"column":47},"end":{"line":118,"column":80}}],"line":118},"7":{"loc":{"start":{"line":142,"column":6},"end":{"line":144,"column":7}},"type":"if","locations":[{"start":{"line":142,"column":6},"end":{"line":144,"column":7}},{"start":{"line":142,"column":6},"end":{"line":144,"column":7}}],"line":142},"8":{"loc":{"start":{"line":148,"column":8},"end":{"line":148,"column":70}},"type":"if","locations":[{"start":{"line":148,"column":8},"end":{"line":148,"column":70}},{"start":{"line":148,"column":8},"end":{"line":148,"column":70}}],"line":148},"9":{"loc":{"start":{"line":151,"column":8},"end":{"line":169,"column":9}},"type":"if","locations":[{"start":{"line":151,"column":8},"end":{"line":169,"column":9}},{"start":{"line":151,"column":8},"end":{"line":169,"column":9}}],"line":151},"10":{"loc":{"start":{"line":152,"column":10},"end":{"line":154,"column":11}},"type":"if","locations":[{"start":{"line":152,"column":10},"end":{"line":154,"column":11}},{"start":{"line":152,"column":10},"end":{"line":154,"column":11}}],"line":152},"11":{"loc":{"start":{"line":161,"column":12},"end":{"line":163,"column":13}},"type":"if","locations":[{"start":{"line":161,"column":12},"end":{"line":163,"column":13}},{"start":{"line":161,"column":12},"end":{"line":163,"column":13}}],"line":161},"12":{"loc":{"start":{"line":165,"column":15},"end":{"line":169,"column":9}},"type":"if","locations":[{"start":{"line":165,"column":15},"end":{"line":169,"column":9}},{"start":{"line":165,"column":15},"end":{"line":169,"column":9}}],"line":165},"13":{"loc":{"start":{"line":189,"column":23},"end":{"line":189,"column":115}},"type":"binary-expr","locations":[{"start":{"line":189,"column":23},"end":{"line":189,"column":56}},{"start":{"line":189,"column":61},"end":{"line":189,"column":85}},{"start":{"line":189,"column":89},"end":{"line":189,"column":114}}],"line":189},"14":{"loc":{"start":{"line":194,"column":2},"end":{"line":206,"column":3}},"type":"if","locations":[{"start":{"line":194,"column":2},"end":{"line":206,"column":3}},{"start":{"line":194,"column":2},"end":{"line":206,"column":3}}],"line":194},"15":{"loc":{"start":{"line":196,"column":4},"end":{"line":202,"column":5}},"type":"if","locations":[{"start":{"line":196,"column":4},"end":{"line":202,"column":5}},{"start":{"line":196,"column":4},"end":{"line":202,"column":5}}],"line":196},"16":{"loc":{"start":{"line":226,"column":2},"end":{"line":232,"column":3}},"type":"if","locations":[{"start":{"line":226,"column":2},"end":{"line":232,"column":3}},{"start":{"line":226,"column":2},"end":{"line":232,"column":3}}],"line":226},"17":{"loc":{"start":{"line":226,"column":6},"end":{"line":226,"column":48}},"type":"binary-expr","locations":[{"start":{"line":226,"column":6},"end":{"line":226,"column":25}},{"start":{"line":226,"column":29},"end":{"line":226,"column":48}}],"line":226},"18":{"loc":{"start":{"line":235,"column":2},"end":{"line":244,"column":3}},"type":"switch","locations":[{"start":{"line":236,"column":4},"end":{"line":236,"column":56}},{"start":{"line":237,"column":4},"end":{"line":237,"column":62}},{"start":{"line":238,"column":4},"end":{"line":238,"column":57}},{"start":{"line":239,"column":4},"end":{"line":239,"column":51}},{"start":{"line":240,"column":4},"end":{"line":240,"column":57}},{"start":{"line":241,"column":4},"end":{"line":241,"column":62}},{"start":{"line":242,"column":4},"end":{"line":242,"column":56}},{"start":{"line":243,"column":4},"end":{"line":243,"column":19}}],"line":235},"19":{"loc":{"start":{"line":265,"column":4},"end":{"line":267,"column":5}},"type":"if","locations":[{"start":{"line":265,"column":4},"end":{"line":267,"column":5}},{"start":{"line":265,"column":4},"end":{"line":267,"column":5}}],"line":265},"20":{"loc":{"start":{"line":287,"column":0},"end":{"line":294,"column":1}},"type":"if","locations":[{"start":{"line":287,"column":0},"end":{"line":294,"column":1}},{"start":{"line":287,"column":0},"end":{"line":294,"column":1}}],"line":287},"21":{"loc":{"start":{"line":289,"column":21},"end":{"line":293,"column":3}},"type":"binary-expr","locations":[{"start":{"line":289,"column":21},"end":{"line":289,"column":37}},{"start":{"line":289,"column":41},"end":{"line":293,"column":3}}],"line":289},"22":{"loc":{"start":{"line":290,"column":11},"end":{"line":292,"column":33}},"type":"binary-expr","locations":[{"start":{"line":290,"column":11},"end":{"line":290,"column":36}},{"start":{"line":291,"column":6},"end":{"line":291,"column":21}},{"start":{"line":292,"column":6},"end":{"line":292,"column":33}}],"line":290}},"s":{"0":1,"1":1,"2":1,"3":1,"4":13,"5":13,"6":13,"7":13,"8":13,"9":0,"10":13,"11":17,"12":17,"13":17,"14":17,"15":17,"16":17,"17":17,"18":23716492,"19":17,"20":17,"21":17,"22":17,"23":13,"24":13,"25":13,"26":13,"27":13,"28":0,"29":13,"30":12,"31":12,"32":12,"33":11,"34":11,"35":11,"36":11,"37":11,"38":11,"39":15,"40":0,"41":0,"42":0,"43":15,"44":15,"45":15,"46":1,"47":1,"48":1,"49":1,"50":1,"51":1,"52":0,"53":0,"54":0,"55":0,"56":0,"57":0,"58":0,"59":0,"60":0,"61":0,"62":0,"63":0,"64":0,"65":0,"66":0,"67":0,"68":0,"69":0,"70":0,"71":0,"72":0,"73":0,"74":1,"75":0,"76":1,"77":6,"78":6,"79":6,"80":6,"81":6,"82":6,"83":4,"84":4,"85":2,"86":2,"87":2,"88":2,"89":4,"90":4,"91":6,"92":6,"93":6,"94":6,"95":6,"96":0,"97":0,"98":6,"99":6,"100":6,"101":0,"102":0,"103":0,"104":0,"105":0,"106":0,"107":0,"108":0,"109":0,"110":0,"111":0,"112":0,"113":0,"114":0,"115":6,"116":6,"117":6,"118":6,"119":31,"120":31,"121":0,"122":0,"123":0,"124":31,"125":31,"126":31,"127":31,"128":31,"129":29,"130":29,"131":1,"132":1,"133":0},"f":{"0":13,"1":13,"2":13,"3":0,"4":17,"5":17,"6":13,"7":13,"8":13,"9":0,"10":12,"11":11,"12":15,"13":1,"14":1,"15":1,"16":0,"17":6,"18":6,"19":31,"20":29,"21":0},"b":{"0":[1,1,0,0],"1":[1,1,0,1],"2":[1,1,0,1],"3":[2],"4":[0],"5":[0,15],"6":[15,0],"7":[1,0],"8":[0,0],"9":[0,0],"10":[0,0],"11":[0,0],"12":[0,0],"13":[6,4,0],"14":[4,2],"15":[2,2],"16":[0,6],"17":[6,0],"18":[0,0,0,0,0,0,0,6],"19":[0,0],"20":[1,0],"21":[1,0],"22":[0,0,0]},"_coverageSchema":"1a1c01bbd47fc00a2c39e90264f33305004495a9","hash":"89946c86beb56bc10293a3009875d5803ec86b81","contentHash":"0bf45569af2ce38715815acbb4983538d9102afb53873ae5ebeb71dd511d550d"} } diff --git a/coverage/coverage-summary.json b/coverage/coverage-summary.json index 1bc6057..8de1e86 100644 --- a/coverage/coverage-summary.json +++ b/coverage/coverage-summary.json @@ -1,5 +1,5 @@ -{"total": {"lines":{"total":194,"covered":153,"skipped":0,"pct":78.87},"statements":{"total":208,"covered":155,"skipped":0,"pct":74.52},"functions":{"total":26,"covered":22,"skipped":0,"pct":84.62},"branches":{"total":94,"covered":51,"skipped":0,"pct":54.26}} -,"/Users/donald/Project/browser-image-compression/lib/image-compression.js": {"lines":{"total":46,"covered":44,"skipped":0,"pct":95.65},"functions":{"total":3,"covered":3,"skipped":0,"pct":100},"statements":{"total":46,"covered":44,"skipped":0,"pct":95.65},"branches":{"total":19,"covered":16,"skipped":0,"pct":84.21}} +{"total": {"lines":{"total":197,"covered":156,"skipped":0,"pct":79.19},"statements":{"total":211,"covered":158,"skipped":0,"pct":74.88},"functions":{"total":26,"covered":22,"skipped":0,"pct":84.62},"branches":{"total":101,"covered":56,"skipped":0,"pct":55.45}} +,"/Users/donald/Project/browser-image-compression/lib/image-compression.js": {"lines":{"total":49,"covered":47,"skipped":0,"pct":95.92},"functions":{"total":3,"covered":3,"skipped":0,"pct":100},"statements":{"total":49,"covered":47,"skipped":0,"pct":95.92},"branches":{"total":26,"covered":21,"skipped":0,"pct":80.77}} ,"/Users/donald/Project/browser-image-compression/lib/index.js": {"lines":{"total":28,"covered":25,"skipped":0,"pct":89.29},"functions":{"total":1,"covered":1,"skipped":0,"pct":100},"statements":{"total":28,"covered":25,"skipped":0,"pct":89.29},"branches":{"total":17,"covered":12,"skipped":0,"pct":70.59}} ,"/Users/donald/Project/browser-image-compression/lib/utils.js": {"lines":{"total":120,"covered":84,"skipped":0,"pct":70},"functions":{"total":22,"covered":18,"skipped":0,"pct":81.82},"statements":{"total":134,"covered":86,"skipped":0,"pct":64.18},"branches":{"total":58,"covered":23,"skipped":0,"pct":39.66}} } diff --git a/coverage/image-compression.js.html b/coverage/image-compression.js.html index a8fff55..43a2808 100644 --- a/coverage/image-compression.js.html +++ b/coverage/image-compression.js.html @@ -23,16 +23,16 @@

All files image-compression.js

- 95.65% + 95.92% Statements - 44/46 + 47/49
- 84.21% + 80.77% Branches - 16/19 + 21/26
@@ -44,9 +44,9 @@

All files image-compression.js

- 95.65% + 95.92% Lines - 44/46 + 47/49
@@ -166,7 +166,12 @@

All files image-compression.js

107 108 109 -110  +110 +111 +112 +113 +114 +115        @@ -200,8 +205,8 @@

All files image-compression.js

      -12x -12x +15x +15x       @@ -234,6 +239,10 @@

All files image-compression.js

6x   6x +6x +  +  +6x   4x 4x @@ -241,27 +250,28 @@

All files image-compression.js

  2x 2x +2x     2x 2x -6x -6x -6x +9x +9x +9x   -6x +9x   -6x +9x 2x   -6x +9x   -6x +9x   -6x +9x   -6x -6x +9x +9x       @@ -341,27 +351,32 @@

All files image-compression.js

  let tempFile = await canvasToFile(orientationFixedCanvas, options.fileType || file.type, file.name, file.lastModified, quality) incProgress() +  + const origExceedMaxSize = tempFile.size > maxSizeByte + const sizeBecomeLarger = tempFile.size > file.size +  // check if we need to compress or resize - if (tempFile.size <= maxSizeByte) { + if (!origExceedMaxSize && !sizeBecomeLarger) { // no need to compress setProgress(100) return tempFile }   - const originalSize = tempFile.size - let currentSize = originalSize + const sourceSize = file.size + const renderedSize = tempFile.size + let currentSize = renderedSize let compressedFile let newCanvas, ctx let canvas = orientationFixedCanvas - while (remainingTrials-- && currentSize > maxSizeByte) { - const newWidth = canvas.width * 0.9 - const newHeight = canvas.height * 0.9; + while (remainingTrials-- && (currentSize > maxSizeByte || currentSize > sourceSize)) { + const newWidth = origExceedMaxSize ? canvas.width * 0.95 : canvas.width + const newHeight = origExceedMaxSize ? canvas.height * 0.95 : canvas.height; [newCanvas, ctx] = getNewCanvasAndCtx(newWidth, newHeight)   ctx.drawImage(canvas, 0, 0, newWidth, newHeight)   if (file.type === 'image/jpeg') { - quality *= 0.9 + quality *= 0.95 } compressedFile = await canvasToFile(newCanvas, options.fileType || file.type, file.name, file.lastModified, quality)   @@ -370,7 +385,7 @@

All files image-compression.js

canvas = newCanvas   currentSize = compressedFile.size - setProgress(Math.min(99, Math.floor((originalSize - currentSize) / (originalSize - maxSizeByte) * 100))) + setProgress(Math.min(99, Math.floor((renderedSize - currentSize) / (renderedSize - maxSizeByte) * 100))) }   // garbage clean canvas for safari @@ -391,7 +406,7 @@

All files image-compression.js

diff --git a/coverage/index.html b/coverage/index.html index 00845d6..ca5e590 100644 --- a/coverage/index.html +++ b/coverage/index.html @@ -23,16 +23,16 @@

All files

- 74.52% + 74.88% Statements - 155/208 + 158/211
- 54.26% + 55.45% Branches - 51/94 + 56/101
@@ -44,9 +44,9 @@

All files

- 78.87% + 79.19% Lines - 153/194 + 156/197
@@ -74,17 +74,17 @@

All files

image-compression.js - +
- 95.65% - 44/46 - 84.21% - 16/19 + 95.92% + 47/49 + 80.77% + 21/26 100% 3/3 - 95.65% - 44/46 + 95.92% + 47/49 @@ -125,7 +125,7 @@

All files

diff --git a/coverage/index.js.html b/coverage/index.js.html index 74eef48..4360a3a 100644 --- a/coverage/index.js.html +++ b/coverage/index.js.html @@ -321,7 +321,7 @@

All files index.js

imageCompression.handleMaxWidthOrHeight = handleMaxWidthOrHeight imageCompression.followExifOrientation = followExifOrientation imageCompression.cleanupMemory = cleanupCanvasMemory -imageCompression.version = '1.0.8' +imageCompression.version = '1.0.9'   export default imageCompression   @@ -331,7 +331,7 @@

All files index.js

diff --git a/coverage/lcov-report/image-compression.js.html b/coverage/lcov-report/image-compression.js.html index a8fff55..43a2808 100644 --- a/coverage/lcov-report/image-compression.js.html +++ b/coverage/lcov-report/image-compression.js.html @@ -23,16 +23,16 @@

All files image-compression.js

- 95.65% + 95.92% Statements - 44/46 + 47/49
- 84.21% + 80.77% Branches - 16/19 + 21/26
@@ -44,9 +44,9 @@

All files image-compression.js

- 95.65% + 95.92% Lines - 44/46 + 47/49
@@ -166,7 +166,12 @@

All files image-compression.js

107 108 109 -110  +110 +111 +112 +113 +114 +115        @@ -200,8 +205,8 @@

All files image-compression.js

      -12x -12x +15x +15x       @@ -234,6 +239,10 @@

All files image-compression.js

6x   6x +6x +  +  +6x   4x 4x @@ -241,27 +250,28 @@

All files image-compression.js

  2x 2x +2x     2x 2x -6x -6x -6x +9x +9x +9x   -6x +9x   -6x +9x 2x   -6x +9x   -6x +9x   -6x +9x   -6x -6x +9x +9x       @@ -341,27 +351,32 @@

All files image-compression.js

  let tempFile = await canvasToFile(orientationFixedCanvas, options.fileType || file.type, file.name, file.lastModified, quality) incProgress() +  + const origExceedMaxSize = tempFile.size > maxSizeByte + const sizeBecomeLarger = tempFile.size > file.size +  // check if we need to compress or resize - if (tempFile.size <= maxSizeByte) { + if (!origExceedMaxSize && !sizeBecomeLarger) { // no need to compress setProgress(100) return tempFile }   - const originalSize = tempFile.size - let currentSize = originalSize + const sourceSize = file.size + const renderedSize = tempFile.size + let currentSize = renderedSize let compressedFile let newCanvas, ctx let canvas = orientationFixedCanvas - while (remainingTrials-- && currentSize > maxSizeByte) { - const newWidth = canvas.width * 0.9 - const newHeight = canvas.height * 0.9; + while (remainingTrials-- && (currentSize > maxSizeByte || currentSize > sourceSize)) { + const newWidth = origExceedMaxSize ? canvas.width * 0.95 : canvas.width + const newHeight = origExceedMaxSize ? canvas.height * 0.95 : canvas.height; [newCanvas, ctx] = getNewCanvasAndCtx(newWidth, newHeight)   ctx.drawImage(canvas, 0, 0, newWidth, newHeight)   if (file.type === 'image/jpeg') { - quality *= 0.9 + quality *= 0.95 } compressedFile = await canvasToFile(newCanvas, options.fileType || file.type, file.name, file.lastModified, quality)   @@ -370,7 +385,7 @@

All files image-compression.js

canvas = newCanvas   currentSize = compressedFile.size - setProgress(Math.min(99, Math.floor((originalSize - currentSize) / (originalSize - maxSizeByte) * 100))) + setProgress(Math.min(99, Math.floor((renderedSize - currentSize) / (renderedSize - maxSizeByte) * 100))) }   // garbage clean canvas for safari @@ -391,7 +406,7 @@

All files image-compression.js

diff --git a/coverage/lcov-report/index.html b/coverage/lcov-report/index.html index 00845d6..ca5e590 100644 --- a/coverage/lcov-report/index.html +++ b/coverage/lcov-report/index.html @@ -23,16 +23,16 @@

All files

- 74.52% + 74.88% Statements - 155/208 + 158/211
- 54.26% + 55.45% Branches - 51/94 + 56/101
@@ -44,9 +44,9 @@

All files

- 78.87% + 79.19% Lines - 153/194 + 156/197
@@ -74,17 +74,17 @@

All files

image-compression.js - +
- 95.65% - 44/46 - 84.21% - 16/19 + 95.92% + 47/49 + 80.77% + 21/26 100% 3/3 - 95.65% - 44/46 + 95.92% + 47/49 @@ -125,7 +125,7 @@

All files

diff --git a/coverage/lcov-report/index.js.html b/coverage/lcov-report/index.js.html index 74eef48..4360a3a 100644 --- a/coverage/lcov-report/index.js.html +++ b/coverage/lcov-report/index.js.html @@ -321,7 +321,7 @@

All files index.js

imageCompression.handleMaxWidthOrHeight = handleMaxWidthOrHeight imageCompression.followExifOrientation = followExifOrientation imageCompression.cleanupMemory = cleanupCanvasMemory -imageCompression.version = '1.0.8' +imageCompression.version = '1.0.9'   export default imageCompression   @@ -331,7 +331,7 @@

All files index.js

diff --git a/coverage/lcov-report/utils.js.html b/coverage/lcov-report/utils.js.html index 08b6112..28597b4 100644 --- a/coverage/lcov-report/utils.js.html +++ b/coverage/lcov-report/utils.js.html @@ -382,19 +382,19 @@

All files utils.js

      -14x -14x -14x -14x -14x -14x -14x -18315383x +17x +17x +17x +17x +17x +17x +17x +23716492x   -14x -14x -14x -14x +17x +17x +17x +17x       @@ -468,15 +468,15 @@

All files utils.js

      -12x +15x         -12x -12x +15x +15x   -12x +15x       @@ -612,19 +612,19 @@

All files utils.js

      -28x -28x +31x +31x           -28x -28x +31x +31x   -28x -28x -28x +31x +31x +31x       @@ -633,8 +633,8 @@

All files utils.js

      -26x -26x +29x +29x     1x @@ -833,7 +833,7 @@

All files utils.js

const height = canvas.height const maxWidthOrHeight = options.maxWidthOrHeight   - const needToHandle = Number.isInteger(maxWidthOrHeight) && (width > maxWidthOrHeight || height > maxWidthOrHeight) + const needToHandle = Number.isFinite(maxWidthOrHeight) && (width > maxWidthOrHeight || height > maxWidthOrHeight)   let newCanvas = canvas let ctx @@ -946,7 +946,7 @@

All files utils.js

diff --git a/coverage/lcov.info b/coverage/lcov.info index 9708c21..99f42eb 100644 --- a/coverage/lcov.info +++ b/coverage/lcov.info @@ -7,13 +7,13 @@ FNF:3 FNH:3 FNDA:6,compress FNDA:36,incProgress -FNDA:12,setProgress +FNDA:15,setProgress DA:25,6 DA:28,36 DA:29,36 DA:30,0 -DA:35,12 -DA:36,12 +DA:35,15 +DA:36,15 DA:37,0 DA:41,6 DA:43,6 @@ -30,53 +30,63 @@ DA:63,6 DA:65,6 DA:66,6 DA:68,6 -DA:70,4 -DA:71,4 -DA:74,2 -DA:75,2 +DA:69,6 +DA:72,6 +DA:74,4 +DA:75,4 DA:78,2 DA:79,2 -DA:80,6 -DA:81,6 -DA:82,6 -DA:84,6 -DA:86,6 -DA:87,2 -DA:89,6 -DA:91,6 -DA:93,6 -DA:95,6 -DA:96,6 -DA:101,2 -DA:102,2 -DA:103,2 -DA:104,2 -DA:105,2 +DA:80,2 +DA:83,2 +DA:84,2 +DA:85,9 +DA:86,9 +DA:87,9 +DA:89,9 +DA:91,9 +DA:92,2 +DA:94,9 +DA:96,9 +DA:98,9 +DA:100,9 +DA:101,9 +DA:106,2 DA:107,2 DA:108,2 -LF:46 -LH:44 +DA:109,2 +DA:110,2 +DA:112,2 +DA:113,2 +LF:49 +LH:47 BRDA:27,0,0,36 BRDA:29,1,0,0 BRDA:29,1,1,36 BRDA:36,2,0,0 -BRDA:36,2,1,12 +BRDA:36,2,1,15 BRDA:41,3,0,6 BRDA:41,3,1,6 BRDA:58,4,0,6 BRDA:58,4,1,0 BRDA:65,5,0,6 BRDA:65,5,1,6 -BRDA:68,6,0,4 -BRDA:68,6,1,2 -BRDA:79,7,0,8 -BRDA:79,7,1,8 -BRDA:86,8,0,2 -BRDA:86,8,1,4 -BRDA:89,9,0,6 -BRDA:89,9,1,6 -BRF:19 -BRH:16 +BRDA:72,6,0,4 +BRDA:72,6,1,2 +BRDA:72,7,0,6 +BRDA:72,7,1,4 +BRDA:84,8,0,11 +BRDA:84,8,1,11 +BRDA:84,8,2,2 +BRDA:85,9,0,9 +BRDA:85,9,1,0 +BRDA:86,10,0,9 +BRDA:86,10,1,0 +BRDA:91,11,0,2 +BRDA:91,11,1,7 +BRDA:94,12,0,9 +BRDA:94,12,1,9 +BRF:26 +BRH:21 end_of_record TN: SF:lib/index.js @@ -164,23 +174,23 @@ FNDA:13,getDataUrlFromFile FNDA:13,(anonymous_1) FNDA:13,(anonymous_2) FNDA:0,(anonymous_3) -FNDA:14,getFilefromDataUrl -FNDA:14,(anonymous_5) +FNDA:17,getFilefromDataUrl +FNDA:17,(anonymous_5) FNDA:13,loadImage FNDA:13,(anonymous_7) FNDA:13,(anonymous_8) FNDA:0,(anonymous_9) FNDA:12,drawImageInCanvas FNDA:11,drawFileInCanvas -FNDA:12,canvasToFile +FNDA:15,canvasToFile FNDA:1,getExifOrientation FNDA:1,(anonymous_14) FNDA:1,(anonymous_15) FNDA:0,(anonymous_16) FNDA:6,handleMaxWidthOrHeight FNDA:6,followExifOrientation -FNDA:28,getNewCanvasAndCtx -FNDA:26,cleanupCanvasMemory +FNDA:31,getNewCanvasAndCtx +FNDA:29,cleanupCanvasMemory FNDA:0,(anonymous_21) DA:1,1 DA:4,1 @@ -191,18 +201,18 @@ DA:16,13 DA:17,13 DA:18,13 DA:19,13 -DA:32,14 -DA:33,14 -DA:34,14 -DA:35,14 -DA:36,14 -DA:37,14 -DA:38,14 -DA:39,18315383 -DA:41,14 -DA:42,14 -DA:43,14 -DA:44,14 +DA:32,17 +DA:33,17 +DA:34,17 +DA:35,17 +DA:36,17 +DA:37,17 +DA:38,17 +DA:39,23716492 +DA:41,17 +DA:42,17 +DA:43,17 +DA:44,17 DA:68,13 DA:69,13 DA:70,13 @@ -217,13 +227,13 @@ DA:99,11 DA:100,11 DA:102,11 DA:103,11 -DA:118,12 +DA:118,15 DA:119,0 DA:120,0 DA:121,0 -DA:123,12 -DA:124,12 -DA:126,12 +DA:123,15 +DA:124,15 +DA:126,15 DA:138,1 DA:139,1 DA:140,1 @@ -287,18 +297,18 @@ DA:243,6 DA:246,6 DA:248,6 DA:250,6 -DA:262,28 -DA:263,28 +DA:262,31 +DA:263,31 DA:264,0 DA:265,0 DA:266,0 -DA:269,28 -DA:270,28 -DA:272,28 -DA:273,28 -DA:274,28 -DA:283,26 -DA:284,26 +DA:269,31 +DA:270,31 +DA:272,31 +DA:273,31 +DA:274,31 +DA:283,29 +DA:284,29 DA:287,1 DA:289,1 DA:290,0 @@ -319,8 +329,8 @@ BRDA:6,2,3,1 BRDA:31,3,0,2 BRDA:116,4,0,0 BRDA:118,5,0,0 -BRDA:118,5,1,12 -BRDA:118,6,0,12 +BRDA:118,5,1,15 +BRDA:118,6,0,15 BRDA:118,6,1,0 BRDA:142,7,0,1 BRDA:142,7,1,0 diff --git a/coverage/utils.js.html b/coverage/utils.js.html index 08b6112..28597b4 100644 --- a/coverage/utils.js.html +++ b/coverage/utils.js.html @@ -382,19 +382,19 @@

All files utils.js

      -14x -14x -14x -14x -14x -14x -14x -18315383x +17x +17x +17x +17x +17x +17x +17x +23716492x   -14x -14x -14x -14x +17x +17x +17x +17x       @@ -468,15 +468,15 @@

All files utils.js

      -12x +15x         -12x -12x +15x +15x   -12x +15x       @@ -612,19 +612,19 @@

All files utils.js

      -28x -28x +31x +31x           -28x -28x +31x +31x   -28x -28x -28x +31x +31x +31x       @@ -633,8 +633,8 @@

All files utils.js

      -26x -26x +29x +29x     1x @@ -833,7 +833,7 @@

All files utils.js

const height = canvas.height const maxWidthOrHeight = options.maxWidthOrHeight   - const needToHandle = Number.isInteger(maxWidthOrHeight) && (width > maxWidthOrHeight || height > maxWidthOrHeight) + const needToHandle = Number.isFinite(maxWidthOrHeight) && (width > maxWidthOrHeight || height > maxWidthOrHeight)   let newCanvas = canvas let ctx @@ -946,7 +946,7 @@

All files utils.js

diff --git a/dist/browser-image-compression.js b/dist/browser-image-compression.js index ac3af47..a65ae89 100644 --- a/dist/browser-image-compression.js +++ b/dist/browser-image-compression.js @@ -1,9 +1,9 @@ /** * Browser Image Compression - * v1.0.8 + * v1.0.9 * by Donald * https://github.com/Donaldcwl/browser-image-compression */ -!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):(e=e||self).imageCompression=n()}(this,(function(){"use strict";function _defineProperty(e,n,r){return n in e?Object.defineProperty(e,n,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[n]=r,e}function ownKeys(e,n){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var t=Object.getOwnPropertySymbols(e);n&&(t=t.filter((function(n){return Object.getOwnPropertyDescriptor(e,n).enumerable}))),r.push.apply(r,t)}return r}function _objectSpread2(e){for(var n=1;n2&&void 0!==arguments[2]?arguments[2]:Date.now();return new Promise((function(t){for(var i=e.split(","),o=i[0].match(/:(.*?);/)[1],a=atob(i[1]),s=a.length,c=new Uint8Array(s);s--;)c[s]=a.charCodeAt(s);var l=new Blob([c],{type:o});l.name=n,l.lastModified=r,t(l)}))}function loadImage(e){return new Promise((function(n,r){var t=new Image;t.onload=function(){return n(t)},t.onerror=function(e){return r(e)},t.src=e}))}function drawImageInCanvas(e){var n=_slicedToArray(getNewCanvasAndCtx(e.width,e.height),2),r=n[0];return n[1].drawImage(e,0,0,r.width,r.height),r}function drawFileInCanvas(e){return new Promise((function(n,r){var t,i,o=function $Try_1_Post(){try{return i=drawImageInCanvas(t),n([t,i])}catch(e){return r(e)}},a=function $Try_1_Catch(n){try{return getDataUrlFromFile(e).then((function(e){try{return loadImage(e).then((function(e){try{return t=e,o()}catch(e){return r(e)}}),r)}catch(e){return r(e)}}),r)}catch(e){return r(e)}};try{return createImageBitmap(e).then((function(e){try{return t=e,o()}catch(e){return a()}}),a)}catch(e){a()}}))}function canvasToFile(e,n,r,t){var i=arguments.length>4&&void 0!==arguments[4]?arguments[4]:1;return new Promise((function(o,a){var s;return"function"==typeof OffscreenCanvas&&e instanceof OffscreenCanvas?e.convertToBlob({type:n,quality:i}).then(function(e){try{return(s=e).name=r,s.lastModified=t,$If_4.call(this)}catch(e){return a(e)}}.bind(this),a):getFilefromDataUrl(e.toDataURL(n,i),r,t).then(function(e){try{return s=e,$If_4.call(this)}catch(e){return a(e)}}.bind(this),a);function $If_4(){return o(s)}}))}function getExifOrientation(e){return new Promise((function(n,r){var t=new CustomFileReader;t.onload=function(e){var r=new DataView(e.target.result);if(65496!=r.getUint16(0,!1))return n(-2);for(var t=r.byteLength,i=2;io||i>o)){var s=_slicedToArray(getNewCanvasAndCtx(t,i),2);a=s[0],r=s[1],t>i?(a.width=o,a.height=i/t*o):(a.width=t/i*o,a.height=o),r.drawImage(e,0,0,a.width,a.height),cleanupCanvasMemory(e)}return a}function followExifOrientation(e,n){var r=e.width,t=e.height,i=_slicedToArray(getNewCanvasAndCtx(r,t),2),o=i[0],a=i[1];switch(40&&void 0!==arguments[0]?arguments[0]:5;i+=e,"function"==typeof n.onProgress&&n.onProgress(Math.min(i,100))}function setProgress(e){i=Math.min(Math.max(e,i),100),"function"==typeof n.onProgress&&n.onProgress(i)}return i=0,o=n.maxIteration||10,a=1024*n.maxSizeMB*1024,incProgress(),drawFileInCanvas(e).then(function(i){try{var C=_slicedToArray(i,2);return C[0],s=C[1],incProgress(),c=handleMaxWidthOrHeight(s,n),incProgress(),new Promise((function(r,t){var i;if(!(i=n.exifOrientation))return getExifOrientation(e).then(function(e){try{return i=e,$If_2.call(this)}catch(e){return t(e)}}.bind(this),t);function $If_2(){return r(i)}return $If_2.call(this)})).then(function(i){try{return n.exifOrientation=i,incProgress(),l=followExifOrientation(c,n.exifOrientation),incProgress(),u=1,canvasToFile(l,n.fileType||e.type,e.name,e.lastModified,u).then(function(i){try{{if(f=i,incProgress(),m=f.size>a,g=f.size>e.size,!m&&!g)return setProgress(100),r(f);var C;function $Loop_3(){if(o--&&(h>a||h>d)){var r,i,s=_slicedToArray(getNewCanvasAndCtx(r=m?.95*w.width:w.width,i=m?.95*w.height:w.height),2);return v=s[0],s[1].drawImage(w,0,0,r,i),"image/jpeg"===e.type&&(u*=.95),canvasToFile(v,n.fileType||e.type,e.name,e.lastModified,u).then((function(e){try{return y=e,cleanupCanvasMemory(w),w=v,h=y.size,setProgress(Math.min(99,Math.floor((p-h)/(p-a)*100))),$Loop_3}catch(e){return t(e)}}),t)}return[1]}return d=e.size,p=f.size,h=p,w=l,(C=function(e){for(;e;){if(e.then)return void e.then(C,t);try{if(e.pop){if(e.length)return e.pop()?$Loop_3_exit.call(this):e;e=$Loop_3}else e=e.call(this)}catch(e){return t(e)}}}.bind(this))($Loop_3);function $Loop_3_exit(){return cleanupCanvasMemory(w),cleanupCanvasMemory(v),cleanupCanvasMemory(c),cleanupCanvasMemory(l),cleanupCanvasMemory(s),setProgress(100),r(y)}}}catch(e){return t(e)}}.bind(this),t)}catch(e){return t(e)}}.bind(this),t)}catch(e){return t(e)}}.bind(this),t)}))}e&&(Number.isInteger=Number.isInteger||function(e){return"number"==typeof e&&isFinite(e)&&Math.floor(e)===e});var r,t,i=0;function generateLib(){return function createSourceObject(e){return URL.createObjectURL(new Blob([e],{type:"application/javascript"}))}("\n function imageCompression (){return (".concat(imageCompression,").apply(null, arguments)}\n\n imageCompression.getDataUrlFromFile = ").concat(imageCompression.getDataUrlFromFile,"\n imageCompression.getFilefromDataUrl = ").concat(imageCompression.getFilefromDataUrl,"\n imageCompression.loadImage = ").concat(imageCompression.loadImage,"\n imageCompression.drawImageInCanvas = ").concat(imageCompression.drawImageInCanvas,"\n imageCompression.drawFileInCanvas = ").concat(imageCompression.drawFileInCanvas,"\n imageCompression.canvasToFile = ").concat(imageCompression.canvasToFile,"\n imageCompression.getExifOrientation = ").concat(imageCompression.getExifOrientation,"\n imageCompression.handleMaxWidthOrHeight = ").concat(imageCompression.handleMaxWidthOrHeight,"\n imageCompression.followExifOrientation = ").concat(imageCompression.followExifOrientation,"\n imageCompression.cleanupMemory = ").concat(imageCompression.cleanupMemory,"\n\n getDataUrlFromFile = imageCompression.getDataUrlFromFile\n getFilefromDataUrl = imageCompression.getFilefromDataUrl\n loadImage = imageCompression.loadImage\n drawImageInCanvas = imageCompression.drawImageInCanvas\n drawFileInCanvas = imageCompression.drawFileInCanvas\n canvasToFile = imageCompression.canvasToFile\n getExifOrientation = imageCompression.getExifOrientation\n handleMaxWidthOrHeight = imageCompression.handleMaxWidthOrHeight\n followExifOrientation = imageCompression.followExifOrientation\n cleanupMemory = imageCompression.cleanupMemory\n\n getNewCanvasAndCtx = ").concat(getNewCanvasAndCtx,"\n \n CustomFileReader = FileReader\n \n CustomFile = File\n \n function _slicedToArray(arr, n) { return arr }\n \n function _typeof(a) { return typeof a }\n\n function compress (){return (").concat(compress,").apply(null, arguments)}\n "))}function generateWorkerScript(){return function createWorker(e){return"function"==typeof e&&(e="(".concat(f,")()")),new Worker(URL.createObjectURL(new Blob([e])))}("\n let scriptImported = false\n self.addEventListener('message', async (e) => {\n const { file, id, imageCompressionLibUrl, options } = e.data\n options.onProgress = (progress) => self.postMessage({ progress, id })\n try {\n if (!scriptImported) {\n // console.log('[worker] importScripts', imageCompressionLibUrl)\n self.importScripts(imageCompressionLibUrl)\n scriptImported = true\n }\n // console.log('[worker] self', self)\n const compressedFile = await imageCompression(file, options)\n self.postMessage({ file: compressedFile, id })\n } catch (e) {\n // console.error('[worker] error', e)\n self.postMessage({ error: e.message + '\\n' + e.stack, id })\n }\n })\n ")}function imageCompression(e,n){return new Promise((function(o,a){var s,c,l;if(n.maxSizeMB=n.maxSizeMB||Number.POSITIVE_INFINITY,c="boolean"==typeof n.useWebWorker&&n.useWebWorker,delete n.useWebWorker,!(e instanceof Blob||e instanceof CustomFile))return a(new Error("The file given is not an instance of Blob or File"));if(!/^image/.test(e.type))return a(new Error("The file given is not an image"));if(l="undefined"!=typeof WorkerGlobalScope&&self instanceof WorkerGlobalScope,!c||"function"!=typeof Worker||l)return compress(e,n).then(function(e){try{return s=e,$If_3.call(this)}catch(e){return a(e)}}.bind(this),a);var u=function(){try{return $If_3.call(this)}catch(e){return a(e)}}.bind(this),f=function $Try_1_Catch(r){try{return compress(e,n).then((function(e){try{return s=e,u()}catch(e){return a(e)}}),a)}catch(e){return a(e)}};try{return function compressOnWebWorker(e,n){return new Promise((function(o,a){return new Promise((function(s,c){var l=i++;return r||(r=generateLib()),t||(t=generateWorkerScript()),t.addEventListener("message",(function handler(e){if(e.data.id===l){if(void 0!==e.data.progress&&e.data.progress<100)return void n.onProgress(e.data.progress);t.removeEventListener("message",handler),e.data.error&&a(new Error(e.data.error)),o(e.data.file)}})),t.postMessage({file:e,id:l,imageCompressionLibUrl:r,options:_objectSpread2({},n,{onProgress:void 0})}),s()}))}))}(e,n).then((function(e){try{return s=e,u()}catch(e){return f()}}),f)}catch(e){f()}function $If_3(){try{s.name=e.name,s.lastModified=e.lastModified}catch(e){}return o(s)}}))}return imageCompression.getDataUrlFromFile=getDataUrlFromFile,imageCompression.getFilefromDataUrl=getFilefromDataUrl,imageCompression.loadImage=loadImage,imageCompression.drawImageInCanvas=drawImageInCanvas,imageCompression.drawFileInCanvas=drawFileInCanvas,imageCompression.canvasToFile=canvasToFile,imageCompression.getExifOrientation=getExifOrientation,imageCompression.handleMaxWidthOrHeight=handleMaxWidthOrHeight,imageCompression.followExifOrientation=followExifOrientation,imageCompression.cleanupMemory=cleanupCanvasMemory,imageCompression.version="1.0.8",imageCompression})); +!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):(e=e||self).imageCompression=n()}(this,(function(){"use strict";function _defineProperty(e,n,r){return n in e?Object.defineProperty(e,n,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[n]=r,e}function ownKeys(e,n){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var t=Object.getOwnPropertySymbols(e);n&&(t=t.filter((function(n){return Object.getOwnPropertyDescriptor(e,n).enumerable}))),r.push.apply(r,t)}return r}function _objectSpread2(e){for(var n=1;n2&&void 0!==arguments[2]?arguments[2]:Date.now();return new Promise((function(t){for(var i=e.split(","),o=i[0].match(/:(.*?);/)[1],a=atob(i[1]),s=a.length,c=new Uint8Array(s);s--;)c[s]=a.charCodeAt(s);var l=new Blob([c],{type:o});l.name=n,l.lastModified=r,t(l)}))}function loadImage(e){return new Promise((function(n,r){var t=new Image;t.onload=function(){return n(t)},t.onerror=function(e){return r(e)},t.src=e}))}function drawImageInCanvas(e){var n=_slicedToArray(getNewCanvasAndCtx(e.width,e.height),2),r=n[0];return n[1].drawImage(e,0,0,r.width,r.height),r}function drawFileInCanvas(e){return new Promise((function(n,r){var t,i,o=function $Try_1_Post(){try{return i=drawImageInCanvas(t),n([t,i])}catch(e){return r(e)}},a=function $Try_1_Catch(n){try{return getDataUrlFromFile(e).then((function(e){try{return loadImage(e).then((function(e){try{return t=e,o()}catch(e){return r(e)}}),r)}catch(e){return r(e)}}),r)}catch(e){return r(e)}};try{return createImageBitmap(e).then((function(e){try{return t=e,o()}catch(e){return a()}}),a)}catch(e){a()}}))}function canvasToFile(e,n,r,t){var i=arguments.length>4&&void 0!==arguments[4]?arguments[4]:1;return new Promise((function(o,a){var s;return"function"==typeof OffscreenCanvas&&e instanceof OffscreenCanvas?e.convertToBlob({type:n,quality:i}).then(function(e){try{return(s=e).name=r,s.lastModified=t,$If_4.call(this)}catch(e){return a(e)}}.bind(this),a):getFilefromDataUrl(e.toDataURL(n,i),r,t).then(function(e){try{return s=e,$If_4.call(this)}catch(e){return a(e)}}.bind(this),a);function $If_4(){return o(s)}}))}function getExifOrientation(e){return new Promise((function(n,r){var t=new CustomFileReader;t.onload=function(e){var r=new DataView(e.target.result);if(65496!=r.getUint16(0,!1))return n(-2);for(var t=r.byteLength,i=2;io||i>o)){var s=_slicedToArray(getNewCanvasAndCtx(t,i),2);a=s[0],r=s[1],t>i?(a.width=o,a.height=i/t*o):(a.width=t/i*o,a.height=o),r.drawImage(e,0,0,a.width,a.height),cleanupCanvasMemory(e)}return a}function followExifOrientation(e,n){var r=e.width,t=e.height,i=_slicedToArray(getNewCanvasAndCtx(r,t),2),o=i[0],a=i[1];switch(40&&void 0!==arguments[0]?arguments[0]:5;i+=e,"function"==typeof n.onProgress&&n.onProgress(Math.min(i,100))}function setProgress(e){i=Math.min(Math.max(e,i),100),"function"==typeof n.onProgress&&n.onProgress(i)}return i=0,o=n.maxIteration||10,a=1024*n.maxSizeMB*1024,incProgress(),drawFileInCanvas(e).then(function(i){try{var C=_slicedToArray(i,2);return C[0],s=C[1],incProgress(),c=handleMaxWidthOrHeight(s,n),incProgress(),new Promise((function(r,t){var i;if(!(i=n.exifOrientation))return getExifOrientation(e).then(function(e){try{return i=e,$If_2.call(this)}catch(e){return t(e)}}.bind(this),t);function $If_2(){return r(i)}return $If_2.call(this)})).then(function(i){try{return n.exifOrientation=i,incProgress(),l=followExifOrientation(c,n.exifOrientation),incProgress(),u=1,canvasToFile(l,n.fileType||e.type,e.name,e.lastModified,u).then(function(i){try{{if(f=i,incProgress(),m=f.size>a,g=f.size>e.size,!m&&!g)return setProgress(100),r(f);var C;function $Loop_3(){if(o--&&(h>a||h>d)){var r,i,s=_slicedToArray(getNewCanvasAndCtx(r=m?.95*w.width:w.width,i=m?.95*w.height:w.height),2);return v=s[0],s[1].drawImage(w,0,0,r,i),"image/jpeg"===e.type&&(u*=.95),canvasToFile(v,n.fileType||e.type,e.name,e.lastModified,u).then((function(e){try{return y=e,cleanupCanvasMemory(w),w=v,h=y.size,setProgress(Math.min(99,Math.floor((p-h)/(p-a)*100))),$Loop_3}catch(e){return t(e)}}),t)}return[1]}return d=e.size,p=f.size,h=p,w=l,(C=function(e){for(;e;){if(e.then)return void e.then(C,t);try{if(e.pop){if(e.length)return e.pop()?$Loop_3_exit.call(this):e;e=$Loop_3}else e=e.call(this)}catch(e){return t(e)}}}.bind(this))($Loop_3);function $Loop_3_exit(){return cleanupCanvasMemory(w),cleanupCanvasMemory(v),cleanupCanvasMemory(c),cleanupCanvasMemory(l),cleanupCanvasMemory(s),setProgress(100),r(y)}}}catch(e){return t(e)}}.bind(this),t)}catch(e){return t(e)}}.bind(this),t)}catch(e){return t(e)}}.bind(this),t)}))}e&&(Number.isInteger=Number.isInteger||function(e){return"number"==typeof e&&isFinite(e)&&Math.floor(e)===e});var r,t,i=0;function generateLib(){return function createSourceObject(e){return URL.createObjectURL(new Blob([e],{type:"application/javascript"}))}("\n function imageCompression (){return (".concat(imageCompression,").apply(null, arguments)}\n\n imageCompression.getDataUrlFromFile = ").concat(imageCompression.getDataUrlFromFile,"\n imageCompression.getFilefromDataUrl = ").concat(imageCompression.getFilefromDataUrl,"\n imageCompression.loadImage = ").concat(imageCompression.loadImage,"\n imageCompression.drawImageInCanvas = ").concat(imageCompression.drawImageInCanvas,"\n imageCompression.drawFileInCanvas = ").concat(imageCompression.drawFileInCanvas,"\n imageCompression.canvasToFile = ").concat(imageCompression.canvasToFile,"\n imageCompression.getExifOrientation = ").concat(imageCompression.getExifOrientation,"\n imageCompression.handleMaxWidthOrHeight = ").concat(imageCompression.handleMaxWidthOrHeight,"\n imageCompression.followExifOrientation = ").concat(imageCompression.followExifOrientation,"\n imageCompression.cleanupMemory = ").concat(imageCompression.cleanupMemory,"\n\n getDataUrlFromFile = imageCompression.getDataUrlFromFile\n getFilefromDataUrl = imageCompression.getFilefromDataUrl\n loadImage = imageCompression.loadImage\n drawImageInCanvas = imageCompression.drawImageInCanvas\n drawFileInCanvas = imageCompression.drawFileInCanvas\n canvasToFile = imageCompression.canvasToFile\n getExifOrientation = imageCompression.getExifOrientation\n handleMaxWidthOrHeight = imageCompression.handleMaxWidthOrHeight\n followExifOrientation = imageCompression.followExifOrientation\n cleanupMemory = imageCompression.cleanupMemory\n\n getNewCanvasAndCtx = ").concat(getNewCanvasAndCtx,"\n \n CustomFileReader = FileReader\n \n CustomFile = File\n \n function _slicedToArray(arr, n) { return arr }\n \n function _typeof(a) { return typeof a }\n\n function compress (){return (").concat(compress,").apply(null, arguments)}\n "))}function generateWorkerScript(){return function createWorker(e){return"function"==typeof e&&(e="(".concat(f,")()")),new Worker(URL.createObjectURL(new Blob([e])))}("\n let scriptImported = false\n self.addEventListener('message', async (e) => {\n const { file, id, imageCompressionLibUrl, options } = e.data\n options.onProgress = (progress) => self.postMessage({ progress, id })\n try {\n if (!scriptImported) {\n // console.log('[worker] importScripts', imageCompressionLibUrl)\n self.importScripts(imageCompressionLibUrl)\n scriptImported = true\n }\n // console.log('[worker] self', self)\n const compressedFile = await imageCompression(file, options)\n self.postMessage({ file: compressedFile, id })\n } catch (e) {\n // console.error('[worker] error', e)\n self.postMessage({ error: e.message + '\\n' + e.stack, id })\n }\n })\n ")}function imageCompression(e,n){return new Promise((function(o,a){var s,c,l;if(n.maxSizeMB=n.maxSizeMB||Number.POSITIVE_INFINITY,c="boolean"==typeof n.useWebWorker&&n.useWebWorker,delete n.useWebWorker,!(e instanceof Blob||e instanceof CustomFile))return a(new Error("The file given is not an instance of Blob or File"));if(!/^image/.test(e.type))return a(new Error("The file given is not an image"));if(l="undefined"!=typeof WorkerGlobalScope&&self instanceof WorkerGlobalScope,!c||"function"!=typeof Worker||l)return compress(e,n).then(function(e){try{return s=e,$If_3.call(this)}catch(e){return a(e)}}.bind(this),a);var u=function(){try{return $If_3.call(this)}catch(e){return a(e)}}.bind(this),f=function $Try_1_Catch(r){try{return compress(e,n).then((function(e){try{return s=e,u()}catch(e){return a(e)}}),a)}catch(e){return a(e)}};try{return function compressOnWebWorker(e,n){return new Promise((function(o,a){return new Promise((function(s,c){var l=i++;return r||(r=generateLib()),t||(t=generateWorkerScript()),t.addEventListener("message",(function handler(e){if(e.data.id===l){if(void 0!==e.data.progress&&e.data.progress<100)return void n.onProgress(e.data.progress);t.removeEventListener("message",handler),e.data.error&&a(new Error(e.data.error)),o(e.data.file)}})),t.postMessage({file:e,id:l,imageCompressionLibUrl:r,options:_objectSpread2({},n,{onProgress:void 0})}),s()}))}))}(e,n).then((function(e){try{return s=e,u()}catch(e){return f()}}),f)}catch(e){f()}function $If_3(){try{s.name=e.name,s.lastModified=e.lastModified}catch(e){}return o(s)}}))}return imageCompression.getDataUrlFromFile=getDataUrlFromFile,imageCompression.getFilefromDataUrl=getFilefromDataUrl,imageCompression.loadImage=loadImage,imageCompression.drawImageInCanvas=drawImageInCanvas,imageCompression.drawFileInCanvas=drawFileInCanvas,imageCompression.canvasToFile=canvasToFile,imageCompression.getExifOrientation=getExifOrientation,imageCompression.handleMaxWidthOrHeight=handleMaxWidthOrHeight,imageCompression.followExifOrientation=followExifOrientation,imageCompression.cleanupMemory=cleanupCanvasMemory,imageCompression.version="1.0.9",imageCompression})); //# sourceMappingURL=browser-image-compression.js.map diff --git a/dist/browser-image-compression.js.map b/dist/browser-image-compression.js.map index e99b9f9..b65c02f 100644 --- a/dist/browser-image-compression.js.map +++ b/dist/browser-image-compression.js.map @@ -1 +1 @@ -{"version":3,"file":"browser-image-compression.js","sources":["../lib/utils.js","../lib/image-compression.js","../lib/web-worker.js","../lib/index.js"],"sourcesContent":["const isBrowser = typeof window !== 'undefined' // change browser environment to support SSR\n\n// add support for cordova-plugin-file\nconst moduleMapper = isBrowser && window.cordova && window.cordova.require && window.cordova.require('cordova/modulemapper')\nexport const CustomFile = isBrowser && ((moduleMapper && moduleMapper.getOriginalSymbol(window, 'File')) || File)\nexport const CustomFileReader = isBrowser && ((moduleMapper && moduleMapper.getOriginalSymbol(window, 'FileReader')) || FileReader)\n\n/**\n * getDataUrlFromFile\n *\n * @param {File} file\n * @returns {Promise}\n */\nexport function getDataUrlFromFile (file) {\n return new Promise((resolve, reject) => {\n const reader = new CustomFileReader()\n reader.onload = () => resolve(reader.result)\n reader.onerror = (e) => reject(e)\n reader.readAsDataURL(file)\n })\n}\n\n/**\n * getFilefromDataUrl\n *\n * @param {string} dataurl\n * @param {string} filename\n * @param {number} [lastModified=Date.now()]\n * @returns {Promise}\n */\nexport function getFilefromDataUrl (dataurl, filename, lastModified = Date.now()) {\n return new Promise((resolve) => {\n const arr = dataurl.split(',')\n const mime = arr[0].match(/:(.*?);/)[1]\n const bstr = atob(arr[1])\n let n = bstr.length\n const u8arr = new Uint8Array(n)\n while (n--) {\n u8arr[n] = bstr.charCodeAt(n)\n }\n const file = new Blob([u8arr], { type: mime })\n file.name = filename\n file.lastModified = lastModified\n resolve(file)\n\n // Safari has issue with File constructor not being able to POST in FormData\n // https://github.com/Donaldcwl/browser-image-compression/issues/8\n // https://bugs.webkit.org/show_bug.cgi?id=165081\n // let file\n // try {\n // file = new File([u8arr], filename, { type: mime }) // Edge do not support File constructor\n // } catch (e) {\n // file = new Blob([u8arr], { type: mime })\n // file.name = filename\n // file.lastModified = lastModified\n // }\n // resolve(file)\n })\n}\n\n/**\n * loadImage\n *\n * @param {string} src\n * @returns {Promise}\n */\nexport function loadImage (src) {\n return new Promise((resolve, reject) => {\n const img = new Image()\n img.onload = () => resolve(img)\n img.onerror = (e) => reject(e)\n img.src = src\n })\n}\n\n/**\n * drawImageInCanvas\n *\n * @param {HTMLImageElement} img\n * @returns {HTMLCanvasElement}\n */\nexport function drawImageInCanvas (img) {\n const [canvas, ctx] = getNewCanvasAndCtx(img.width, img.height)\n ctx.drawImage(img, 0, 0, canvas.width, canvas.height)\n return canvas\n}\n\n/**\n * drawFileInCanvas\n *\n * @param {File} file\n * @returns {Promise<[ImageBitmap | HTMLImageElement, HTMLCanvasElement]>}\n */\nexport async function drawFileInCanvas (file) {\n let img\n try {\n img = await createImageBitmap(file)\n } catch (e) {\n const dataUrl = await getDataUrlFromFile(file)\n img = await loadImage(dataUrl)\n }\n const canvas = drawImageInCanvas(img)\n return [img, canvas]\n}\n\n/**\n * canvasToFile\n *\n * @param canvas\n * @param {string} fileType\n * @param {string} fileName\n * @param {number} fileLastModified\n * @param {number} [quality]\n * @returns {Promise}\n */\nexport async function canvasToFile (canvas, fileType, fileName, fileLastModified, quality = 1) {\n let file\n if (typeof OffscreenCanvas === 'function' && canvas instanceof OffscreenCanvas) {\n file = await canvas.convertToBlob({ type: fileType, quality })\n file.name = fileName\n file.lastModified = fileLastModified\n } else {\n const dataUrl = canvas.toDataURL(fileType, quality)\n file = await getFilefromDataUrl(dataUrl, fileName, fileLastModified)\n }\n return file\n}\n\n/**\n * getExifOrientation\n * get image exif orientation info\n * source: https://stackoverflow.com/a/32490603/10395024\n *\n * @param {File} file\n * @returns {Promise} - orientation id, see https://i.stack.imgur.com/VGsAj.gif\n */\nexport function getExifOrientation (file) {\n return new Promise((resolve, reject) => {\n const reader = new CustomFileReader()\n reader.onload = (e) => {\n const view = new DataView(e.target.result)\n if (view.getUint16(0, false) != 0xFFD8) {\n return resolve(-2)\n }\n const length = view.byteLength\n let offset = 2\n while (offset < length) {\n if (view.getUint16(offset + 2, false) <= 8) return resolve(-1)\n const marker = view.getUint16(offset, false)\n offset += 2\n if (marker == 0xFFE1) {\n if (view.getUint32(offset += 2, false) != 0x45786966) {\n return resolve(-1)\n }\n\n var little = view.getUint16(offset += 6, false) == 0x4949\n offset += view.getUint32(offset + 4, little)\n var tags = view.getUint16(offset, little)\n offset += 2\n for (var i = 0; i < tags; i++) {\n if (view.getUint16(offset + (i * 12), little) == 0x0112) {\n return resolve(view.getUint16(offset + (i * 12) + 8, little))\n }\n }\n } else if ((marker & 0xFF00) != 0xFF00) {\n break\n } else {\n offset += view.getUint16(offset, false)\n }\n }\n return resolve(-1)\n }\n reader.onerror = (e) => reject(e)\n reader.readAsArrayBuffer(file)\n })\n}\n\n/**\n *\n * @param {HTMLCanvasElement} canvas\n * @param options\n * @returns {HTMLCanvasElement>}\n */\nexport function handleMaxWidthOrHeight (canvas, options) {\n const width = canvas.width\n const height = canvas.height\n const maxWidthOrHeight = options.maxWidthOrHeight\n\n const needToHandle = Number.isFinite(maxWidthOrHeight) && (width > maxWidthOrHeight || height > maxWidthOrHeight)\n\n let newCanvas = canvas\n let ctx\n\n if (needToHandle) {\n [newCanvas, ctx] = getNewCanvasAndCtx(width, height)\n if (width > height) {\n newCanvas.width = maxWidthOrHeight\n newCanvas.height = (height / width) * maxWidthOrHeight\n } else {\n newCanvas.width = (width / height) * maxWidthOrHeight\n newCanvas.height = maxWidthOrHeight\n }\n ctx.drawImage(canvas, 0, 0, newCanvas.width, newCanvas.height)\n\n cleanupCanvasMemory(canvas)\n }\n\n return newCanvas\n}\n\n/**\n * followExifOrientation\n * source: https://stackoverflow.com/a/40867559/10395024\n *\n * @param {HTMLCanvasElement} canvas\n * @param {number} exifOrientation\n * @returns {HTMLCanvasElement} canvas\n */\nexport function followExifOrientation (canvas, exifOrientation) {\n const width = canvas.width\n const height = canvas.height\n\n const [newCanvas, ctx] = getNewCanvasAndCtx(width, height)\n\n // set proper canvas dimensions before transform & export\n if (4 < exifOrientation && exifOrientation < 9) {\n newCanvas.width = height\n newCanvas.height = width\n } else {\n newCanvas.width = width\n newCanvas.height = height\n }\n\n // transform context before drawing image\n switch (exifOrientation) {\n case 2: ctx.transform(-1, 0, 0, 1, width, 0); break;\n case 3: ctx.transform(-1, 0, 0, -1, width, height); break;\n case 4: ctx.transform(1, 0, 0, -1, 0, height); break;\n case 5: ctx.transform(0, 1, 1, 0, 0, 0); break;\n case 6: ctx.transform(0, 1, -1, 0, height, 0); break;\n case 7: ctx.transform(0, -1, -1, 0, height, width); break;\n case 8: ctx.transform(0, -1, 1, 0, 0, width); break;\n default: break;\n }\n\n ctx.drawImage(canvas, 0, 0, width, height)\n\n cleanupCanvasMemory(canvas)\n\n return newCanvas\n}\n\n/**\n * get new Canvas and it's context\n * @param width\n * @param height\n * @returns {[HTMLCanvasElement, CanvasRenderingContext2D]}\n */\nexport function getNewCanvasAndCtx (width, height) {\n let canvas\n let ctx\n try {\n canvas = new OffscreenCanvas(width, height)\n ctx = canvas.getContext('2d')\n if (ctx === null) {\n throw new Error('getContext of OffscreenCanvas returns null')\n }\n } catch (e) {\n canvas = document.createElement('canvas')\n ctx = canvas.getContext('2d')\n }\n canvas.width = width\n canvas.height = height\n return [canvas, ctx]\n}\n\n/**\n * clear Canvas memory\n * @param canvas\n * @returns null\n */\nexport function cleanupCanvasMemory (canvas) {\n canvas.width = 0\n canvas.height = 0\n}\n\nif (isBrowser) {\n// Polyfill for Number.isInteger\n Number.isInteger = Number.isInteger || function (value) {\n return typeof value === 'number' &&\n isFinite(value) &&\n Math.floor(value) === value\n }\n}\n","import {\n canvasToFile,\n cleanupCanvasMemory,\n drawFileInCanvas,\n followExifOrientation,\n getExifOrientation,\n getNewCanvasAndCtx,\n handleMaxWidthOrHeight\n} from './utils'\n\n/**\n * Compress an image file.\n *\n * @param {File} file\n * @param {Object} options - { maxSizeMB=Number.POSITIVE_INFINITY, maxWidthOrHeight, useWebWorker=false, maxIteration = 10, exifOrientation, fileType }\n * @param {number} [options.maxSizeMB=Number.POSITIVE_INFINITY]\n * @param {number} [options.maxWidthOrHeight=undefined]\n * @param {number} [options.maxIteration=10]\n * @param {number} [options.exifOrientation] - default to be the exif orientation from the image file\n * @param {Function} [options.onProgress] - a function takes one progress argument (progress from 0 to 100)\n * @param {string} [options.fileType] - default to be the original mime type from the image file\n * @returns {Promise}\n */\nexport default async function compress (file, options) {\n let progress = 0\n\n function incProgress (inc = 5) {\n progress += inc\n if (typeof options.onProgress === 'function') {\n options.onProgress(Math.min(progress, 100))\n }\n }\n\n function setProgress (p) {\n progress = Math.min(Math.max(p, progress), 100)\n if (typeof options.onProgress === 'function') {\n options.onProgress(progress)\n }\n }\n\n let remainingTrials = options.maxIteration || 10\n\n const maxSizeByte = options.maxSizeMB * 1024 * 1024\n\n incProgress()\n\n // drawFileInCanvas\n let [img, origCanvas] = await drawFileInCanvas(file)\n\n incProgress()\n\n // handleMaxWidthOrHeight\n const maxWidthOrHeightFixedCanvas = handleMaxWidthOrHeight(origCanvas, options)\n\n incProgress()\n\n // exifOrientation\n options.exifOrientation = options.exifOrientation || await getExifOrientation(file)\n incProgress()\n const orientationFixedCanvas = followExifOrientation(maxWidthOrHeightFixedCanvas, options.exifOrientation)\n incProgress()\n\n let quality = 1\n\n let tempFile = await canvasToFile(orientationFixedCanvas, options.fileType || file.type, file.name, file.lastModified, quality)\n incProgress()\n\n const origExceedMaxSize = tempFile.size > maxSizeByte\n const sizeBecomeLarger = tempFile.size > file.size\n\n // check if we need to compress or resize\n if (!origExceedMaxSize && !sizeBecomeLarger) {\n // no need to compress\n setProgress(100)\n return tempFile\n }\n\n const sourceSize = file.size\n const renderedSize = tempFile.size\n let currentSize = renderedSize\n let compressedFile\n let newCanvas, ctx\n let canvas = orientationFixedCanvas\n while (remainingTrials-- && (currentSize > maxSizeByte || currentSize > sourceSize)) {\n const newWidth = origExceedMaxSize ? canvas.width * 0.95 : canvas.width\n const newHeight = origExceedMaxSize ? canvas.height * 0.95 : canvas.height;\n [newCanvas, ctx] = getNewCanvasAndCtx(newWidth, newHeight)\n\n ctx.drawImage(canvas, 0, 0, newWidth, newHeight)\n\n if (file.type === 'image/jpeg') {\n quality *= 0.95\n }\n compressedFile = await canvasToFile(newCanvas, options.fileType || file.type, file.name, file.lastModified, quality)\n\n cleanupCanvasMemory(canvas)\n\n canvas = newCanvas\n\n currentSize = compressedFile.size\n setProgress(Math.min(99, Math.floor((renderedSize - currentSize) / (renderedSize - maxSizeByte) * 100)))\n }\n\n // garbage clean canvas for safari\n // ref: https://bugs.webkit.org/show_bug.cgi?id=195325\n cleanupCanvasMemory(canvas)\n cleanupCanvasMemory(newCanvas)\n cleanupCanvasMemory(maxWidthOrHeightFixedCanvas)\n cleanupCanvasMemory(orientationFixedCanvas)\n cleanupCanvasMemory(origCanvas)\n\n setProgress(100)\n return compressedFile\n}\n","import lib from './index'\nimport compress from './image-compression'\nimport { getNewCanvasAndCtx } from './utils'\n\nlet cnt = 0\nlet imageCompressionLibUrl\nlet worker\n\nfunction createWorker (script) {\n if (typeof script === 'function') {\n script = `(${f})()`\n }\n return new Worker(URL.createObjectURL(new Blob([script])))\n}\n\nfunction createSourceObject (str) {\n return URL.createObjectURL(new Blob([str], { type: 'application/javascript' }))\n}\n\nfunction generateLib () {\n // prepare the lib to be used inside WebWorker\n return createSourceObject(`\n function imageCompression (){return (${lib}).apply(null, arguments)}\n\n imageCompression.getDataUrlFromFile = ${lib.getDataUrlFromFile}\n imageCompression.getFilefromDataUrl = ${lib.getFilefromDataUrl}\n imageCompression.loadImage = ${lib.loadImage}\n imageCompression.drawImageInCanvas = ${lib.drawImageInCanvas}\n imageCompression.drawFileInCanvas = ${lib.drawFileInCanvas}\n imageCompression.canvasToFile = ${lib.canvasToFile}\n imageCompression.getExifOrientation = ${lib.getExifOrientation}\n imageCompression.handleMaxWidthOrHeight = ${lib.handleMaxWidthOrHeight}\n imageCompression.followExifOrientation = ${lib.followExifOrientation}\n imageCompression.cleanupMemory = ${lib.cleanupMemory}\n\n getDataUrlFromFile = imageCompression.getDataUrlFromFile\n getFilefromDataUrl = imageCompression.getFilefromDataUrl\n loadImage = imageCompression.loadImage\n drawImageInCanvas = imageCompression.drawImageInCanvas\n drawFileInCanvas = imageCompression.drawFileInCanvas\n canvasToFile = imageCompression.canvasToFile\n getExifOrientation = imageCompression.getExifOrientation\n handleMaxWidthOrHeight = imageCompression.handleMaxWidthOrHeight\n followExifOrientation = imageCompression.followExifOrientation\n cleanupMemory = imageCompression.cleanupMemory\n\n getNewCanvasAndCtx = ${getNewCanvasAndCtx}\n \n CustomFileReader = FileReader\n \n CustomFile = File\n \n function _slicedToArray(arr, n) { return arr }\n \n function _typeof(a) { return typeof a }\n\n function compress (){return (${compress}).apply(null, arguments)}\n `)\n}\n\nfunction generateWorkerScript () {\n // code to be run in the WebWorker\n return createWorker(`\n let scriptImported = false\n self.addEventListener('message', async (e) => {\n const { file, id, imageCompressionLibUrl, options } = e.data\n options.onProgress = (progress) => self.postMessage({ progress, id })\n try {\n if (!scriptImported) {\n // console.log('[worker] importScripts', imageCompressionLibUrl)\n self.importScripts(imageCompressionLibUrl)\n scriptImported = true\n }\n // console.log('[worker] self', self)\n const compressedFile = await imageCompression(file, options)\n self.postMessage({ file: compressedFile, id })\n } catch (e) {\n // console.error('[worker] error', e)\n self.postMessage({ error: e.message + '\\\\n' + e.stack, id })\n }\n })\n `)\n}\n\nexport function compressOnWebWorker (file, options) {\n return new Promise(async (resolve, reject) => {\n let id = cnt++\n\n if (!imageCompressionLibUrl) {\n imageCompressionLibUrl = generateLib()\n }\n\n if (!worker) {\n worker = generateWorkerScript()\n }\n\n function handler (e) {\n if (e.data.id === id) {\n if (e.data.progress !== undefined && e.data.progress < 100) {\n options.onProgress(e.data.progress)\n return\n }\n worker.removeEventListener('message', handler)\n if (e.data.error) {\n reject(new Error(e.data.error))\n }\n resolve(e.data.file)\n }\n }\n\n worker.addEventListener('message', handler)\n worker.postMessage({\n file,\n id,\n imageCompressionLibUrl,\n options: { ...options, onProgress: undefined }\n })\n })\n}\n","import compress from './image-compression'\nimport {\n canvasToFile,\n drawFileInCanvas,\n drawImageInCanvas,\n getDataUrlFromFile,\n getFilefromDataUrl,\n loadImage,\n getExifOrientation,\n handleMaxWidthOrHeight,\n followExifOrientation,\n CustomFile,\n cleanupCanvasMemory\n} from './utils'\nimport { compressOnWebWorker } from './web-worker'\n\n/**\n * Compress an image file.\n *\n * @param {File} file\n * @param {Object} options - { maxSizeMB=Number.POSITIVE_INFINITY, maxWidthOrHeight, useWebWorker=false, maxIteration = 10, exifOrientation, fileType }\n * @param {number} [options.maxSizeMB=Number.POSITIVE_INFINITY]\n * @param {number} [options.maxWidthOrHeight=undefined]\n * @param {boolean} [options.useWebWorker=false]\n * @param {number} [options.maxIteration=10]\n * @param {number} [options.exifOrientation] - default to be the exif orientation from the image file\n * @param {Function} [options.onProgress] - a function takes one progress argument (progress from 0 to 100)\n * @param {string} [options.fileType] - default to be the original mime type from the image file\n * @returns {Promise}\n */\nasync function imageCompression (file, options) {\n\n let compressedFile\n\n options.maxSizeMB = options.maxSizeMB || Number.POSITIVE_INFINITY\n const useWebWorker = typeof options.useWebWorker === 'boolean' ? options.useWebWorker : false\n delete options.useWebWorker\n\n if (!(file instanceof Blob || file instanceof CustomFile)) {\n throw new Error('The file given is not an instance of Blob or File')\n } else if (!/^image/.test(file.type)) {\n throw new Error('The file given is not an image')\n }\n\n // try run in web worker, fall back to run in main thread\n const inWebWorker = typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope\n\n // if ((useWebWorker && typeof Worker === 'function') || inWebWorker) {\n // console.log('run compression in web worker')\n // } else {\n // console.log('run compression in main thread')\n // }\n\n if (useWebWorker && typeof Worker === 'function' && !inWebWorker) {\n try {\n // console.log(1)\n // \"compressOnWebWorker\" is kind of like a recursion to call \"imageCompression\" again inside web worker\n compressedFile = await compressOnWebWorker(file, options)\n } catch (e) {\n // console.warn('Run compression in web worker failed:', e, ', fall back to main thread')\n // console.log(1.5)\n compressedFile = await compress(file, options)\n }\n } else {\n // console.log(2)\n compressedFile = await compress(file, options)\n }\n\n try {\n compressedFile.name = file.name\n compressedFile.lastModified = file.lastModified\n } catch (e) {}\n\n return compressedFile\n}\n\nimageCompression.getDataUrlFromFile = getDataUrlFromFile\nimageCompression.getFilefromDataUrl = getFilefromDataUrl\nimageCompression.loadImage = loadImage\nimageCompression.drawImageInCanvas = drawImageInCanvas\nimageCompression.drawFileInCanvas = drawFileInCanvas\nimageCompression.canvasToFile = canvasToFile\nimageCompression.getExifOrientation = getExifOrientation\nimageCompression.handleMaxWidthOrHeight = handleMaxWidthOrHeight\nimageCompression.followExifOrientation = followExifOrientation\nimageCompression.cleanupMemory = cleanupCanvasMemory\nimageCompression.version = '1.0.8'\n\nexport default imageCompression\n"],"names":["isBrowser","window","moduleMapper","getDataUrlFromFile","resolve","reader","dataurl","split","n","bstr","filename","file","lastModified","canvas","view","length","offset","getUint32","marker","maxWidthOrHeight","width","followExifOrientation","height","ctx","inc","drawFileInCanvas","exifOrientation","orientationFixedCanvas","options","fileType","imageCompressionLibUrl","cnt","generateLib","script","imageCompression","getFilefromDataUrl","loadImage","drawImageInCanvas","canvasToFile","getExifOrientation"],"mappings":";;;;;;;m/CAAA,IAAMA,EAA8B,oBAAlBC,2EAKoF,uCAQ5DC,2HAiBnC,SAASC,mDACPC,6DAEqBC,uNAiCdC,EAAAC,mCACK,gBAEjBC,EAAIC,6BAEJD,8DAUYE,EACdC,EAAAC,eACAR,28BAwC2CS,qcAoB7CC,mEAEMV,mCAGQW,6DAEgBC,8GASpBF,EAAAG,UAAgBD,0GAKPA,EAAA,iCAGTE,0KA+BVC,oJAqBuBC,4CAQTP,EAAV,0CAEAA,qBASYQ,yCACZC,mPAmBuCA,wJAI9B,sHA8BbC,2BAHWD,kaC3OTE,iLAPK,6DAUGC,iBAARd,yKAUMe,sUAWAC,EAAwBC,EAAAC,6PAsB5BhB,o8BC/FJ,IACIiB,IADAC,IAcJ,SAASC,okEAPPC,gkDCyDwBtB,0zBAa1BuB,iBAAA/B,mBAAsCA,mBACtC+B,iBAAAC,sCACAD,iBAAAE,oBACAF,iBAAAG,oCACAH,iBAAAT,kCAEAS,iBAAAI,0BAxFAJ,iBAAAK"} \ No newline at end of file +{"version":3,"file":"browser-image-compression.js","sources":["../lib/utils.js","../lib/image-compression.js","../lib/web-worker.js","../lib/index.js"],"sourcesContent":["const isBrowser = typeof window !== 'undefined' // change browser environment to support SSR\n\n// add support for cordova-plugin-file\nconst moduleMapper = isBrowser && window.cordova && window.cordova.require && window.cordova.require('cordova/modulemapper')\nexport const CustomFile = isBrowser && ((moduleMapper && moduleMapper.getOriginalSymbol(window, 'File')) || File)\nexport const CustomFileReader = isBrowser && ((moduleMapper && moduleMapper.getOriginalSymbol(window, 'FileReader')) || FileReader)\n\n/**\n * getDataUrlFromFile\n *\n * @param {File} file\n * @returns {Promise}\n */\nexport function getDataUrlFromFile (file) {\n return new Promise((resolve, reject) => {\n const reader = new CustomFileReader()\n reader.onload = () => resolve(reader.result)\n reader.onerror = (e) => reject(e)\n reader.readAsDataURL(file)\n })\n}\n\n/**\n * getFilefromDataUrl\n *\n * @param {string} dataurl\n * @param {string} filename\n * @param {number} [lastModified=Date.now()]\n * @returns {Promise}\n */\nexport function getFilefromDataUrl (dataurl, filename, lastModified = Date.now()) {\n return new Promise((resolve) => {\n const arr = dataurl.split(',')\n const mime = arr[0].match(/:(.*?);/)[1]\n const bstr = atob(arr[1])\n let n = bstr.length\n const u8arr = new Uint8Array(n)\n while (n--) {\n u8arr[n] = bstr.charCodeAt(n)\n }\n const file = new Blob([u8arr], { type: mime })\n file.name = filename\n file.lastModified = lastModified\n resolve(file)\n\n // Safari has issue with File constructor not being able to POST in FormData\n // https://github.com/Donaldcwl/browser-image-compression/issues/8\n // https://bugs.webkit.org/show_bug.cgi?id=165081\n // let file\n // try {\n // file = new File([u8arr], filename, { type: mime }) // Edge do not support File constructor\n // } catch (e) {\n // file = new Blob([u8arr], { type: mime })\n // file.name = filename\n // file.lastModified = lastModified\n // }\n // resolve(file)\n })\n}\n\n/**\n * loadImage\n *\n * @param {string} src\n * @returns {Promise}\n */\nexport function loadImage (src) {\n return new Promise((resolve, reject) => {\n const img = new Image()\n img.onload = () => resolve(img)\n img.onerror = (e) => reject(e)\n img.src = src\n })\n}\n\n/**\n * drawImageInCanvas\n *\n * @param {HTMLImageElement} img\n * @returns {HTMLCanvasElement}\n */\nexport function drawImageInCanvas (img) {\n const [canvas, ctx] = getNewCanvasAndCtx(img.width, img.height)\n ctx.drawImage(img, 0, 0, canvas.width, canvas.height)\n return canvas\n}\n\n/**\n * drawFileInCanvas\n *\n * @param {File} file\n * @returns {Promise<[ImageBitmap | HTMLImageElement, HTMLCanvasElement]>}\n */\nexport async function drawFileInCanvas (file) {\n let img\n try {\n img = await createImageBitmap(file)\n } catch (e) {\n const dataUrl = await getDataUrlFromFile(file)\n img = await loadImage(dataUrl)\n }\n const canvas = drawImageInCanvas(img)\n return [img, canvas]\n}\n\n/**\n * canvasToFile\n *\n * @param canvas\n * @param {string} fileType\n * @param {string} fileName\n * @param {number} fileLastModified\n * @param {number} [quality]\n * @returns {Promise}\n */\nexport async function canvasToFile (canvas, fileType, fileName, fileLastModified, quality = 1) {\n let file\n if (typeof OffscreenCanvas === 'function' && canvas instanceof OffscreenCanvas) {\n file = await canvas.convertToBlob({ type: fileType, quality })\n file.name = fileName\n file.lastModified = fileLastModified\n } else {\n const dataUrl = canvas.toDataURL(fileType, quality)\n file = await getFilefromDataUrl(dataUrl, fileName, fileLastModified)\n }\n return file\n}\n\n/**\n * getExifOrientation\n * get image exif orientation info\n * source: https://stackoverflow.com/a/32490603/10395024\n *\n * @param {File} file\n * @returns {Promise} - orientation id, see https://i.stack.imgur.com/VGsAj.gif\n */\nexport function getExifOrientation (file) {\n return new Promise((resolve, reject) => {\n const reader = new CustomFileReader()\n reader.onload = (e) => {\n const view = new DataView(e.target.result)\n if (view.getUint16(0, false) != 0xFFD8) {\n return resolve(-2)\n }\n const length = view.byteLength\n let offset = 2\n while (offset < length) {\n if (view.getUint16(offset + 2, false) <= 8) return resolve(-1)\n const marker = view.getUint16(offset, false)\n offset += 2\n if (marker == 0xFFE1) {\n if (view.getUint32(offset += 2, false) != 0x45786966) {\n return resolve(-1)\n }\n\n var little = view.getUint16(offset += 6, false) == 0x4949\n offset += view.getUint32(offset + 4, little)\n var tags = view.getUint16(offset, little)\n offset += 2\n for (var i = 0; i < tags; i++) {\n if (view.getUint16(offset + (i * 12), little) == 0x0112) {\n return resolve(view.getUint16(offset + (i * 12) + 8, little))\n }\n }\n } else if ((marker & 0xFF00) != 0xFF00) {\n break\n } else {\n offset += view.getUint16(offset, false)\n }\n }\n return resolve(-1)\n }\n reader.onerror = (e) => reject(e)\n reader.readAsArrayBuffer(file)\n })\n}\n\n/**\n *\n * @param {HTMLCanvasElement} canvas\n * @param options\n * @returns {HTMLCanvasElement>}\n */\nexport function handleMaxWidthOrHeight (canvas, options) {\n const width = canvas.width\n const height = canvas.height\n const maxWidthOrHeight = options.maxWidthOrHeight\n\n const needToHandle = Number.isFinite(maxWidthOrHeight) && (width > maxWidthOrHeight || height > maxWidthOrHeight)\n\n let newCanvas = canvas\n let ctx\n\n if (needToHandle) {\n [newCanvas, ctx] = getNewCanvasAndCtx(width, height)\n if (width > height) {\n newCanvas.width = maxWidthOrHeight\n newCanvas.height = (height / width) * maxWidthOrHeight\n } else {\n newCanvas.width = (width / height) * maxWidthOrHeight\n newCanvas.height = maxWidthOrHeight\n }\n ctx.drawImage(canvas, 0, 0, newCanvas.width, newCanvas.height)\n\n cleanupCanvasMemory(canvas)\n }\n\n return newCanvas\n}\n\n/**\n * followExifOrientation\n * source: https://stackoverflow.com/a/40867559/10395024\n *\n * @param {HTMLCanvasElement} canvas\n * @param {number} exifOrientation\n * @returns {HTMLCanvasElement} canvas\n */\nexport function followExifOrientation (canvas, exifOrientation) {\n const width = canvas.width\n const height = canvas.height\n\n const [newCanvas, ctx] = getNewCanvasAndCtx(width, height)\n\n // set proper canvas dimensions before transform & export\n if (4 < exifOrientation && exifOrientation < 9) {\n newCanvas.width = height\n newCanvas.height = width\n } else {\n newCanvas.width = width\n newCanvas.height = height\n }\n\n // transform context before drawing image\n switch (exifOrientation) {\n case 2: ctx.transform(-1, 0, 0, 1, width, 0); break;\n case 3: ctx.transform(-1, 0, 0, -1, width, height); break;\n case 4: ctx.transform(1, 0, 0, -1, 0, height); break;\n case 5: ctx.transform(0, 1, 1, 0, 0, 0); break;\n case 6: ctx.transform(0, 1, -1, 0, height, 0); break;\n case 7: ctx.transform(0, -1, -1, 0, height, width); break;\n case 8: ctx.transform(0, -1, 1, 0, 0, width); break;\n default: break;\n }\n\n ctx.drawImage(canvas, 0, 0, width, height)\n\n cleanupCanvasMemory(canvas)\n\n return newCanvas\n}\n\n/**\n * get new Canvas and it's context\n * @param width\n * @param height\n * @returns {[HTMLCanvasElement, CanvasRenderingContext2D]}\n */\nexport function getNewCanvasAndCtx (width, height) {\n let canvas\n let ctx\n try {\n canvas = new OffscreenCanvas(width, height)\n ctx = canvas.getContext('2d')\n if (ctx === null) {\n throw new Error('getContext of OffscreenCanvas returns null')\n }\n } catch (e) {\n canvas = document.createElement('canvas')\n ctx = canvas.getContext('2d')\n }\n canvas.width = width\n canvas.height = height\n return [canvas, ctx]\n}\n\n/**\n * clear Canvas memory\n * @param canvas\n * @returns null\n */\nexport function cleanupCanvasMemory (canvas) {\n canvas.width = 0\n canvas.height = 0\n}\n\nif (isBrowser) {\n// Polyfill for Number.isInteger\n Number.isInteger = Number.isInteger || function (value) {\n return typeof value === 'number' &&\n isFinite(value) &&\n Math.floor(value) === value\n }\n}\n","import {\n canvasToFile,\n cleanupCanvasMemory,\n drawFileInCanvas,\n followExifOrientation,\n getExifOrientation,\n getNewCanvasAndCtx,\n handleMaxWidthOrHeight\n} from './utils'\n\n/**\n * Compress an image file.\n *\n * @param {File} file\n * @param {Object} options - { maxSizeMB=Number.POSITIVE_INFINITY, maxWidthOrHeight, useWebWorker=false, maxIteration = 10, exifOrientation, fileType }\n * @param {number} [options.maxSizeMB=Number.POSITIVE_INFINITY]\n * @param {number} [options.maxWidthOrHeight=undefined]\n * @param {number} [options.maxIteration=10]\n * @param {number} [options.exifOrientation] - default to be the exif orientation from the image file\n * @param {Function} [options.onProgress] - a function takes one progress argument (progress from 0 to 100)\n * @param {string} [options.fileType] - default to be the original mime type from the image file\n * @returns {Promise}\n */\nexport default async function compress (file, options) {\n let progress = 0\n\n function incProgress (inc = 5) {\n progress += inc\n if (typeof options.onProgress === 'function') {\n options.onProgress(Math.min(progress, 100))\n }\n }\n\n function setProgress (p) {\n progress = Math.min(Math.max(p, progress), 100)\n if (typeof options.onProgress === 'function') {\n options.onProgress(progress)\n }\n }\n\n let remainingTrials = options.maxIteration || 10\n\n const maxSizeByte = options.maxSizeMB * 1024 * 1024\n\n incProgress()\n\n // drawFileInCanvas\n let [img, origCanvas] = await drawFileInCanvas(file)\n\n incProgress()\n\n // handleMaxWidthOrHeight\n const maxWidthOrHeightFixedCanvas = handleMaxWidthOrHeight(origCanvas, options)\n\n incProgress()\n\n // exifOrientation\n options.exifOrientation = options.exifOrientation || await getExifOrientation(file)\n incProgress()\n const orientationFixedCanvas = followExifOrientation(maxWidthOrHeightFixedCanvas, options.exifOrientation)\n incProgress()\n\n let quality = 1\n\n let tempFile = await canvasToFile(orientationFixedCanvas, options.fileType || file.type, file.name, file.lastModified, quality)\n incProgress()\n\n const origExceedMaxSize = tempFile.size > maxSizeByte\n const sizeBecomeLarger = tempFile.size > file.size\n\n // check if we need to compress or resize\n if (!origExceedMaxSize && !sizeBecomeLarger) {\n // no need to compress\n setProgress(100)\n return tempFile\n }\n\n const sourceSize = file.size\n const renderedSize = tempFile.size\n let currentSize = renderedSize\n let compressedFile\n let newCanvas, ctx\n let canvas = orientationFixedCanvas\n while (remainingTrials-- && (currentSize > maxSizeByte || currentSize > sourceSize)) {\n const newWidth = origExceedMaxSize ? canvas.width * 0.95 : canvas.width\n const newHeight = origExceedMaxSize ? canvas.height * 0.95 : canvas.height;\n [newCanvas, ctx] = getNewCanvasAndCtx(newWidth, newHeight)\n\n ctx.drawImage(canvas, 0, 0, newWidth, newHeight)\n\n if (file.type === 'image/jpeg') {\n quality *= 0.95\n }\n compressedFile = await canvasToFile(newCanvas, options.fileType || file.type, file.name, file.lastModified, quality)\n\n cleanupCanvasMemory(canvas)\n\n canvas = newCanvas\n\n currentSize = compressedFile.size\n setProgress(Math.min(99, Math.floor((renderedSize - currentSize) / (renderedSize - maxSizeByte) * 100)))\n }\n\n // garbage clean canvas for safari\n // ref: https://bugs.webkit.org/show_bug.cgi?id=195325\n cleanupCanvasMemory(canvas)\n cleanupCanvasMemory(newCanvas)\n cleanupCanvasMemory(maxWidthOrHeightFixedCanvas)\n cleanupCanvasMemory(orientationFixedCanvas)\n cleanupCanvasMemory(origCanvas)\n\n setProgress(100)\n return compressedFile\n}\n","import lib from './index'\nimport compress from './image-compression'\nimport { getNewCanvasAndCtx } from './utils'\n\nlet cnt = 0\nlet imageCompressionLibUrl\nlet worker\n\nfunction createWorker (script) {\n if (typeof script === 'function') {\n script = `(${f})()`\n }\n return new Worker(URL.createObjectURL(new Blob([script])))\n}\n\nfunction createSourceObject (str) {\n return URL.createObjectURL(new Blob([str], { type: 'application/javascript' }))\n}\n\nfunction generateLib () {\n // prepare the lib to be used inside WebWorker\n return createSourceObject(`\n function imageCompression (){return (${lib}).apply(null, arguments)}\n\n imageCompression.getDataUrlFromFile = ${lib.getDataUrlFromFile}\n imageCompression.getFilefromDataUrl = ${lib.getFilefromDataUrl}\n imageCompression.loadImage = ${lib.loadImage}\n imageCompression.drawImageInCanvas = ${lib.drawImageInCanvas}\n imageCompression.drawFileInCanvas = ${lib.drawFileInCanvas}\n imageCompression.canvasToFile = ${lib.canvasToFile}\n imageCompression.getExifOrientation = ${lib.getExifOrientation}\n imageCompression.handleMaxWidthOrHeight = ${lib.handleMaxWidthOrHeight}\n imageCompression.followExifOrientation = ${lib.followExifOrientation}\n imageCompression.cleanupMemory = ${lib.cleanupMemory}\n\n getDataUrlFromFile = imageCompression.getDataUrlFromFile\n getFilefromDataUrl = imageCompression.getFilefromDataUrl\n loadImage = imageCompression.loadImage\n drawImageInCanvas = imageCompression.drawImageInCanvas\n drawFileInCanvas = imageCompression.drawFileInCanvas\n canvasToFile = imageCompression.canvasToFile\n getExifOrientation = imageCompression.getExifOrientation\n handleMaxWidthOrHeight = imageCompression.handleMaxWidthOrHeight\n followExifOrientation = imageCompression.followExifOrientation\n cleanupMemory = imageCompression.cleanupMemory\n\n getNewCanvasAndCtx = ${getNewCanvasAndCtx}\n \n CustomFileReader = FileReader\n \n CustomFile = File\n \n function _slicedToArray(arr, n) { return arr }\n \n function _typeof(a) { return typeof a }\n\n function compress (){return (${compress}).apply(null, arguments)}\n `)\n}\n\nfunction generateWorkerScript () {\n // code to be run in the WebWorker\n return createWorker(`\n let scriptImported = false\n self.addEventListener('message', async (e) => {\n const { file, id, imageCompressionLibUrl, options } = e.data\n options.onProgress = (progress) => self.postMessage({ progress, id })\n try {\n if (!scriptImported) {\n // console.log('[worker] importScripts', imageCompressionLibUrl)\n self.importScripts(imageCompressionLibUrl)\n scriptImported = true\n }\n // console.log('[worker] self', self)\n const compressedFile = await imageCompression(file, options)\n self.postMessage({ file: compressedFile, id })\n } catch (e) {\n // console.error('[worker] error', e)\n self.postMessage({ error: e.message + '\\\\n' + e.stack, id })\n }\n })\n `)\n}\n\nexport function compressOnWebWorker (file, options) {\n return new Promise(async (resolve, reject) => {\n let id = cnt++\n\n if (!imageCompressionLibUrl) {\n imageCompressionLibUrl = generateLib()\n }\n\n if (!worker) {\n worker = generateWorkerScript()\n }\n\n function handler (e) {\n if (e.data.id === id) {\n if (e.data.progress !== undefined && e.data.progress < 100) {\n options.onProgress(e.data.progress)\n return\n }\n worker.removeEventListener('message', handler)\n if (e.data.error) {\n reject(new Error(e.data.error))\n }\n resolve(e.data.file)\n }\n }\n\n worker.addEventListener('message', handler)\n worker.postMessage({\n file,\n id,\n imageCompressionLibUrl,\n options: { ...options, onProgress: undefined }\n })\n })\n}\n","import compress from './image-compression'\nimport {\n canvasToFile,\n drawFileInCanvas,\n drawImageInCanvas,\n getDataUrlFromFile,\n getFilefromDataUrl,\n loadImage,\n getExifOrientation,\n handleMaxWidthOrHeight,\n followExifOrientation,\n CustomFile,\n cleanupCanvasMemory\n} from './utils'\nimport { compressOnWebWorker } from './web-worker'\n\n/**\n * Compress an image file.\n *\n * @param {File} file\n * @param {Object} options - { maxSizeMB=Number.POSITIVE_INFINITY, maxWidthOrHeight, useWebWorker=false, maxIteration = 10, exifOrientation, fileType }\n * @param {number} [options.maxSizeMB=Number.POSITIVE_INFINITY]\n * @param {number} [options.maxWidthOrHeight=undefined]\n * @param {boolean} [options.useWebWorker=false]\n * @param {number} [options.maxIteration=10]\n * @param {number} [options.exifOrientation] - default to be the exif orientation from the image file\n * @param {Function} [options.onProgress] - a function takes one progress argument (progress from 0 to 100)\n * @param {string} [options.fileType] - default to be the original mime type from the image file\n * @returns {Promise}\n */\nasync function imageCompression (file, options) {\n\n let compressedFile\n\n options.maxSizeMB = options.maxSizeMB || Number.POSITIVE_INFINITY\n const useWebWorker = typeof options.useWebWorker === 'boolean' ? options.useWebWorker : false\n delete options.useWebWorker\n\n if (!(file instanceof Blob || file instanceof CustomFile)) {\n throw new Error('The file given is not an instance of Blob or File')\n } else if (!/^image/.test(file.type)) {\n throw new Error('The file given is not an image')\n }\n\n // try run in web worker, fall back to run in main thread\n const inWebWorker = typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope\n\n // if ((useWebWorker && typeof Worker === 'function') || inWebWorker) {\n // console.log('run compression in web worker')\n // } else {\n // console.log('run compression in main thread')\n // }\n\n if (useWebWorker && typeof Worker === 'function' && !inWebWorker) {\n try {\n // console.log(1)\n // \"compressOnWebWorker\" is kind of like a recursion to call \"imageCompression\" again inside web worker\n compressedFile = await compressOnWebWorker(file, options)\n } catch (e) {\n // console.warn('Run compression in web worker failed:', e, ', fall back to main thread')\n // console.log(1.5)\n compressedFile = await compress(file, options)\n }\n } else {\n // console.log(2)\n compressedFile = await compress(file, options)\n }\n\n try {\n compressedFile.name = file.name\n compressedFile.lastModified = file.lastModified\n } catch (e) {}\n\n return compressedFile\n}\n\nimageCompression.getDataUrlFromFile = getDataUrlFromFile\nimageCompression.getFilefromDataUrl = getFilefromDataUrl\nimageCompression.loadImage = loadImage\nimageCompression.drawImageInCanvas = drawImageInCanvas\nimageCompression.drawFileInCanvas = drawFileInCanvas\nimageCompression.canvasToFile = canvasToFile\nimageCompression.getExifOrientation = getExifOrientation\nimageCompression.handleMaxWidthOrHeight = handleMaxWidthOrHeight\nimageCompression.followExifOrientation = followExifOrientation\nimageCompression.cleanupMemory = cleanupCanvasMemory\nimageCompression.version = '1.0.9'\n\nexport default imageCompression\n"],"names":["isBrowser","window","moduleMapper","getDataUrlFromFile","resolve","reader","dataurl","split","n","bstr","filename","file","lastModified","canvas","view","length","offset","getUint32","marker","maxWidthOrHeight","width","followExifOrientation","height","ctx","inc","drawFileInCanvas","exifOrientation","orientationFixedCanvas","options","fileType","imageCompressionLibUrl","cnt","generateLib","script","imageCompression","getFilefromDataUrl","loadImage","drawImageInCanvas","canvasToFile","getExifOrientation"],"mappings":";;;;;;;m/CAAA,IAAMA,EAA8B,oBAAlBC,2EAKoF,uCAQ5DC,2HAiBnC,SAASC,mDACPC,6DAEqBC,uNAiCdC,EAAAC,mCACK,gBAEjBC,EAAIC,6BAEJD,8DAUYE,EACdC,EAAAC,eACAR,28BAwC2CS,qcAoB7CC,mEAEMV,mCAGQW,6DAEgBC,8GASpBF,EAAAG,UAAgBD,0GAKPA,EAAA,iCAGTE,0KA+BVC,oJAqBuBC,4CAQTP,EAAV,0CAEAA,qBASYQ,yCACZC,mPAmBuCA,wJAI9B,sHA8BbC,2BAHWD,kaC3OTE,iLAPK,6DAUGC,iBAARd,yKAUMe,sUAWAC,EAAwBC,EAAAC,6PAsB5BhB,o8BC/FJ,IACIiB,IADAC,IAcJ,SAASC,okEAPPC,gkDCyDwBtB,0zBAa1BuB,iBAAA/B,mBAAsCA,mBACtC+B,iBAAAC,sCACAD,iBAAAE,oBACAF,iBAAAG,oCACAH,iBAAAT,kCAEAS,iBAAAI,0BAxFAJ,iBAAAK"} \ No newline at end of file diff --git a/dist/browser-image-compression.mjs b/dist/browser-image-compression.mjs index 994305a..08896bc 100644 --- a/dist/browser-image-compression.mjs +++ b/dist/browser-image-compression.mjs @@ -1,9 +1,9 @@ /** * Browser Image Compression - * v1.0.8 + * v1.0.9 * by Donald * https://github.com/Donaldcwl/browser-image-compression */ -function _defineProperty(e,n,r){return n in e?Object.defineProperty(e,n,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[n]=r,e}function ownKeys(e,n){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var t=Object.getOwnPropertySymbols(e);n&&(t=t.filter((function(n){return Object.getOwnPropertyDescriptor(e,n).enumerable}))),r.push.apply(r,t)}return r}function _objectSpread2(e){for(var n=1;n2&&void 0!==arguments[2]?arguments[2]:Date.now();return new Promise((function(t){for(var i=e.split(","),o=i[0].match(/:(.*?);/)[1],a=atob(i[1]),s=a.length,c=new Uint8Array(s);s--;)c[s]=a.charCodeAt(s);var l=new Blob([c],{type:o});l.name=n,l.lastModified=r,t(l)}))}function loadImage(e){return new Promise((function(n,r){var t=new Image;t.onload=function(){return n(t)},t.onerror=function(e){return r(e)},t.src=e}))}function drawImageInCanvas(e){var n=_slicedToArray(getNewCanvasAndCtx(e.width,e.height),2),r=n[0];return n[1].drawImage(e,0,0,r.width,r.height),r}function drawFileInCanvas(e){return new Promise((function(n,r){var t,i,o=function $Try_1_Post(){try{return i=drawImageInCanvas(t),n([t,i])}catch(e){return r(e)}},a=function $Try_1_Catch(n){try{return getDataUrlFromFile(e).then((function(e){try{return loadImage(e).then((function(e){try{return t=e,o()}catch(e){return r(e)}}),r)}catch(e){return r(e)}}),r)}catch(e){return r(e)}};try{return createImageBitmap(e).then((function(e){try{return t=e,o()}catch(e){return a()}}),a)}catch(e){a()}}))}function canvasToFile(e,n,r,t){var i=arguments.length>4&&void 0!==arguments[4]?arguments[4]:1;return new Promise((function(o,a){var s;return"function"==typeof OffscreenCanvas&&e instanceof OffscreenCanvas?e.convertToBlob({type:n,quality:i}).then(function(e){try{return(s=e).name=r,s.lastModified=t,$If_4.call(this)}catch(e){return a(e)}}.bind(this),a):getFilefromDataUrl(e.toDataURL(n,i),r,t).then(function(e){try{return s=e,$If_4.call(this)}catch(e){return a(e)}}.bind(this),a);function $If_4(){return o(s)}}))}function getExifOrientation(e){return new Promise((function(n,r){var t=new CustomFileReader;t.onload=function(e){var r=new DataView(e.target.result);if(65496!=r.getUint16(0,!1))return n(-2);for(var t=r.byteLength,i=2;io||i>o)){var s=_slicedToArray(getNewCanvasAndCtx(t,i),2);a=s[0],r=s[1],t>i?(a.width=o,a.height=i/t*o):(a.width=t/i*o,a.height=o),r.drawImage(e,0,0,a.width,a.height),cleanupCanvasMemory(e)}return a}function followExifOrientation(e,n){var r=e.width,t=e.height,i=_slicedToArray(getNewCanvasAndCtx(r,t),2),o=i[0],a=i[1];switch(40&&void 0!==arguments[0]?arguments[0]:5;i+=e,"function"==typeof n.onProgress&&n.onProgress(Math.min(i,100))}function setProgress(e){i=Math.min(Math.max(e,i),100),"function"==typeof n.onProgress&&n.onProgress(i)}return i=0,o=n.maxIteration||10,a=1024*n.maxSizeMB*1024,incProgress(),drawFileInCanvas(e).then(function(i){try{var C=_slicedToArray(i,2);return C[0],s=C[1],incProgress(),c=handleMaxWidthOrHeight(s,n),incProgress(),new Promise((function(r,t){var i;if(!(i=n.exifOrientation))return getExifOrientation(e).then(function(e){try{return i=e,$If_2.call(this)}catch(e){return t(e)}}.bind(this),t);function $If_2(){return r(i)}return $If_2.call(this)})).then(function(i){try{return n.exifOrientation=i,incProgress(),l=followExifOrientation(c,n.exifOrientation),incProgress(),u=1,canvasToFile(l,n.fileType||e.type,e.name,e.lastModified,u).then(function(i){try{{if(m=i,incProgress(),f=m.size>a,g=m.size>e.size,!f&&!g)return setProgress(100),r(m);var C;function $Loop_3(){if(o--&&(h>a||h>p)){var r,i,s=_slicedToArray(getNewCanvasAndCtx(r=f?.95*y.width:y.width,i=f?.95*y.height:y.height),2);return w=s[0],s[1].drawImage(y,0,0,r,i),"image/jpeg"===e.type&&(u*=.95),canvasToFile(w,n.fileType||e.type,e.name,e.lastModified,u).then((function(e){try{return v=e,cleanupCanvasMemory(y),y=w,h=v.size,setProgress(Math.min(99,Math.floor((d-h)/(d-a)*100))),$Loop_3}catch(e){return t(e)}}),t)}return[1]}return p=e.size,d=m.size,h=d,y=l,(C=function(e){for(;e;){if(e.then)return void e.then(C,t);try{if(e.pop){if(e.length)return e.pop()?$Loop_3_exit.call(this):e;e=$Loop_3}else e=e.call(this)}catch(e){return t(e)}}}.bind(this))($Loop_3);function $Loop_3_exit(){return cleanupCanvasMemory(y),cleanupCanvasMemory(w),cleanupCanvasMemory(c),cleanupCanvasMemory(l),cleanupCanvasMemory(s),setProgress(100),r(v)}}}catch(e){return t(e)}}.bind(this),t)}catch(e){return t(e)}}.bind(this),t)}catch(e){return t(e)}}.bind(this),t)}))}e&&(Number.isInteger=Number.isInteger||function(e){return"number"==typeof e&&isFinite(e)&&Math.floor(e)===e});var r,t,i=0;function generateLib(){return function createSourceObject(e){return URL.createObjectURL(new Blob([e],{type:"application/javascript"}))}("\n function imageCompression (){return (".concat(imageCompression,").apply(null, arguments)}\n\n imageCompression.getDataUrlFromFile = ").concat(imageCompression.getDataUrlFromFile,"\n imageCompression.getFilefromDataUrl = ").concat(imageCompression.getFilefromDataUrl,"\n imageCompression.loadImage = ").concat(imageCompression.loadImage,"\n imageCompression.drawImageInCanvas = ").concat(imageCompression.drawImageInCanvas,"\n imageCompression.drawFileInCanvas = ").concat(imageCompression.drawFileInCanvas,"\n imageCompression.canvasToFile = ").concat(imageCompression.canvasToFile,"\n imageCompression.getExifOrientation = ").concat(imageCompression.getExifOrientation,"\n imageCompression.handleMaxWidthOrHeight = ").concat(imageCompression.handleMaxWidthOrHeight,"\n imageCompression.followExifOrientation = ").concat(imageCompression.followExifOrientation,"\n imageCompression.cleanupMemory = ").concat(imageCompression.cleanupMemory,"\n\n getDataUrlFromFile = imageCompression.getDataUrlFromFile\n getFilefromDataUrl = imageCompression.getFilefromDataUrl\n loadImage = imageCompression.loadImage\n drawImageInCanvas = imageCompression.drawImageInCanvas\n drawFileInCanvas = imageCompression.drawFileInCanvas\n canvasToFile = imageCompression.canvasToFile\n getExifOrientation = imageCompression.getExifOrientation\n handleMaxWidthOrHeight = imageCompression.handleMaxWidthOrHeight\n followExifOrientation = imageCompression.followExifOrientation\n cleanupMemory = imageCompression.cleanupMemory\n\n getNewCanvasAndCtx = ").concat(getNewCanvasAndCtx,"\n \n CustomFileReader = FileReader\n \n CustomFile = File\n \n function _slicedToArray(arr, n) { return arr }\n \n function _typeof(a) { return typeof a }\n\n function compress (){return (").concat(compress,").apply(null, arguments)}\n "))}function generateWorkerScript(){return function createWorker(e){return"function"==typeof e&&(e="(".concat(f,")()")),new Worker(URL.createObjectURL(new Blob([e])))}("\n let scriptImported = false\n self.addEventListener('message', async (e) => {\n const { file, id, imageCompressionLibUrl, options } = e.data\n options.onProgress = (progress) => self.postMessage({ progress, id })\n try {\n if (!scriptImported) {\n // console.log('[worker] importScripts', imageCompressionLibUrl)\n self.importScripts(imageCompressionLibUrl)\n scriptImported = true\n }\n // console.log('[worker] self', self)\n const compressedFile = await imageCompression(file, options)\n self.postMessage({ file: compressedFile, id })\n } catch (e) {\n // console.error('[worker] error', e)\n self.postMessage({ error: e.message + '\\n' + e.stack, id })\n }\n })\n ")}function imageCompression(e,n){return new Promise((function(o,a){var s,c,l;if(n.maxSizeMB=n.maxSizeMB||Number.POSITIVE_INFINITY,c="boolean"==typeof n.useWebWorker&&n.useWebWorker,delete n.useWebWorker,!(e instanceof Blob||e instanceof CustomFile))return a(new Error("The file given is not an instance of Blob or File"));if(!/^image/.test(e.type))return a(new Error("The file given is not an image"));if(l="undefined"!=typeof WorkerGlobalScope&&self instanceof WorkerGlobalScope,!c||"function"!=typeof Worker||l)return compress(e,n).then(function(e){try{return s=e,$If_3.call(this)}catch(e){return a(e)}}.bind(this),a);var u=function(){try{return $If_3.call(this)}catch(e){return a(e)}}.bind(this),m=function $Try_1_Catch(r){try{return compress(e,n).then((function(e){try{return s=e,u()}catch(e){return a(e)}}),a)}catch(e){return a(e)}};try{return function compressOnWebWorker(e,n){return new Promise((function(o,a){return new Promise((function(s,c){var l=i++;return r||(r=generateLib()),t||(t=generateWorkerScript()),t.addEventListener("message",(function handler(e){if(e.data.id===l){if(void 0!==e.data.progress&&e.data.progress<100)return void n.onProgress(e.data.progress);t.removeEventListener("message",handler),e.data.error&&a(new Error(e.data.error)),o(e.data.file)}})),t.postMessage({file:e,id:l,imageCompressionLibUrl:r,options:_objectSpread2({},n,{onProgress:void 0})}),s()}))}))}(e,n).then((function(e){try{return s=e,u()}catch(e){return m()}}),m)}catch(e){m()}function $If_3(){try{s.name=e.name,s.lastModified=e.lastModified}catch(e){}return o(s)}}))}imageCompression.getDataUrlFromFile=getDataUrlFromFile,imageCompression.getFilefromDataUrl=getFilefromDataUrl,imageCompression.loadImage=loadImage,imageCompression.drawImageInCanvas=drawImageInCanvas,imageCompression.drawFileInCanvas=drawFileInCanvas,imageCompression.canvasToFile=canvasToFile,imageCompression.getExifOrientation=getExifOrientation,imageCompression.handleMaxWidthOrHeight=handleMaxWidthOrHeight,imageCompression.followExifOrientation=followExifOrientation,imageCompression.cleanupMemory=cleanupCanvasMemory,imageCompression.version="1.0.8";export default imageCompression; +function _defineProperty(e,n,r){return n in e?Object.defineProperty(e,n,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[n]=r,e}function ownKeys(e,n){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var t=Object.getOwnPropertySymbols(e);n&&(t=t.filter((function(n){return Object.getOwnPropertyDescriptor(e,n).enumerable}))),r.push.apply(r,t)}return r}function _objectSpread2(e){for(var n=1;n2&&void 0!==arguments[2]?arguments[2]:Date.now();return new Promise((function(t){for(var i=e.split(","),o=i[0].match(/:(.*?);/)[1],a=atob(i[1]),s=a.length,c=new Uint8Array(s);s--;)c[s]=a.charCodeAt(s);var l=new Blob([c],{type:o});l.name=n,l.lastModified=r,t(l)}))}function loadImage(e){return new Promise((function(n,r){var t=new Image;t.onload=function(){return n(t)},t.onerror=function(e){return r(e)},t.src=e}))}function drawImageInCanvas(e){var n=_slicedToArray(getNewCanvasAndCtx(e.width,e.height),2),r=n[0];return n[1].drawImage(e,0,0,r.width,r.height),r}function drawFileInCanvas(e){return new Promise((function(n,r){var t,i,o=function $Try_1_Post(){try{return i=drawImageInCanvas(t),n([t,i])}catch(e){return r(e)}},a=function $Try_1_Catch(n){try{return getDataUrlFromFile(e).then((function(e){try{return loadImage(e).then((function(e){try{return t=e,o()}catch(e){return r(e)}}),r)}catch(e){return r(e)}}),r)}catch(e){return r(e)}};try{return createImageBitmap(e).then((function(e){try{return t=e,o()}catch(e){return a()}}),a)}catch(e){a()}}))}function canvasToFile(e,n,r,t){var i=arguments.length>4&&void 0!==arguments[4]?arguments[4]:1;return new Promise((function(o,a){var s;return"function"==typeof OffscreenCanvas&&e instanceof OffscreenCanvas?e.convertToBlob({type:n,quality:i}).then(function(e){try{return(s=e).name=r,s.lastModified=t,$If_4.call(this)}catch(e){return a(e)}}.bind(this),a):getFilefromDataUrl(e.toDataURL(n,i),r,t).then(function(e){try{return s=e,$If_4.call(this)}catch(e){return a(e)}}.bind(this),a);function $If_4(){return o(s)}}))}function getExifOrientation(e){return new Promise((function(n,r){var t=new CustomFileReader;t.onload=function(e){var r=new DataView(e.target.result);if(65496!=r.getUint16(0,!1))return n(-2);for(var t=r.byteLength,i=2;io||i>o)){var s=_slicedToArray(getNewCanvasAndCtx(t,i),2);a=s[0],r=s[1],t>i?(a.width=o,a.height=i/t*o):(a.width=t/i*o,a.height=o),r.drawImage(e,0,0,a.width,a.height),cleanupCanvasMemory(e)}return a}function followExifOrientation(e,n){var r=e.width,t=e.height,i=_slicedToArray(getNewCanvasAndCtx(r,t),2),o=i[0],a=i[1];switch(40&&void 0!==arguments[0]?arguments[0]:5;i+=e,"function"==typeof n.onProgress&&n.onProgress(Math.min(i,100))}function setProgress(e){i=Math.min(Math.max(e,i),100),"function"==typeof n.onProgress&&n.onProgress(i)}return i=0,o=n.maxIteration||10,a=1024*n.maxSizeMB*1024,incProgress(),drawFileInCanvas(e).then(function(i){try{var C=_slicedToArray(i,2);return C[0],s=C[1],incProgress(),c=handleMaxWidthOrHeight(s,n),incProgress(),new Promise((function(r,t){var i;if(!(i=n.exifOrientation))return getExifOrientation(e).then(function(e){try{return i=e,$If_2.call(this)}catch(e){return t(e)}}.bind(this),t);function $If_2(){return r(i)}return $If_2.call(this)})).then(function(i){try{return n.exifOrientation=i,incProgress(),l=followExifOrientation(c,n.exifOrientation),incProgress(),u=1,canvasToFile(l,n.fileType||e.type,e.name,e.lastModified,u).then(function(i){try{{if(m=i,incProgress(),f=m.size>a,g=m.size>e.size,!f&&!g)return setProgress(100),r(m);var C;function $Loop_3(){if(o--&&(h>a||h>p)){var r,i,s=_slicedToArray(getNewCanvasAndCtx(r=f?.95*y.width:y.width,i=f?.95*y.height:y.height),2);return w=s[0],s[1].drawImage(y,0,0,r,i),"image/jpeg"===e.type&&(u*=.95),canvasToFile(w,n.fileType||e.type,e.name,e.lastModified,u).then((function(e){try{return v=e,cleanupCanvasMemory(y),y=w,h=v.size,setProgress(Math.min(99,Math.floor((d-h)/(d-a)*100))),$Loop_3}catch(e){return t(e)}}),t)}return[1]}return p=e.size,d=m.size,h=d,y=l,(C=function(e){for(;e;){if(e.then)return void e.then(C,t);try{if(e.pop){if(e.length)return e.pop()?$Loop_3_exit.call(this):e;e=$Loop_3}else e=e.call(this)}catch(e){return t(e)}}}.bind(this))($Loop_3);function $Loop_3_exit(){return cleanupCanvasMemory(y),cleanupCanvasMemory(w),cleanupCanvasMemory(c),cleanupCanvasMemory(l),cleanupCanvasMemory(s),setProgress(100),r(v)}}}catch(e){return t(e)}}.bind(this),t)}catch(e){return t(e)}}.bind(this),t)}catch(e){return t(e)}}.bind(this),t)}))}e&&(Number.isInteger=Number.isInteger||function(e){return"number"==typeof e&&isFinite(e)&&Math.floor(e)===e});var r,t,i=0;function generateLib(){return function createSourceObject(e){return URL.createObjectURL(new Blob([e],{type:"application/javascript"}))}("\n function imageCompression (){return (".concat(imageCompression,").apply(null, arguments)}\n\n imageCompression.getDataUrlFromFile = ").concat(imageCompression.getDataUrlFromFile,"\n imageCompression.getFilefromDataUrl = ").concat(imageCompression.getFilefromDataUrl,"\n imageCompression.loadImage = ").concat(imageCompression.loadImage,"\n imageCompression.drawImageInCanvas = ").concat(imageCompression.drawImageInCanvas,"\n imageCompression.drawFileInCanvas = ").concat(imageCompression.drawFileInCanvas,"\n imageCompression.canvasToFile = ").concat(imageCompression.canvasToFile,"\n imageCompression.getExifOrientation = ").concat(imageCompression.getExifOrientation,"\n imageCompression.handleMaxWidthOrHeight = ").concat(imageCompression.handleMaxWidthOrHeight,"\n imageCompression.followExifOrientation = ").concat(imageCompression.followExifOrientation,"\n imageCompression.cleanupMemory = ").concat(imageCompression.cleanupMemory,"\n\n getDataUrlFromFile = imageCompression.getDataUrlFromFile\n getFilefromDataUrl = imageCompression.getFilefromDataUrl\n loadImage = imageCompression.loadImage\n drawImageInCanvas = imageCompression.drawImageInCanvas\n drawFileInCanvas = imageCompression.drawFileInCanvas\n canvasToFile = imageCompression.canvasToFile\n getExifOrientation = imageCompression.getExifOrientation\n handleMaxWidthOrHeight = imageCompression.handleMaxWidthOrHeight\n followExifOrientation = imageCompression.followExifOrientation\n cleanupMemory = imageCompression.cleanupMemory\n\n getNewCanvasAndCtx = ").concat(getNewCanvasAndCtx,"\n \n CustomFileReader = FileReader\n \n CustomFile = File\n \n function _slicedToArray(arr, n) { return arr }\n \n function _typeof(a) { return typeof a }\n\n function compress (){return (").concat(compress,").apply(null, arguments)}\n "))}function generateWorkerScript(){return function createWorker(e){return"function"==typeof e&&(e="(".concat(f,")()")),new Worker(URL.createObjectURL(new Blob([e])))}("\n let scriptImported = false\n self.addEventListener('message', async (e) => {\n const { file, id, imageCompressionLibUrl, options } = e.data\n options.onProgress = (progress) => self.postMessage({ progress, id })\n try {\n if (!scriptImported) {\n // console.log('[worker] importScripts', imageCompressionLibUrl)\n self.importScripts(imageCompressionLibUrl)\n scriptImported = true\n }\n // console.log('[worker] self', self)\n const compressedFile = await imageCompression(file, options)\n self.postMessage({ file: compressedFile, id })\n } catch (e) {\n // console.error('[worker] error', e)\n self.postMessage({ error: e.message + '\\n' + e.stack, id })\n }\n })\n ")}function imageCompression(e,n){return new Promise((function(o,a){var s,c,l;if(n.maxSizeMB=n.maxSizeMB||Number.POSITIVE_INFINITY,c="boolean"==typeof n.useWebWorker&&n.useWebWorker,delete n.useWebWorker,!(e instanceof Blob||e instanceof CustomFile))return a(new Error("The file given is not an instance of Blob or File"));if(!/^image/.test(e.type))return a(new Error("The file given is not an image"));if(l="undefined"!=typeof WorkerGlobalScope&&self instanceof WorkerGlobalScope,!c||"function"!=typeof Worker||l)return compress(e,n).then(function(e){try{return s=e,$If_3.call(this)}catch(e){return a(e)}}.bind(this),a);var u=function(){try{return $If_3.call(this)}catch(e){return a(e)}}.bind(this),m=function $Try_1_Catch(r){try{return compress(e,n).then((function(e){try{return s=e,u()}catch(e){return a(e)}}),a)}catch(e){return a(e)}};try{return function compressOnWebWorker(e,n){return new Promise((function(o,a){return new Promise((function(s,c){var l=i++;return r||(r=generateLib()),t||(t=generateWorkerScript()),t.addEventListener("message",(function handler(e){if(e.data.id===l){if(void 0!==e.data.progress&&e.data.progress<100)return void n.onProgress(e.data.progress);t.removeEventListener("message",handler),e.data.error&&a(new Error(e.data.error)),o(e.data.file)}})),t.postMessage({file:e,id:l,imageCompressionLibUrl:r,options:_objectSpread2({},n,{onProgress:void 0})}),s()}))}))}(e,n).then((function(e){try{return s=e,u()}catch(e){return m()}}),m)}catch(e){m()}function $If_3(){try{s.name=e.name,s.lastModified=e.lastModified}catch(e){}return o(s)}}))}imageCompression.getDataUrlFromFile=getDataUrlFromFile,imageCompression.getFilefromDataUrl=getFilefromDataUrl,imageCompression.loadImage=loadImage,imageCompression.drawImageInCanvas=drawImageInCanvas,imageCompression.drawFileInCanvas=drawFileInCanvas,imageCompression.canvasToFile=canvasToFile,imageCompression.getExifOrientation=getExifOrientation,imageCompression.handleMaxWidthOrHeight=handleMaxWidthOrHeight,imageCompression.followExifOrientation=followExifOrientation,imageCompression.cleanupMemory=cleanupCanvasMemory,imageCompression.version="1.0.9";export default imageCompression; //# sourceMappingURL=browser-image-compression.mjs.map diff --git a/dist/browser-image-compression.mjs.map b/dist/browser-image-compression.mjs.map index c21cef5..f787e5c 100644 --- a/dist/browser-image-compression.mjs.map +++ b/dist/browser-image-compression.mjs.map @@ -1 +1 @@ -{"version":3,"file":"browser-image-compression.mjs","sources":["../lib/utils.js","../lib/image-compression.js","../lib/web-worker.js","../lib/index.js"],"sourcesContent":["const isBrowser = typeof window !== 'undefined' // change browser environment to support SSR\n\n// add support for cordova-plugin-file\nconst moduleMapper = isBrowser && window.cordova && window.cordova.require && window.cordova.require('cordova/modulemapper')\nexport const CustomFile = isBrowser && ((moduleMapper && moduleMapper.getOriginalSymbol(window, 'File')) || File)\nexport const CustomFileReader = isBrowser && ((moduleMapper && moduleMapper.getOriginalSymbol(window, 'FileReader')) || FileReader)\n\n/**\n * getDataUrlFromFile\n *\n * @param {File} file\n * @returns {Promise}\n */\nexport function getDataUrlFromFile (file) {\n return new Promise((resolve, reject) => {\n const reader = new CustomFileReader()\n reader.onload = () => resolve(reader.result)\n reader.onerror = (e) => reject(e)\n reader.readAsDataURL(file)\n })\n}\n\n/**\n * getFilefromDataUrl\n *\n * @param {string} dataurl\n * @param {string} filename\n * @param {number} [lastModified=Date.now()]\n * @returns {Promise}\n */\nexport function getFilefromDataUrl (dataurl, filename, lastModified = Date.now()) {\n return new Promise((resolve) => {\n const arr = dataurl.split(',')\n const mime = arr[0].match(/:(.*?);/)[1]\n const bstr = atob(arr[1])\n let n = bstr.length\n const u8arr = new Uint8Array(n)\n while (n--) {\n u8arr[n] = bstr.charCodeAt(n)\n }\n const file = new Blob([u8arr], { type: mime })\n file.name = filename\n file.lastModified = lastModified\n resolve(file)\n\n // Safari has issue with File constructor not being able to POST in FormData\n // https://github.com/Donaldcwl/browser-image-compression/issues/8\n // https://bugs.webkit.org/show_bug.cgi?id=165081\n // let file\n // try {\n // file = new File([u8arr], filename, { type: mime }) // Edge do not support File constructor\n // } catch (e) {\n // file = new Blob([u8arr], { type: mime })\n // file.name = filename\n // file.lastModified = lastModified\n // }\n // resolve(file)\n })\n}\n\n/**\n * loadImage\n *\n * @param {string} src\n * @returns {Promise}\n */\nexport function loadImage (src) {\n return new Promise((resolve, reject) => {\n const img = new Image()\n img.onload = () => resolve(img)\n img.onerror = (e) => reject(e)\n img.src = src\n })\n}\n\n/**\n * drawImageInCanvas\n *\n * @param {HTMLImageElement} img\n * @returns {HTMLCanvasElement}\n */\nexport function drawImageInCanvas (img) {\n const [canvas, ctx] = getNewCanvasAndCtx(img.width, img.height)\n ctx.drawImage(img, 0, 0, canvas.width, canvas.height)\n return canvas\n}\n\n/**\n * drawFileInCanvas\n *\n * @param {File} file\n * @returns {Promise<[ImageBitmap | HTMLImageElement, HTMLCanvasElement]>}\n */\nexport async function drawFileInCanvas (file) {\n let img\n try {\n img = await createImageBitmap(file)\n } catch (e) {\n const dataUrl = await getDataUrlFromFile(file)\n img = await loadImage(dataUrl)\n }\n const canvas = drawImageInCanvas(img)\n return [img, canvas]\n}\n\n/**\n * canvasToFile\n *\n * @param canvas\n * @param {string} fileType\n * @param {string} fileName\n * @param {number} fileLastModified\n * @param {number} [quality]\n * @returns {Promise}\n */\nexport async function canvasToFile (canvas, fileType, fileName, fileLastModified, quality = 1) {\n let file\n if (typeof OffscreenCanvas === 'function' && canvas instanceof OffscreenCanvas) {\n file = await canvas.convertToBlob({ type: fileType, quality })\n file.name = fileName\n file.lastModified = fileLastModified\n } else {\n const dataUrl = canvas.toDataURL(fileType, quality)\n file = await getFilefromDataUrl(dataUrl, fileName, fileLastModified)\n }\n return file\n}\n\n/**\n * getExifOrientation\n * get image exif orientation info\n * source: https://stackoverflow.com/a/32490603/10395024\n *\n * @param {File} file\n * @returns {Promise} - orientation id, see https://i.stack.imgur.com/VGsAj.gif\n */\nexport function getExifOrientation (file) {\n return new Promise((resolve, reject) => {\n const reader = new CustomFileReader()\n reader.onload = (e) => {\n const view = new DataView(e.target.result)\n if (view.getUint16(0, false) != 0xFFD8) {\n return resolve(-2)\n }\n const length = view.byteLength\n let offset = 2\n while (offset < length) {\n if (view.getUint16(offset + 2, false) <= 8) return resolve(-1)\n const marker = view.getUint16(offset, false)\n offset += 2\n if (marker == 0xFFE1) {\n if (view.getUint32(offset += 2, false) != 0x45786966) {\n return resolve(-1)\n }\n\n var little = view.getUint16(offset += 6, false) == 0x4949\n offset += view.getUint32(offset + 4, little)\n var tags = view.getUint16(offset, little)\n offset += 2\n for (var i = 0; i < tags; i++) {\n if (view.getUint16(offset + (i * 12), little) == 0x0112) {\n return resolve(view.getUint16(offset + (i * 12) + 8, little))\n }\n }\n } else if ((marker & 0xFF00) != 0xFF00) {\n break\n } else {\n offset += view.getUint16(offset, false)\n }\n }\n return resolve(-1)\n }\n reader.onerror = (e) => reject(e)\n reader.readAsArrayBuffer(file)\n })\n}\n\n/**\n *\n * @param {HTMLCanvasElement} canvas\n * @param options\n * @returns {HTMLCanvasElement>}\n */\nexport function handleMaxWidthOrHeight (canvas, options) {\n const width = canvas.width\n const height = canvas.height\n const maxWidthOrHeight = options.maxWidthOrHeight\n\n const needToHandle = Number.isFinite(maxWidthOrHeight) && (width > maxWidthOrHeight || height > maxWidthOrHeight)\n\n let newCanvas = canvas\n let ctx\n\n if (needToHandle) {\n [newCanvas, ctx] = getNewCanvasAndCtx(width, height)\n if (width > height) {\n newCanvas.width = maxWidthOrHeight\n newCanvas.height = (height / width) * maxWidthOrHeight\n } else {\n newCanvas.width = (width / height) * maxWidthOrHeight\n newCanvas.height = maxWidthOrHeight\n }\n ctx.drawImage(canvas, 0, 0, newCanvas.width, newCanvas.height)\n\n cleanupCanvasMemory(canvas)\n }\n\n return newCanvas\n}\n\n/**\n * followExifOrientation\n * source: https://stackoverflow.com/a/40867559/10395024\n *\n * @param {HTMLCanvasElement} canvas\n * @param {number} exifOrientation\n * @returns {HTMLCanvasElement} canvas\n */\nexport function followExifOrientation (canvas, exifOrientation) {\n const width = canvas.width\n const height = canvas.height\n\n const [newCanvas, ctx] = getNewCanvasAndCtx(width, height)\n\n // set proper canvas dimensions before transform & export\n if (4 < exifOrientation && exifOrientation < 9) {\n newCanvas.width = height\n newCanvas.height = width\n } else {\n newCanvas.width = width\n newCanvas.height = height\n }\n\n // transform context before drawing image\n switch (exifOrientation) {\n case 2: ctx.transform(-1, 0, 0, 1, width, 0); break;\n case 3: ctx.transform(-1, 0, 0, -1, width, height); break;\n case 4: ctx.transform(1, 0, 0, -1, 0, height); break;\n case 5: ctx.transform(0, 1, 1, 0, 0, 0); break;\n case 6: ctx.transform(0, 1, -1, 0, height, 0); break;\n case 7: ctx.transform(0, -1, -1, 0, height, width); break;\n case 8: ctx.transform(0, -1, 1, 0, 0, width); break;\n default: break;\n }\n\n ctx.drawImage(canvas, 0, 0, width, height)\n\n cleanupCanvasMemory(canvas)\n\n return newCanvas\n}\n\n/**\n * get new Canvas and it's context\n * @param width\n * @param height\n * @returns {[HTMLCanvasElement, CanvasRenderingContext2D]}\n */\nexport function getNewCanvasAndCtx (width, height) {\n let canvas\n let ctx\n try {\n canvas = new OffscreenCanvas(width, height)\n ctx = canvas.getContext('2d')\n if (ctx === null) {\n throw new Error('getContext of OffscreenCanvas returns null')\n }\n } catch (e) {\n canvas = document.createElement('canvas')\n ctx = canvas.getContext('2d')\n }\n canvas.width = width\n canvas.height = height\n return [canvas, ctx]\n}\n\n/**\n * clear Canvas memory\n * @param canvas\n * @returns null\n */\nexport function cleanupCanvasMemory (canvas) {\n canvas.width = 0\n canvas.height = 0\n}\n\nif (isBrowser) {\n// Polyfill for Number.isInteger\n Number.isInteger = Number.isInteger || function (value) {\n return typeof value === 'number' &&\n isFinite(value) &&\n Math.floor(value) === value\n }\n}\n","import {\n canvasToFile,\n cleanupCanvasMemory,\n drawFileInCanvas,\n followExifOrientation,\n getExifOrientation,\n getNewCanvasAndCtx,\n handleMaxWidthOrHeight\n} from './utils'\n\n/**\n * Compress an image file.\n *\n * @param {File} file\n * @param {Object} options - { maxSizeMB=Number.POSITIVE_INFINITY, maxWidthOrHeight, useWebWorker=false, maxIteration = 10, exifOrientation, fileType }\n * @param {number} [options.maxSizeMB=Number.POSITIVE_INFINITY]\n * @param {number} [options.maxWidthOrHeight=undefined]\n * @param {number} [options.maxIteration=10]\n * @param {number} [options.exifOrientation] - default to be the exif orientation from the image file\n * @param {Function} [options.onProgress] - a function takes one progress argument (progress from 0 to 100)\n * @param {string} [options.fileType] - default to be the original mime type from the image file\n * @returns {Promise}\n */\nexport default async function compress (file, options) {\n let progress = 0\n\n function incProgress (inc = 5) {\n progress += inc\n if (typeof options.onProgress === 'function') {\n options.onProgress(Math.min(progress, 100))\n }\n }\n\n function setProgress (p) {\n progress = Math.min(Math.max(p, progress), 100)\n if (typeof options.onProgress === 'function') {\n options.onProgress(progress)\n }\n }\n\n let remainingTrials = options.maxIteration || 10\n\n const maxSizeByte = options.maxSizeMB * 1024 * 1024\n\n incProgress()\n\n // drawFileInCanvas\n let [img, origCanvas] = await drawFileInCanvas(file)\n\n incProgress()\n\n // handleMaxWidthOrHeight\n const maxWidthOrHeightFixedCanvas = handleMaxWidthOrHeight(origCanvas, options)\n\n incProgress()\n\n // exifOrientation\n options.exifOrientation = options.exifOrientation || await getExifOrientation(file)\n incProgress()\n const orientationFixedCanvas = followExifOrientation(maxWidthOrHeightFixedCanvas, options.exifOrientation)\n incProgress()\n\n let quality = 1\n\n let tempFile = await canvasToFile(orientationFixedCanvas, options.fileType || file.type, file.name, file.lastModified, quality)\n incProgress()\n\n const origExceedMaxSize = tempFile.size > maxSizeByte\n const sizeBecomeLarger = tempFile.size > file.size\n\n // check if we need to compress or resize\n if (!origExceedMaxSize && !sizeBecomeLarger) {\n // no need to compress\n setProgress(100)\n return tempFile\n }\n\n const sourceSize = file.size\n const renderedSize = tempFile.size\n let currentSize = renderedSize\n let compressedFile\n let newCanvas, ctx\n let canvas = orientationFixedCanvas\n while (remainingTrials-- && (currentSize > maxSizeByte || currentSize > sourceSize)) {\n const newWidth = origExceedMaxSize ? canvas.width * 0.95 : canvas.width\n const newHeight = origExceedMaxSize ? canvas.height * 0.95 : canvas.height;\n [newCanvas, ctx] = getNewCanvasAndCtx(newWidth, newHeight)\n\n ctx.drawImage(canvas, 0, 0, newWidth, newHeight)\n\n if (file.type === 'image/jpeg') {\n quality *= 0.95\n }\n compressedFile = await canvasToFile(newCanvas, options.fileType || file.type, file.name, file.lastModified, quality)\n\n cleanupCanvasMemory(canvas)\n\n canvas = newCanvas\n\n currentSize = compressedFile.size\n setProgress(Math.min(99, Math.floor((renderedSize - currentSize) / (renderedSize - maxSizeByte) * 100)))\n }\n\n // garbage clean canvas for safari\n // ref: https://bugs.webkit.org/show_bug.cgi?id=195325\n cleanupCanvasMemory(canvas)\n cleanupCanvasMemory(newCanvas)\n cleanupCanvasMemory(maxWidthOrHeightFixedCanvas)\n cleanupCanvasMemory(orientationFixedCanvas)\n cleanupCanvasMemory(origCanvas)\n\n setProgress(100)\n return compressedFile\n}\n","import lib from './index'\nimport compress from './image-compression'\nimport { getNewCanvasAndCtx } from './utils'\n\nlet cnt = 0\nlet imageCompressionLibUrl\nlet worker\n\nfunction createWorker (script) {\n if (typeof script === 'function') {\n script = `(${f})()`\n }\n return new Worker(URL.createObjectURL(new Blob([script])))\n}\n\nfunction createSourceObject (str) {\n return URL.createObjectURL(new Blob([str], { type: 'application/javascript' }))\n}\n\nfunction generateLib () {\n // prepare the lib to be used inside WebWorker\n return createSourceObject(`\n function imageCompression (){return (${lib}).apply(null, arguments)}\n\n imageCompression.getDataUrlFromFile = ${lib.getDataUrlFromFile}\n imageCompression.getFilefromDataUrl = ${lib.getFilefromDataUrl}\n imageCompression.loadImage = ${lib.loadImage}\n imageCompression.drawImageInCanvas = ${lib.drawImageInCanvas}\n imageCompression.drawFileInCanvas = ${lib.drawFileInCanvas}\n imageCompression.canvasToFile = ${lib.canvasToFile}\n imageCompression.getExifOrientation = ${lib.getExifOrientation}\n imageCompression.handleMaxWidthOrHeight = ${lib.handleMaxWidthOrHeight}\n imageCompression.followExifOrientation = ${lib.followExifOrientation}\n imageCompression.cleanupMemory = ${lib.cleanupMemory}\n\n getDataUrlFromFile = imageCompression.getDataUrlFromFile\n getFilefromDataUrl = imageCompression.getFilefromDataUrl\n loadImage = imageCompression.loadImage\n drawImageInCanvas = imageCompression.drawImageInCanvas\n drawFileInCanvas = imageCompression.drawFileInCanvas\n canvasToFile = imageCompression.canvasToFile\n getExifOrientation = imageCompression.getExifOrientation\n handleMaxWidthOrHeight = imageCompression.handleMaxWidthOrHeight\n followExifOrientation = imageCompression.followExifOrientation\n cleanupMemory = imageCompression.cleanupMemory\n\n getNewCanvasAndCtx = ${getNewCanvasAndCtx}\n \n CustomFileReader = FileReader\n \n CustomFile = File\n \n function _slicedToArray(arr, n) { return arr }\n \n function _typeof(a) { return typeof a }\n\n function compress (){return (${compress}).apply(null, arguments)}\n `)\n}\n\nfunction generateWorkerScript () {\n // code to be run in the WebWorker\n return createWorker(`\n let scriptImported = false\n self.addEventListener('message', async (e) => {\n const { file, id, imageCompressionLibUrl, options } = e.data\n options.onProgress = (progress) => self.postMessage({ progress, id })\n try {\n if (!scriptImported) {\n // console.log('[worker] importScripts', imageCompressionLibUrl)\n self.importScripts(imageCompressionLibUrl)\n scriptImported = true\n }\n // console.log('[worker] self', self)\n const compressedFile = await imageCompression(file, options)\n self.postMessage({ file: compressedFile, id })\n } catch (e) {\n // console.error('[worker] error', e)\n self.postMessage({ error: e.message + '\\\\n' + e.stack, id })\n }\n })\n `)\n}\n\nexport function compressOnWebWorker (file, options) {\n return new Promise(async (resolve, reject) => {\n let id = cnt++\n\n if (!imageCompressionLibUrl) {\n imageCompressionLibUrl = generateLib()\n }\n\n if (!worker) {\n worker = generateWorkerScript()\n }\n\n function handler (e) {\n if (e.data.id === id) {\n if (e.data.progress !== undefined && e.data.progress < 100) {\n options.onProgress(e.data.progress)\n return\n }\n worker.removeEventListener('message', handler)\n if (e.data.error) {\n reject(new Error(e.data.error))\n }\n resolve(e.data.file)\n }\n }\n\n worker.addEventListener('message', handler)\n worker.postMessage({\n file,\n id,\n imageCompressionLibUrl,\n options: { ...options, onProgress: undefined }\n })\n })\n}\n","import compress from './image-compression'\nimport {\n canvasToFile,\n drawFileInCanvas,\n drawImageInCanvas,\n getDataUrlFromFile,\n getFilefromDataUrl,\n loadImage,\n getExifOrientation,\n handleMaxWidthOrHeight,\n followExifOrientation,\n CustomFile,\n cleanupCanvasMemory\n} from './utils'\nimport { compressOnWebWorker } from './web-worker'\n\n/**\n * Compress an image file.\n *\n * @param {File} file\n * @param {Object} options - { maxSizeMB=Number.POSITIVE_INFINITY, maxWidthOrHeight, useWebWorker=false, maxIteration = 10, exifOrientation, fileType }\n * @param {number} [options.maxSizeMB=Number.POSITIVE_INFINITY]\n * @param {number} [options.maxWidthOrHeight=undefined]\n * @param {boolean} [options.useWebWorker=false]\n * @param {number} [options.maxIteration=10]\n * @param {number} [options.exifOrientation] - default to be the exif orientation from the image file\n * @param {Function} [options.onProgress] - a function takes one progress argument (progress from 0 to 100)\n * @param {string} [options.fileType] - default to be the original mime type from the image file\n * @returns {Promise}\n */\nasync function imageCompression (file, options) {\n\n let compressedFile\n\n options.maxSizeMB = options.maxSizeMB || Number.POSITIVE_INFINITY\n const useWebWorker = typeof options.useWebWorker === 'boolean' ? options.useWebWorker : false\n delete options.useWebWorker\n\n if (!(file instanceof Blob || file instanceof CustomFile)) {\n throw new Error('The file given is not an instance of Blob or File')\n } else if (!/^image/.test(file.type)) {\n throw new Error('The file given is not an image')\n }\n\n // try run in web worker, fall back to run in main thread\n const inWebWorker = typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope\n\n // if ((useWebWorker && typeof Worker === 'function') || inWebWorker) {\n // console.log('run compression in web worker')\n // } else {\n // console.log('run compression in main thread')\n // }\n\n if (useWebWorker && typeof Worker === 'function' && !inWebWorker) {\n try {\n // console.log(1)\n // \"compressOnWebWorker\" is kind of like a recursion to call \"imageCompression\" again inside web worker\n compressedFile = await compressOnWebWorker(file, options)\n } catch (e) {\n // console.warn('Run compression in web worker failed:', e, ', fall back to main thread')\n // console.log(1.5)\n compressedFile = await compress(file, options)\n }\n } else {\n // console.log(2)\n compressedFile = await compress(file, options)\n }\n\n try {\n compressedFile.name = file.name\n compressedFile.lastModified = file.lastModified\n } catch (e) {}\n\n return compressedFile\n}\n\nimageCompression.getDataUrlFromFile = getDataUrlFromFile\nimageCompression.getFilefromDataUrl = getFilefromDataUrl\nimageCompression.loadImage = loadImage\nimageCompression.drawImageInCanvas = drawImageInCanvas\nimageCompression.drawFileInCanvas = drawFileInCanvas\nimageCompression.canvasToFile = canvasToFile\nimageCompression.getExifOrientation = getExifOrientation\nimageCompression.handleMaxWidthOrHeight = handleMaxWidthOrHeight\nimageCompression.followExifOrientation = followExifOrientation\nimageCompression.cleanupMemory = cleanupCanvasMemory\nimageCompression.version = '1.0.8'\n\nexport default imageCompression\n"],"names":["isBrowser","window","moduleMapper","getDataUrlFromFile","resolve","reader","dataurl","split","n","bstr","filename","file","lastModified","canvas","view","length","offset","getUint32","marker","maxWidthOrHeight","width","followExifOrientation","height","ctx","inc","drawFileInCanvas","exifOrientation","orientationFixedCanvas","options","fileType","imageCompressionLibUrl","cnt","generateLib","script","imageCompression","getFilefromDataUrl","loadImage","drawImageInCanvas","canvasToFile","getExifOrientation"],"mappings":";;;;;;;4yCAAA,IAAMA,EAA8B,oBAAlBC,2EAKoF,uCAQ5DC,2HAiBnC,SAASC,mDACPC,6DAEqBC,uNAiCdC,EAAAC,mCACK,gBAEjBC,EAAIC,6BAEJD,8DAUYE,EACdC,EAAAC,eACAR,28BAwC2CS,qcAoB7CC,mEAEMV,mCAGQW,6DAEgBC,8GASpBF,EAAAG,UAAgBD,0GAKPA,EAAA,iCAGTE,0KA+BVC,oJAqBuBC,4CAQTP,EAAV,0CAEAA,qBASYQ,yCACZC,mPAmBuCA,wJAI9B,sHA8BbC,2BAHWD,kaC3OTE,iLAPK,6DAUGC,iBAARd,yKAUMe,sUAWAC,EAAwBC,EAAAC,6PAsB5BhB,o8BC/FJ,IACIiB,IADAC,IAcJ,SAASC,okEAPPC,gkDCyDwBtB,mzBAa1BuB,iBAAA/B,mBAAsCA,mBACtC+B,iBAAAC,sCACAD,iBAAAE,oBACAF,iBAAAG,oCACAH,iBAAAT,kCAEAS,iBAAAI,0BAxFAJ,iBAAAK"} \ No newline at end of file +{"version":3,"file":"browser-image-compression.mjs","sources":["../lib/utils.js","../lib/image-compression.js","../lib/web-worker.js","../lib/index.js"],"sourcesContent":["const isBrowser = typeof window !== 'undefined' // change browser environment to support SSR\n\n// add support for cordova-plugin-file\nconst moduleMapper = isBrowser && window.cordova && window.cordova.require && window.cordova.require('cordova/modulemapper')\nexport const CustomFile = isBrowser && ((moduleMapper && moduleMapper.getOriginalSymbol(window, 'File')) || File)\nexport const CustomFileReader = isBrowser && ((moduleMapper && moduleMapper.getOriginalSymbol(window, 'FileReader')) || FileReader)\n\n/**\n * getDataUrlFromFile\n *\n * @param {File} file\n * @returns {Promise}\n */\nexport function getDataUrlFromFile (file) {\n return new Promise((resolve, reject) => {\n const reader = new CustomFileReader()\n reader.onload = () => resolve(reader.result)\n reader.onerror = (e) => reject(e)\n reader.readAsDataURL(file)\n })\n}\n\n/**\n * getFilefromDataUrl\n *\n * @param {string} dataurl\n * @param {string} filename\n * @param {number} [lastModified=Date.now()]\n * @returns {Promise}\n */\nexport function getFilefromDataUrl (dataurl, filename, lastModified = Date.now()) {\n return new Promise((resolve) => {\n const arr = dataurl.split(',')\n const mime = arr[0].match(/:(.*?);/)[1]\n const bstr = atob(arr[1])\n let n = bstr.length\n const u8arr = new Uint8Array(n)\n while (n--) {\n u8arr[n] = bstr.charCodeAt(n)\n }\n const file = new Blob([u8arr], { type: mime })\n file.name = filename\n file.lastModified = lastModified\n resolve(file)\n\n // Safari has issue with File constructor not being able to POST in FormData\n // https://github.com/Donaldcwl/browser-image-compression/issues/8\n // https://bugs.webkit.org/show_bug.cgi?id=165081\n // let file\n // try {\n // file = new File([u8arr], filename, { type: mime }) // Edge do not support File constructor\n // } catch (e) {\n // file = new Blob([u8arr], { type: mime })\n // file.name = filename\n // file.lastModified = lastModified\n // }\n // resolve(file)\n })\n}\n\n/**\n * loadImage\n *\n * @param {string} src\n * @returns {Promise}\n */\nexport function loadImage (src) {\n return new Promise((resolve, reject) => {\n const img = new Image()\n img.onload = () => resolve(img)\n img.onerror = (e) => reject(e)\n img.src = src\n })\n}\n\n/**\n * drawImageInCanvas\n *\n * @param {HTMLImageElement} img\n * @returns {HTMLCanvasElement}\n */\nexport function drawImageInCanvas (img) {\n const [canvas, ctx] = getNewCanvasAndCtx(img.width, img.height)\n ctx.drawImage(img, 0, 0, canvas.width, canvas.height)\n return canvas\n}\n\n/**\n * drawFileInCanvas\n *\n * @param {File} file\n * @returns {Promise<[ImageBitmap | HTMLImageElement, HTMLCanvasElement]>}\n */\nexport async function drawFileInCanvas (file) {\n let img\n try {\n img = await createImageBitmap(file)\n } catch (e) {\n const dataUrl = await getDataUrlFromFile(file)\n img = await loadImage(dataUrl)\n }\n const canvas = drawImageInCanvas(img)\n return [img, canvas]\n}\n\n/**\n * canvasToFile\n *\n * @param canvas\n * @param {string} fileType\n * @param {string} fileName\n * @param {number} fileLastModified\n * @param {number} [quality]\n * @returns {Promise}\n */\nexport async function canvasToFile (canvas, fileType, fileName, fileLastModified, quality = 1) {\n let file\n if (typeof OffscreenCanvas === 'function' && canvas instanceof OffscreenCanvas) {\n file = await canvas.convertToBlob({ type: fileType, quality })\n file.name = fileName\n file.lastModified = fileLastModified\n } else {\n const dataUrl = canvas.toDataURL(fileType, quality)\n file = await getFilefromDataUrl(dataUrl, fileName, fileLastModified)\n }\n return file\n}\n\n/**\n * getExifOrientation\n * get image exif orientation info\n * source: https://stackoverflow.com/a/32490603/10395024\n *\n * @param {File} file\n * @returns {Promise} - orientation id, see https://i.stack.imgur.com/VGsAj.gif\n */\nexport function getExifOrientation (file) {\n return new Promise((resolve, reject) => {\n const reader = new CustomFileReader()\n reader.onload = (e) => {\n const view = new DataView(e.target.result)\n if (view.getUint16(0, false) != 0xFFD8) {\n return resolve(-2)\n }\n const length = view.byteLength\n let offset = 2\n while (offset < length) {\n if (view.getUint16(offset + 2, false) <= 8) return resolve(-1)\n const marker = view.getUint16(offset, false)\n offset += 2\n if (marker == 0xFFE1) {\n if (view.getUint32(offset += 2, false) != 0x45786966) {\n return resolve(-1)\n }\n\n var little = view.getUint16(offset += 6, false) == 0x4949\n offset += view.getUint32(offset + 4, little)\n var tags = view.getUint16(offset, little)\n offset += 2\n for (var i = 0; i < tags; i++) {\n if (view.getUint16(offset + (i * 12), little) == 0x0112) {\n return resolve(view.getUint16(offset + (i * 12) + 8, little))\n }\n }\n } else if ((marker & 0xFF00) != 0xFF00) {\n break\n } else {\n offset += view.getUint16(offset, false)\n }\n }\n return resolve(-1)\n }\n reader.onerror = (e) => reject(e)\n reader.readAsArrayBuffer(file)\n })\n}\n\n/**\n *\n * @param {HTMLCanvasElement} canvas\n * @param options\n * @returns {HTMLCanvasElement>}\n */\nexport function handleMaxWidthOrHeight (canvas, options) {\n const width = canvas.width\n const height = canvas.height\n const maxWidthOrHeight = options.maxWidthOrHeight\n\n const needToHandle = Number.isFinite(maxWidthOrHeight) && (width > maxWidthOrHeight || height > maxWidthOrHeight)\n\n let newCanvas = canvas\n let ctx\n\n if (needToHandle) {\n [newCanvas, ctx] = getNewCanvasAndCtx(width, height)\n if (width > height) {\n newCanvas.width = maxWidthOrHeight\n newCanvas.height = (height / width) * maxWidthOrHeight\n } else {\n newCanvas.width = (width / height) * maxWidthOrHeight\n newCanvas.height = maxWidthOrHeight\n }\n ctx.drawImage(canvas, 0, 0, newCanvas.width, newCanvas.height)\n\n cleanupCanvasMemory(canvas)\n }\n\n return newCanvas\n}\n\n/**\n * followExifOrientation\n * source: https://stackoverflow.com/a/40867559/10395024\n *\n * @param {HTMLCanvasElement} canvas\n * @param {number} exifOrientation\n * @returns {HTMLCanvasElement} canvas\n */\nexport function followExifOrientation (canvas, exifOrientation) {\n const width = canvas.width\n const height = canvas.height\n\n const [newCanvas, ctx] = getNewCanvasAndCtx(width, height)\n\n // set proper canvas dimensions before transform & export\n if (4 < exifOrientation && exifOrientation < 9) {\n newCanvas.width = height\n newCanvas.height = width\n } else {\n newCanvas.width = width\n newCanvas.height = height\n }\n\n // transform context before drawing image\n switch (exifOrientation) {\n case 2: ctx.transform(-1, 0, 0, 1, width, 0); break;\n case 3: ctx.transform(-1, 0, 0, -1, width, height); break;\n case 4: ctx.transform(1, 0, 0, -1, 0, height); break;\n case 5: ctx.transform(0, 1, 1, 0, 0, 0); break;\n case 6: ctx.transform(0, 1, -1, 0, height, 0); break;\n case 7: ctx.transform(0, -1, -1, 0, height, width); break;\n case 8: ctx.transform(0, -1, 1, 0, 0, width); break;\n default: break;\n }\n\n ctx.drawImage(canvas, 0, 0, width, height)\n\n cleanupCanvasMemory(canvas)\n\n return newCanvas\n}\n\n/**\n * get new Canvas and it's context\n * @param width\n * @param height\n * @returns {[HTMLCanvasElement, CanvasRenderingContext2D]}\n */\nexport function getNewCanvasAndCtx (width, height) {\n let canvas\n let ctx\n try {\n canvas = new OffscreenCanvas(width, height)\n ctx = canvas.getContext('2d')\n if (ctx === null) {\n throw new Error('getContext of OffscreenCanvas returns null')\n }\n } catch (e) {\n canvas = document.createElement('canvas')\n ctx = canvas.getContext('2d')\n }\n canvas.width = width\n canvas.height = height\n return [canvas, ctx]\n}\n\n/**\n * clear Canvas memory\n * @param canvas\n * @returns null\n */\nexport function cleanupCanvasMemory (canvas) {\n canvas.width = 0\n canvas.height = 0\n}\n\nif (isBrowser) {\n// Polyfill for Number.isInteger\n Number.isInteger = Number.isInteger || function (value) {\n return typeof value === 'number' &&\n isFinite(value) &&\n Math.floor(value) === value\n }\n}\n","import {\n canvasToFile,\n cleanupCanvasMemory,\n drawFileInCanvas,\n followExifOrientation,\n getExifOrientation,\n getNewCanvasAndCtx,\n handleMaxWidthOrHeight\n} from './utils'\n\n/**\n * Compress an image file.\n *\n * @param {File} file\n * @param {Object} options - { maxSizeMB=Number.POSITIVE_INFINITY, maxWidthOrHeight, useWebWorker=false, maxIteration = 10, exifOrientation, fileType }\n * @param {number} [options.maxSizeMB=Number.POSITIVE_INFINITY]\n * @param {number} [options.maxWidthOrHeight=undefined]\n * @param {number} [options.maxIteration=10]\n * @param {number} [options.exifOrientation] - default to be the exif orientation from the image file\n * @param {Function} [options.onProgress] - a function takes one progress argument (progress from 0 to 100)\n * @param {string} [options.fileType] - default to be the original mime type from the image file\n * @returns {Promise}\n */\nexport default async function compress (file, options) {\n let progress = 0\n\n function incProgress (inc = 5) {\n progress += inc\n if (typeof options.onProgress === 'function') {\n options.onProgress(Math.min(progress, 100))\n }\n }\n\n function setProgress (p) {\n progress = Math.min(Math.max(p, progress), 100)\n if (typeof options.onProgress === 'function') {\n options.onProgress(progress)\n }\n }\n\n let remainingTrials = options.maxIteration || 10\n\n const maxSizeByte = options.maxSizeMB * 1024 * 1024\n\n incProgress()\n\n // drawFileInCanvas\n let [img, origCanvas] = await drawFileInCanvas(file)\n\n incProgress()\n\n // handleMaxWidthOrHeight\n const maxWidthOrHeightFixedCanvas = handleMaxWidthOrHeight(origCanvas, options)\n\n incProgress()\n\n // exifOrientation\n options.exifOrientation = options.exifOrientation || await getExifOrientation(file)\n incProgress()\n const orientationFixedCanvas = followExifOrientation(maxWidthOrHeightFixedCanvas, options.exifOrientation)\n incProgress()\n\n let quality = 1\n\n let tempFile = await canvasToFile(orientationFixedCanvas, options.fileType || file.type, file.name, file.lastModified, quality)\n incProgress()\n\n const origExceedMaxSize = tempFile.size > maxSizeByte\n const sizeBecomeLarger = tempFile.size > file.size\n\n // check if we need to compress or resize\n if (!origExceedMaxSize && !sizeBecomeLarger) {\n // no need to compress\n setProgress(100)\n return tempFile\n }\n\n const sourceSize = file.size\n const renderedSize = tempFile.size\n let currentSize = renderedSize\n let compressedFile\n let newCanvas, ctx\n let canvas = orientationFixedCanvas\n while (remainingTrials-- && (currentSize > maxSizeByte || currentSize > sourceSize)) {\n const newWidth = origExceedMaxSize ? canvas.width * 0.95 : canvas.width\n const newHeight = origExceedMaxSize ? canvas.height * 0.95 : canvas.height;\n [newCanvas, ctx] = getNewCanvasAndCtx(newWidth, newHeight)\n\n ctx.drawImage(canvas, 0, 0, newWidth, newHeight)\n\n if (file.type === 'image/jpeg') {\n quality *= 0.95\n }\n compressedFile = await canvasToFile(newCanvas, options.fileType || file.type, file.name, file.lastModified, quality)\n\n cleanupCanvasMemory(canvas)\n\n canvas = newCanvas\n\n currentSize = compressedFile.size\n setProgress(Math.min(99, Math.floor((renderedSize - currentSize) / (renderedSize - maxSizeByte) * 100)))\n }\n\n // garbage clean canvas for safari\n // ref: https://bugs.webkit.org/show_bug.cgi?id=195325\n cleanupCanvasMemory(canvas)\n cleanupCanvasMemory(newCanvas)\n cleanupCanvasMemory(maxWidthOrHeightFixedCanvas)\n cleanupCanvasMemory(orientationFixedCanvas)\n cleanupCanvasMemory(origCanvas)\n\n setProgress(100)\n return compressedFile\n}\n","import lib from './index'\nimport compress from './image-compression'\nimport { getNewCanvasAndCtx } from './utils'\n\nlet cnt = 0\nlet imageCompressionLibUrl\nlet worker\n\nfunction createWorker (script) {\n if (typeof script === 'function') {\n script = `(${f})()`\n }\n return new Worker(URL.createObjectURL(new Blob([script])))\n}\n\nfunction createSourceObject (str) {\n return URL.createObjectURL(new Blob([str], { type: 'application/javascript' }))\n}\n\nfunction generateLib () {\n // prepare the lib to be used inside WebWorker\n return createSourceObject(`\n function imageCompression (){return (${lib}).apply(null, arguments)}\n\n imageCompression.getDataUrlFromFile = ${lib.getDataUrlFromFile}\n imageCompression.getFilefromDataUrl = ${lib.getFilefromDataUrl}\n imageCompression.loadImage = ${lib.loadImage}\n imageCompression.drawImageInCanvas = ${lib.drawImageInCanvas}\n imageCompression.drawFileInCanvas = ${lib.drawFileInCanvas}\n imageCompression.canvasToFile = ${lib.canvasToFile}\n imageCompression.getExifOrientation = ${lib.getExifOrientation}\n imageCompression.handleMaxWidthOrHeight = ${lib.handleMaxWidthOrHeight}\n imageCompression.followExifOrientation = ${lib.followExifOrientation}\n imageCompression.cleanupMemory = ${lib.cleanupMemory}\n\n getDataUrlFromFile = imageCompression.getDataUrlFromFile\n getFilefromDataUrl = imageCompression.getFilefromDataUrl\n loadImage = imageCompression.loadImage\n drawImageInCanvas = imageCompression.drawImageInCanvas\n drawFileInCanvas = imageCompression.drawFileInCanvas\n canvasToFile = imageCompression.canvasToFile\n getExifOrientation = imageCompression.getExifOrientation\n handleMaxWidthOrHeight = imageCompression.handleMaxWidthOrHeight\n followExifOrientation = imageCompression.followExifOrientation\n cleanupMemory = imageCompression.cleanupMemory\n\n getNewCanvasAndCtx = ${getNewCanvasAndCtx}\n \n CustomFileReader = FileReader\n \n CustomFile = File\n \n function _slicedToArray(arr, n) { return arr }\n \n function _typeof(a) { return typeof a }\n\n function compress (){return (${compress}).apply(null, arguments)}\n `)\n}\n\nfunction generateWorkerScript () {\n // code to be run in the WebWorker\n return createWorker(`\n let scriptImported = false\n self.addEventListener('message', async (e) => {\n const { file, id, imageCompressionLibUrl, options } = e.data\n options.onProgress = (progress) => self.postMessage({ progress, id })\n try {\n if (!scriptImported) {\n // console.log('[worker] importScripts', imageCompressionLibUrl)\n self.importScripts(imageCompressionLibUrl)\n scriptImported = true\n }\n // console.log('[worker] self', self)\n const compressedFile = await imageCompression(file, options)\n self.postMessage({ file: compressedFile, id })\n } catch (e) {\n // console.error('[worker] error', e)\n self.postMessage({ error: e.message + '\\\\n' + e.stack, id })\n }\n })\n `)\n}\n\nexport function compressOnWebWorker (file, options) {\n return new Promise(async (resolve, reject) => {\n let id = cnt++\n\n if (!imageCompressionLibUrl) {\n imageCompressionLibUrl = generateLib()\n }\n\n if (!worker) {\n worker = generateWorkerScript()\n }\n\n function handler (e) {\n if (e.data.id === id) {\n if (e.data.progress !== undefined && e.data.progress < 100) {\n options.onProgress(e.data.progress)\n return\n }\n worker.removeEventListener('message', handler)\n if (e.data.error) {\n reject(new Error(e.data.error))\n }\n resolve(e.data.file)\n }\n }\n\n worker.addEventListener('message', handler)\n worker.postMessage({\n file,\n id,\n imageCompressionLibUrl,\n options: { ...options, onProgress: undefined }\n })\n })\n}\n","import compress from './image-compression'\nimport {\n canvasToFile,\n drawFileInCanvas,\n drawImageInCanvas,\n getDataUrlFromFile,\n getFilefromDataUrl,\n loadImage,\n getExifOrientation,\n handleMaxWidthOrHeight,\n followExifOrientation,\n CustomFile,\n cleanupCanvasMemory\n} from './utils'\nimport { compressOnWebWorker } from './web-worker'\n\n/**\n * Compress an image file.\n *\n * @param {File} file\n * @param {Object} options - { maxSizeMB=Number.POSITIVE_INFINITY, maxWidthOrHeight, useWebWorker=false, maxIteration = 10, exifOrientation, fileType }\n * @param {number} [options.maxSizeMB=Number.POSITIVE_INFINITY]\n * @param {number} [options.maxWidthOrHeight=undefined]\n * @param {boolean} [options.useWebWorker=false]\n * @param {number} [options.maxIteration=10]\n * @param {number} [options.exifOrientation] - default to be the exif orientation from the image file\n * @param {Function} [options.onProgress] - a function takes one progress argument (progress from 0 to 100)\n * @param {string} [options.fileType] - default to be the original mime type from the image file\n * @returns {Promise}\n */\nasync function imageCompression (file, options) {\n\n let compressedFile\n\n options.maxSizeMB = options.maxSizeMB || Number.POSITIVE_INFINITY\n const useWebWorker = typeof options.useWebWorker === 'boolean' ? options.useWebWorker : false\n delete options.useWebWorker\n\n if (!(file instanceof Blob || file instanceof CustomFile)) {\n throw new Error('The file given is not an instance of Blob or File')\n } else if (!/^image/.test(file.type)) {\n throw new Error('The file given is not an image')\n }\n\n // try run in web worker, fall back to run in main thread\n const inWebWorker = typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope\n\n // if ((useWebWorker && typeof Worker === 'function') || inWebWorker) {\n // console.log('run compression in web worker')\n // } else {\n // console.log('run compression in main thread')\n // }\n\n if (useWebWorker && typeof Worker === 'function' && !inWebWorker) {\n try {\n // console.log(1)\n // \"compressOnWebWorker\" is kind of like a recursion to call \"imageCompression\" again inside web worker\n compressedFile = await compressOnWebWorker(file, options)\n } catch (e) {\n // console.warn('Run compression in web worker failed:', e, ', fall back to main thread')\n // console.log(1.5)\n compressedFile = await compress(file, options)\n }\n } else {\n // console.log(2)\n compressedFile = await compress(file, options)\n }\n\n try {\n compressedFile.name = file.name\n compressedFile.lastModified = file.lastModified\n } catch (e) {}\n\n return compressedFile\n}\n\nimageCompression.getDataUrlFromFile = getDataUrlFromFile\nimageCompression.getFilefromDataUrl = getFilefromDataUrl\nimageCompression.loadImage = loadImage\nimageCompression.drawImageInCanvas = drawImageInCanvas\nimageCompression.drawFileInCanvas = drawFileInCanvas\nimageCompression.canvasToFile = canvasToFile\nimageCompression.getExifOrientation = getExifOrientation\nimageCompression.handleMaxWidthOrHeight = handleMaxWidthOrHeight\nimageCompression.followExifOrientation = followExifOrientation\nimageCompression.cleanupMemory = cleanupCanvasMemory\nimageCompression.version = '1.0.9'\n\nexport default imageCompression\n"],"names":["isBrowser","window","moduleMapper","getDataUrlFromFile","resolve","reader","dataurl","split","n","bstr","filename","file","lastModified","canvas","view","length","offset","getUint32","marker","maxWidthOrHeight","width","followExifOrientation","height","ctx","inc","drawFileInCanvas","exifOrientation","orientationFixedCanvas","options","fileType","imageCompressionLibUrl","cnt","generateLib","script","imageCompression","getFilefromDataUrl","loadImage","drawImageInCanvas","canvasToFile","getExifOrientation"],"mappings":";;;;;;;4yCAAA,IAAMA,EAA8B,oBAAlBC,2EAKoF,uCAQ5DC,2HAiBnC,SAASC,mDACPC,6DAEqBC,uNAiCdC,EAAAC,mCACK,gBAEjBC,EAAIC,6BAEJD,8DAUYE,EACdC,EAAAC,eACAR,28BAwC2CS,qcAoB7CC,mEAEMV,mCAGQW,6DAEgBC,8GASpBF,EAAAG,UAAgBD,0GAKPA,EAAA,iCAGTE,0KA+BVC,oJAqBuBC,4CAQTP,EAAV,0CAEAA,qBASYQ,yCACZC,mPAmBuCA,wJAI9B,sHA8BbC,2BAHWD,kaC3OTE,iLAPK,6DAUGC,iBAARd,yKAUMe,sUAWAC,EAAwBC,EAAAC,6PAsB5BhB,o8BC/FJ,IACIiB,IADAC,IAcJ,SAASC,okEAPPC,gkDCyDwBtB,mzBAa1BuB,iBAAA/B,mBAAsCA,mBACtC+B,iBAAAC,sCACAD,iBAAAE,oBACAF,iBAAAG,oCACAH,iBAAAT,kCAEAS,iBAAAI,0BAxFAJ,iBAAAK"} \ No newline at end of file diff --git a/example/React App/package.json b/example/React App/package.json index f6d9371..4ae8475 100644 --- a/example/React App/package.json +++ b/example/React App/package.json @@ -6,7 +6,7 @@ "@testing-library/jest-dom": "^4.2.4", "@testing-library/react": "^9.3.2", "@testing-library/user-event": "^7.1.2", - "browser-image-compression": "^1.0.8", + "browser-image-compression": "^1.0.9", "react": "^16.13.0", "react-dom": "^16.13.0", "react-scripts": "3.4.0" diff --git a/example/basic.html b/example/basic.html index f7bb9d7..65e7650 100644 --- a/example/basic.html +++ b/example/basic.html @@ -96,6 +96,6 @@ }) } - + diff --git a/lib/index.d.ts b/lib/index.d.ts index 790ae12..94ff80d 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -1,5 +1,9 @@ -declare module 'browser-image-compression' { - interface Options { +// Type definitions for browser-image-compression 1.0 +// Project: https://github.com/Donaldcwl/browser-image-compression +// Definitions by: Donald +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +interface Options { /** @default Number.POSITIVE_INFINITY */ maxSizeMB?: number; /** @default undefined */ @@ -7,16 +11,17 @@ declare module 'browser-image-compression' { /** @default false */ useWebWorker?: boolean; /** @default 10 */ - maxIteration?: number, + maxIteration?: number; /** Default to be the exif orientation from the image file */ - exifOrientation?: number, + exifOrientation?: number; /** A function takes one progress argument (progress from 0 to 100) */ - onProgress?: (progress: number) => void, + onProgress?: (progress: number) => void; /** Default to be the original mime type from the image file */ - fileType?: string - } + fileType?: string; +} - function imageCompression (image: Blob, options: Options): Promise; +declare function imageCompression(image: File | Blob, options: Options): Promise; - export = imageCompression; -} +export as namespace imageCompression; + +export = imageCompression; diff --git a/lib/index.js b/lib/index.js index d9edd01..0b09de4 100644 --- a/lib/index.js +++ b/lib/index.js @@ -84,6 +84,6 @@ imageCompression.getExifOrientation = getExifOrientation imageCompression.handleMaxWidthOrHeight = handleMaxWidthOrHeight imageCompression.followExifOrientation = followExifOrientation imageCompression.cleanupMemory = cleanupCanvasMemory -imageCompression.version = '1.0.8' +imageCompression.version = '1.0.9' export default imageCompression diff --git a/package-lock.json b/package-lock.json index eeeef08..f1e6e0f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "browser-image-compression", - "version": "1.0.8", + "version": "1.0.9", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 8179e49..932db1a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "browser-image-compression", - "version": "1.0.8", + "version": "1.0.9", "description": "Compress images in the browser", "main": "dist/browser-image-compression.js", "module": "dist/browser-image-compression.mjs",