From 729951f706db5a793f17cd57ff870f2a67e20ec8 Mon Sep 17 00:00:00 2001 From: Donald Date: Fri, 8 Mar 2019 15:38:49 +0800 Subject: [PATCH] release v1.0.1 - fixing bug related to wrong image output resolution in some cases --- README.md | 13 + coverage/badge.svg | 2 +- coverage/coverage-final.json | 6 +- coverage/coverage-summary.json | 6 +- coverage/image-compression.js.html | 38 +-- coverage/index.html | 32 +- coverage/index.js.html | 30 +- .../lcov-report/image-compression.js.html | 38 +-- coverage/lcov-report/index.html | 32 +- coverage/lcov-report/index.js.html | 30 +- coverage/lcov-report/utils.js.html | 150 ++++----- coverage/lcov.info | 284 +++++++++--------- coverage/utils.js.html | 150 ++++----- dist/browser-image-compression.js | 2 +- dist/browser-image-compression.js.map | 2 +- dist/browser-image-compression.mjs | 2 +- dist/browser-image-compression.mjs.map | 2 +- example/basic.html | 6 +- example/development.html | 54 ++++ lib/utils.js | 6 +- package.json | 2 +- test/index.spec.js | 83 +++++ 22 files changed, 560 insertions(+), 410 deletions(-) create mode 100644 example/development.html diff --git a/README.md b/README.md index 5f930c9..38cafac 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,9 @@ Javascript module to be run in the web browser for image compression. - **Multi-thread** (web worker) non-blocking compression are supported through options. ## Change log ## +v1.0.1 +- fixing bug related to wrong image output resolution in some case + v1.0.0 - breaking change: change "imageCompression" function signature - use of OffscreenCanvas when support, fallback to document.createElement('canvas') @@ -141,6 +144,16 @@ cd browser-image-compression/example # open "basic.html" on your browser ``` +## Contribution ## +1. fork the repo and git clone it +2. run `npm run watch` # it will watch code change in lib/ folder and generate js in dist/ folder +3. add/update code in lib/ folder +4. try the code by opening example/development.html which will load the js in dist/ folder +5. add/update test in test/ folder +6. `npm run test` +7. push to your forked repo on github +8. make a pull request to this repo + [dist]: https://github.com/Donaldcwl/browser-image-compression/tree/master/dist [example]: https://github.com/Donaldcwl/browser-image-compression/tree/master/example [delivrjs]: https://cdn.jsdelivr.net/ diff --git a/coverage/badge.svg b/coverage/badge.svg index 46a1b56..606cf04 100644 --- a/coverage/badge.svg +++ b/coverage/badge.svg @@ -1 +1 @@ - CoverageCoverage65.12%65.12% \ No newline at end of file + CoverageCoverage68.02%68.02% \ No newline at end of file diff --git a/coverage/coverage-final.json b/coverage/coverage-final.json index 1381e4d..7603b4a 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":15,"column":24},"end":{"line":15,"column":50}},"1":{"start":{"line":17,"column":22},"end":{"line":17,"column":53}},"2":{"start":{"line":20,"column":2},"end":{"line":22,"column":3}},"3":{"start":{"line":21,"column":4},"end":{"line":21,"column":15}},"4":{"start":{"line":27,"column":13},"end":{"line":27,"column":41}},"5":{"start":{"line":28,"column":2},"end":{"line":28,"column":15}},"6":{"start":{"line":29,"column":2},"end":{"line":29,"column":18}},"7":{"start":{"line":32,"column":14},"end":{"line":32,"column":58}},"8":{"start":{"line":33,"column":2},"end":{"line":33,"column":19}},"9":{"start":{"line":34,"column":2},"end":{"line":34,"column":22}},"10":{"start":{"line":36,"column":2},"end":{"line":38,"column":3}},"11":{"start":{"line":37,"column":4},"end":{"line":37,"column":15}},"12":{"start":{"line":41,"column":2},"end":{"line":41,"column":85}},"13":{"start":{"line":42,"column":2},"end":{"line":42,"column":70}},"14":{"start":{"line":44,"column":16},"end":{"line":44,"column":17}},"15":{"start":{"line":45,"column":23},"end":{"line":45,"column":99}},"16":{"start":{"line":46,"column":2},"end":{"line":67,"column":3}},"17":{"start":{"line":47,"column":4},"end":{"line":55,"column":5}},"18":{"start":{"line":48,"column":6},"end":{"line":48,"column":25}},"19":{"start":{"line":49,"column":6},"end":{"line":49,"column":26}},"20":{"start":{"line":51,"column":18},"end":{"line":51,"column":41}},"21":{"start":{"line":52,"column":6},"end":{"line":52,"column":59}},"22":{"start":{"line":54,"column":6},"end":{"line":54,"column":99}},"23":{"start":{"line":57,"column":4},"end":{"line":66,"column":5}},"24":{"start":{"line":58,"column":6},"end":{"line":58,"column":25}},"25":{"start":{"line":59,"column":6},"end":{"line":59,"column":26}},"26":{"start":{"line":61,"column":18},"end":{"line":61,"column":41}},"27":{"start":{"line":62,"column":6},"end":{"line":62,"column":59}},"28":{"start":{"line":64,"column":6},"end":{"line":64,"column":20}},"29":{"start":{"line":65,"column":6},"end":{"line":65,"column":99}},"30":{"start":{"line":69,"column":2},"end":{"line":69,"column":23}}},"fnMap":{"0":{"name":"compress","decl":{"start":{"line":14,"column":30},"end":{"line":14,"column":38}},"loc":{"start":{"line":14,"column":55},"end":{"line":70,"column":1}},"line":14}},"branchMap":{"0":{"loc":{"start":{"line":15,"column":24},"end":{"line":15,"column":50}},"type":"binary-expr","locations":[{"start":{"line":15,"column":24},"end":{"line":15,"column":44}},{"start":{"line":15,"column":48},"end":{"line":15,"column":50}}],"line":15},"1":{"loc":{"start":{"line":20,"column":2},"end":{"line":22,"column":3}},"type":"if","locations":[{"start":{"line":20,"column":2},"end":{"line":22,"column":3}},{"start":{"line":20,"column":2},"end":{"line":22,"column":3}}],"line":20},"2":{"loc":{"start":{"line":20,"column":6},"end":{"line":20,"column":81}},"type":"binary-expr","locations":[{"start":{"line":20,"column":6},"end":{"line":20,"column":30}},{"start":{"line":20,"column":34},"end":{"line":20,"column":81}}],"line":20},"3":{"loc":{"start":{"line":36,"column":2},"end":{"line":38,"column":3}},"type":"if","locations":[{"start":{"line":36,"column":2},"end":{"line":38,"column":3}},{"start":{"line":36,"column":2},"end":{"line":38,"column":3}}],"line":36},"4":{"loc":{"start":{"line":36,"column":6},"end":{"line":36,"column":44}},"type":"binary-expr","locations":[{"start":{"line":36,"column":6},"end":{"line":36,"column":30}},{"start":{"line":36,"column":34},"end":{"line":36,"column":44}}],"line":36},"5":{"loc":{"start":{"line":41,"column":28},"end":{"line":41,"column":85}},"type":"binary-expr","locations":[{"start":{"line":41,"column":28},"end":{"line":41,"column":51}},{"start":{"line":41,"column":55},"end":{"line":41,"column":85}}],"line":41},"6":{"loc":{"start":{"line":46,"column":2},"end":{"line":67,"column":3}},"type":"if","locations":[{"start":{"line":46,"column":2},"end":{"line":67,"column":3}},{"start":{"line":46,"column":2},"end":{"line":67,"column":3}}],"line":46},"7":{"loc":{"start":{"line":47,"column":11},"end":{"line":47,"column":65}},"type":"binary-expr","locations":[{"start":{"line":47,"column":11},"end":{"line":47,"column":28}},{"start":{"line":47,"column":32},"end":{"line":47,"column":65}}],"line":47},"8":{"loc":{"start":{"line":57,"column":11},"end":{"line":57,"column":65}},"type":"binary-expr","locations":[{"start":{"line":57,"column":11},"end":{"line":57,"column":28}},{"start":{"line":57,"column":32},"end":{"line":57,"column":65}}],"line":57}},"s":{"0":2,"1":2,"2":2,"3":0,"4":2,"5":2,"6":2,"7":2,"8":2,"9":2,"10":2,"11":0,"12":2,"13":2,"14":2,"15":2,"16":2,"17":1,"18":5,"19":5,"20":5,"21":5,"22":5,"23":1,"24":0,"25":0,"26":0,"27":0,"28":0,"29":0,"30":2},"f":{"0":2},"b":{"0":[2,2],"1":[0,2],"2":[2,0],"3":[0,2],"4":[2,0],"5":[2,0],"6":[1,1],"7":[6,6],"8":[1,1]},"_coverageSchema":"43e27e138ebf9cfc5966b082cf9a028302ed4184","hash":"beebf5b646233c5693357688f175a13472f43722","contentHash":"1aae39d6fc0b0b06d108acdaeda7f46c"} -,"/Users/donald/Project/browser-image-compression/lib/index.js": {"path":"/Users/donald/Project/browser-image-compression/lib/index.js","statementMap":{"0":{"start":{"line":31,"column":2},"end":{"line":31,"column":67}},"1":{"start":{"line":32,"column":2},"end":{"line":32,"column":96}},"2":{"start":{"line":34,"column":2},"end":{"line":38,"column":3}},"3":{"start":{"line":35,"column":4},"end":{"line":35,"column":72}},"4":{"start":{"line":36,"column":9},"end":{"line":38,"column":3}},"5":{"start":{"line":37,"column":4},"end":{"line":37,"column":53}},"6":{"start":{"line":41,"column":22},"end":{"line":41,"column":99}},"7":{"start":{"line":49,"column":2},"end":{"line":59,"column":3}},"8":{"start":{"line":50,"column":4},"end":{"line":56,"column":5}},"9":{"start":{"line":52,"column":6},"end":{"line":52,"column":63}},"10":{"start":{"line":55,"column":6},"end":{"line":55,"column":52}},"11":{"start":{"line":58,"column":4},"end":{"line":58,"column":50}},"12":{"start":{"line":61,"column":2},"end":{"line":64,"column":16}},"13":{"start":{"line":62,"column":4},"end":{"line":62,"column":35}},"14":{"start":{"line":63,"column":4},"end":{"line":63,"column":51}},"15":{"start":{"line":66,"column":2},"end":{"line":66,"column":23}},"16":{"start":{"line":69,"column":0},"end":{"line":69,"column":56}},"17":{"start":{"line":70,"column":0},"end":{"line":70,"column":56}},"18":{"start":{"line":71,"column":0},"end":{"line":71,"column":38}},"19":{"start":{"line":72,"column":0},"end":{"line":72,"column":54}},"20":{"start":{"line":73,"column":0},"end":{"line":73,"column":52}},"21":{"start":{"line":74,"column":0},"end":{"line":74,"column":44}},"22":{"start":{"line":75,"column":0},"end":{"line":75,"column":56}},"23":{"start":{"line":76,"column":0},"end":{"line":76,"column":64}},"24":{"start":{"line":77,"column":0},"end":{"line":77,"column":62}}},"fnMap":{"0":{"name":"imageCompression","decl":{"start":{"line":27,"column":15},"end":{"line":27,"column":31}},"loc":{"start":{"line":27,"column":48},"end":{"line":67,"column":1}},"line":27}},"branchMap":{"0":{"loc":{"start":{"line":31,"column":22},"end":{"line":31,"column":67}},"type":"binary-expr","locations":[{"start":{"line":31,"column":22},"end":{"line":31,"column":39}},{"start":{"line":31,"column":43},"end":{"line":31,"column":67}}],"line":31},"1":{"loc":{"start":{"line":32,"column":25},"end":{"line":32,"column":96}},"type":"cond-expr","locations":[{"start":{"line":32,"column":69},"end":{"line":32,"column":89}},{"start":{"line":32,"column":92},"end":{"line":32,"column":96}}],"line":32},"2":{"loc":{"start":{"line":34,"column":2},"end":{"line":38,"column":3}},"type":"if","locations":[{"start":{"line":34,"column":2},"end":{"line":38,"column":3}},{"start":{"line":34,"column":2},"end":{"line":38,"column":3}}],"line":34},"3":{"loc":{"start":{"line":34,"column":8},"end":{"line":34,"column":52}},"type":"binary-expr","locations":[{"start":{"line":34,"column":8},"end":{"line":34,"column":28}},{"start":{"line":34,"column":32},"end":{"line":34,"column":52}}],"line":34},"4":{"loc":{"start":{"line":36,"column":9},"end":{"line":38,"column":3}},"type":"if","locations":[{"start":{"line":36,"column":9},"end":{"line":38,"column":3}},{"start":{"line":36,"column":9},"end":{"line":38,"column":3}}],"line":36},"5":{"loc":{"start":{"line":41,"column":22},"end":{"line":41,"column":99}},"type":"binary-expr","locations":[{"start":{"line":41,"column":22},"end":{"line":41,"column":62}},{"start":{"line":41,"column":66},"end":{"line":41,"column":99}}],"line":41},"6":{"loc":{"start":{"line":49,"column":2},"end":{"line":59,"column":3}},"type":"if","locations":[{"start":{"line":49,"column":2},"end":{"line":59,"column":3}},{"start":{"line":49,"column":2},"end":{"line":59,"column":3}}],"line":49},"7":{"loc":{"start":{"line":49,"column":6},"end":{"line":49,"column":74}},"type":"binary-expr","locations":[{"start":{"line":49,"column":6},"end":{"line":49,"column":26}},{"start":{"line":49,"column":30},"end":{"line":49,"column":58}},{"start":{"line":49,"column":62},"end":{"line":49,"column":74}}],"line":49}},"s":{"0":5,"1":5,"2":5,"3":2,"4":3,"5":1,"6":2,"7":2,"8":0,"9":0,"10":0,"11":2,"12":2,"13":2,"14":2,"15":2,"16":1,"17":1,"18":1,"19":1,"20":1,"21":1,"22":1,"23":1,"24":1},"f":{"0":5},"b":{"0":[5,0],"1":[5,0],"2":[2,3],"3":[5,5],"4":[1,2],"5":[2,0],"6":[0,2],"7":[2,0,0]},"_coverageSchema":"43e27e138ebf9cfc5966b082cf9a028302ed4184","hash":"c6faf098a3ae71adbc865c5284132f0b67861195","contentHash":"ca857e27e9624169077901ec50dd53e3"} -,"/Users/donald/Project/browser-image-compression/lib/utils.js": {"path":"/Users/donald/Project/browser-image-compression/lib/utils.js","statementMap":{"0":{"start":{"line":8,"column":2},"end":{"line":13,"column":4}},"1":{"start":{"line":9,"column":19},"end":{"line":9,"column":35}},"2":{"start":{"line":10,"column":4},"end":{"line":10,"column":48}},"3":{"start":{"line":10,"column":26},"end":{"line":10,"column":48}},"4":{"start":{"line":11,"column":4},"end":{"line":11,"column":37}},"5":{"start":{"line":11,"column":28},"end":{"line":11,"column":37}},"6":{"start":{"line":12,"column":4},"end":{"line":12,"column":30}},"7":{"start":{"line":25,"column":2},"end":{"line":43,"column":4}},"8":{"start":{"line":26,"column":16},"end":{"line":26,"column":34}},"9":{"start":{"line":27,"column":17},"end":{"line":27,"column":43}},"10":{"start":{"line":28,"column":17},"end":{"line":28,"column":29}},"11":{"start":{"line":29,"column":12},"end":{"line":29,"column":23}},"12":{"start":{"line":30,"column":18},"end":{"line":30,"column":35}},"13":{"start":{"line":31,"column":4},"end":{"line":33,"column":5}},"14":{"start":{"line":32,"column":6},"end":{"line":32,"column":35}},"15":{"start":{"line":35,"column":4},"end":{"line":41,"column":5}},"16":{"start":{"line":36,"column":6},"end":{"line":36,"column":56}},"17":{"start":{"line":38,"column":6},"end":{"line":38,"column":46}},"18":{"start":{"line":39,"column":6},"end":{"line":39,"column":26}},"19":{"start":{"line":40,"column":6},"end":{"line":40,"column":38}},"20":{"start":{"line":42,"column":4},"end":{"line":42,"column":17}},"21":{"start":{"line":53,"column":2},"end":{"line":58,"column":4}},"22":{"start":{"line":54,"column":16},"end":{"line":54,"column":27}},"23":{"start":{"line":55,"column":4},"end":{"line":55,"column":35}},"24":{"start":{"line":55,"column":23},"end":{"line":55,"column":35}},"25":{"start":{"line":56,"column":4},"end":{"line":56,"column":34}},"26":{"start":{"line":56,"column":25},"end":{"line":56,"column":34}},"27":{"start":{"line":57,"column":4},"end":{"line":57,"column":17}},"28":{"start":{"line":69,"column":2},"end":{"line":73,"column":3}},"29":{"start":{"line":70,"column":4},"end":{"line":70,"column":55}},"30":{"start":{"line":72,"column":4},"end":{"line":72,"column":45}},"31":{"start":{"line":74,"column":14},"end":{"line":74,"column":37}},"32":{"start":{"line":75,"column":2},"end":{"line":75,"column":55}},"33":{"start":{"line":76,"column":2},"end":{"line":76,"column":15}},"34":{"start":{"line":87,"column":2},"end":{"line":92,"column":3}},"35":{"start":{"line":88,"column":4},"end":{"line":88,"column":39}},"36":{"start":{"line":90,"column":20},"end":{"line":90,"column":50}},"37":{"start":{"line":91,"column":4},"end":{"line":91,"column":34}},"38":{"start":{"line":93,"column":17},"end":{"line":93,"column":39}},"39":{"start":{"line":94,"column":2},"end":{"line":94,"column":22}},"40":{"start":{"line":109,"column":2},"end":{"line":116,"column":3}},"41":{"start":{"line":110,"column":4},"end":{"line":110,"column":76}},"42":{"start":{"line":111,"column":4},"end":{"line":111,"column":34}},"43":{"start":{"line":112,"column":4},"end":{"line":112,"column":50}},"44":{"start":{"line":114,"column":20},"end":{"line":114,"column":55}},"45":{"start":{"line":115,"column":4},"end":{"line":115,"column":82}},"46":{"start":{"line":117,"column":2},"end":{"line":117,"column":23}},"47":{"start":{"line":129,"column":2},"end":{"line":166,"column":4}},"48":{"start":{"line":130,"column":19},"end":{"line":130,"column":35}},"49":{"start":{"line":131,"column":4},"end":{"line":163,"column":5}},"50":{"start":{"line":132,"column":19},"end":{"line":132,"column":48}},"51":{"start":{"line":133,"column":6},"end":{"line":135,"column":7}},"52":{"start":{"line":134,"column":8},"end":{"line":134,"column":26}},"53":{"start":{"line":136,"column":21},"end":{"line":136,"column":36}},"54":{"start":{"line":137,"column":19},"end":{"line":137,"column":20}},"55":{"start":{"line":138,"column":6},"end":{"line":161,"column":7}},"56":{"start":{"line":139,"column":8},"end":{"line":139,"column":70}},"57":{"start":{"line":139,"column":52},"end":{"line":139,"column":70}},"58":{"start":{"line":140,"column":23},"end":{"line":140,"column":52}},"59":{"start":{"line":141,"column":8},"end":{"line":141,"column":19}},"60":{"start":{"line":142,"column":8},"end":{"line":160,"column":9}},"61":{"start":{"line":143,"column":10},"end":{"line":145,"column":11}},"62":{"start":{"line":144,"column":12},"end":{"line":144,"column":30}},"63":{"start":{"line":147,"column":23},"end":{"line":147,"column":67}},"64":{"start":{"line":148,"column":10},"end":{"line":148,"column":54}},"65":{"start":{"line":149,"column":21},"end":{"line":149,"column":51}},"66":{"start":{"line":150,"column":10},"end":{"line":150,"column":21}},"67":{"start":{"line":151,"column":10},"end":{"line":155,"column":11}},"68":{"start":{"line":152,"column":12},"end":{"line":154,"column":13}},"69":{"start":{"line":153,"column":14},"end":{"line":153,"column":75}},"70":{"start":{"line":156,"column":15},"end":{"line":160,"column":9}},"71":{"start":{"line":157,"column":10},"end":{"line":157,"column":15}},"72":{"start":{"line":159,"column":10},"end":{"line":159,"column":49}},"73":{"start":{"line":162,"column":6},"end":{"line":162,"column":24}},"74":{"start":{"line":164,"column":4},"end":{"line":164,"column":37}},"75":{"start":{"line":164,"column":28},"end":{"line":164,"column":37}},"76":{"start":{"line":165,"column":4},"end":{"line":165,"column":34}},"77":{"start":{"line":177,"column":14},"end":{"line":177,"column":37}},"78":{"start":{"line":179,"column":27},"end":{"line":179,"column":51}},"79":{"start":{"line":180,"column":23},"end":{"line":180,"column":124}},"80":{"start":{"line":181,"column":2},"end":{"line":192,"column":3}},"81":{"start":{"line":182,"column":4},"end":{"line":188,"column":5}},"82":{"start":{"line":183,"column":6},"end":{"line":183,"column":37}},"83":{"start":{"line":184,"column":6},"end":{"line":184,"column":65}},"84":{"start":{"line":186,"column":6},"end":{"line":186,"column":64}},"85":{"start":{"line":187,"column":6},"end":{"line":187,"column":38}},"86":{"start":{"line":190,"column":4},"end":{"line":190,"column":28}},"87":{"start":{"line":191,"column":4},"end":{"line":191,"column":30}},"88":{"start":{"line":194,"column":2},"end":{"line":194,"column":55}},"89":{"start":{"line":195,"column":2},"end":{"line":195,"column":31}},"90":{"start":{"line":208,"column":14},"end":{"line":208,"column":37}},"91":{"start":{"line":210,"column":16},"end":{"line":210,"column":25}},"92":{"start":{"line":211,"column":17},"end":{"line":211,"column":27}},"93":{"start":{"line":214,"column":2},"end":{"line":220,"column":3}},"94":{"start":{"line":215,"column":4},"end":{"line":215,"column":25}},"95":{"start":{"line":216,"column":4},"end":{"line":216,"column":25}},"96":{"start":{"line":218,"column":4},"end":{"line":218,"column":24}},"97":{"start":{"line":219,"column":4},"end":{"line":219,"column":26}},"98":{"start":{"line":223,"column":2},"end":{"line":232,"column":3}},"99":{"start":{"line":224,"column":12},"end":{"line":224,"column":49}},"100":{"start":{"line":224,"column":50},"end":{"line":224,"column":56}},"101":{"start":{"line":225,"column":12},"end":{"line":225,"column":55}},"102":{"start":{"line":225,"column":56},"end":{"line":225,"column":62}},"103":{"start":{"line":226,"column":12},"end":{"line":226,"column":50}},"104":{"start":{"line":226,"column":51},"end":{"line":226,"column":57}},"105":{"start":{"line":227,"column":12},"end":{"line":227,"column":44}},"106":{"start":{"line":227,"column":45},"end":{"line":227,"column":51}},"107":{"start":{"line":228,"column":12},"end":{"line":228,"column":50}},"108":{"start":{"line":228,"column":51},"end":{"line":228,"column":57}},"109":{"start":{"line":229,"column":12},"end":{"line":229,"column":55}},"110":{"start":{"line":229,"column":56},"end":{"line":229,"column":62}},"111":{"start":{"line":230,"column":12},"end":{"line":230,"column":49}},"112":{"start":{"line":230,"column":50},"end":{"line":230,"column":56}},"113":{"start":{"line":231,"column":13},"end":{"line":231,"column":19}},"114":{"start":{"line":234,"column":2},"end":{"line":234,"column":26}},"115":{"start":{"line":236,"column":2},"end":{"line":236,"column":15}}},"fnMap":{"0":{"name":"getDataUrlFromFile","decl":{"start":{"line":7,"column":16},"end":{"line":7,"column":34}},"loc":{"start":{"line":7,"column":42},"end":{"line":14,"column":1}},"line":7},"1":{"name":"(anonymous_1)","decl":{"start":{"line":8,"column":21},"end":{"line":8,"column":22}},"loc":{"start":{"line":8,"column":42},"end":{"line":13,"column":3}},"line":8},"2":{"name":"(anonymous_2)","decl":{"start":{"line":10,"column":20},"end":{"line":10,"column":21}},"loc":{"start":{"line":10,"column":26},"end":{"line":10,"column":48}},"line":10},"3":{"name":"(anonymous_3)","decl":{"start":{"line":11,"column":21},"end":{"line":11,"column":22}},"loc":{"start":{"line":11,"column":28},"end":{"line":11,"column":37}},"line":11},"4":{"name":"getFilefromDataUrl","decl":{"start":{"line":24,"column":16},"end":{"line":24,"column":34}},"loc":{"start":{"line":24,"column":82},"end":{"line":44,"column":1}},"line":24},"5":{"name":"(anonymous_5)","decl":{"start":{"line":25,"column":21},"end":{"line":25,"column":22}},"loc":{"start":{"line":25,"column":34},"end":{"line":43,"column":3}},"line":25},"6":{"name":"loadImage","decl":{"start":{"line":52,"column":16},"end":{"line":52,"column":25}},"loc":{"start":{"line":52,"column":32},"end":{"line":59,"column":1}},"line":52},"7":{"name":"(anonymous_7)","decl":{"start":{"line":53,"column":21},"end":{"line":53,"column":22}},"loc":{"start":{"line":53,"column":42},"end":{"line":58,"column":3}},"line":53},"8":{"name":"(anonymous_8)","decl":{"start":{"line":55,"column":17},"end":{"line":55,"column":18}},"loc":{"start":{"line":55,"column":23},"end":{"line":55,"column":35}},"line":55},"9":{"name":"(anonymous_9)","decl":{"start":{"line":56,"column":18},"end":{"line":56,"column":19}},"loc":{"start":{"line":56,"column":25},"end":{"line":56,"column":34}},"line":56},"10":{"name":"drawImageInCanvas","decl":{"start":{"line":67,"column":16},"end":{"line":67,"column":33}},"loc":{"start":{"line":67,"column":40},"end":{"line":77,"column":1}},"line":67},"11":{"name":"drawFileInCanvas","decl":{"start":{"line":85,"column":22},"end":{"line":85,"column":38}},"loc":{"start":{"line":85,"column":46},"end":{"line":95,"column":1}},"line":85},"12":{"name":"canvasToFile","decl":{"start":{"line":107,"column":22},"end":{"line":107,"column":34}},"loc":{"start":{"line":107,"column":95},"end":{"line":118,"column":1}},"line":107},"13":{"name":"getExifOrientation","decl":{"start":{"line":128,"column":16},"end":{"line":128,"column":34}},"loc":{"start":{"line":128,"column":42},"end":{"line":167,"column":1}},"line":128},"14":{"name":"(anonymous_14)","decl":{"start":{"line":129,"column":21},"end":{"line":129,"column":22}},"loc":{"start":{"line":129,"column":42},"end":{"line":166,"column":3}},"line":129},"15":{"name":"(anonymous_15)","decl":{"start":{"line":131,"column":20},"end":{"line":131,"column":21}},"loc":{"start":{"line":131,"column":27},"end":{"line":163,"column":5}},"line":131},"16":{"name":"(anonymous_16)","decl":{"start":{"line":164,"column":21},"end":{"line":164,"column":22}},"loc":{"start":{"line":164,"column":28},"end":{"line":164,"column":37}},"line":164},"17":{"name":"handleMaxWidthOrHeight","decl":{"start":{"line":176,"column":16},"end":{"line":176,"column":38}},"loc":{"start":{"line":176,"column":62},"end":{"line":196,"column":1}},"line":176},"18":{"name":"followExifOrientation","decl":{"start":{"line":207,"column":16},"end":{"line":207,"column":37}},"loc":{"start":{"line":207,"column":69},"end":{"line":237,"column":1}},"line":207}},"branchMap":{"0":{"loc":{"start":{"line":24,"column":55},"end":{"line":24,"column":80}},"type":"default-arg","locations":[{"start":{"line":24,"column":70},"end":{"line":24,"column":80}}],"line":24},"1":{"loc":{"start":{"line":69,"column":2},"end":{"line":73,"column":3}},"type":"if","locations":[{"start":{"line":69,"column":2},"end":{"line":73,"column":3}},{"start":{"line":69,"column":2},"end":{"line":73,"column":3}}],"line":69},"2":{"loc":{"start":{"line":107,"column":82},"end":{"line":107,"column":93}},"type":"default-arg","locations":[{"start":{"line":107,"column":92},"end":{"line":107,"column":93}}],"line":107},"3":{"loc":{"start":{"line":109,"column":2},"end":{"line":116,"column":3}},"type":"if","locations":[{"start":{"line":109,"column":2},"end":{"line":116,"column":3}},{"start":{"line":109,"column":2},"end":{"line":116,"column":3}}],"line":109},"4":{"loc":{"start":{"line":109,"column":6},"end":{"line":109,"column":80}},"type":"binary-expr","locations":[{"start":{"line":109,"column":6},"end":{"line":109,"column":43}},{"start":{"line":109,"column":47},"end":{"line":109,"column":80}}],"line":109},"5":{"loc":{"start":{"line":133,"column":6},"end":{"line":135,"column":7}},"type":"if","locations":[{"start":{"line":133,"column":6},"end":{"line":135,"column":7}},{"start":{"line":133,"column":6},"end":{"line":135,"column":7}}],"line":133},"6":{"loc":{"start":{"line":139,"column":8},"end":{"line":139,"column":70}},"type":"if","locations":[{"start":{"line":139,"column":8},"end":{"line":139,"column":70}},{"start":{"line":139,"column":8},"end":{"line":139,"column":70}}],"line":139},"7":{"loc":{"start":{"line":142,"column":8},"end":{"line":160,"column":9}},"type":"if","locations":[{"start":{"line":142,"column":8},"end":{"line":160,"column":9}},{"start":{"line":142,"column":8},"end":{"line":160,"column":9}}],"line":142},"8":{"loc":{"start":{"line":143,"column":10},"end":{"line":145,"column":11}},"type":"if","locations":[{"start":{"line":143,"column":10},"end":{"line":145,"column":11}},{"start":{"line":143,"column":10},"end":{"line":145,"column":11}}],"line":143},"9":{"loc":{"start":{"line":152,"column":12},"end":{"line":154,"column":13}},"type":"if","locations":[{"start":{"line":152,"column":12},"end":{"line":154,"column":13}},{"start":{"line":152,"column":12},"end":{"line":154,"column":13}}],"line":152},"10":{"loc":{"start":{"line":156,"column":15},"end":{"line":160,"column":9}},"type":"if","locations":[{"start":{"line":156,"column":15},"end":{"line":160,"column":9}},{"start":{"line":156,"column":15},"end":{"line":160,"column":9}}],"line":156},"11":{"loc":{"start":{"line":180,"column":23},"end":{"line":180,"column":124}},"type":"binary-expr","locations":[{"start":{"line":180,"column":23},"end":{"line":180,"column":57}},{"start":{"line":180,"column":62},"end":{"line":180,"column":90}},{"start":{"line":180,"column":94},"end":{"line":180,"column":123}}],"line":180},"12":{"loc":{"start":{"line":181,"column":2},"end":{"line":192,"column":3}},"type":"if","locations":[{"start":{"line":181,"column":2},"end":{"line":192,"column":3}},{"start":{"line":181,"column":2},"end":{"line":192,"column":3}}],"line":181},"13":{"loc":{"start":{"line":182,"column":4},"end":{"line":188,"column":5}},"type":"if","locations":[{"start":{"line":182,"column":4},"end":{"line":188,"column":5}},{"start":{"line":182,"column":4},"end":{"line":188,"column":5}}],"line":182},"14":{"loc":{"start":{"line":214,"column":2},"end":{"line":220,"column":3}},"type":"if","locations":[{"start":{"line":214,"column":2},"end":{"line":220,"column":3}},{"start":{"line":214,"column":2},"end":{"line":220,"column":3}}],"line":214},"15":{"loc":{"start":{"line":214,"column":6},"end":{"line":214,"column":48}},"type":"binary-expr","locations":[{"start":{"line":214,"column":6},"end":{"line":214,"column":25}},{"start":{"line":214,"column":29},"end":{"line":214,"column":48}}],"line":214},"16":{"loc":{"start":{"line":223,"column":2},"end":{"line":232,"column":3}},"type":"switch","locations":[{"start":{"line":224,"column":4},"end":{"line":224,"column":56}},{"start":{"line":225,"column":4},"end":{"line":225,"column":62}},{"start":{"line":226,"column":4},"end":{"line":226,"column":57}},{"start":{"line":227,"column":4},"end":{"line":227,"column":51}},{"start":{"line":228,"column":4},"end":{"line":228,"column":57}},{"start":{"line":229,"column":4},"end":{"line":229,"column":62}},{"start":{"line":230,"column":4},"end":{"line":230,"column":56}},{"start":{"line":231,"column":4},"end":{"line":231,"column":19}}],"line":223}},"s":{"0":4,"1":4,"2":4,"3":4,"4":4,"5":0,"6":4,"7":8,"8":8,"9":8,"10":8,"11":8,"12":8,"13":8,"14":11825174,"15":8,"16":8,"17":7,"18":7,"19":7,"20":8,"21":5,"22":5,"23":5,"24":5,"25":5,"26":0,"27":5,"28":4,"29":0,"30":4,"31":4,"32":4,"33":4,"34":3,"35":3,"36":3,"37":3,"38":3,"39":3,"40":7,"41":0,"42":0,"43":0,"44":7,"45":7,"46":7,"47":1,"48":1,"49":1,"50":1,"51":1,"52":1,"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":2,"78":2,"79":2,"80":2,"81":0,"82":0,"83":0,"84":0,"85":0,"86":2,"87":2,"88":2,"89":2,"90":2,"91":2,"92":2,"93":2,"94":0,"95":0,"96":2,"97":2,"98":2,"99":0,"100":0,"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":2,"114":2,"115":2},"f":{"0":4,"1":4,"2":4,"3":0,"4":8,"5":8,"6":5,"7":5,"8":5,"9":0,"10":4,"11":3,"12":7,"13":1,"14":1,"15":1,"16":0,"17":2,"18":2},"b":{"0":[8],"1":[0,4],"2":[0],"3":[0,7],"4":[7,0],"5":[1,0],"6":[0,0],"7":[0,0],"8":[0,0],"9":[0,0],"10":[0,0],"11":[2,0,0],"12":[0,2],"13":[0,0],"14":[0,2],"15":[2,0],"16":[0,0,0,0,0,0,0,2]},"_coverageSchema":"43e27e138ebf9cfc5966b082cf9a028302ed4184","hash":"a607eca64dbde11b3f11f4c3455396fbac241119","contentHash":"959705d371a0eae7e2a6bb5448fc98ee"} +{"/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":15,"column":24},"end":{"line":15,"column":50}},"1":{"start":{"line":17,"column":22},"end":{"line":17,"column":53}},"2":{"start":{"line":20,"column":2},"end":{"line":22,"column":3}},"3":{"start":{"line":21,"column":4},"end":{"line":21,"column":15}},"4":{"start":{"line":27,"column":13},"end":{"line":27,"column":41}},"5":{"start":{"line":28,"column":2},"end":{"line":28,"column":15}},"6":{"start":{"line":29,"column":2},"end":{"line":29,"column":18}},"7":{"start":{"line":32,"column":14},"end":{"line":32,"column":58}},"8":{"start":{"line":33,"column":2},"end":{"line":33,"column":19}},"9":{"start":{"line":34,"column":2},"end":{"line":34,"column":22}},"10":{"start":{"line":36,"column":2},"end":{"line":38,"column":3}},"11":{"start":{"line":37,"column":4},"end":{"line":37,"column":15}},"12":{"start":{"line":41,"column":2},"end":{"line":41,"column":85}},"13":{"start":{"line":42,"column":2},"end":{"line":42,"column":70}},"14":{"start":{"line":44,"column":16},"end":{"line":44,"column":17}},"15":{"start":{"line":45,"column":23},"end":{"line":45,"column":99}},"16":{"start":{"line":46,"column":2},"end":{"line":67,"column":3}},"17":{"start":{"line":47,"column":4},"end":{"line":55,"column":5}},"18":{"start":{"line":48,"column":6},"end":{"line":48,"column":25}},"19":{"start":{"line":49,"column":6},"end":{"line":49,"column":26}},"20":{"start":{"line":51,"column":18},"end":{"line":51,"column":41}},"21":{"start":{"line":52,"column":6},"end":{"line":52,"column":59}},"22":{"start":{"line":54,"column":6},"end":{"line":54,"column":99}},"23":{"start":{"line":57,"column":4},"end":{"line":66,"column":5}},"24":{"start":{"line":58,"column":6},"end":{"line":58,"column":25}},"25":{"start":{"line":59,"column":6},"end":{"line":59,"column":26}},"26":{"start":{"line":61,"column":18},"end":{"line":61,"column":41}},"27":{"start":{"line":62,"column":6},"end":{"line":62,"column":59}},"28":{"start":{"line":64,"column":6},"end":{"line":64,"column":20}},"29":{"start":{"line":65,"column":6},"end":{"line":65,"column":99}},"30":{"start":{"line":69,"column":2},"end":{"line":69,"column":23}}},"fnMap":{"0":{"name":"compress","decl":{"start":{"line":14,"column":30},"end":{"line":14,"column":38}},"loc":{"start":{"line":14,"column":55},"end":{"line":70,"column":1}},"line":14}},"branchMap":{"0":{"loc":{"start":{"line":15,"column":24},"end":{"line":15,"column":50}},"type":"binary-expr","locations":[{"start":{"line":15,"column":24},"end":{"line":15,"column":44}},{"start":{"line":15,"column":48},"end":{"line":15,"column":50}}],"line":15},"1":{"loc":{"start":{"line":20,"column":2},"end":{"line":22,"column":3}},"type":"if","locations":[{"start":{"line":20,"column":2},"end":{"line":22,"column":3}},{"start":{"line":20,"column":2},"end":{"line":22,"column":3}}],"line":20},"2":{"loc":{"start":{"line":20,"column":6},"end":{"line":20,"column":81}},"type":"binary-expr","locations":[{"start":{"line":20,"column":6},"end":{"line":20,"column":30}},{"start":{"line":20,"column":34},"end":{"line":20,"column":81}}],"line":20},"3":{"loc":{"start":{"line":36,"column":2},"end":{"line":38,"column":3}},"type":"if","locations":[{"start":{"line":36,"column":2},"end":{"line":38,"column":3}},{"start":{"line":36,"column":2},"end":{"line":38,"column":3}}],"line":36},"4":{"loc":{"start":{"line":36,"column":6},"end":{"line":36,"column":44}},"type":"binary-expr","locations":[{"start":{"line":36,"column":6},"end":{"line":36,"column":30}},{"start":{"line":36,"column":34},"end":{"line":36,"column":44}}],"line":36},"5":{"loc":{"start":{"line":41,"column":28},"end":{"line":41,"column":85}},"type":"binary-expr","locations":[{"start":{"line":41,"column":28},"end":{"line":41,"column":51}},{"start":{"line":41,"column":55},"end":{"line":41,"column":85}}],"line":41},"6":{"loc":{"start":{"line":46,"column":2},"end":{"line":67,"column":3}},"type":"if","locations":[{"start":{"line":46,"column":2},"end":{"line":67,"column":3}},{"start":{"line":46,"column":2},"end":{"line":67,"column":3}}],"line":46},"7":{"loc":{"start":{"line":47,"column":11},"end":{"line":47,"column":65}},"type":"binary-expr","locations":[{"start":{"line":47,"column":11},"end":{"line":47,"column":28}},{"start":{"line":47,"column":32},"end":{"line":47,"column":65}}],"line":47},"8":{"loc":{"start":{"line":57,"column":11},"end":{"line":57,"column":65}},"type":"binary-expr","locations":[{"start":{"line":57,"column":11},"end":{"line":57,"column":28}},{"start":{"line":57,"column":32},"end":{"line":57,"column":65}}],"line":57}},"s":{"0":6,"1":6,"2":6,"3":0,"4":6,"5":6,"6":6,"7":6,"8":6,"9":6,"10":6,"11":0,"12":6,"13":6,"14":6,"15":6,"16":6,"17":3,"18":5,"19":5,"20":5,"21":5,"22":5,"23":3,"24":0,"25":0,"26":0,"27":0,"28":0,"29":0,"30":6},"f":{"0":6},"b":{"0":[6,6],"1":[0,6],"2":[6,0],"3":[0,6],"4":[6,0],"5":[6,0],"6":[3,3],"7":[8,8],"8":[3,3]},"_coverageSchema":"43e27e138ebf9cfc5966b082cf9a028302ed4184","hash":"beebf5b646233c5693357688f175a13472f43722","contentHash":"1aae39d6fc0b0b06d108acdaeda7f46c"} +,"/Users/donald/Project/browser-image-compression/lib/index.js": {"path":"/Users/donald/Project/browser-image-compression/lib/index.js","statementMap":{"0":{"start":{"line":31,"column":2},"end":{"line":31,"column":67}},"1":{"start":{"line":32,"column":2},"end":{"line":32,"column":96}},"2":{"start":{"line":34,"column":2},"end":{"line":38,"column":3}},"3":{"start":{"line":35,"column":4},"end":{"line":35,"column":72}},"4":{"start":{"line":36,"column":9},"end":{"line":38,"column":3}},"5":{"start":{"line":37,"column":4},"end":{"line":37,"column":53}},"6":{"start":{"line":41,"column":22},"end":{"line":41,"column":99}},"7":{"start":{"line":49,"column":2},"end":{"line":59,"column":3}},"8":{"start":{"line":50,"column":4},"end":{"line":56,"column":5}},"9":{"start":{"line":52,"column":6},"end":{"line":52,"column":63}},"10":{"start":{"line":55,"column":6},"end":{"line":55,"column":52}},"11":{"start":{"line":58,"column":4},"end":{"line":58,"column":50}},"12":{"start":{"line":61,"column":2},"end":{"line":64,"column":16}},"13":{"start":{"line":62,"column":4},"end":{"line":62,"column":35}},"14":{"start":{"line":63,"column":4},"end":{"line":63,"column":51}},"15":{"start":{"line":66,"column":2},"end":{"line":66,"column":23}},"16":{"start":{"line":69,"column":0},"end":{"line":69,"column":56}},"17":{"start":{"line":70,"column":0},"end":{"line":70,"column":56}},"18":{"start":{"line":71,"column":0},"end":{"line":71,"column":38}},"19":{"start":{"line":72,"column":0},"end":{"line":72,"column":54}},"20":{"start":{"line":73,"column":0},"end":{"line":73,"column":52}},"21":{"start":{"line":74,"column":0},"end":{"line":74,"column":44}},"22":{"start":{"line":75,"column":0},"end":{"line":75,"column":56}},"23":{"start":{"line":76,"column":0},"end":{"line":76,"column":64}},"24":{"start":{"line":77,"column":0},"end":{"line":77,"column":62}}},"fnMap":{"0":{"name":"imageCompression","decl":{"start":{"line":27,"column":15},"end":{"line":27,"column":31}},"loc":{"start":{"line":27,"column":48},"end":{"line":67,"column":1}},"line":27}},"branchMap":{"0":{"loc":{"start":{"line":31,"column":22},"end":{"line":31,"column":67}},"type":"binary-expr","locations":[{"start":{"line":31,"column":22},"end":{"line":31,"column":39}},{"start":{"line":31,"column":43},"end":{"line":31,"column":67}}],"line":31},"1":{"loc":{"start":{"line":32,"column":25},"end":{"line":32,"column":96}},"type":"cond-expr","locations":[{"start":{"line":32,"column":69},"end":{"line":32,"column":89}},{"start":{"line":32,"column":92},"end":{"line":32,"column":96}}],"line":32},"2":{"loc":{"start":{"line":34,"column":2},"end":{"line":38,"column":3}},"type":"if","locations":[{"start":{"line":34,"column":2},"end":{"line":38,"column":3}},{"start":{"line":34,"column":2},"end":{"line":38,"column":3}}],"line":34},"3":{"loc":{"start":{"line":34,"column":8},"end":{"line":34,"column":52}},"type":"binary-expr","locations":[{"start":{"line":34,"column":8},"end":{"line":34,"column":28}},{"start":{"line":34,"column":32},"end":{"line":34,"column":52}}],"line":34},"4":{"loc":{"start":{"line":36,"column":9},"end":{"line":38,"column":3}},"type":"if","locations":[{"start":{"line":36,"column":9},"end":{"line":38,"column":3}},{"start":{"line":36,"column":9},"end":{"line":38,"column":3}}],"line":36},"5":{"loc":{"start":{"line":41,"column":22},"end":{"line":41,"column":99}},"type":"binary-expr","locations":[{"start":{"line":41,"column":22},"end":{"line":41,"column":62}},{"start":{"line":41,"column":66},"end":{"line":41,"column":99}}],"line":41},"6":{"loc":{"start":{"line":49,"column":2},"end":{"line":59,"column":3}},"type":"if","locations":[{"start":{"line":49,"column":2},"end":{"line":59,"column":3}},{"start":{"line":49,"column":2},"end":{"line":59,"column":3}}],"line":49},"7":{"loc":{"start":{"line":49,"column":6},"end":{"line":49,"column":74}},"type":"binary-expr","locations":[{"start":{"line":49,"column":6},"end":{"line":49,"column":26}},{"start":{"line":49,"column":30},"end":{"line":49,"column":58}},{"start":{"line":49,"column":62},"end":{"line":49,"column":74}}],"line":49}},"s":{"0":9,"1":9,"2":9,"3":2,"4":7,"5":1,"6":6,"7":6,"8":0,"9":0,"10":0,"11":6,"12":6,"13":6,"14":6,"15":6,"16":1,"17":1,"18":1,"19":1,"20":1,"21":1,"22":1,"23":1,"24":1},"f":{"0":9},"b":{"0":[9,2],"1":[9,0],"2":[2,7],"3":[9,9],"4":[1,6],"5":[6,0],"6":[0,6],"7":[6,0,0]},"_coverageSchema":"43e27e138ebf9cfc5966b082cf9a028302ed4184","hash":"c6faf098a3ae71adbc865c5284132f0b67861195","contentHash":"ca857e27e9624169077901ec50dd53e3"} +,"/Users/donald/Project/browser-image-compression/lib/utils.js": {"path":"/Users/donald/Project/browser-image-compression/lib/utils.js","statementMap":{"0":{"start":{"line":8,"column":2},"end":{"line":13,"column":4}},"1":{"start":{"line":9,"column":19},"end":{"line":9,"column":35}},"2":{"start":{"line":10,"column":4},"end":{"line":10,"column":48}},"3":{"start":{"line":10,"column":26},"end":{"line":10,"column":48}},"4":{"start":{"line":11,"column":4},"end":{"line":11,"column":37}},"5":{"start":{"line":11,"column":28},"end":{"line":11,"column":37}},"6":{"start":{"line":12,"column":4},"end":{"line":12,"column":30}},"7":{"start":{"line":25,"column":2},"end":{"line":43,"column":4}},"8":{"start":{"line":26,"column":16},"end":{"line":26,"column":34}},"9":{"start":{"line":27,"column":17},"end":{"line":27,"column":43}},"10":{"start":{"line":28,"column":17},"end":{"line":28,"column":29}},"11":{"start":{"line":29,"column":12},"end":{"line":29,"column":23}},"12":{"start":{"line":30,"column":18},"end":{"line":30,"column":35}},"13":{"start":{"line":31,"column":4},"end":{"line":33,"column":5}},"14":{"start":{"line":32,"column":6},"end":{"line":32,"column":35}},"15":{"start":{"line":35,"column":4},"end":{"line":41,"column":5}},"16":{"start":{"line":36,"column":6},"end":{"line":36,"column":56}},"17":{"start":{"line":38,"column":6},"end":{"line":38,"column":46}},"18":{"start":{"line":39,"column":6},"end":{"line":39,"column":26}},"19":{"start":{"line":40,"column":6},"end":{"line":40,"column":38}},"20":{"start":{"line":42,"column":4},"end":{"line":42,"column":17}},"21":{"start":{"line":53,"column":2},"end":{"line":58,"column":4}},"22":{"start":{"line":54,"column":16},"end":{"line":54,"column":27}},"23":{"start":{"line":55,"column":4},"end":{"line":55,"column":35}},"24":{"start":{"line":55,"column":23},"end":{"line":55,"column":35}},"25":{"start":{"line":56,"column":4},"end":{"line":56,"column":34}},"26":{"start":{"line":56,"column":25},"end":{"line":56,"column":34}},"27":{"start":{"line":57,"column":4},"end":{"line":57,"column":17}},"28":{"start":{"line":69,"column":2},"end":{"line":73,"column":3}},"29":{"start":{"line":70,"column":4},"end":{"line":70,"column":55}},"30":{"start":{"line":72,"column":4},"end":{"line":72,"column":45}},"31":{"start":{"line":74,"column":14},"end":{"line":74,"column":37}},"32":{"start":{"line":75,"column":2},"end":{"line":75,"column":55}},"33":{"start":{"line":76,"column":2},"end":{"line":76,"column":15}},"34":{"start":{"line":87,"column":2},"end":{"line":92,"column":3}},"35":{"start":{"line":88,"column":4},"end":{"line":88,"column":39}},"36":{"start":{"line":90,"column":20},"end":{"line":90,"column":50}},"37":{"start":{"line":91,"column":4},"end":{"line":91,"column":34}},"38":{"start":{"line":93,"column":17},"end":{"line":93,"column":39}},"39":{"start":{"line":94,"column":2},"end":{"line":94,"column":22}},"40":{"start":{"line":109,"column":2},"end":{"line":116,"column":3}},"41":{"start":{"line":110,"column":4},"end":{"line":110,"column":76}},"42":{"start":{"line":111,"column":4},"end":{"line":111,"column":34}},"43":{"start":{"line":112,"column":4},"end":{"line":112,"column":50}},"44":{"start":{"line":114,"column":20},"end":{"line":114,"column":55}},"45":{"start":{"line":115,"column":4},"end":{"line":115,"column":82}},"46":{"start":{"line":117,"column":2},"end":{"line":117,"column":23}},"47":{"start":{"line":129,"column":2},"end":{"line":166,"column":4}},"48":{"start":{"line":130,"column":19},"end":{"line":130,"column":35}},"49":{"start":{"line":131,"column":4},"end":{"line":163,"column":5}},"50":{"start":{"line":132,"column":19},"end":{"line":132,"column":48}},"51":{"start":{"line":133,"column":6},"end":{"line":135,"column":7}},"52":{"start":{"line":134,"column":8},"end":{"line":134,"column":26}},"53":{"start":{"line":136,"column":21},"end":{"line":136,"column":36}},"54":{"start":{"line":137,"column":19},"end":{"line":137,"column":20}},"55":{"start":{"line":138,"column":6},"end":{"line":161,"column":7}},"56":{"start":{"line":139,"column":8},"end":{"line":139,"column":70}},"57":{"start":{"line":139,"column":52},"end":{"line":139,"column":70}},"58":{"start":{"line":140,"column":23},"end":{"line":140,"column":52}},"59":{"start":{"line":141,"column":8},"end":{"line":141,"column":19}},"60":{"start":{"line":142,"column":8},"end":{"line":160,"column":9}},"61":{"start":{"line":143,"column":10},"end":{"line":145,"column":11}},"62":{"start":{"line":144,"column":12},"end":{"line":144,"column":30}},"63":{"start":{"line":147,"column":23},"end":{"line":147,"column":67}},"64":{"start":{"line":148,"column":10},"end":{"line":148,"column":54}},"65":{"start":{"line":149,"column":21},"end":{"line":149,"column":51}},"66":{"start":{"line":150,"column":10},"end":{"line":150,"column":21}},"67":{"start":{"line":151,"column":10},"end":{"line":155,"column":11}},"68":{"start":{"line":152,"column":12},"end":{"line":154,"column":13}},"69":{"start":{"line":153,"column":14},"end":{"line":153,"column":75}},"70":{"start":{"line":156,"column":15},"end":{"line":160,"column":9}},"71":{"start":{"line":157,"column":10},"end":{"line":157,"column":15}},"72":{"start":{"line":159,"column":10},"end":{"line":159,"column":49}},"73":{"start":{"line":162,"column":6},"end":{"line":162,"column":24}},"74":{"start":{"line":164,"column":4},"end":{"line":164,"column":37}},"75":{"start":{"line":164,"column":28},"end":{"line":164,"column":37}},"76":{"start":{"line":165,"column":4},"end":{"line":165,"column":34}},"77":{"start":{"line":177,"column":14},"end":{"line":177,"column":37}},"78":{"start":{"line":179,"column":27},"end":{"line":179,"column":51}},"79":{"start":{"line":180,"column":23},"end":{"line":180,"column":124}},"80":{"start":{"line":181,"column":2},"end":{"line":192,"column":3}},"81":{"start":{"line":182,"column":4},"end":{"line":188,"column":5}},"82":{"start":{"line":183,"column":6},"end":{"line":183,"column":37}},"83":{"start":{"line":184,"column":6},"end":{"line":184,"column":65}},"84":{"start":{"line":186,"column":6},"end":{"line":186,"column":64}},"85":{"start":{"line":187,"column":6},"end":{"line":187,"column":38}},"86":{"start":{"line":190,"column":4},"end":{"line":190,"column":28}},"87":{"start":{"line":191,"column":4},"end":{"line":191,"column":30}},"88":{"start":{"line":194,"column":2},"end":{"line":194,"column":55}},"89":{"start":{"line":195,"column":2},"end":{"line":195,"column":31}},"90":{"start":{"line":208,"column":14},"end":{"line":208,"column":37}},"91":{"start":{"line":210,"column":16},"end":{"line":210,"column":28}},"92":{"start":{"line":211,"column":17},"end":{"line":211,"column":30}},"93":{"start":{"line":214,"column":2},"end":{"line":220,"column":3}},"94":{"start":{"line":215,"column":4},"end":{"line":215,"column":25}},"95":{"start":{"line":216,"column":4},"end":{"line":216,"column":25}},"96":{"start":{"line":218,"column":4},"end":{"line":218,"column":24}},"97":{"start":{"line":219,"column":4},"end":{"line":219,"column":26}},"98":{"start":{"line":223,"column":2},"end":{"line":232,"column":3}},"99":{"start":{"line":224,"column":12},"end":{"line":224,"column":49}},"100":{"start":{"line":224,"column":50},"end":{"line":224,"column":56}},"101":{"start":{"line":225,"column":12},"end":{"line":225,"column":55}},"102":{"start":{"line":225,"column":56},"end":{"line":225,"column":62}},"103":{"start":{"line":226,"column":12},"end":{"line":226,"column":50}},"104":{"start":{"line":226,"column":51},"end":{"line":226,"column":57}},"105":{"start":{"line":227,"column":12},"end":{"line":227,"column":44}},"106":{"start":{"line":227,"column":45},"end":{"line":227,"column":51}},"107":{"start":{"line":228,"column":12},"end":{"line":228,"column":50}},"108":{"start":{"line":228,"column":51},"end":{"line":228,"column":57}},"109":{"start":{"line":229,"column":12},"end":{"line":229,"column":55}},"110":{"start":{"line":229,"column":56},"end":{"line":229,"column":62}},"111":{"start":{"line":230,"column":12},"end":{"line":230,"column":49}},"112":{"start":{"line":230,"column":50},"end":{"line":230,"column":56}},"113":{"start":{"line":231,"column":13},"end":{"line":231,"column":19}},"114":{"start":{"line":234,"column":2},"end":{"line":234,"column":55}},"115":{"start":{"line":236,"column":2},"end":{"line":236,"column":15}}},"fnMap":{"0":{"name":"getDataUrlFromFile","decl":{"start":{"line":7,"column":16},"end":{"line":7,"column":34}},"loc":{"start":{"line":7,"column":42},"end":{"line":14,"column":1}},"line":7},"1":{"name":"(anonymous_1)","decl":{"start":{"line":8,"column":21},"end":{"line":8,"column":22}},"loc":{"start":{"line":8,"column":42},"end":{"line":13,"column":3}},"line":8},"2":{"name":"(anonymous_2)","decl":{"start":{"line":10,"column":20},"end":{"line":10,"column":21}},"loc":{"start":{"line":10,"column":26},"end":{"line":10,"column":48}},"line":10},"3":{"name":"(anonymous_3)","decl":{"start":{"line":11,"column":21},"end":{"line":11,"column":22}},"loc":{"start":{"line":11,"column":28},"end":{"line":11,"column":37}},"line":11},"4":{"name":"getFilefromDataUrl","decl":{"start":{"line":24,"column":16},"end":{"line":24,"column":34}},"loc":{"start":{"line":24,"column":82},"end":{"line":44,"column":1}},"line":24},"5":{"name":"(anonymous_5)","decl":{"start":{"line":25,"column":21},"end":{"line":25,"column":22}},"loc":{"start":{"line":25,"column":34},"end":{"line":43,"column":3}},"line":25},"6":{"name":"loadImage","decl":{"start":{"line":52,"column":16},"end":{"line":52,"column":25}},"loc":{"start":{"line":52,"column":32},"end":{"line":59,"column":1}},"line":52},"7":{"name":"(anonymous_7)","decl":{"start":{"line":53,"column":21},"end":{"line":53,"column":22}},"loc":{"start":{"line":53,"column":42},"end":{"line":58,"column":3}},"line":53},"8":{"name":"(anonymous_8)","decl":{"start":{"line":55,"column":17},"end":{"line":55,"column":18}},"loc":{"start":{"line":55,"column":23},"end":{"line":55,"column":35}},"line":55},"9":{"name":"(anonymous_9)","decl":{"start":{"line":56,"column":18},"end":{"line":56,"column":19}},"loc":{"start":{"line":56,"column":25},"end":{"line":56,"column":34}},"line":56},"10":{"name":"drawImageInCanvas","decl":{"start":{"line":67,"column":16},"end":{"line":67,"column":33}},"loc":{"start":{"line":67,"column":40},"end":{"line":77,"column":1}},"line":67},"11":{"name":"drawFileInCanvas","decl":{"start":{"line":85,"column":22},"end":{"line":85,"column":38}},"loc":{"start":{"line":85,"column":46},"end":{"line":95,"column":1}},"line":85},"12":{"name":"canvasToFile","decl":{"start":{"line":107,"column":22},"end":{"line":107,"column":34}},"loc":{"start":{"line":107,"column":95},"end":{"line":118,"column":1}},"line":107},"13":{"name":"getExifOrientation","decl":{"start":{"line":128,"column":16},"end":{"line":128,"column":34}},"loc":{"start":{"line":128,"column":42},"end":{"line":167,"column":1}},"line":128},"14":{"name":"(anonymous_14)","decl":{"start":{"line":129,"column":21},"end":{"line":129,"column":22}},"loc":{"start":{"line":129,"column":42},"end":{"line":166,"column":3}},"line":129},"15":{"name":"(anonymous_15)","decl":{"start":{"line":131,"column":20},"end":{"line":131,"column":21}},"loc":{"start":{"line":131,"column":27},"end":{"line":163,"column":5}},"line":131},"16":{"name":"(anonymous_16)","decl":{"start":{"line":164,"column":21},"end":{"line":164,"column":22}},"loc":{"start":{"line":164,"column":28},"end":{"line":164,"column":37}},"line":164},"17":{"name":"handleMaxWidthOrHeight","decl":{"start":{"line":176,"column":16},"end":{"line":176,"column":38}},"loc":{"start":{"line":176,"column":62},"end":{"line":196,"column":1}},"line":176},"18":{"name":"followExifOrientation","decl":{"start":{"line":207,"column":16},"end":{"line":207,"column":37}},"loc":{"start":{"line":207,"column":69},"end":{"line":237,"column":1}},"line":207}},"branchMap":{"0":{"loc":{"start":{"line":24,"column":55},"end":{"line":24,"column":80}},"type":"default-arg","locations":[{"start":{"line":24,"column":70},"end":{"line":24,"column":80}}],"line":24},"1":{"loc":{"start":{"line":69,"column":2},"end":{"line":73,"column":3}},"type":"if","locations":[{"start":{"line":69,"column":2},"end":{"line":73,"column":3}},{"start":{"line":69,"column":2},"end":{"line":73,"column":3}}],"line":69},"2":{"loc":{"start":{"line":107,"column":82},"end":{"line":107,"column":93}},"type":"default-arg","locations":[{"start":{"line":107,"column":92},"end":{"line":107,"column":93}}],"line":107},"3":{"loc":{"start":{"line":109,"column":2},"end":{"line":116,"column":3}},"type":"if","locations":[{"start":{"line":109,"column":2},"end":{"line":116,"column":3}},{"start":{"line":109,"column":2},"end":{"line":116,"column":3}}],"line":109},"4":{"loc":{"start":{"line":109,"column":6},"end":{"line":109,"column":80}},"type":"binary-expr","locations":[{"start":{"line":109,"column":6},"end":{"line":109,"column":43}},{"start":{"line":109,"column":47},"end":{"line":109,"column":80}}],"line":109},"5":{"loc":{"start":{"line":133,"column":6},"end":{"line":135,"column":7}},"type":"if","locations":[{"start":{"line":133,"column":6},"end":{"line":135,"column":7}},{"start":{"line":133,"column":6},"end":{"line":135,"column":7}}],"line":133},"6":{"loc":{"start":{"line":139,"column":8},"end":{"line":139,"column":70}},"type":"if","locations":[{"start":{"line":139,"column":8},"end":{"line":139,"column":70}},{"start":{"line":139,"column":8},"end":{"line":139,"column":70}}],"line":139},"7":{"loc":{"start":{"line":142,"column":8},"end":{"line":160,"column":9}},"type":"if","locations":[{"start":{"line":142,"column":8},"end":{"line":160,"column":9}},{"start":{"line":142,"column":8},"end":{"line":160,"column":9}}],"line":142},"8":{"loc":{"start":{"line":143,"column":10},"end":{"line":145,"column":11}},"type":"if","locations":[{"start":{"line":143,"column":10},"end":{"line":145,"column":11}},{"start":{"line":143,"column":10},"end":{"line":145,"column":11}}],"line":143},"9":{"loc":{"start":{"line":152,"column":12},"end":{"line":154,"column":13}},"type":"if","locations":[{"start":{"line":152,"column":12},"end":{"line":154,"column":13}},{"start":{"line":152,"column":12},"end":{"line":154,"column":13}}],"line":152},"10":{"loc":{"start":{"line":156,"column":15},"end":{"line":160,"column":9}},"type":"if","locations":[{"start":{"line":156,"column":15},"end":{"line":160,"column":9}},{"start":{"line":156,"column":15},"end":{"line":160,"column":9}}],"line":156},"11":{"loc":{"start":{"line":180,"column":23},"end":{"line":180,"column":124}},"type":"binary-expr","locations":[{"start":{"line":180,"column":23},"end":{"line":180,"column":57}},{"start":{"line":180,"column":62},"end":{"line":180,"column":90}},{"start":{"line":180,"column":94},"end":{"line":180,"column":123}}],"line":180},"12":{"loc":{"start":{"line":181,"column":2},"end":{"line":192,"column":3}},"type":"if","locations":[{"start":{"line":181,"column":2},"end":{"line":192,"column":3}},{"start":{"line":181,"column":2},"end":{"line":192,"column":3}}],"line":181},"13":{"loc":{"start":{"line":182,"column":4},"end":{"line":188,"column":5}},"type":"if","locations":[{"start":{"line":182,"column":4},"end":{"line":188,"column":5}},{"start":{"line":182,"column":4},"end":{"line":188,"column":5}}],"line":182},"14":{"loc":{"start":{"line":214,"column":2},"end":{"line":220,"column":3}},"type":"if","locations":[{"start":{"line":214,"column":2},"end":{"line":220,"column":3}},{"start":{"line":214,"column":2},"end":{"line":220,"column":3}}],"line":214},"15":{"loc":{"start":{"line":214,"column":6},"end":{"line":214,"column":48}},"type":"binary-expr","locations":[{"start":{"line":214,"column":6},"end":{"line":214,"column":25}},{"start":{"line":214,"column":29},"end":{"line":214,"column":48}}],"line":214},"16":{"loc":{"start":{"line":223,"column":2},"end":{"line":232,"column":3}},"type":"switch","locations":[{"start":{"line":224,"column":4},"end":{"line":224,"column":56}},{"start":{"line":225,"column":4},"end":{"line":225,"column":62}},{"start":{"line":226,"column":4},"end":{"line":226,"column":57}},{"start":{"line":227,"column":4},"end":{"line":227,"column":51}},{"start":{"line":228,"column":4},"end":{"line":228,"column":57}},{"start":{"line":229,"column":4},"end":{"line":229,"column":62}},{"start":{"line":230,"column":4},"end":{"line":230,"column":56}},{"start":{"line":231,"column":4},"end":{"line":231,"column":19}}],"line":223}},"s":{"0":12,"1":12,"2":12,"3":12,"4":12,"5":0,"6":12,"7":12,"8":12,"9":12,"10":12,"11":12,"12":12,"13":12,"14":12578552,"15":12,"16":12,"17":11,"18":11,"19":11,"20":12,"21":13,"22":13,"23":13,"24":13,"25":13,"26":0,"27":13,"28":12,"29":0,"30":12,"31":12,"32":12,"33":12,"34":11,"35":11,"36":11,"37":11,"38":11,"39":11,"40":11,"41":0,"42":0,"43":0,"44":11,"45":11,"46":11,"47":1,"48":1,"49":1,"50":1,"51":1,"52":1,"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":4,"82":2,"83":2,"84":2,"85":2,"86":2,"87":2,"88":6,"89":6,"90":6,"91":6,"92":6,"93":6,"94":0,"95":0,"96":6,"97":6,"98":6,"99":0,"100":0,"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":6,"114":6,"115":6},"f":{"0":12,"1":12,"2":12,"3":0,"4":12,"5":12,"6":13,"7":13,"8":13,"9":0,"10":12,"11":11,"12":11,"13":1,"14":1,"15":1,"16":0,"17":6,"18":6},"b":{"0":[12],"1":[0,12],"2":[0],"3":[0,11],"4":[11,0],"5":[1,0],"6":[0,0],"7":[0,0],"8":[0,0],"9":[0,0],"10":[0,0],"11":[6,4,0],"12":[4,2],"13":[2,2],"14":[0,6],"15":[6,0],"16":[0,0,0,0,0,0,0,6]},"_coverageSchema":"43e27e138ebf9cfc5966b082cf9a028302ed4184","hash":"1603c36e0b57721a330f54f37b51d78d490110bf","contentHash":"6539cdc1ebd20865e5cf1a3ecd0e79a0"} } diff --git a/coverage/coverage-summary.json b/coverage/coverage-summary.json index 4675f35..b9c8487 100644 --- a/coverage/coverage-summary.json +++ b/coverage/coverage-summary.json @@ -1,5 +1,5 @@ -{"total": {"lines":{"total":159,"covered":110,"skipped":0,"pct":69.18},"statements":{"total":172,"covered":112,"skipped":0,"pct":65.12},"functions":{"total":21,"covered":18,"skipped":0,"pct":85.71},"branches":{"total":74,"covered":34,"skipped":0,"pct":45.95}} +{"total": {"lines":{"total":159,"covered":115,"skipped":0,"pct":72.33},"statements":{"total":172,"covered":117,"skipped":0,"pct":68.02},"functions":{"total":21,"covered":18,"skipped":0,"pct":85.71},"branches":{"total":74,"covered":39,"skipped":0,"pct":52.7}} ,"/Users/donald/Project/browser-image-compression/lib/image-compression.js": {"lines":{"total":31,"covered":23,"skipped":0,"pct":74.19},"functions":{"total":1,"covered":1,"skipped":0,"pct":100},"statements":{"total":31,"covered":23,"skipped":0,"pct":74.19},"branches":{"total":18,"covered":13,"skipped":0,"pct":72.22}} -,"/Users/donald/Project/browser-image-compression/lib/index.js": {"lines":{"total":25,"covered":22,"skipped":0,"pct":88},"functions":{"total":1,"covered":1,"skipped":0,"pct":100},"statements":{"total":25,"covered":22,"skipped":0,"pct":88},"branches":{"total":17,"covered":11,"skipped":0,"pct":64.71}} -,"/Users/donald/Project/browser-image-compression/lib/utils.js": {"lines":{"total":103,"covered":65,"skipped":0,"pct":63.11},"functions":{"total":19,"covered":16,"skipped":0,"pct":84.21},"statements":{"total":116,"covered":67,"skipped":0,"pct":57.76},"branches":{"total":39,"covered":10,"skipped":0,"pct":25.64}} +,"/Users/donald/Project/browser-image-compression/lib/index.js": {"lines":{"total":25,"covered":22,"skipped":0,"pct":88},"functions":{"total":1,"covered":1,"skipped":0,"pct":100},"statements":{"total":25,"covered":22,"skipped":0,"pct":88},"branches":{"total":17,"covered":12,"skipped":0,"pct":70.59}} +,"/Users/donald/Project/browser-image-compression/lib/utils.js": {"lines":{"total":103,"covered":70,"skipped":0,"pct":67.96},"functions":{"total":19,"covered":16,"skipped":0,"pct":84.21},"statements":{"total":116,"covered":72,"skipped":0,"pct":62.07},"branches":{"total":39,"covered":14,"skipped":0,"pct":35.9}} } diff --git a/coverage/image-compression.js.html b/coverage/image-compression.js.html index a12e116..f6e76f5 100644 --- a/coverage/image-compression.js.html +++ b/coverage/image-compression.js.html @@ -129,39 +129,39 @@

      -2x +6x   -2x +6x     -2x +6x             -2x -2x -2x +6x +6x +6x     -2x -2x -2x +6x +6x +6x   -2x +6x         -2x -2x +6x +6x   -2x -2x -2x -1x +6x +6x +6x +3x 5x 5x   @@ -171,7 +171,7 @@

5x     -1x +3x       @@ -183,7 +183,7 @@

      -2x +6x  
import { canvasToFile, drawFileInCanvas, followExifOrientation, getExifOrientation, handleMaxWidthOrHeight } from './utils'
  
 /**
@@ -259,7 +259,7 @@ 

diff --git a/coverage/index.html b/coverage/index.html index 815cbb6..bfc2217 100644 --- a/coverage/index.html +++ b/coverage/index.html @@ -20,14 +20,14 @@

- 65.12% + 68.02% Statements - 112/172 + 117/172
- 45.95% + 52.7% Branches - 34/74 + 39/74
85.71% @@ -35,9 +35,9 @@

18/21

- 69.18% + 72.33% Lines - 110/159 + 115/159

@@ -79,8 +79,8 @@

88% 22/25 - 64.71% - 11/17 + 70.59% + 12/17 100% 1/1 88% @@ -89,15 +89,15 @@

utils.js -
- 57.76% - 67/116 - 25.64% - 10/39 +
+ 62.07% + 72/116 + 35.9% + 14/39 84.21% 16/19 - 63.11% - 65/103 + 67.96% + 70/103 @@ -106,7 +106,7 @@

diff --git a/coverage/index.js.html b/coverage/index.js.html index 60862b8..db94942 100644 --- a/coverage/index.js.html +++ b/coverage/index.js.html @@ -25,9 +25,9 @@

22/25
- 64.71% + 70.59% Branches - 11/17 + 12/17
100% @@ -155,17 +155,17 @@

      -5x -5x +9x +9x   -5x +9x 2x -3x +7x 1x       -2x +6x       @@ -173,7 +173,7 @@

      -2x +6x       @@ -182,15 +182,15 @@

      -2x +6x     -2x -2x -2x +6x +6x +6x     -2x +6x     1x @@ -234,7 +234,7 @@

  let compressedFile   - options.maxSizeMB = options.maxSizeMB || Number.POSITIVE_INFINITY + options.maxSizeMB = options.maxSizeMB || Number.POSITIVE_INFINITY options.useWebWorker = typeof options.useWebWorker === 'boolean' ? options.useWebWorker : true   if (!(file instanceof Blob || file instanceof File)) { @@ -289,7 +289,7 @@

diff --git a/coverage/lcov-report/image-compression.js.html b/coverage/lcov-report/image-compression.js.html index a12e116..a16aca9 100644 --- a/coverage/lcov-report/image-compression.js.html +++ b/coverage/lcov-report/image-compression.js.html @@ -129,39 +129,39 @@

      -2x +6x   -2x +6x     -2x +6x             -2x -2x -2x +6x +6x +6x     -2x -2x -2x +6x +6x +6x   -2x +6x         -2x -2x +6x +6x   -2x -2x -2x -1x +6x +6x +6x +3x 5x 5x   @@ -171,7 +171,7 @@

5x     -1x +3x       @@ -183,7 +183,7 @@

      -2x +6x  
import { canvasToFile, drawFileInCanvas, followExifOrientation, getExifOrientation, handleMaxWidthOrHeight } from './utils'
  
 /**
@@ -259,7 +259,7 @@ 

diff --git a/coverage/lcov-report/index.html b/coverage/lcov-report/index.html index 815cbb6..9f8936c 100644 --- a/coverage/lcov-report/index.html +++ b/coverage/lcov-report/index.html @@ -20,14 +20,14 @@

- 65.12% + 68.02% Statements - 112/172 + 117/172
- 45.95% + 52.7% Branches - 34/74 + 39/74
85.71% @@ -35,9 +35,9 @@

18/21

- 69.18% + 72.33% Lines - 110/159 + 115/159

@@ -79,8 +79,8 @@

88% 22/25 - 64.71% - 11/17 + 70.59% + 12/17 100% 1/1 88% @@ -89,15 +89,15 @@

utils.js -
- 57.76% - 67/116 - 25.64% - 10/39 +
+ 62.07% + 72/116 + 35.9% + 14/39 84.21% 16/19 - 63.11% - 65/103 + 67.96% + 70/103 @@ -106,7 +106,7 @@

diff --git a/coverage/lcov-report/index.js.html b/coverage/lcov-report/index.js.html index 60862b8..a9838ec 100644 --- a/coverage/lcov-report/index.js.html +++ b/coverage/lcov-report/index.js.html @@ -25,9 +25,9 @@

22/25
- 64.71% + 70.59% Branches - 11/17 + 12/17
100% @@ -155,17 +155,17 @@

      -5x -5x +9x +9x   -5x +9x 2x -3x +7x 1x       -2x +6x       @@ -173,7 +173,7 @@

      -2x +6x       @@ -182,15 +182,15 @@

      -2x +6x     -2x -2x -2x +6x +6x +6x     -2x +6x     1x @@ -234,7 +234,7 @@

  let compressedFile   - options.maxSizeMB = options.maxSizeMB || Number.POSITIVE_INFINITY + options.maxSizeMB = options.maxSizeMB || Number.POSITIVE_INFINITY options.useWebWorker = typeof options.useWebWorker === 'boolean' ? options.useWebWorker : true   if (!(file instanceof Blob || file instanceof File)) { @@ -289,7 +289,7 @@

diff --git a/coverage/lcov-report/utils.js.html b/coverage/lcov-report/utils.js.html index 79ac7b6..699f2c8 100644 --- a/coverage/lcov-report/utils.js.html +++ b/coverage/lcov-report/utils.js.html @@ -20,14 +20,14 @@

- 57.76% + 62.07% Statements - 67/116 + 72/116
- 25.64% + 35.9% Branches - 10/39 + 14/39
84.21% @@ -35,9 +35,9 @@

16/19

- 63.11% + 67.96% Lines - 65/103 + 70/103

@@ -289,11 +289,11 @@

      -4x -4x -4x -4x -4x +12x +12x +12x +12x +12x       @@ -306,24 +306,24 @@

      -8x -8x -8x -8x -8x -8x -8x -11825174x +12x +12x +12x +12x +12x +12x +12x +12578552x     -8x -8x +12x +12x   -7x -7x -7x +11x +11x +11x   -8x +12x       @@ -334,11 +334,11 @@

      -5x -5x -5x -5x -5x +13x +13x +13x +13x +13x       @@ -350,14 +350,14 @@

      -4x +12x     -4x +12x   -4x -4x -4x +12x +12x +12x       @@ -368,14 +368,14 @@

      -3x -3x +11x +11x   -3x -3x +11x +11x   -3x -3x +11x +11x       @@ -390,15 +390,15 @@

      -7x +11x         -7x -7x +11x +11x   -7x +11x       @@ -458,18 +458,14 @@

      -2x +6x   +6x +6x +6x +4x 2x 2x -2x -  -  -  -  -  -  -    2x 2x @@ -479,6 +475,8 @@

2x     +6x +6x       @@ -489,22 +487,24 @@

      -2x   -2x -2x   +6x   -2x +6x +6x +  +  +6x       -2x -2x +6x +6x       -2x +6x       @@ -512,12 +512,12 @@

      -2x +6x     -2x +6x   -2x +6x  
/**
  * getDataUrlFromFile
  *
@@ -697,14 +697,14 @@ 

const ctx = canvas.getContext('2d')   const maxWidthOrHeight = options.maxWidthOrHeight - const needToHandle = Number.isInteger(maxWidthOrHeight) && (img.width > maxWidthOrHeight || img.height > maxWidthOrHeight) - Iif (needToHandle) { - if (img.width > img.height) { - canvas.width = maxWidthOrHeight - canvas.height = (img.height / img.width) * maxWidthOrHeight + const needToHandle = Number.isInteger(maxWidthOrHeight) && (img.width > maxWidthOrHeight || img.height > maxWidthOrHeight) + if (needToHandle) { + if (img.width > img.height) { + canvas.width = maxWidthOrHeight + canvas.height = (img.height / img.width) * maxWidthOrHeight } else { - canvas.width = (img.width / img.height) * maxWidthOrHeight - canvas.height = maxWidthOrHeight + canvas.width = (img.width / img.height) * maxWidthOrHeight + canvas.height = maxWidthOrHeight } } else { canvas.width = img.width @@ -727,8 +727,8 @@

export function followExifOrientation (img, canvas, exifOrientation) { const ctx = canvas.getContext('2d')   - const width = img.width - const height = img.height + const width = canvas.width + const height = canvas.height   // set proper canvas dimensions before transform & export Iif (4 < exifOrientation && exifOrientation < 9) { @@ -751,7 +751,7 @@

default: break; }   - ctx.drawImage(img, 0, 0) + ctx.drawImage(img, 0, 0, canvas.width, canvas.height)   return canvas }

@@ -760,7 +760,7 @@

diff --git a/coverage/lcov.info b/coverage/lcov.info index 92ba717..1027522 100644 --- a/coverage/lcov.info +++ b/coverage/lcov.info @@ -3,58 +3,58 @@ SF:/Users/donald/Project/browser-image-compression/lib/image-compression.js FN:14,compress FNF:1 FNH:1 -FNDA:2,compress -DA:15,2 -DA:17,2 -DA:20,2 +FNDA:6,compress +DA:15,6 +DA:17,6 +DA:20,6 DA:21,0 -DA:27,2 -DA:28,2 -DA:29,2 -DA:32,2 -DA:33,2 -DA:34,2 -DA:36,2 +DA:27,6 +DA:28,6 +DA:29,6 +DA:32,6 +DA:33,6 +DA:34,6 +DA:36,6 DA:37,0 -DA:41,2 -DA:42,2 -DA:44,2 -DA:45,2 -DA:46,2 -DA:47,1 +DA:41,6 +DA:42,6 +DA:44,6 +DA:45,6 +DA:46,6 +DA:47,3 DA:48,5 DA:49,5 DA:51,5 DA:52,5 DA:54,5 -DA:57,1 +DA:57,3 DA:58,0 DA:59,0 DA:61,0 DA:62,0 DA:64,0 DA:65,0 -DA:69,2 +DA:69,6 LF:31 LH:23 -BRDA:15,0,0,2 -BRDA:15,0,1,2 +BRDA:15,0,0,6 +BRDA:15,0,1,6 BRDA:20,1,0,0 -BRDA:20,1,1,2 -BRDA:20,2,0,2 +BRDA:20,1,1,6 +BRDA:20,2,0,6 BRDA:20,2,1,0 BRDA:36,3,0,0 -BRDA:36,3,1,2 -BRDA:36,4,0,2 +BRDA:36,3,1,6 +BRDA:36,4,0,6 BRDA:36,4,1,0 -BRDA:41,5,0,2 +BRDA:41,5,0,6 BRDA:41,5,1,0 -BRDA:46,6,0,1 -BRDA:46,6,1,1 -BRDA:47,7,0,6 -BRDA:47,7,1,6 -BRDA:57,8,0,1 -BRDA:57,8,1,1 +BRDA:46,6,0,3 +BRDA:46,6,1,3 +BRDA:47,7,0,8 +BRDA:47,7,1,8 +BRDA:57,8,0,3 +BRDA:57,8,1,3 BRF:18 BRH:13 end_of_record @@ -63,23 +63,23 @@ SF:/Users/donald/Project/browser-image-compression/lib/index.js FN:27,imageCompression FNF:1 FNH:1 -FNDA:5,imageCompression -DA:31,5 -DA:32,5 -DA:34,5 +FNDA:9,imageCompression +DA:31,9 +DA:32,9 +DA:34,9 DA:35,2 -DA:36,3 +DA:36,7 DA:37,1 -DA:41,2 -DA:49,2 +DA:41,6 +DA:49,6 DA:50,0 DA:52,0 DA:55,0 -DA:58,2 -DA:61,2 -DA:62,2 -DA:63,2 -DA:66,2 +DA:58,6 +DA:61,6 +DA:62,6 +DA:63,6 +DA:66,6 DA:69,1 DA:70,1 DA:71,1 @@ -91,25 +91,25 @@ DA:76,1 DA:77,1 LF:25 LH:22 -BRDA:31,0,0,5 -BRDA:31,0,1,0 -BRDA:32,1,0,5 +BRDA:31,0,0,9 +BRDA:31,0,1,2 +BRDA:32,1,0,9 BRDA:32,1,1,0 BRDA:34,2,0,2 -BRDA:34,2,1,3 -BRDA:34,3,0,5 -BRDA:34,3,1,5 +BRDA:34,2,1,7 +BRDA:34,3,0,9 +BRDA:34,3,1,9 BRDA:36,4,0,1 -BRDA:36,4,1,2 -BRDA:41,5,0,2 +BRDA:36,4,1,6 +BRDA:41,5,0,6 BRDA:41,5,1,0 BRDA:49,6,0,0 -BRDA:49,6,1,2 -BRDA:49,7,0,2 +BRDA:49,6,1,6 +BRDA:49,7,0,6 BRDA:49,7,1,0 BRDA:49,7,2,0 BRF:17 -BRH:11 +BRH:12 end_of_record TN: SF:/Users/donald/Project/browser-image-compression/lib/utils.js @@ -134,68 +134,68 @@ FN:176,handleMaxWidthOrHeight FN:207,followExifOrientation FNF:19 FNH:16 -FNDA:4,getDataUrlFromFile -FNDA:4,(anonymous_1) -FNDA:4,(anonymous_2) +FNDA:12,getDataUrlFromFile +FNDA:12,(anonymous_1) +FNDA:12,(anonymous_2) FNDA:0,(anonymous_3) -FNDA:8,getFilefromDataUrl -FNDA:8,(anonymous_5) -FNDA:5,loadImage -FNDA:5,(anonymous_7) -FNDA:5,(anonymous_8) +FNDA:12,getFilefromDataUrl +FNDA:12,(anonymous_5) +FNDA:13,loadImage +FNDA:13,(anonymous_7) +FNDA:13,(anonymous_8) FNDA:0,(anonymous_9) -FNDA:4,drawImageInCanvas -FNDA:3,drawFileInCanvas -FNDA:7,canvasToFile +FNDA:12,drawImageInCanvas +FNDA:11,drawFileInCanvas +FNDA:11,canvasToFile FNDA:1,getExifOrientation FNDA:1,(anonymous_14) FNDA:1,(anonymous_15) FNDA:0,(anonymous_16) -FNDA:2,handleMaxWidthOrHeight -FNDA:2,followExifOrientation -DA:8,4 -DA:9,4 -DA:10,4 -DA:11,4 -DA:12,4 -DA:25,8 -DA:26,8 -DA:27,8 -DA:28,8 -DA:29,8 -DA:30,8 -DA:31,8 -DA:32,11825174 -DA:35,8 -DA:36,8 -DA:38,7 -DA:39,7 -DA:40,7 -DA:42,8 -DA:53,5 -DA:54,5 -DA:55,5 -DA:56,5 -DA:57,5 -DA:69,4 +FNDA:6,handleMaxWidthOrHeight +FNDA:6,followExifOrientation +DA:8,12 +DA:9,12 +DA:10,12 +DA:11,12 +DA:12,12 +DA:25,12 +DA:26,12 +DA:27,12 +DA:28,12 +DA:29,12 +DA:30,12 +DA:31,12 +DA:32,12578552 +DA:35,12 +DA:36,12 +DA:38,11 +DA:39,11 +DA:40,11 +DA:42,12 +DA:53,13 +DA:54,13 +DA:55,13 +DA:56,13 +DA:57,13 +DA:69,12 DA:70,0 -DA:72,4 -DA:74,4 -DA:75,4 -DA:76,4 -DA:87,3 -DA:88,3 -DA:90,3 -DA:91,3 -DA:93,3 -DA:94,3 -DA:109,7 +DA:72,12 +DA:74,12 +DA:75,12 +DA:76,12 +DA:87,11 +DA:88,11 +DA:90,11 +DA:91,11 +DA:93,11 +DA:94,11 +DA:109,11 DA:110,0 DA:111,0 DA:112,0 -DA:114,7 -DA:115,7 -DA:117,7 +DA:114,11 +DA:115,11 +DA:117,11 DA:129,1 DA:130,1 DA:131,1 @@ -224,28 +224,28 @@ DA:159,0 DA:162,0 DA:164,1 DA:165,1 -DA:177,2 -DA:179,2 -DA:180,2 -DA:181,2 -DA:182,0 -DA:183,0 -DA:184,0 -DA:186,0 -DA:187,0 +DA:177,6 +DA:179,6 +DA:180,6 +DA:181,6 +DA:182,4 +DA:183,2 +DA:184,2 +DA:186,2 +DA:187,2 DA:190,2 DA:191,2 -DA:194,2 -DA:195,2 -DA:208,2 -DA:210,2 -DA:211,2 -DA:214,2 +DA:194,6 +DA:195,6 +DA:208,6 +DA:210,6 +DA:211,6 +DA:214,6 DA:215,0 DA:216,0 -DA:218,2 -DA:219,2 -DA:223,2 +DA:218,6 +DA:219,6 +DA:223,6 DA:224,0 DA:225,0 DA:226,0 @@ -253,18 +253,18 @@ DA:227,0 DA:228,0 DA:229,0 DA:230,0 -DA:231,2 -DA:234,2 -DA:236,2 +DA:231,6 +DA:234,6 +DA:236,6 LF:103 -LH:65 -BRDA:24,0,0,8 +LH:70 +BRDA:24,0,0,12 BRDA:69,1,0,0 -BRDA:69,1,1,4 +BRDA:69,1,1,12 BRDA:107,2,0,0 BRDA:109,3,0,0 -BRDA:109,3,1,7 -BRDA:109,4,0,7 +BRDA:109,3,1,11 +BRDA:109,4,0,11 BRDA:109,4,1,0 BRDA:133,5,0,1 BRDA:133,5,1,0 @@ -278,16 +278,16 @@ BRDA:152,9,0,0 BRDA:152,9,1,0 BRDA:156,10,0,0 BRDA:156,10,1,0 -BRDA:180,11,0,2 -BRDA:180,11,1,0 +BRDA:180,11,0,6 +BRDA:180,11,1,4 BRDA:180,11,2,0 -BRDA:181,12,0,0 +BRDA:181,12,0,4 BRDA:181,12,1,2 -BRDA:182,13,0,0 -BRDA:182,13,1,0 +BRDA:182,13,0,2 +BRDA:182,13,1,2 BRDA:214,14,0,0 -BRDA:214,14,1,2 -BRDA:214,15,0,2 +BRDA:214,14,1,6 +BRDA:214,15,0,6 BRDA:214,15,1,0 BRDA:223,16,0,0 BRDA:223,16,1,0 @@ -296,7 +296,7 @@ BRDA:223,16,3,0 BRDA:223,16,4,0 BRDA:223,16,5,0 BRDA:223,16,6,0 -BRDA:223,16,7,2 +BRDA:223,16,7,6 BRF:39 -BRH:10 +BRH:14 end_of_record diff --git a/coverage/utils.js.html b/coverage/utils.js.html index 79ac7b6..5263980 100644 --- a/coverage/utils.js.html +++ b/coverage/utils.js.html @@ -20,14 +20,14 @@

- 57.76% + 62.07% Statements - 67/116 + 72/116
- 25.64% + 35.9% Branches - 10/39 + 14/39
84.21% @@ -35,9 +35,9 @@

16/19

- 63.11% + 67.96% Lines - 65/103 + 70/103

@@ -289,11 +289,11 @@

      -4x -4x -4x -4x -4x +12x +12x +12x +12x +12x       @@ -306,24 +306,24 @@

      -8x -8x -8x -8x -8x -8x -8x -11825174x +12x +12x +12x +12x +12x +12x +12x +12578552x     -8x -8x +12x +12x   -7x -7x -7x +11x +11x +11x   -8x +12x       @@ -334,11 +334,11 @@

      -5x -5x -5x -5x -5x +13x +13x +13x +13x +13x       @@ -350,14 +350,14 @@

      -4x +12x     -4x +12x   -4x -4x -4x +12x +12x +12x       @@ -368,14 +368,14 @@

      -3x -3x +11x +11x   -3x -3x +11x +11x   -3x -3x +11x +11x       @@ -390,15 +390,15 @@

      -7x +11x         -7x -7x +11x +11x   -7x +11x       @@ -458,18 +458,14 @@

      -2x +6x   +6x +6x +6x +4x 2x 2x -2x -  -  -  -  -  -  -    2x 2x @@ -479,6 +475,8 @@

2x     +6x +6x       @@ -489,22 +487,24 @@

      -2x   -2x -2x   +6x   -2x +6x +6x +  +  +6x       -2x -2x +6x +6x       -2x +6x       @@ -512,12 +512,12 @@

      -2x +6x     -2x +6x   -2x +6x  
/**
  * getDataUrlFromFile
  *
@@ -697,14 +697,14 @@ 

const ctx = canvas.getContext('2d')   const maxWidthOrHeight = options.maxWidthOrHeight - const needToHandle = Number.isInteger(maxWidthOrHeight) && (img.width > maxWidthOrHeight || img.height > maxWidthOrHeight) - Iif (needToHandle) { - if (img.width > img.height) { - canvas.width = maxWidthOrHeight - canvas.height = (img.height / img.width) * maxWidthOrHeight + const needToHandle = Number.isInteger(maxWidthOrHeight) && (img.width > maxWidthOrHeight || img.height > maxWidthOrHeight) + if (needToHandle) { + if (img.width > img.height) { + canvas.width = maxWidthOrHeight + canvas.height = (img.height / img.width) * maxWidthOrHeight } else { - canvas.width = (img.width / img.height) * maxWidthOrHeight - canvas.height = maxWidthOrHeight + canvas.width = (img.width / img.height) * maxWidthOrHeight + canvas.height = maxWidthOrHeight } } else { canvas.width = img.width @@ -727,8 +727,8 @@

export function followExifOrientation (img, canvas, exifOrientation) { const ctx = canvas.getContext('2d')   - const width = img.width - const height = img.height + const width = canvas.width + const height = canvas.height   // set proper canvas dimensions before transform & export Iif (4 < exifOrientation && exifOrientation < 9) { @@ -751,7 +751,7 @@

default: break; }   - ctx.drawImage(img, 0, 0) + ctx.drawImage(img, 0, 0, canvas.width, canvas.height)   return canvas }

@@ -760,7 +760,7 @@

diff --git a/dist/browser-image-compression.js b/dist/browser-image-compression.js index a3fdc56..02d0396 100644 --- a/dist/browser-image-compression.js +++ b/dist/browser-image-compression.js @@ -1,2 +1,2 @@ -!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 getDataUrlFromFile(e){return new Promise(function(n,t){var r=new FileReader;r.onload=function(){return n(r.result)},r.onerror=function(e){return t(e)},r.readAsDataURL(e)})}function getFilefromDataUrl(e,n){var t=arguments.length>2&&void 0!==arguments[2]?arguments[2]:Date.now();return new Promise(function(r){for(var i,a=e.split(","),o=a[0].match(/:(.*?);/)[1],s=atob(a[1]),c=s.length,f=new Uint8Array(c);c--;)f[c]=s.charCodeAt(c);try{i=new File([f],n,{type:o})}catch(e){(i=new Blob([f],{type:o})).name=n,i.lastModified=t}r(i)})}function loadImage(e){return new Promise(function(n,t){var r=new Image;r.onload=function(){return n(r)},r.onerror=function(e){return t(e)},r.src=e})}function drawImageInCanvas(e){var n;return(n="function"==typeof OffscreenCanvas?new OffscreenCanvas(e.width,e.height):document.createElement("canvas")).getContext("2d").drawImage(e,0,0,n.width,n.height),n}function drawFileInCanvas(e){return new Promise(function(n,t){var r,i,a=function $Try_2_Post(){try{return i=drawImageInCanvas(r),n([r,i])}catch(e){return t(e)}},o=function $Try_2_Catch(n){try{return getDataUrlFromFile(e).then(function(e){try{return loadImage(e).then(function(e){try{return r=e,a()}catch(e){return t(e)}},t)}catch(e){return t(e)}},t)}catch(e){return t(e)}};try{return createImageBitmap(e).then(function(e){try{return r=e,a()}catch(e){return o()}},o)}catch(e){o()}})}function canvasToFile(e,n,t,r){var i=arguments.length>4&&void 0!==arguments[4]?arguments[4]:1;return new Promise(function(a,o){var s;return"function"==typeof OffscreenCanvas&&e instanceof OffscreenCanvas?e.convertToBlob({type:n,quality:i}).then(function(e){try{return(s=e).name=t,s.lastModified=r,$If_4.call(this)}catch(e){return o(e)}}.bind(this),o):getFilefromDataUrl(e.toDataURL(n,i),t,r).then(function(e){try{return s=e,$If_4.call(this)}catch(e){return o(e)}}.bind(this),o);function $If_4(){return a(s)}})}function getExifOrientation(e){return new Promise(function(n,t){var r=new FileReader;r.onload=function(e){var t=new DataView(e.target.result);if(65496!=t.getUint16(0,!1))return n(-2);for(var r=t.byteLength,i=2;ii||e.height>i);return a?e.width>e.height?(n.width=i,n.height=e.height/e.width*i):(n.width=e.width/e.height*i,n.height=i):(n.width=e.width,n.height=e.height),r.drawImage(e,0,0,n.width,n.height),[n,a]}function followExifOrientation(e,n,t){var r=n.getContext("2d"),i=e.width,a=e.height;switch(4a?(s.width*=.9,s.height*=.9,s.getContext("2d").drawImage(o,0,0,s.width,s.height),canvasToFile(s,e.type,e.name,e.lastModified,m).then(function(e){try{return l=e,$Loop_4}catch(e){return r(e)}},r)):[1]},h=function $Loop_4_exit(){return c.call(this)};return(f=function(e){for(;e;){if(e.then)return void e.then(f,r);try{if(e.pop){if(e.length)return e.pop()?h.call(this):e;e=u}else e=e.call(this)}catch(e){return r(e)}}}.bind(this))(u)}var g,d=function $Loop_6(){return i--&&l.size>a?(s.width*=.9,s.height*=.9,s.getContext("2d").drawImage(o,0,0,s.width,s.height),m*=.9,canvasToFile(s,e.type,e.name,e.lastModified,m).then(function(e){try{return l=e,$Loop_6}catch(e){return r(e)}},r)):[1]},p=function $Loop_6_exit(){return c.call(this)};return(g=function(e){for(;e;){if(e.then)return void e.then(g,r);try{if(e.pop){if(e.length)return e.pop()?p.call(this):e;e=d}else e=e.call(this)}catch(e){return r(e)}}}.bind(this))(d)}catch(e){return r(e)}}.bind(this),r)}catch(e){return r(e)}}.bind(this),r)}catch(e){return r(e)}}.bind(this),r)})}var e,n=0;var t=function createWorker(e){return new Worker(URL.createObjectURL(new Blob(["(".concat(e,")()")])))}(function(){self.addEventListener("message",function(e){return new Promise(function(n,t){var r,i,a,o,s=e.data;r=s.file,i=s.id,a=s.imageCompressionLibUrl,o=s.options;var c=function $Try_1_Post(){try{return n()}catch(e){return t(e)}},f=function $Try_1_Catch(e){try{return self.postMessage({error:e.message,id:i}),c()}catch(e){return t(e)}};try{var u;return importScripts(a),imageCompression(r,o).then(function(e){try{return u=e,self.postMessage({file:u,id:i}),c()}catch(e){return f(e)}},f)}catch(e){f(e)}})})});function compressOnWebWorker(r,i){return new Promise(function(a,o){return new Promise(function(s,c){e||(e=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 \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\n function compress (){return (").concat(compress,").apply(null, arguments)}\n ")));var f=n++;return t.addEventListener("message",function handler(e){e.data.id===f&&(t.removeEventListener("message",handler),e.data.error&&o(e.data.error),a(e.data.file))}),t.postMessage({file:r,id:f,imageCompressionLibUrl:e,options:i}),s()})})}function imageCompression(e,n){return new Promise(function(t,r){var i,a;if(n.maxSizeMB=n.maxSizeMB||Number.POSITIVE_INFINITY,n.useWebWorker="boolean"!=typeof n.useWebWorker||n.useWebWorker,!(e instanceof Blob||e instanceof File))return r(new Error("The file given is not an instance of Blob or File"));if(!/^image/.test(e.type))return r(new Error("The file given is not an image"));if(a="undefined"!=typeof WorkerGlobalScope&&self instanceof WorkerGlobalScope,!n.useWebWorker||"function"!=typeof Worker||a)return compress(e,n).then(function(e){try{return i=e,$If_3.call(this)}catch(e){return r(e)}}.bind(this),r);var o=function(){try{return $If_3.call(this)}catch(e){return r(e)}}.bind(this),s=function $Try_1_Catch(t){try{return compress(e,n).then(function(e){try{return i=e,o()}catch(e){return r(e)}},r)}catch(e){return r(e)}};try{return compressOnWebWorker(e,n).then(function(e){try{return i=e,o()}catch(e){return s()}},s)}catch(e){s()}function $If_3(){try{i.name=e.name,i.lastModified=e.lastModified}catch(e){}return t(i)}})}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}); +!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 getDataUrlFromFile(e){return new Promise(function(n,t){var r=new FileReader;r.onload=function(){return n(r.result)},r.onerror=function(e){return t(e)},r.readAsDataURL(e)})}function getFilefromDataUrl(e,n){var t=arguments.length>2&&void 0!==arguments[2]?arguments[2]:Date.now();return new Promise(function(r){for(var i,a=e.split(","),o=a[0].match(/:(.*?);/)[1],s=atob(a[1]),c=s.length,f=new Uint8Array(c);c--;)f[c]=s.charCodeAt(c);try{i=new File([f],n,{type:o})}catch(e){(i=new Blob([f],{type:o})).name=n,i.lastModified=t}r(i)})}function loadImage(e){return new Promise(function(n,t){var r=new Image;r.onload=function(){return n(r)},r.onerror=function(e){return t(e)},r.src=e})}function drawImageInCanvas(e){var n;return(n="function"==typeof OffscreenCanvas?new OffscreenCanvas(e.width,e.height):document.createElement("canvas")).getContext("2d").drawImage(e,0,0,n.width,n.height),n}function drawFileInCanvas(e){return new Promise(function(n,t){var r,i,a=function $Try_2_Post(){try{return i=drawImageInCanvas(r),n([r,i])}catch(e){return t(e)}},o=function $Try_2_Catch(n){try{return getDataUrlFromFile(e).then(function(e){try{return loadImage(e).then(function(e){try{return r=e,a()}catch(e){return t(e)}},t)}catch(e){return t(e)}},t)}catch(e){return t(e)}};try{return createImageBitmap(e).then(function(e){try{return r=e,a()}catch(e){return o()}},o)}catch(e){o()}})}function canvasToFile(e,n,t,r){var i=arguments.length>4&&void 0!==arguments[4]?arguments[4]:1;return new Promise(function(a,o){var s;return"function"==typeof OffscreenCanvas&&e instanceof OffscreenCanvas?e.convertToBlob({type:n,quality:i}).then(function(e){try{return(s=e).name=t,s.lastModified=r,$If_4.call(this)}catch(e){return o(e)}}.bind(this),o):getFilefromDataUrl(e.toDataURL(n,i),t,r).then(function(e){try{return s=e,$If_4.call(this)}catch(e){return o(e)}}.bind(this),o);function $If_4(){return a(s)}})}function getExifOrientation(e){return new Promise(function(n,t){var r=new FileReader;r.onload=function(e){var t=new DataView(e.target.result);if(65496!=t.getUint16(0,!1))return n(-2);for(var r=t.byteLength,i=2;ii||e.height>i);return a?e.width>e.height?(n.width=i,n.height=e.height/e.width*i):(n.width=e.width/e.height*i,n.height=i):(n.width=e.width,n.height=e.height),r.drawImage(e,0,0,n.width,n.height),[n,a]}function followExifOrientation(e,n,t){var r=n.getContext("2d"),i=n.width,a=n.height;switch(4a?(s.width*=.9,s.height*=.9,s.getContext("2d").drawImage(o,0,0,s.width,s.height),canvasToFile(s,e.type,e.name,e.lastModified,m).then(function(e){try{return h=e,$Loop_4}catch(e){return r(e)}},r)):[1]},l=function $Loop_4_exit(){return c.call(this)};return(f=function(e){for(;e;){if(e.then)return void e.then(f,r);try{if(e.pop){if(e.length)return e.pop()?l.call(this):e;e=u}else e=e.call(this)}catch(e){return r(e)}}}.bind(this))(u)}var g,d=function $Loop_6(){return i--&&h.size>a?(s.width*=.9,s.height*=.9,s.getContext("2d").drawImage(o,0,0,s.width,s.height),m*=.9,canvasToFile(s,e.type,e.name,e.lastModified,m).then(function(e){try{return h=e,$Loop_6}catch(e){return r(e)}},r)):[1]},p=function $Loop_6_exit(){return c.call(this)};return(g=function(e){for(;e;){if(e.then)return void e.then(g,r);try{if(e.pop){if(e.length)return e.pop()?p.call(this):e;e=d}else e=e.call(this)}catch(e){return r(e)}}}.bind(this))(d)}catch(e){return r(e)}}.bind(this),r)}catch(e){return r(e)}}.bind(this),r)}catch(e){return r(e)}}.bind(this),r)})}var e,n=0;var t=function createWorker(e){return new Worker(URL.createObjectURL(new Blob(["(".concat(e,")()")])))}(function(){self.addEventListener("message",function(e){return new Promise(function(n,t){var r,i,a,o,s=e.data;r=s.file,i=s.id,a=s.imageCompressionLibUrl,o=s.options;var c=function $Try_1_Post(){try{return n()}catch(e){return t(e)}},f=function $Try_1_Catch(e){try{return self.postMessage({error:e.message,id:i}),c()}catch(e){return t(e)}};try{var u;return importScripts(a),imageCompression(r,o).then(function(e){try{return u=e,self.postMessage({file:u,id:i}),c()}catch(e){return f(e)}},f)}catch(e){f(e)}})})});function compressOnWebWorker(r,i){return new Promise(function(a,o){return new Promise(function(s,c){e||(e=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 \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\n function compress (){return (").concat(compress,").apply(null, arguments)}\n ")));var f=n++;return t.addEventListener("message",function handler(e){e.data.id===f&&(t.removeEventListener("message",handler),e.data.error&&o(e.data.error),a(e.data.file))}),t.postMessage({file:r,id:f,imageCompressionLibUrl:e,options:i}),s()})})}function imageCompression(e,n){return new Promise(function(t,r){var i,a;if(n.maxSizeMB=n.maxSizeMB||Number.POSITIVE_INFINITY,n.useWebWorker="boolean"!=typeof n.useWebWorker||n.useWebWorker,!(e instanceof Blob||e instanceof File))return r(new Error("The file given is not an instance of Blob or File"));if(!/^image/.test(e.type))return r(new Error("The file given is not an image"));if(a="undefined"!=typeof WorkerGlobalScope&&self instanceof WorkerGlobalScope,!n.useWebWorker||"function"!=typeof Worker||a)return compress(e,n).then(function(e){try{return i=e,$If_3.call(this)}catch(e){return r(e)}}.bind(this),r);var o=function(){try{return $If_3.call(this)}catch(e){return r(e)}}.bind(this),s=function $Try_1_Catch(t){try{return compress(e,n).then(function(e){try{return i=e,o()}catch(e){return r(e)}},r)}catch(e){return r(e)}};try{return compressOnWebWorker(e,n).then(function(e){try{return i=e,o()}catch(e){return s()}},s)}catch(e){s()}function $If_3(){try{i.name=e.name,i.lastModified=e.lastModified}catch(e){}return t(i)}})}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}); //# sourceMappingURL=browser-image-compression.js.map diff --git a/dist/browser-image-compression.js.map b/dist/browser-image-compression.js.map index ebf4c18..51eab58 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":["/**\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 FileReader()\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 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 let canvas\n if (typeof OffscreenCanvas === 'function') {\n canvas = new OffscreenCanvas(img.width, img.height)\n } else {\n canvas = document.createElement('canvas')\n }\n const ctx = canvas.getContext('2d')\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 compressedFile\n if (typeof OffscreenCanvas === 'function' && canvas instanceof OffscreenCanvas) {\n compressedFile = await canvas.convertToBlob({ type: fileType, quality })\n compressedFile.name = fileName\n compressedFile.lastModified = fileLastModified\n } else {\n const dataUrl = canvas.toDataURL(fileType, quality)\n compressedFile = await getFilefromDataUrl(dataUrl, fileName, fileLastModified)\n }\n return compressedFile\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 FileReader()\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 img\n * @param canvas\n * @param options\n * @returns {Promise<[HTMLCanvasElement, boolean]>}\n */\nexport function handleMaxWidthOrHeight (img, canvas, options) {\n const ctx = canvas.getContext('2d')\n\n const maxWidthOrHeight = options.maxWidthOrHeight\n const needToHandle = Number.isInteger(maxWidthOrHeight) && (img.width > maxWidthOrHeight || img.height > maxWidthOrHeight)\n if (needToHandle) {\n if (img.width > img.height) {\n canvas.width = maxWidthOrHeight\n canvas.height = (img.height / img.width) * maxWidthOrHeight\n } else {\n canvas.width = (img.width / img.height) * maxWidthOrHeight\n canvas.height = maxWidthOrHeight\n }\n } else {\n canvas.width = img.width\n canvas.height = img.height\n }\n\n ctx.drawImage(img, 0, 0, canvas.width, canvas.height)\n return [canvas, needToHandle]\n}\n\n/**\n * followExifOrientation\n * source: https://stackoverflow.com/a/40867559/10395024\n *\n * @param {HTMLImageElement} img\n * @param {HTMLCanvasElement} canvas\n * @param {number} exifOrientation\n * @returns {HTMLCanvasElement} canvas\n */\nexport function followExifOrientation (img, canvas, exifOrientation) {\n const ctx = canvas.getContext('2d')\n\n const width = img.width\n const height = img.height\n\n // set proper canvas dimensions before transform & export\n if (4 < exifOrientation && exifOrientation < 9) {\n canvas.width = height\n canvas.height = width\n } else {\n canvas.width = width\n canvas.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(img, 0, 0)\n\n return canvas\n}","import { canvasToFile, drawFileInCanvas, followExifOrientation, getExifOrientation, handleMaxWidthOrHeight } from './utils'\n\n/**\n * Compress an image file.\n *\n * @param {File} file\n * @param {Object} options - { maxSizeMB=Number.POSITIVE_INFINITY, maxWidthOrHeight, useWebWorker=true, maxIteration = 10, exifOrientation }\n * @param {number} [options.maxSizeMB=Number.POSITIVE_INFINITY]\n * @param {number} [options.maxWidthOrHeight=undefined] * @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 * @returns {Promise}\n */\nexport default async function compress (file, options) {\n let remainingTrials = options.maxIteration || 10\n\n const maxSizeByte = options.maxSizeMB * 1024 * 1024\n\n // no need to compress or resize\n if (file.size <= maxSizeByte && typeof options.maxWidthOrHeight === 'undefined') {\n return file\n }\n\n let img, canvas, isResized\n\n // drawFileInCanvas\n let temp = await drawFileInCanvas(file)\n img = temp[0]\n canvas = temp[1]\n\n // handleMaxWidthOrHeight\n let temp2 = handleMaxWidthOrHeight(img, canvas, options)\n canvas = temp2[0]\n isResized = temp2[1]\n // no need to compress or resize\n if (file.size <= maxSizeByte && !isResized) {\n return file\n }\n\n // exifOrientation\n options.exifOrientation = options.exifOrientation || await getExifOrientation(file)\n canvas = followExifOrientation(img, canvas, options.exifOrientation)\n\n let quality = 1\n let compressedFile = await canvasToFile(canvas, file.type, file.name, file.lastModified, quality)\n if (file.type === 'image/png') {\n while (remainingTrials-- && compressedFile.size > maxSizeByte) {\n canvas.width *= 0.9\n canvas.height *= 0.9\n\n const ctx = canvas.getContext('2d')\n ctx.drawImage(img, 0, 0, canvas.width, canvas.height)\n\n compressedFile = await canvasToFile(canvas, file.type, file.name, file.lastModified, quality)\n }\n } else { // if (file.type === 'image/jpeg') {\n while (remainingTrials-- && compressedFile.size > maxSizeByte) {\n canvas.width *= 0.9\n canvas.height *= 0.9\n\n const ctx = canvas.getContext('2d')\n ctx.drawImage(img, 0, 0, canvas.width, canvas.height)\n\n quality *= 0.9\n compressedFile = await canvasToFile(canvas, file.type, file.name, file.lastModified, quality)\n }\n }\n\n return compressedFile\n}","import imageCompression from './index'\nimport compress from './image-compression'\n\nlet cnt = 0\nlet imageCompressionLibUrl\n\nfunction createWorker (f) {\n return new Worker(URL.createObjectURL(new Blob([`(${f})()`])))\n}\n\nconst worker = createWorker(() => {\n self.addEventListener('message', async (e) => {\n const { file, id, imageCompressionLibUrl, options } = e.data\n try {\n // console.log('[worker] importScripts', imageCompressionLibUrl)\n importScripts(imageCompressionLibUrl)\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, id })\n }\n })\n})\n\nfunction createSourceObject (str) {\n return URL.createObjectURL(new Blob([str], { type: 'application/javascript' }))\n}\n\nexport function compressOnWebWorker (file, options) {\n return new Promise(async (resolve, reject) => {\n if (!imageCompressionLibUrl) {\n imageCompressionLibUrl = createSourceObject(`\n function imageCompression (){return (${imageCompression}).apply(null, arguments)}\n \n imageCompression.getDataUrlFromFile = ${imageCompression.getDataUrlFromFile}\n imageCompression.getFilefromDataUrl = ${imageCompression.getFilefromDataUrl}\n imageCompression.loadImage = ${imageCompression.loadImage}\n imageCompression.drawImageInCanvas = ${imageCompression.drawImageInCanvas}\n imageCompression.drawFileInCanvas = ${imageCompression.drawFileInCanvas}\n imageCompression.canvasToFile = ${imageCompression.canvasToFile}\n imageCompression.getExifOrientation = ${imageCompression.getExifOrientation}\n imageCompression.handleMaxWidthOrHeight = ${imageCompression.handleMaxWidthOrHeight}\n imageCompression.followExifOrientation = ${imageCompression.followExifOrientation}\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\n function compress (){return (${compress}).apply(null, arguments)}\n `)\n // imageCompressionLibUrl = new URL('../dist/browser-image-compression.js', window.location.href).href\n }\n let id = cnt++\n\n function handler (e) {\n if (e.data.id === id) {\n worker.removeEventListener('message', handler)\n if (e.data.error) {\n reject(e.data.error)\n }\n resolve(e.data.file)\n }\n }\n\n worker.addEventListener('message', handler)\n worker.postMessage({ file, id, imageCompressionLibUrl, options })\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} from './utils.js'\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=true, maxIteration = 10, exifOrientation }\n * @param {number} [options.maxSizeMB=Number.POSITIVE_INFINITY]\n * @param {number} [options.maxWidthOrHeight=undefined] * @param {number} [options.maxWidthOrHeight=undefined]\n * @param {boolean} [options.useWebWorker=true]\n * @param {number} [options.maxIteration=10]\n * @param {number} [options.exifOrientation=] - default to be the exif orientation 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 options.useWebWorker = typeof options.useWebWorker === 'boolean' ? options.useWebWorker : true\n\n if (!(file instanceof Blob || file instanceof File)) {\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 (inWebWorker) {\n // console.log('run compression in web worker')\n // } else {\n // console.log('run compression in main thread')\n // }\n\n if (options.useWebWorker && typeof Worker === 'function' && !inWebWorker) {\n try {\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.error('run compression in web worker failed', e)\n compressedFile = await compress(file, options)\n }\n } else {\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\n\nexport default imageCompression\n"],"names":["reader","onerror","e","Promise","dataurl","split","bstr","length","filename","Blob","resolve","loadImage","quality","$return","$error","type","fileName","offset","view","getUint32","getUint16","little","file","maxWidthOrHeight","options","needToHandle","img","canvas","ctx","width","transform","compress","maxIteration","then","$await_9","name","cnt","imageCompression","getDataUrlFromFile","drawImageInCanvas","getExifOrientation","getFilefromDataUrl","drawFileInCanvas","canvasToFile","handleMaxWidthOrHeight","followExifOrientation"],"mappings":"mUAwBEA,EAAAC,QAAO,SAAAC,qJAYH,IAAAC,gCACAC,EAAAC,qDACAC,EAAKC,wEAaqBC,wBAE5B,IAAAC,yBACAD,mBAGJE,gBAgBcC,2zBAiCP,mCAA2EC,kEAA3E,IAAAT,QAAA,SAAAU,EAAAC,iGAEDC,yDAC4BC,2lBAsBFC,YACtBC,EAAAC,YAA4B,6DAI5BD,EAAAE,iBAAAC,uEAMAH,EAAAE,8DAMWlB,wBACXoB,qEAcNC,EAAiBC,mBACjBC,kKAkBSC,6CAIPC,kEA2BAC,qBAEFC,sCAOFF,EAAAE,qDAGiDD,EAAAE,6EACvCF,EAAAE,UAAc,EAAG,EAAG,uBAAaF,EAAAE,2EACjCF,EAAAE,UAAc,4BAAyBF,EAAAE,6DCvMnCC,8EAGFP,EAAAQ,+SAUmBC,KAAA,SAAAC,gBAAAA,8eAULC,w5BC/C5B,MAAIC,UAIJ,uYAaMlC,ukBAyCgBmC,iBAAAC,6OAGVD,iBAAAE,+OAGJF,iBAAAG,+gECNWL,0HAMnBE,iBAAAI,mBAAsCA,mBACtCJ,iBAAA1B,oBACA0B,iBAAAE,oCACAF,iBAAAK,iBAAAA,iBACAL,iBAAAM,0BACAN,iBAAAG,sCACAH,iBAAAO,8CACAP,iBAAAQ,sBAAAA"} \ 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":["/**\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 FileReader()\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 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 let canvas\n if (typeof OffscreenCanvas === 'function') {\n canvas = new OffscreenCanvas(img.width, img.height)\n } else {\n canvas = document.createElement('canvas')\n }\n const ctx = canvas.getContext('2d')\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 compressedFile\n if (typeof OffscreenCanvas === 'function' && canvas instanceof OffscreenCanvas) {\n compressedFile = await canvas.convertToBlob({ type: fileType, quality })\n compressedFile.name = fileName\n compressedFile.lastModified = fileLastModified\n } else {\n const dataUrl = canvas.toDataURL(fileType, quality)\n compressedFile = await getFilefromDataUrl(dataUrl, fileName, fileLastModified)\n }\n return compressedFile\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 FileReader()\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 img\n * @param canvas\n * @param options\n * @returns {Promise<[HTMLCanvasElement, boolean]>}\n */\nexport function handleMaxWidthOrHeight (img, canvas, options) {\n const ctx = canvas.getContext('2d')\n\n const maxWidthOrHeight = options.maxWidthOrHeight\n const needToHandle = Number.isInteger(maxWidthOrHeight) && (img.width > maxWidthOrHeight || img.height > maxWidthOrHeight)\n if (needToHandle) {\n if (img.width > img.height) {\n canvas.width = maxWidthOrHeight\n canvas.height = (img.height / img.width) * maxWidthOrHeight\n } else {\n canvas.width = (img.width / img.height) * maxWidthOrHeight\n canvas.height = maxWidthOrHeight\n }\n } else {\n canvas.width = img.width\n canvas.height = img.height\n }\n\n ctx.drawImage(img, 0, 0, canvas.width, canvas.height)\n return [canvas, needToHandle]\n}\n\n/**\n * followExifOrientation\n * source: https://stackoverflow.com/a/40867559/10395024\n *\n * @param {HTMLImageElement} img\n * @param {HTMLCanvasElement} canvas\n * @param {number} exifOrientation\n * @returns {HTMLCanvasElement} canvas\n */\nexport function followExifOrientation (img, canvas, exifOrientation) {\n const ctx = canvas.getContext('2d')\n\n const width = canvas.width\n const height = canvas.height\n\n // set proper canvas dimensions before transform & export\n if (4 < exifOrientation && exifOrientation < 9) {\n canvas.width = height\n canvas.height = width\n } else {\n canvas.width = width\n canvas.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(img, 0, 0, canvas.width, canvas.height)\n\n return canvas\n}","import { canvasToFile, drawFileInCanvas, followExifOrientation, getExifOrientation, handleMaxWidthOrHeight } from './utils'\n\n/**\n * Compress an image file.\n *\n * @param {File} file\n * @param {Object} options - { maxSizeMB=Number.POSITIVE_INFINITY, maxWidthOrHeight, useWebWorker=true, maxIteration = 10, exifOrientation }\n * @param {number} [options.maxSizeMB=Number.POSITIVE_INFINITY]\n * @param {number} [options.maxWidthOrHeight=undefined] * @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 * @returns {Promise}\n */\nexport default async function compress (file, options) {\n let remainingTrials = options.maxIteration || 10\n\n const maxSizeByte = options.maxSizeMB * 1024 * 1024\n\n // no need to compress or resize\n if (file.size <= maxSizeByte && typeof options.maxWidthOrHeight === 'undefined') {\n return file\n }\n\n let img, canvas, isResized\n\n // drawFileInCanvas\n let temp = await drawFileInCanvas(file)\n img = temp[0]\n canvas = temp[1]\n\n // handleMaxWidthOrHeight\n let temp2 = handleMaxWidthOrHeight(img, canvas, options)\n canvas = temp2[0]\n isResized = temp2[1]\n // no need to compress or resize\n if (file.size <= maxSizeByte && !isResized) {\n return file\n }\n\n // exifOrientation\n options.exifOrientation = options.exifOrientation || await getExifOrientation(file)\n canvas = followExifOrientation(img, canvas, options.exifOrientation)\n\n let quality = 1\n let compressedFile = await canvasToFile(canvas, file.type, file.name, file.lastModified, quality)\n if (file.type === 'image/png') {\n while (remainingTrials-- && compressedFile.size > maxSizeByte) {\n canvas.width *= 0.9\n canvas.height *= 0.9\n\n const ctx = canvas.getContext('2d')\n ctx.drawImage(img, 0, 0, canvas.width, canvas.height)\n\n compressedFile = await canvasToFile(canvas, file.type, file.name, file.lastModified, quality)\n }\n } else { // if (file.type === 'image/jpeg') {\n while (remainingTrials-- && compressedFile.size > maxSizeByte) {\n canvas.width *= 0.9\n canvas.height *= 0.9\n\n const ctx = canvas.getContext('2d')\n ctx.drawImage(img, 0, 0, canvas.width, canvas.height)\n\n quality *= 0.9\n compressedFile = await canvasToFile(canvas, file.type, file.name, file.lastModified, quality)\n }\n }\n\n return compressedFile\n}","import imageCompression from './index'\nimport compress from './image-compression'\n\nlet cnt = 0\nlet imageCompressionLibUrl\n\nfunction createWorker (f) {\n return new Worker(URL.createObjectURL(new Blob([`(${f})()`])))\n}\n\nconst worker = createWorker(() => {\n self.addEventListener('message', async (e) => {\n const { file, id, imageCompressionLibUrl, options } = e.data\n try {\n // console.log('[worker] importScripts', imageCompressionLibUrl)\n importScripts(imageCompressionLibUrl)\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, id })\n }\n })\n})\n\nfunction createSourceObject (str) {\n return URL.createObjectURL(new Blob([str], { type: 'application/javascript' }))\n}\n\nexport function compressOnWebWorker (file, options) {\n return new Promise(async (resolve, reject) => {\n if (!imageCompressionLibUrl) {\n imageCompressionLibUrl = createSourceObject(`\n function imageCompression (){return (${imageCompression}).apply(null, arguments)}\n \n imageCompression.getDataUrlFromFile = ${imageCompression.getDataUrlFromFile}\n imageCompression.getFilefromDataUrl = ${imageCompression.getFilefromDataUrl}\n imageCompression.loadImage = ${imageCompression.loadImage}\n imageCompression.drawImageInCanvas = ${imageCompression.drawImageInCanvas}\n imageCompression.drawFileInCanvas = ${imageCompression.drawFileInCanvas}\n imageCompression.canvasToFile = ${imageCompression.canvasToFile}\n imageCompression.getExifOrientation = ${imageCompression.getExifOrientation}\n imageCompression.handleMaxWidthOrHeight = ${imageCompression.handleMaxWidthOrHeight}\n imageCompression.followExifOrientation = ${imageCompression.followExifOrientation}\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\n function compress (){return (${compress}).apply(null, arguments)}\n `)\n // imageCompressionLibUrl = new URL('../dist/browser-image-compression.js', window.location.href).href\n }\n let id = cnt++\n\n function handler (e) {\n if (e.data.id === id) {\n worker.removeEventListener('message', handler)\n if (e.data.error) {\n reject(e.data.error)\n }\n resolve(e.data.file)\n }\n }\n\n worker.addEventListener('message', handler)\n worker.postMessage({ file, id, imageCompressionLibUrl, options })\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} from './utils.js'\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=true, maxIteration = 10, exifOrientation }\n * @param {number} [options.maxSizeMB=Number.POSITIVE_INFINITY]\n * @param {number} [options.maxWidthOrHeight=undefined] * @param {number} [options.maxWidthOrHeight=undefined]\n * @param {boolean} [options.useWebWorker=true]\n * @param {number} [options.maxIteration=10]\n * @param {number} [options.exifOrientation=] - default to be the exif orientation 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 options.useWebWorker = typeof options.useWebWorker === 'boolean' ? options.useWebWorker : true\n\n if (!(file instanceof Blob || file instanceof File)) {\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 (inWebWorker) {\n // console.log('run compression in web worker')\n // } else {\n // console.log('run compression in main thread')\n // }\n\n if (options.useWebWorker && typeof Worker === 'function' && !inWebWorker) {\n try {\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.error('run compression in web worker failed', e)\n compressedFile = await compress(file, options)\n }\n } else {\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\n\nexport default imageCompression\n"],"names":["reader","onerror","e","Promise","dataurl","split","bstr","length","filename","Blob","resolve","loadImage","quality","$return","$error","type","fileName","offset","view","getUint32","getUint16","little","file","maxWidthOrHeight","options","needToHandle","img","canvas","ctx","width","transform","compress","maxIteration","then","$await_9","name","cnt","imageCompression","getDataUrlFromFile","drawImageInCanvas","getExifOrientation","getFilefromDataUrl","drawFileInCanvas","canvasToFile","handleMaxWidthOrHeight","followExifOrientation"],"mappings":"mUAwBEA,EAAAC,QAAO,SAAAC,qJAYH,IAAAC,gCACAC,EAAAC,qDACAC,EAAKC,wEAaqBC,wBAE5B,IAAAC,yBACAD,mBAGJE,gBAgBcC,2zBAiCP,mCAA2EC,kEAA3E,IAAAT,QAAA,SAAAU,EAAAC,iGAEDC,yDAC4BC,2lBAsBFC,YACtBC,EAAAC,YAA4B,6DAI5BD,EAAAE,iBAAAC,uEAMAH,EAAAE,8DAMWlB,wBACXoB,qEAcNC,EAAiBC,mBACjBC,kKAkBSC,6CAIPC,kEA2BAC,qBAEFC,IAAAA,kCAOFF,EAAAE,qDAGiDD,EAAAE,6EACvCF,EAAAE,UAAc,EAAG,EAAG,uBAAaF,EAAAE,2EACjCF,EAAAE,UAAc,4BAAyBF,EAAAE,8ECvMnCC,8EAGFP,EAAAQ,+SAUmBC,KAAA,SAAAC,gBAAAA,8eAULC,w5BC/C5B,MAAIC,UAIJ,uYAaMlC,ukBAyCgBmC,iBAAAC,6OAGVD,iBAAAE,+OAGJF,iBAAAG,+gECNWL,0HAMnBE,iBAAAI,mBAAsCA,mBACtCJ,iBAAA1B,oBACA0B,iBAAAE,oCACAF,iBAAAK,iBAAAA,iBACAL,iBAAAM,0BACAN,iBAAAG,sCACAH,iBAAAO,8CACAP,iBAAAQ,sBAAAA"} \ No newline at end of file diff --git a/dist/browser-image-compression.mjs b/dist/browser-image-compression.mjs index 9338e9e..699d7fe 100644 --- a/dist/browser-image-compression.mjs +++ b/dist/browser-image-compression.mjs @@ -1,2 +1,2 @@ -function getDataUrlFromFile(e){return new Promise(function(n,t){var r=new FileReader;r.onload=function(){return n(r.result)},r.onerror=function(e){return t(e)},r.readAsDataURL(e)})}function getFilefromDataUrl(e,n){var t=arguments.length>2&&void 0!==arguments[2]?arguments[2]:Date.now();return new Promise(function(r){for(var i,a=e.split(","),o=a[0].match(/:(.*?);/)[1],s=atob(a[1]),c=s.length,u=new Uint8Array(c);c--;)u[c]=s.charCodeAt(c);try{i=new File([u],n,{type:o})}catch(e){(i=new Blob([u],{type:o})).name=n,i.lastModified=t}r(i)})}function loadImage(e){return new Promise(function(n,t){var r=new Image;r.onload=function(){return n(r)},r.onerror=function(e){return t(e)},r.src=e})}function drawImageInCanvas(e){var n;return(n="function"==typeof OffscreenCanvas?new OffscreenCanvas(e.width,e.height):document.createElement("canvas")).getContext("2d").drawImage(e,0,0,n.width,n.height),n}function drawFileInCanvas(e){return new Promise(function(n,t){var r,i,a=function $Try_2_Post(){try{return i=drawImageInCanvas(r),n([r,i])}catch(e){return t(e)}},o=function $Try_2_Catch(n){try{return getDataUrlFromFile(e).then(function(e){try{return loadImage(e).then(function(e){try{return r=e,a()}catch(e){return t(e)}},t)}catch(e){return t(e)}},t)}catch(e){return t(e)}};try{return createImageBitmap(e).then(function(e){try{return r=e,a()}catch(e){return o()}},o)}catch(e){o()}})}function canvasToFile(e,n,t,r){var i=arguments.length>4&&void 0!==arguments[4]?arguments[4]:1;return new Promise(function(a,o){var s;return"function"==typeof OffscreenCanvas&&e instanceof OffscreenCanvas?e.convertToBlob({type:n,quality:i}).then(function(e){try{return(s=e).name=t,s.lastModified=r,$If_4.call(this)}catch(e){return o(e)}}.bind(this),o):getFilefromDataUrl(e.toDataURL(n,i),t,r).then(function(e){try{return s=e,$If_4.call(this)}catch(e){return o(e)}}.bind(this),o);function $If_4(){return a(s)}})}function getExifOrientation(e){return new Promise(function(n,t){var r=new FileReader;r.onload=function(e){var t=new DataView(e.target.result);if(65496!=t.getUint16(0,!1))return n(-2);for(var r=t.byteLength,i=2;ii||e.height>i);return a?e.width>e.height?(n.width=i,n.height=e.height/e.width*i):(n.width=e.width/e.height*i,n.height=i):(n.width=e.width,n.height=e.height),r.drawImage(e,0,0,n.width,n.height),[n,a]}function followExifOrientation(e,n,t){var r=n.getContext("2d"),i=e.width,a=e.height;switch(4a?(s.width*=.9,s.height*=.9,s.getContext("2d").drawImage(o,0,0,s.width,s.height),canvasToFile(s,e.type,e.name,e.lastModified,m).then(function(e){try{return h=e,$Loop_4}catch(e){return r(e)}},r)):[1]},l=function $Loop_4_exit(){return c.call(this)};return(u=function(e){for(;e;){if(e.then)return void e.then(u,r);try{if(e.pop){if(e.length)return e.pop()?l.call(this):e;e=f}else e=e.call(this)}catch(e){return r(e)}}}.bind(this))(f)}var g,d=function $Loop_6(){return i--&&h.size>a?(s.width*=.9,s.height*=.9,s.getContext("2d").drawImage(o,0,0,s.width,s.height),m*=.9,canvasToFile(s,e.type,e.name,e.lastModified,m).then(function(e){try{return h=e,$Loop_6}catch(e){return r(e)}},r)):[1]},p=function $Loop_6_exit(){return c.call(this)};return(g=function(e){for(;e;){if(e.then)return void e.then(g,r);try{if(e.pop){if(e.length)return e.pop()?p.call(this):e;e=d}else e=e.call(this)}catch(e){return r(e)}}}.bind(this))(d)}catch(e){return r(e)}}.bind(this),r)}catch(e){return r(e)}}.bind(this),r)}catch(e){return r(e)}}.bind(this),r)})}var e,n=0;var t=function createWorker(e){return new Worker(URL.createObjectURL(new Blob(["(".concat(e,")()")])))}(function(){self.addEventListener("message",function(e){return new Promise(function(n,t){var r,i,a,o,s=e.data;r=s.file,i=s.id,a=s.imageCompressionLibUrl,o=s.options;var c=function $Try_1_Post(){try{return n()}catch(e){return t(e)}},u=function $Try_1_Catch(e){try{return self.postMessage({error:e.message,id:i}),c()}catch(e){return t(e)}};try{var f;return importScripts(a),imageCompression(r,o).then(function(e){try{return f=e,self.postMessage({file:f,id:i}),c()}catch(e){return u(e)}},u)}catch(e){u(e)}})})});function compressOnWebWorker(r,i){return new Promise(function(a,o){return new Promise(function(s,c){e||(e=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 \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\n function compress (){return (").concat(compress,").apply(null, arguments)}\n ")));var u=n++;return t.addEventListener("message",function handler(e){e.data.id===u&&(t.removeEventListener("message",handler),e.data.error&&o(e.data.error),a(e.data.file))}),t.postMessage({file:r,id:u,imageCompressionLibUrl:e,options:i}),s()})})}function imageCompression(e,n){return new Promise(function(t,r){var i,a;if(n.maxSizeMB=n.maxSizeMB||Number.POSITIVE_INFINITY,n.useWebWorker="boolean"!=typeof n.useWebWorker||n.useWebWorker,!(e instanceof Blob||e instanceof File))return r(new Error("The file given is not an instance of Blob or File"));if(!/^image/.test(e.type))return r(new Error("The file given is not an image"));if(a="undefined"!=typeof WorkerGlobalScope&&self instanceof WorkerGlobalScope,!n.useWebWorker||"function"!=typeof Worker||a)return compress(e,n).then(function(e){try{return i=e,$If_3.call(this)}catch(e){return r(e)}}.bind(this),r);var o=function(){try{return $If_3.call(this)}catch(e){return r(e)}}.bind(this),s=function $Try_1_Catch(t){try{return compress(e,n).then(function(e){try{return i=e,o()}catch(e){return r(e)}},r)}catch(e){return r(e)}};try{return compressOnWebWorker(e,n).then(function(e){try{return i=e,o()}catch(e){return s()}},s)}catch(e){s()}function $If_3(){try{i.name=e.name,i.lastModified=e.lastModified}catch(e){}return t(i)}})}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;export default imageCompression; +function getDataUrlFromFile(e){return new Promise(function(t,n){var r=new FileReader;r.onload=function(){return t(r.result)},r.onerror=function(e){return n(e)},r.readAsDataURL(e)})}function getFilefromDataUrl(e,t){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:Date.now();return new Promise(function(r){for(var i,a=e.split(","),o=a[0].match(/:(.*?);/)[1],s=atob(a[1]),c=s.length,u=new Uint8Array(c);c--;)u[c]=s.charCodeAt(c);try{i=new File([u],t,{type:o})}catch(e){(i=new Blob([u],{type:o})).name=t,i.lastModified=n}r(i)})}function loadImage(e){return new Promise(function(t,n){var r=new Image;r.onload=function(){return t(r)},r.onerror=function(e){return n(e)},r.src=e})}function drawImageInCanvas(e){var t;return(t="function"==typeof OffscreenCanvas?new OffscreenCanvas(e.width,e.height):document.createElement("canvas")).getContext("2d").drawImage(e,0,0,t.width,t.height),t}function drawFileInCanvas(e){return new Promise(function(t,n){var r,i,a=function $Try_2_Post(){try{return i=drawImageInCanvas(r),t([r,i])}catch(e){return n(e)}},o=function $Try_2_Catch(t){try{return getDataUrlFromFile(e).then(function(e){try{return loadImage(e).then(function(e){try{return r=e,a()}catch(e){return n(e)}},n)}catch(e){return n(e)}},n)}catch(e){return n(e)}};try{return createImageBitmap(e).then(function(e){try{return r=e,a()}catch(e){return o()}},o)}catch(e){o()}})}function canvasToFile(e,t,n,r){var i=arguments.length>4&&void 0!==arguments[4]?arguments[4]:1;return new Promise(function(a,o){var s;return"function"==typeof OffscreenCanvas&&e instanceof OffscreenCanvas?e.convertToBlob({type:t,quality:i}).then(function(e){try{return(s=e).name=n,s.lastModified=r,$If_4.call(this)}catch(e){return o(e)}}.bind(this),o):getFilefromDataUrl(e.toDataURL(t,i),n,r).then(function(e){try{return s=e,$If_4.call(this)}catch(e){return o(e)}}.bind(this),o);function $If_4(){return a(s)}})}function getExifOrientation(e){return new Promise(function(t,n){var r=new FileReader;r.onload=function(e){var n=new DataView(e.target.result);if(65496!=n.getUint16(0,!1))return t(-2);for(var r=n.byteLength,i=2;ii||e.height>i);return a?e.width>e.height?(t.width=i,t.height=e.height/e.width*i):(t.width=e.width/e.height*i,t.height=i):(t.width=e.width,t.height=e.height),r.drawImage(e,0,0,t.width,t.height),[t,a]}function followExifOrientation(e,t,n){var r=t.getContext("2d"),i=t.width,a=t.height;switch(4a?(s.width*=.9,s.height*=.9,s.getContext("2d").drawImage(o,0,0,s.width,s.height),canvasToFile(s,e.type,e.name,e.lastModified,m).then(function(e){try{return h=e,$Loop_4}catch(e){return r(e)}},r)):[1]},l=function $Loop_4_exit(){return c.call(this)};return(u=function(e){for(;e;){if(e.then)return void e.then(u,r);try{if(e.pop){if(e.length)return e.pop()?l.call(this):e;e=f}else e=e.call(this)}catch(e){return r(e)}}}.bind(this))(f)}var g,d=function $Loop_6(){return i--&&h.size>a?(s.width*=.9,s.height*=.9,s.getContext("2d").drawImage(o,0,0,s.width,s.height),m*=.9,canvasToFile(s,e.type,e.name,e.lastModified,m).then(function(e){try{return h=e,$Loop_6}catch(e){return r(e)}},r)):[1]},p=function $Loop_6_exit(){return c.call(this)};return(g=function(e){for(;e;){if(e.then)return void e.then(g,r);try{if(e.pop){if(e.length)return e.pop()?p.call(this):e;e=d}else e=e.call(this)}catch(e){return r(e)}}}.bind(this))(d)}catch(e){return r(e)}}.bind(this),r)}catch(e){return r(e)}}.bind(this),r)}catch(e){return r(e)}}.bind(this),r)})}var e,t=0;var n=function createWorker(e){return new Worker(URL.createObjectURL(new Blob(["(".concat(e,")()")])))}(function(){self.addEventListener("message",function(e){return new Promise(function(t,n){var r,i,a,o,s=e.data;r=s.file,i=s.id,a=s.imageCompressionLibUrl,o=s.options;var c=function $Try_1_Post(){try{return t()}catch(e){return n(e)}},u=function $Try_1_Catch(e){try{return self.postMessage({error:e.message,id:i}),c()}catch(e){return n(e)}};try{var f;return importScripts(a),imageCompression(r,o).then(function(e){try{return f=e,self.postMessage({file:f,id:i}),c()}catch(e){return u(e)}},u)}catch(e){u(e)}})})});function compressOnWebWorker(r,i){return new Promise(function(a,o){return new Promise(function(s,c){e||(e=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 \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\n function compress (){return (").concat(compress,").apply(null, arguments)}\n ")));var u=t++;return n.addEventListener("message",function handler(e){e.data.id===u&&(n.removeEventListener("message",handler),e.data.error&&o(e.data.error),a(e.data.file))}),n.postMessage({file:r,id:u,imageCompressionLibUrl:e,options:i}),s()})})}function imageCompression(e,t){return new Promise(function(n,r){var i,a;if(t.maxSizeMB=t.maxSizeMB||Number.POSITIVE_INFINITY,t.useWebWorker="boolean"!=typeof t.useWebWorker||t.useWebWorker,!(e instanceof Blob||e instanceof File))return r(new Error("The file given is not an instance of Blob or File"));if(!/^image/.test(e.type))return r(new Error("The file given is not an image"));if(a="undefined"!=typeof WorkerGlobalScope&&self instanceof WorkerGlobalScope,!t.useWebWorker||"function"!=typeof Worker||a)return compress(e,t).then(function(e){try{return i=e,$If_3.call(this)}catch(e){return r(e)}}.bind(this),r);var o=function(){try{return $If_3.call(this)}catch(e){return r(e)}}.bind(this),s=function $Try_1_Catch(n){try{return compress(e,t).then(function(e){try{return i=e,o()}catch(e){return r(e)}},r)}catch(e){return r(e)}};try{return compressOnWebWorker(e,t).then(function(e){try{return i=e,o()}catch(e){return s()}},s)}catch(e){s()}function $If_3(){try{i.name=e.name,i.lastModified=e.lastModified}catch(e){}return n(i)}})}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;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 021bc0e..fc3ac3e 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":["/**\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 FileReader()\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 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 let canvas\n if (typeof OffscreenCanvas === 'function') {\n canvas = new OffscreenCanvas(img.width, img.height)\n } else {\n canvas = document.createElement('canvas')\n }\n const ctx = canvas.getContext('2d')\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 compressedFile\n if (typeof OffscreenCanvas === 'function' && canvas instanceof OffscreenCanvas) {\n compressedFile = await canvas.convertToBlob({ type: fileType, quality })\n compressedFile.name = fileName\n compressedFile.lastModified = fileLastModified\n } else {\n const dataUrl = canvas.toDataURL(fileType, quality)\n compressedFile = await getFilefromDataUrl(dataUrl, fileName, fileLastModified)\n }\n return compressedFile\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 FileReader()\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 img\n * @param canvas\n * @param options\n * @returns {Promise<[HTMLCanvasElement, boolean]>}\n */\nexport function handleMaxWidthOrHeight (img, canvas, options) {\n const ctx = canvas.getContext('2d')\n\n const maxWidthOrHeight = options.maxWidthOrHeight\n const needToHandle = Number.isInteger(maxWidthOrHeight) && (img.width > maxWidthOrHeight || img.height > maxWidthOrHeight)\n if (needToHandle) {\n if (img.width > img.height) {\n canvas.width = maxWidthOrHeight\n canvas.height = (img.height / img.width) * maxWidthOrHeight\n } else {\n canvas.width = (img.width / img.height) * maxWidthOrHeight\n canvas.height = maxWidthOrHeight\n }\n } else {\n canvas.width = img.width\n canvas.height = img.height\n }\n\n ctx.drawImage(img, 0, 0, canvas.width, canvas.height)\n return [canvas, needToHandle]\n}\n\n/**\n * followExifOrientation\n * source: https://stackoverflow.com/a/40867559/10395024\n *\n * @param {HTMLImageElement} img\n * @param {HTMLCanvasElement} canvas\n * @param {number} exifOrientation\n * @returns {HTMLCanvasElement} canvas\n */\nexport function followExifOrientation (img, canvas, exifOrientation) {\n const ctx = canvas.getContext('2d')\n\n const width = img.width\n const height = img.height\n\n // set proper canvas dimensions before transform & export\n if (4 < exifOrientation && exifOrientation < 9) {\n canvas.width = height\n canvas.height = width\n } else {\n canvas.width = width\n canvas.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(img, 0, 0)\n\n return canvas\n}","import { canvasToFile, drawFileInCanvas, followExifOrientation, getExifOrientation, handleMaxWidthOrHeight } from './utils'\n\n/**\n * Compress an image file.\n *\n * @param {File} file\n * @param {Object} options - { maxSizeMB=Number.POSITIVE_INFINITY, maxWidthOrHeight, useWebWorker=true, maxIteration = 10, exifOrientation }\n * @param {number} [options.maxSizeMB=Number.POSITIVE_INFINITY]\n * @param {number} [options.maxWidthOrHeight=undefined] * @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 * @returns {Promise}\n */\nexport default async function compress (file, options) {\n let remainingTrials = options.maxIteration || 10\n\n const maxSizeByte = options.maxSizeMB * 1024 * 1024\n\n // no need to compress or resize\n if (file.size <= maxSizeByte && typeof options.maxWidthOrHeight === 'undefined') {\n return file\n }\n\n let img, canvas, isResized\n\n // drawFileInCanvas\n let temp = await drawFileInCanvas(file)\n img = temp[0]\n canvas = temp[1]\n\n // handleMaxWidthOrHeight\n let temp2 = handleMaxWidthOrHeight(img, canvas, options)\n canvas = temp2[0]\n isResized = temp2[1]\n // no need to compress or resize\n if (file.size <= maxSizeByte && !isResized) {\n return file\n }\n\n // exifOrientation\n options.exifOrientation = options.exifOrientation || await getExifOrientation(file)\n canvas = followExifOrientation(img, canvas, options.exifOrientation)\n\n let quality = 1\n let compressedFile = await canvasToFile(canvas, file.type, file.name, file.lastModified, quality)\n if (file.type === 'image/png') {\n while (remainingTrials-- && compressedFile.size > maxSizeByte) {\n canvas.width *= 0.9\n canvas.height *= 0.9\n\n const ctx = canvas.getContext('2d')\n ctx.drawImage(img, 0, 0, canvas.width, canvas.height)\n\n compressedFile = await canvasToFile(canvas, file.type, file.name, file.lastModified, quality)\n }\n } else { // if (file.type === 'image/jpeg') {\n while (remainingTrials-- && compressedFile.size > maxSizeByte) {\n canvas.width *= 0.9\n canvas.height *= 0.9\n\n const ctx = canvas.getContext('2d')\n ctx.drawImage(img, 0, 0, canvas.width, canvas.height)\n\n quality *= 0.9\n compressedFile = await canvasToFile(canvas, file.type, file.name, file.lastModified, quality)\n }\n }\n\n return compressedFile\n}","import imageCompression from './index'\nimport compress from './image-compression'\n\nlet cnt = 0\nlet imageCompressionLibUrl\n\nfunction createWorker (f) {\n return new Worker(URL.createObjectURL(new Blob([`(${f})()`])))\n}\n\nconst worker = createWorker(() => {\n self.addEventListener('message', async (e) => {\n const { file, id, imageCompressionLibUrl, options } = e.data\n try {\n // console.log('[worker] importScripts', imageCompressionLibUrl)\n importScripts(imageCompressionLibUrl)\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, id })\n }\n })\n})\n\nfunction createSourceObject (str) {\n return URL.createObjectURL(new Blob([str], { type: 'application/javascript' }))\n}\n\nexport function compressOnWebWorker (file, options) {\n return new Promise(async (resolve, reject) => {\n if (!imageCompressionLibUrl) {\n imageCompressionLibUrl = createSourceObject(`\n function imageCompression (){return (${imageCompression}).apply(null, arguments)}\n \n imageCompression.getDataUrlFromFile = ${imageCompression.getDataUrlFromFile}\n imageCompression.getFilefromDataUrl = ${imageCompression.getFilefromDataUrl}\n imageCompression.loadImage = ${imageCompression.loadImage}\n imageCompression.drawImageInCanvas = ${imageCompression.drawImageInCanvas}\n imageCompression.drawFileInCanvas = ${imageCompression.drawFileInCanvas}\n imageCompression.canvasToFile = ${imageCompression.canvasToFile}\n imageCompression.getExifOrientation = ${imageCompression.getExifOrientation}\n imageCompression.handleMaxWidthOrHeight = ${imageCompression.handleMaxWidthOrHeight}\n imageCompression.followExifOrientation = ${imageCompression.followExifOrientation}\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\n function compress (){return (${compress}).apply(null, arguments)}\n `)\n // imageCompressionLibUrl = new URL('../dist/browser-image-compression.js', window.location.href).href\n }\n let id = cnt++\n\n function handler (e) {\n if (e.data.id === id) {\n worker.removeEventListener('message', handler)\n if (e.data.error) {\n reject(e.data.error)\n }\n resolve(e.data.file)\n }\n }\n\n worker.addEventListener('message', handler)\n worker.postMessage({ file, id, imageCompressionLibUrl, options })\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} from './utils.js'\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=true, maxIteration = 10, exifOrientation }\n * @param {number} [options.maxSizeMB=Number.POSITIVE_INFINITY]\n * @param {number} [options.maxWidthOrHeight=undefined] * @param {number} [options.maxWidthOrHeight=undefined]\n * @param {boolean} [options.useWebWorker=true]\n * @param {number} [options.maxIteration=10]\n * @param {number} [options.exifOrientation=] - default to be the exif orientation 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 options.useWebWorker = typeof options.useWebWorker === 'boolean' ? options.useWebWorker : true\n\n if (!(file instanceof Blob || file instanceof File)) {\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 (inWebWorker) {\n // console.log('run compression in web worker')\n // } else {\n // console.log('run compression in main thread')\n // }\n\n if (options.useWebWorker && typeof Worker === 'function' && !inWebWorker) {\n try {\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.error('run compression in web worker failed', e)\n compressedFile = await compress(file, options)\n }\n } else {\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\n\nexport default imageCompression\n"],"names":["reader","onerror","e","Promise","dataurl","split","bstr","length","filename","Blob","resolve","loadImage","quality","$return","$error","type","fileName","offset","view","getUint32","getUint16","little","file","maxWidthOrHeight","options","needToHandle","img","canvas","ctx","width","transform","compress","maxIteration","then","$await_9","name","cnt","imageCompression","getDataUrlFromFile","drawImageInCanvas","getExifOrientation","getFilefromDataUrl","drawFileInCanvas","canvasToFile","handleMaxWidthOrHeight","followExifOrientation"],"mappings":"6HAwBEA,EAAAC,QAAO,SAAAC,qJAYH,IAAAC,gCACAC,EAAAC,qDACAC,EAAKC,wEAaqBC,wBAE5B,IAAAC,yBACAD,mBAGJE,gBAgBcC,2zBAiCd,mCAAkFC,kEAA3E,IAAAT,QAAA,SAAAU,EAAAC,iGAEDC,yDAC4BC,2lBAsBFC,YACtBC,EAAAC,YAA4B,6DAI5BD,EAAAE,iBAAAC,uEAMAH,EAAAE,8DAMWlB,wBACXoB,qEAcNC,EAAiBC,mBACjBC,kKAkBSC,6CAIPC,kEA2BAC,qBAEFC,sCAOFF,EAAAE,qDAGiDD,EAAAE,6EACvCF,EAAAE,UAAc,EAAG,EAAG,uBAAaF,EAAAE,2EACjCF,EAAAE,UAAc,4BAAyBF,EAAAE,6DCvMnCC,8EAGFP,EAAAQ,+SAUmBC,KAAA,SAAAC,gBAAAA,8eAULC,w5BC/C5B,MAAIC,UAIJ,uYAaMlC,ukBAyCgBmC,iBAAAC,6OAGVD,iBAAAE,+OAGJF,iBAAAG,+gECNWL,mHAMnBE,iBAAAI,mBAAsCA,mBACtCJ,iBAAA1B,oBACA0B,iBAAAE,oCACAF,iBAAAK,iBAAAA,iBACAL,iBAAAM,0BACAN,iBAAAG,sCACAH,iBAAAO,8CACAP,iBAAAQ,sBAAAA"} \ 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":["/**\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 FileReader()\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 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 let canvas\n if (typeof OffscreenCanvas === 'function') {\n canvas = new OffscreenCanvas(img.width, img.height)\n } else {\n canvas = document.createElement('canvas')\n }\n const ctx = canvas.getContext('2d')\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 compressedFile\n if (typeof OffscreenCanvas === 'function' && canvas instanceof OffscreenCanvas) {\n compressedFile = await canvas.convertToBlob({ type: fileType, quality })\n compressedFile.name = fileName\n compressedFile.lastModified = fileLastModified\n } else {\n const dataUrl = canvas.toDataURL(fileType, quality)\n compressedFile = await getFilefromDataUrl(dataUrl, fileName, fileLastModified)\n }\n return compressedFile\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 FileReader()\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 img\n * @param canvas\n * @param options\n * @returns {Promise<[HTMLCanvasElement, boolean]>}\n */\nexport function handleMaxWidthOrHeight (img, canvas, options) {\n const ctx = canvas.getContext('2d')\n\n const maxWidthOrHeight = options.maxWidthOrHeight\n const needToHandle = Number.isInteger(maxWidthOrHeight) && (img.width > maxWidthOrHeight || img.height > maxWidthOrHeight)\n if (needToHandle) {\n if (img.width > img.height) {\n canvas.width = maxWidthOrHeight\n canvas.height = (img.height / img.width) * maxWidthOrHeight\n } else {\n canvas.width = (img.width / img.height) * maxWidthOrHeight\n canvas.height = maxWidthOrHeight\n }\n } else {\n canvas.width = img.width\n canvas.height = img.height\n }\n\n ctx.drawImage(img, 0, 0, canvas.width, canvas.height)\n return [canvas, needToHandle]\n}\n\n/**\n * followExifOrientation\n * source: https://stackoverflow.com/a/40867559/10395024\n *\n * @param {HTMLImageElement} img\n * @param {HTMLCanvasElement} canvas\n * @param {number} exifOrientation\n * @returns {HTMLCanvasElement} canvas\n */\nexport function followExifOrientation (img, canvas, exifOrientation) {\n const ctx = canvas.getContext('2d')\n\n const width = canvas.width\n const height = canvas.height\n\n // set proper canvas dimensions before transform & export\n if (4 < exifOrientation && exifOrientation < 9) {\n canvas.width = height\n canvas.height = width\n } else {\n canvas.width = width\n canvas.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(img, 0, 0, canvas.width, canvas.height)\n\n return canvas\n}","import { canvasToFile, drawFileInCanvas, followExifOrientation, getExifOrientation, handleMaxWidthOrHeight } from './utils'\n\n/**\n * Compress an image file.\n *\n * @param {File} file\n * @param {Object} options - { maxSizeMB=Number.POSITIVE_INFINITY, maxWidthOrHeight, useWebWorker=true, maxIteration = 10, exifOrientation }\n * @param {number} [options.maxSizeMB=Number.POSITIVE_INFINITY]\n * @param {number} [options.maxWidthOrHeight=undefined] * @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 * @returns {Promise}\n */\nexport default async function compress (file, options) {\n let remainingTrials = options.maxIteration || 10\n\n const maxSizeByte = options.maxSizeMB * 1024 * 1024\n\n // no need to compress or resize\n if (file.size <= maxSizeByte && typeof options.maxWidthOrHeight === 'undefined') {\n return file\n }\n\n let img, canvas, isResized\n\n // drawFileInCanvas\n let temp = await drawFileInCanvas(file)\n img = temp[0]\n canvas = temp[1]\n\n // handleMaxWidthOrHeight\n let temp2 = handleMaxWidthOrHeight(img, canvas, options)\n canvas = temp2[0]\n isResized = temp2[1]\n // no need to compress or resize\n if (file.size <= maxSizeByte && !isResized) {\n return file\n }\n\n // exifOrientation\n options.exifOrientation = options.exifOrientation || await getExifOrientation(file)\n canvas = followExifOrientation(img, canvas, options.exifOrientation)\n\n let quality = 1\n let compressedFile = await canvasToFile(canvas, file.type, file.name, file.lastModified, quality)\n if (file.type === 'image/png') {\n while (remainingTrials-- && compressedFile.size > maxSizeByte) {\n canvas.width *= 0.9\n canvas.height *= 0.9\n\n const ctx = canvas.getContext('2d')\n ctx.drawImage(img, 0, 0, canvas.width, canvas.height)\n\n compressedFile = await canvasToFile(canvas, file.type, file.name, file.lastModified, quality)\n }\n } else { // if (file.type === 'image/jpeg') {\n while (remainingTrials-- && compressedFile.size > maxSizeByte) {\n canvas.width *= 0.9\n canvas.height *= 0.9\n\n const ctx = canvas.getContext('2d')\n ctx.drawImage(img, 0, 0, canvas.width, canvas.height)\n\n quality *= 0.9\n compressedFile = await canvasToFile(canvas, file.type, file.name, file.lastModified, quality)\n }\n }\n\n return compressedFile\n}","import imageCompression from './index'\nimport compress from './image-compression'\n\nlet cnt = 0\nlet imageCompressionLibUrl\n\nfunction createWorker (f) {\n return new Worker(URL.createObjectURL(new Blob([`(${f})()`])))\n}\n\nconst worker = createWorker(() => {\n self.addEventListener('message', async (e) => {\n const { file, id, imageCompressionLibUrl, options } = e.data\n try {\n // console.log('[worker] importScripts', imageCompressionLibUrl)\n importScripts(imageCompressionLibUrl)\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, id })\n }\n })\n})\n\nfunction createSourceObject (str) {\n return URL.createObjectURL(new Blob([str], { type: 'application/javascript' }))\n}\n\nexport function compressOnWebWorker (file, options) {\n return new Promise(async (resolve, reject) => {\n if (!imageCompressionLibUrl) {\n imageCompressionLibUrl = createSourceObject(`\n function imageCompression (){return (${imageCompression}).apply(null, arguments)}\n \n imageCompression.getDataUrlFromFile = ${imageCompression.getDataUrlFromFile}\n imageCompression.getFilefromDataUrl = ${imageCompression.getFilefromDataUrl}\n imageCompression.loadImage = ${imageCompression.loadImage}\n imageCompression.drawImageInCanvas = ${imageCompression.drawImageInCanvas}\n imageCompression.drawFileInCanvas = ${imageCompression.drawFileInCanvas}\n imageCompression.canvasToFile = ${imageCompression.canvasToFile}\n imageCompression.getExifOrientation = ${imageCompression.getExifOrientation}\n imageCompression.handleMaxWidthOrHeight = ${imageCompression.handleMaxWidthOrHeight}\n imageCompression.followExifOrientation = ${imageCompression.followExifOrientation}\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\n function compress (){return (${compress}).apply(null, arguments)}\n `)\n // imageCompressionLibUrl = new URL('../dist/browser-image-compression.js', window.location.href).href\n }\n let id = cnt++\n\n function handler (e) {\n if (e.data.id === id) {\n worker.removeEventListener('message', handler)\n if (e.data.error) {\n reject(e.data.error)\n }\n resolve(e.data.file)\n }\n }\n\n worker.addEventListener('message', handler)\n worker.postMessage({ file, id, imageCompressionLibUrl, options })\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} from './utils.js'\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=true, maxIteration = 10, exifOrientation }\n * @param {number} [options.maxSizeMB=Number.POSITIVE_INFINITY]\n * @param {number} [options.maxWidthOrHeight=undefined] * @param {number} [options.maxWidthOrHeight=undefined]\n * @param {boolean} [options.useWebWorker=true]\n * @param {number} [options.maxIteration=10]\n * @param {number} [options.exifOrientation=] - default to be the exif orientation 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 options.useWebWorker = typeof options.useWebWorker === 'boolean' ? options.useWebWorker : true\n\n if (!(file instanceof Blob || file instanceof File)) {\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 (inWebWorker) {\n // console.log('run compression in web worker')\n // } else {\n // console.log('run compression in main thread')\n // }\n\n if (options.useWebWorker && typeof Worker === 'function' && !inWebWorker) {\n try {\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.error('run compression in web worker failed', e)\n compressedFile = await compress(file, options)\n }\n } else {\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\n\nexport default imageCompression\n"],"names":["reader","onerror","e","Promise","dataurl","split","bstr","length","filename","Blob","resolve","loadImage","quality","$return","$error","type","fileName","offset","view","getUint32","getUint16","little","file","maxWidthOrHeight","options","needToHandle","img","canvas","ctx","width","transform","compress","maxIteration","then","$await_9","name","cnt","imageCompression","getDataUrlFromFile","drawImageInCanvas","getExifOrientation","getFilefromDataUrl","drawFileInCanvas","canvasToFile","handleMaxWidthOrHeight","followExifOrientation"],"mappings":"6HAwBEA,EAAAC,QAAO,SAAAC,qJAYH,IAAAC,gCACAC,EAAAC,qDACAC,EAAKC,wEAaqBC,wBAE5B,IAAAC,yBACAD,mBAGJE,gBAgBcC,2zBAiCd,mCAAkFC,kEAA3E,IAAAT,QAAA,SAAAU,EAAAC,iGAEDC,yDAC4BC,2lBAsBFC,YACtBC,EAAAC,YAA4B,6DAI5BD,EAAAE,iBAAAC,uEAMAH,EAAAE,8DAMWlB,wBACXoB,qEAcNC,EAAiBC,mBACjBC,kKAkBSC,6CAIPC,kEA2BAC,qBAEFC,IAAAA,kCAOFF,EAAAE,qDAGiDD,EAAAE,6EACvCF,EAAAE,UAAc,EAAG,EAAG,uBAAaF,EAAAE,2EACjCF,EAAAE,UAAc,4BAAyBF,EAAAE,8ECvMnCC,8EAGFP,EAAAQ,+SAUmBC,KAAA,SAAAC,gBAAAA,8eAULC,w5BC/C5B,MAAIC,UAIJ,uYAaMlC,ukBAyCgBmC,iBAAAC,6OAGVD,iBAAAE,+OAGJF,iBAAAG,+gECNWL,mHAMnBE,iBAAAI,mBAAsCA,mBACtCJ,iBAAA1B,oBACA0B,iBAAAE,oCACAF,iBAAAK,iBAAAA,iBACAL,iBAAAM,0BACAN,iBAAAG,sCACAH,iBAAAO,8CACAP,iBAAAQ,sBAAAA"} \ No newline at end of file diff --git a/example/basic.html b/example/basic.html index 0692582..6c14316 100644 --- a/example/basic.html +++ b/example/basic.html @@ -30,7 +30,7 @@ } logDom.innerHTML = 'Source image size:' + (file.size / 1024 / 1024).toFixed(2) + 'mb' console.log('input', file) - var options = { maxSizeMB: 1, useWebWorker: useWebWorker } + var options = { maxSizeMB: 1, maxWidthOrHeight: 720, useWebWorker: useWebWorker } const output = await imageCompression(file, options) logDom.innerHTML += ', output size:' + (output.size / 1024 / 1024).toFixed(2) + 'mb' console.log('output', output) @@ -49,6 +49,6 @@ }) } - + - \ No newline at end of file + diff --git a/example/development.html b/example/development.html new file mode 100644 index 0000000..970ce5f --- /dev/null +++ b/example/development.html @@ -0,0 +1,54 @@ + + + +

look at console to see logs

+
+
+ +

+
+
+
+ +

+ +
+ + + + diff --git a/lib/utils.js b/lib/utils.js index e0aa467..646eaea 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -207,8 +207,8 @@ export function handleMaxWidthOrHeight (img, canvas, options) { export function followExifOrientation (img, canvas, exifOrientation) { const ctx = canvas.getContext('2d') - const width = img.width - const height = img.height + const width = canvas.width + const height = canvas.height // set proper canvas dimensions before transform & export if (4 < exifOrientation && exifOrientation < 9) { @@ -231,7 +231,7 @@ export function followExifOrientation (img, canvas, exifOrientation) { default: break; } - ctx.drawImage(img, 0, 0) + ctx.drawImage(img, 0, 0, canvas.width, canvas.height) return canvas } \ No newline at end of file diff --git a/package.json b/package.json index a011aab..6d3ed08 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "browser-image-compression", - "version": "1.0.0", + "version": "1.0.1", "description": "Compress images in the browser", "main": "dist/browser-image-compression.js", "module": "dist/browser-image-compression.mjs", diff --git a/test/index.spec.js b/test/index.spec.js index 4364acc..a41fee7 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -99,6 +99,49 @@ describe('Tests', function () { restore() }) + it('resize jpg image file', async () => { + const restore = setupCustomFileAPI() + + const file = new File(JPG_PATH) + // const file = new File(JPG_FILE, JPG_NAME) + // Object.defineProperty(file, 'type', { value: 'image/jpeg' }) + + const maxWidthOrHeight = 720 + + const compressedFile = await imageCompression(file, { maxWidthOrHeight, useWebWorker: false, exifOrientation: -2 }) + + restore() + + const temp = await drawFileInCanvas(compressedFile) + const img = temp[0] + expect(img.width).to.be.at.most(maxWidthOrHeight) + expect(img.height).to.be.at.most(maxWidthOrHeight) + + }) + + it('compress and resize jpg image file', async () => { + const restore = setupCustomFileAPI() + + const file = new File(JPG_PATH) + // const file = new File(JPG_FILE, JPG_NAME) + // Object.defineProperty(file, 'type', { value: 'image/jpeg' }) + + const maxSizeMB = 1 + const maxSizeByte = maxSizeMB * 1024 * 1024 + const maxWidthOrHeight = 720 + + const compressedFile = await imageCompression(file, { maxSizeMB, maxWidthOrHeight, useWebWorker: false, exifOrientation: -2 }) + restore() + + expect(compressedFile.size).to.be.at.most(maxSizeByte) + + const temp = await drawFileInCanvas(compressedFile) + const img = temp[0] + expect(img.width).to.be.at.most(maxWidthOrHeight) + expect(img.height).to.be.at.most(maxWidthOrHeight) + + }) + it('compress png image file', async () => { const restore = setupCustomFileAPI() @@ -113,6 +156,46 @@ describe('Tests', function () { restore() }) + it('resize png image file', async () => { + const restore = setupCustomFileAPI() + + const file = new File(PNG_PATH) + + const maxWidthOrHeight = 720 + + const compressedFile = await imageCompression(file, { maxWidthOrHeight, useWebWorker: false, exifOrientation: -2 }) + restore() + + const temp = await drawFileInCanvas(compressedFile) + + const img = temp[0] + expect(img.width).to.be.at.most(maxWidthOrHeight) + expect(img.height).to.be.at.most(maxWidthOrHeight) + + }) + + it('compress and resize png image file', async () => { + const restore = setupCustomFileAPI() + + const file = new File(PNG_PATH) + + const maxSizeMB = 1 + const maxSizeByte = maxSizeMB * 1024 * 1024 + const maxWidthOrHeight = 720 + + const compressedFile = await imageCompression(file, { maxSizeMB, maxWidthOrHeight, useWebWorker: false, exifOrientation: -2 }) + restore() + + expect(compressedFile.size).to.be.at.most(maxSizeByte) + + const temp = await drawFileInCanvas(compressedFile) + + const img = temp[0] + expect(img.width).to.be.at.most(maxWidthOrHeight) + expect(img.height).to.be.at.most(maxWidthOrHeight) + + }) + it('fails if wrong file provided', async () => { const file = undefined