From a201f4dd1cce5310669d0d407b18732bcc3d5d14 Mon Sep 17 00:00:00 2001 From: Donald Date: Fri, 8 May 2020 00:21:57 +0800 Subject: [PATCH] release 1.0.11 --- README.md | 2 +- coverage/coverage-final.json | 4 ++-- coverage/image-compression.js.html | 2 +- coverage/index.html | 2 +- coverage/index.js.html | 4 ++-- coverage/lcov-report/image-compression.js.html | 2 +- coverage/lcov-report/index.html | 2 +- coverage/lcov-report/index.js.html | 4 ++-- coverage/lcov-report/utils.js.html | 4 ++-- coverage/utils.js.html | 4 ++-- dist/browser-image-compression.js | 4 ++-- dist/browser-image-compression.js.map | 2 +- dist/browser-image-compression.mjs | 4 ++-- dist/browser-image-compression.mjs.map | 2 +- example/basic.html | 2 +- lib/index.js | 2 +- package.json | 2 +- 17 files changed, 24 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 26018c3..7c28432 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ yarn add browser-image-compression ``` or use a CDN like [delivrjs]: ``` -https://cdn.jsdelivr.net/npm/browser-image-compression@1.0.10/dist/browser-image-compression.js +https://cdn.jsdelivr.net/npm/browser-image-compression@1.0.11/dist/browser-image-compression.js or https://cdn.jsdelivr.net/npm/browser-image-compression@latest/dist/browser-image-compression.js ``` diff --git a/coverage/coverage-final.json b/coverage/coverage-final.json index 8dfe632..d7ea072 100644 --- a/coverage/coverage-final.json +++ b/coverage/coverage-final.json @@ -1,4 +1,4 @@ {"D:\\Projects\\browser-image-compression\\lib\\image-compression.js": {"path":"D:\\Projects\\browser-image-compression\\lib\\image-compression.js","statementMap":{"0":{"start":{"line":26,"column":17},"end":{"line":26,"column":18}},"1":{"start":{"line":29,"column":4},"end":{"line":29,"column":19}},"2":{"start":{"line":30,"column":4},"end":{"line":30,"column":47}},"3":{"start":{"line":34,"column":4},"end":{"line":34,"column":51}},"4":{"start":{"line":35,"column":4},"end":{"line":35,"column":32}},"5":{"start":{"line":38,"column":24},"end":{"line":38,"column":50}},"6":{"start":{"line":40,"column":22},"end":{"line":40,"column":53}},"7":{"start":{"line":42,"column":2},"end":{"line":42,"column":15}},"8":{"start":{"line":45,"column":26},"end":{"line":45,"column":54}},"9":{"start":{"line":47,"column":2},"end":{"line":47,"column":15}},"10":{"start":{"line":50,"column":38},"end":{"line":50,"column":81}},"11":{"start":{"line":52,"column":2},"end":{"line":52,"column":15}},"12":{"start":{"line":55,"column":2},"end":{"line":55,"column":85}},"13":{"start":{"line":56,"column":2},"end":{"line":56,"column":15}},"14":{"start":{"line":57,"column":33},"end":{"line":57,"column":175}},"15":{"start":{"line":58,"column":2},"end":{"line":58,"column":15}},"16":{"start":{"line":60,"column":16},"end":{"line":60,"column":17}},"17":{"start":{"line":62,"column":17},"end":{"line":62,"column":129}},"18":{"start":{"line":63,"column":2},"end":{"line":63,"column":15}},"19":{"start":{"line":65,"column":28},"end":{"line":65,"column":55}},"20":{"start":{"line":66,"column":27},"end":{"line":66,"column":52}},"21":{"start":{"line":69,"column":2},"end":{"line":73,"column":3}},"22":{"start":{"line":71,"column":4},"end":{"line":71,"column":20}},"23":{"start":{"line":72,"column":4},"end":{"line":72,"column":19}},"24":{"start":{"line":75,"column":21},"end":{"line":75,"column":30}},"25":{"start":{"line":76,"column":23},"end":{"line":76,"column":36}},"26":{"start":{"line":77,"column":20},"end":{"line":77,"column":32}},"27":{"start":{"line":80,"column":15},"end":{"line":80,"column":37}},"28":{"start":{"line":81,"column":2},"end":{"line":99,"column":3}},"29":{"start":{"line":82,"column":21},"end":{"line":82,"column":75}},"30":{"start":{"line":83,"column":22},"end":{"line":83,"column":78}},"31":{"start":{"line":84,"column":4},"end":{"line":84,"column":62}},"32":{"start":{"line":86,"column":4},"end":{"line":86,"column":52}},"33":{"start":{"line":88,"column":4},"end":{"line":90,"column":5}},"34":{"start":{"line":89,"column":6},"end":{"line":89,"column":21}},"35":{"start":{"line":91,"column":4},"end":{"line":91,"column":120}},"36":{"start":{"line":93,"column":4},"end":{"line":93,"column":31}},"37":{"start":{"line":95,"column":4},"end":{"line":95,"column":22}},"38":{"start":{"line":97,"column":4},"end":{"line":97,"column":37}},"39":{"start":{"line":98,"column":4},"end":{"line":98,"column":108}},"40":{"start":{"line":103,"column":2},"end":{"line":103,"column":29}},"41":{"start":{"line":104,"column":2},"end":{"line":104,"column":32}},"42":{"start":{"line":105,"column":2},"end":{"line":105,"column":50}},"43":{"start":{"line":106,"column":2},"end":{"line":106,"column":45}},"44":{"start":{"line":107,"column":2},"end":{"line":107,"column":33}},"45":{"start":{"line":109,"column":2},"end":{"line":109,"column":18}},"46":{"start":{"line":110,"column":2},"end":{"line":110,"column":23}}},"fnMap":{"0":{"name":"compress","decl":{"start":{"line":25,"column":30},"end":{"line":25,"column":38}},"loc":{"start":{"line":25,"column":55},"end":{"line":111,"column":1}},"line":25},"1":{"name":"incProgress","decl":{"start":{"line":28,"column":11},"end":{"line":28,"column":22}},"loc":{"start":{"line":28,"column":33},"end":{"line":31,"column":3}},"line":28},"2":{"name":"setProgress","decl":{"start":{"line":33,"column":11},"end":{"line":33,"column":22}},"loc":{"start":{"line":33,"column":27},"end":{"line":36,"column":3}},"line":33}},"branchMap":{"0":{"loc":{"start":{"line":28,"column":24},"end":{"line":28,"column":31}},"type":"default-arg","locations":[{"start":{"line":28,"column":30},"end":{"line":28,"column":31}}],"line":28},"1":{"loc":{"start":{"line":38,"column":24},"end":{"line":38,"column":50}},"type":"binary-expr","locations":[{"start":{"line":38,"column":24},"end":{"line":38,"column":44}},{"start":{"line":38,"column":48},"end":{"line":38,"column":50}}],"line":38},"2":{"loc":{"start":{"line":55,"column":28},"end":{"line":55,"column":85}},"type":"binary-expr","locations":[{"start":{"line":55,"column":28},"end":{"line":55,"column":51}},{"start":{"line":55,"column":55},"end":{"line":55,"column":85}}],"line":55},"3":{"loc":{"start":{"line":57,"column":33},"end":{"line":57,"column":175}},"type":"cond-expr","locations":[{"start":{"line":57,"column":70},"end":{"line":57,"column":97}},{"start":{"line":57,"column":100},"end":{"line":57,"column":175}}],"line":57},"4":{"loc":{"start":{"line":62,"column":60},"end":{"line":62,"column":89}},"type":"binary-expr","locations":[{"start":{"line":62,"column":60},"end":{"line":62,"column":76}},{"start":{"line":62,"column":80},"end":{"line":62,"column":89}}],"line":62},"5":{"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},"6":{"loc":{"start":{"line":69,"column":6},"end":{"line":69,"column":45}},"type":"binary-expr","locations":[{"start":{"line":69,"column":6},"end":{"line":69,"column":24}},{"start":{"line":69,"column":28},"end":{"line":69,"column":45}}],"line":69},"7":{"loc":{"start":{"line":81,"column":9},"end":{"line":81,"column":85}},"type":"binary-expr","locations":[{"start":{"line":81,"column":9},"end":{"line":81,"column":26}},{"start":{"line":81,"column":31},"end":{"line":81,"column":56}},{"start":{"line":81,"column":60},"end":{"line":81,"column":84}}],"line":81},"8":{"loc":{"start":{"line":82,"column":21},"end":{"line":82,"column":75}},"type":"cond-expr","locations":[{"start":{"line":82,"column":41},"end":{"line":82,"column":60}},{"start":{"line":82,"column":63},"end":{"line":82,"column":75}}],"line":82},"9":{"loc":{"start":{"line":83,"column":22},"end":{"line":83,"column":78}},"type":"cond-expr","locations":[{"start":{"line":83,"column":42},"end":{"line":83,"column":62}},{"start":{"line":83,"column":65},"end":{"line":83,"column":78}}],"line":83},"10":{"loc":{"start":{"line":88,"column":4},"end":{"line":90,"column":5}},"type":"if","locations":[{"start":{"line":88,"column":4},"end":{"line":90,"column":5}},{"start":{"line":88,"column":4},"end":{"line":90,"column":5}}],"line":88},"11":{"loc":{"start":{"line":91,"column":51},"end":{"line":91,"column":80}},"type":"binary-expr","locations":[{"start":{"line":91,"column":51},"end":{"line":91,"column":67}},{"start":{"line":91,"column":71},"end":{"line":91,"column":80}}],"line":91}},"s":{"0":6,"1":36,"2":36,"3":15,"4":15,"5":6,"6":6,"7":6,"8":6,"9":6,"10":6,"11":6,"12":6,"13":6,"14":6,"15":6,"16":6,"17":6,"18":6,"19":6,"20":6,"21":6,"22":4,"23":4,"24":2,"25":2,"26":2,"27":2,"28":2,"29":9,"30":9,"31":9,"32":9,"33":9,"34":2,"35":9,"36":9,"37":9,"38":9,"39":9,"40":2,"41":2,"42":2,"43":2,"44":2,"45":2,"46":2},"f":{"0":6,"1":36,"2":15},"b":{"0":[36],"1":[6,6],"2":[6,0],"3":[0,6],"4":[6,6],"5":[4,2],"6":[6,4],"7":[11,11,2],"8":[9,0],"9":[9,0],"10":[2,7],"11":[9,9]},"_coverageSchema":"1a1c01bbd47fc00a2c39e90264f33305004495a9","hash":"0ff1ed23ebafe2dbed2d60e775b2c3f44aa78594","contentHash":"83b82e7b3e9015c70c45419fa357b823235f645bb9fe549dcfd4cbe5f0b8fc0d"} -,"D:\\Projects\\browser-image-compression\\lib\\index.js": {"path":"D:\\Projects\\browser-image-compression\\lib\\index.js","statementMap":{"0":{"start":{"line":35,"column":2},"end":{"line":35,"column":67}},"1":{"start":{"line":36,"column":23},"end":{"line":36,"column":94}},"2":{"start":{"line":37,"column":2},"end":{"line":37,"column":29}},"3":{"start":{"line":38,"column":2},"end":{"line":40,"column":3}},"4":{"start":{"line":39,"column":4},"end":{"line":39,"column":33}},"5":{"start":{"line":42,"column":2},"end":{"line":46,"column":3}},"6":{"start":{"line":43,"column":4},"end":{"line":43,"column":72}},"7":{"start":{"line":44,"column":9},"end":{"line":46,"column":3}},"8":{"start":{"line":45,"column":4},"end":{"line":45,"column":53}},"9":{"start":{"line":49,"column":22},"end":{"line":49,"column":99}},"10":{"start":{"line":57,"column":2},"end":{"line":70,"column":3}},"11":{"start":{"line":58,"column":4},"end":{"line":66,"column":5}},"12":{"start":{"line":61,"column":6},"end":{"line":61,"column":63}},"13":{"start":{"line":65,"column":6},"end":{"line":65,"column":52}},"14":{"start":{"line":69,"column":4},"end":{"line":69,"column":50}},"15":{"start":{"line":72,"column":2},"end":{"line":75,"column":16}},"16":{"start":{"line":73,"column":4},"end":{"line":73,"column":35}},"17":{"start":{"line":74,"column":4},"end":{"line":74,"column":51}},"18":{"start":{"line":77,"column":2},"end":{"line":77,"column":23}},"19":{"start":{"line":80,"column":0},"end":{"line":80,"column":56}},"20":{"start":{"line":81,"column":0},"end":{"line":81,"column":56}},"21":{"start":{"line":82,"column":0},"end":{"line":82,"column":38}},"22":{"start":{"line":83,"column":0},"end":{"line":83,"column":54}},"23":{"start":{"line":84,"column":0},"end":{"line":84,"column":52}},"24":{"start":{"line":85,"column":0},"end":{"line":85,"column":44}},"25":{"start":{"line":86,"column":0},"end":{"line":86,"column":56}},"26":{"start":{"line":88,"column":0},"end":{"line":88,"column":64}},"27":{"start":{"line":89,"column":0},"end":{"line":89,"column":62}},"28":{"start":{"line":90,"column":0},"end":{"line":90,"column":52}},"29":{"start":{"line":91,"column":0},"end":{"line":91,"column":35}}},"fnMap":{"0":{"name":"imageCompression","decl":{"start":{"line":31,"column":15},"end":{"line":31,"column":31}},"loc":{"start":{"line":31,"column":48},"end":{"line":78,"column":1}},"line":31},"1":{"name":"(anonymous_1)","decl":{"start":{"line":39,"column":25},"end":{"line":39,"column":26}},"loc":{"start":{"line":39,"column":31},"end":{"line":39,"column":33}},"line":39}},"branchMap":{"0":{"loc":{"start":{"line":35,"column":22},"end":{"line":35,"column":67}},"type":"binary-expr","locations":[{"start":{"line":35,"column":22},"end":{"line":35,"column":39}},{"start":{"line":35,"column":43},"end":{"line":35,"column":67}}],"line":35},"1":{"loc":{"start":{"line":36,"column":23},"end":{"line":36,"column":94}},"type":"cond-expr","locations":[{"start":{"line":36,"column":67},"end":{"line":36,"column":87}},{"start":{"line":36,"column":90},"end":{"line":36,"column":94}}],"line":36},"2":{"loc":{"start":{"line":38,"column":2},"end":{"line":40,"column":3}},"type":"if","locations":[{"start":{"line":38,"column":2},"end":{"line":40,"column":3}},{"start":{"line":38,"column":2},"end":{"line":40,"column":3}}],"line":38},"3":{"loc":{"start":{"line":42,"column":2},"end":{"line":46,"column":3}},"type":"if","locations":[{"start":{"line":42,"column":2},"end":{"line":46,"column":3}},{"start":{"line":42,"column":2},"end":{"line":46,"column":3}}],"line":42},"4":{"loc":{"start":{"line":42,"column":8},"end":{"line":42,"column":58}},"type":"binary-expr","locations":[{"start":{"line":42,"column":8},"end":{"line":42,"column":28}},{"start":{"line":42,"column":32},"end":{"line":42,"column":58}}],"line":42},"5":{"loc":{"start":{"line":44,"column":9},"end":{"line":46,"column":3}},"type":"if","locations":[{"start":{"line":44,"column":9},"end":{"line":46,"column":3}},{"start":{"line":44,"column":9},"end":{"line":46,"column":3}}],"line":44},"6":{"loc":{"start":{"line":49,"column":22},"end":{"line":49,"column":99}},"type":"binary-expr","locations":[{"start":{"line":49,"column":22},"end":{"line":49,"column":62}},{"start":{"line":49,"column":66},"end":{"line":49,"column":99}}],"line":49},"7":{"loc":{"start":{"line":57,"column":2},"end":{"line":70,"column":3}},"type":"if","locations":[{"start":{"line":57,"column":2},"end":{"line":70,"column":3}},{"start":{"line":57,"column":2},"end":{"line":70,"column":3}}],"line":57},"8":{"loc":{"start":{"line":57,"column":6},"end":{"line":57,"column":66}},"type":"binary-expr","locations":[{"start":{"line":57,"column":6},"end":{"line":57,"column":18}},{"start":{"line":57,"column":22},"end":{"line":57,"column":50}},{"start":{"line":57,"column":54},"end":{"line":57,"column":66}}],"line":57}},"s":{"0":9,"1":9,"2":9,"3":9,"4":9,"5":9,"6":2,"7":7,"8":1,"9":6,"10":6,"11":0,"12":0,"13":0,"14":6,"15":6,"16":6,"17":6,"18":6,"19":1,"20":1,"21":1,"22":1,"23":1,"24":1,"25":1,"26":1,"27":1,"28":1,"29":1},"f":{"0":9,"1":51},"b":{"0":[9,2],"1":[9,0],"2":[9,0],"3":[2,7],"4":[9,2],"5":[1,6],"6":[6,0],"7":[0,6],"8":[6,0,0]},"_coverageSchema":"1a1c01bbd47fc00a2c39e90264f33305004495a9","hash":"35e505470aaecc39feb4d81c5f2074412811d7bb","contentHash":"2cf6b61c60368ddf0f76c8c6808b0d723d7e5706074d9755fdf2dbbcc123960e"} -,"D:\\Projects\\browser-image-compression\\lib\\utils.js": {"path":"D:\\Projects\\browser-image-compression\\lib\\utils.js","statementMap":{"0":{"start":{"line":1,"column":18},"end":{"line":1,"column":47}},"1":{"start":{"line":4,"column":21},"end":{"line":4,"column":124}},"2":{"start":{"line":5,"column":26},"end":{"line":5,"column":113}},"3":{"start":{"line":6,"column":32},"end":{"line":6,"column":131}},"4":{"start":{"line":10,"column":42},"end":{"line":30,"column":4}},"5":{"start":{"line":14,"column":4},"end":{"line":19,"column":63}},"6":{"start":{"line":20,"column":24},"end":{"line":20,"column":86}},"7":{"start":{"line":22,"column":26},"end":{"line":22,"column":68}},"8":{"start":{"line":23,"column":25},"end":{"line":23,"column":128}},"9":{"start":{"line":24,"column":2},"end":{"line":24,"column":38}},"10":{"start":{"line":25,"column":24},"end":{"line":25,"column":64}},"11":{"start":{"line":26,"column":14},"end":{"line":26,"column":44}},"12":{"start":{"line":29,"column":2},"end":{"line":29,"column":44}},"13":{"start":{"line":39,"column":2},"end":{"line":44,"column":4}},"14":{"start":{"line":40,"column":19},"end":{"line":40,"column":41}},"15":{"start":{"line":41,"column":4},"end":{"line":41,"column":48}},"16":{"start":{"line":41,"column":26},"end":{"line":41,"column":48}},"17":{"start":{"line":42,"column":4},"end":{"line":42,"column":37}},"18":{"start":{"line":42,"column":28},"end":{"line":42,"column":37}},"19":{"start":{"line":43,"column":4},"end":{"line":43,"column":30}},"20":{"start":{"line":56,"column":2},"end":{"line":82,"column":4}},"21":{"start":{"line":57,"column":16},"end":{"line":57,"column":34}},"22":{"start":{"line":58,"column":17},"end":{"line":58,"column":43}},"23":{"start":{"line":59,"column":17},"end":{"line":59,"column":29}},"24":{"start":{"line":60,"column":12},"end":{"line":60,"column":23}},"25":{"start":{"line":61,"column":18},"end":{"line":61,"column":35}},"26":{"start":{"line":62,"column":4},"end":{"line":64,"column":5}},"27":{"start":{"line":63,"column":6},"end":{"line":63,"column":35}},"28":{"start":{"line":65,"column":17},"end":{"line":65,"column":50}},"29":{"start":{"line":66,"column":4},"end":{"line":66,"column":24}},"30":{"start":{"line":67,"column":4},"end":{"line":67,"column":36}},"31":{"start":{"line":68,"column":4},"end":{"line":68,"column":17}},"32":{"start":{"line":92,"column":2},"end":{"line":97,"column":4}},"33":{"start":{"line":93,"column":16},"end":{"line":93,"column":27}},"34":{"start":{"line":94,"column":4},"end":{"line":94,"column":35}},"35":{"start":{"line":94,"column":23},"end":{"line":94,"column":35}},"36":{"start":{"line":95,"column":4},"end":{"line":95,"column":34}},"37":{"start":{"line":95,"column":25},"end":{"line":95,"column":34}},"38":{"start":{"line":96,"column":4},"end":{"line":96,"column":17}},"39":{"start":{"line":107,"column":24},"end":{"line":107,"column":65}},"40":{"start":{"line":108,"column":2},"end":{"line":108,"column":55}},"41":{"start":{"line":109,"column":2},"end":{"line":109,"column":15}},"42":{"start":{"line":120,"column":2},"end":{"line":125,"column":3}},"43":{"start":{"line":121,"column":4},"end":{"line":121,"column":39}},"44":{"start":{"line":123,"column":20},"end":{"line":123,"column":50}},"45":{"start":{"line":124,"column":4},"end":{"line":124,"column":34}},"46":{"start":{"line":126,"column":17},"end":{"line":126,"column":39}},"47":{"start":{"line":127,"column":2},"end":{"line":127,"column":22}},"48":{"start":{"line":142,"column":2},"end":{"line":149,"column":3}},"49":{"start":{"line":143,"column":4},"end":{"line":143,"column":66}},"50":{"start":{"line":144,"column":4},"end":{"line":144,"column":24}},"51":{"start":{"line":145,"column":4},"end":{"line":145,"column":40}},"52":{"start":{"line":147,"column":20},"end":{"line":147,"column":55}},"53":{"start":{"line":148,"column":4},"end":{"line":148,"column":72}},"54":{"start":{"line":150,"column":2},"end":{"line":150,"column":13}},"55":{"start":{"line":162,"column":2},"end":{"line":199,"column":4}},"56":{"start":{"line":163,"column":19},"end":{"line":163,"column":41}},"57":{"start":{"line":164,"column":4},"end":{"line":196,"column":5}},"58":{"start":{"line":165,"column":19},"end":{"line":165,"column":48}},"59":{"start":{"line":166,"column":6},"end":{"line":168,"column":7}},"60":{"start":{"line":167,"column":8},"end":{"line":167,"column":26}},"61":{"start":{"line":169,"column":21},"end":{"line":169,"column":36}},"62":{"start":{"line":170,"column":19},"end":{"line":170,"column":20}},"63":{"start":{"line":171,"column":6},"end":{"line":194,"column":7}},"64":{"start":{"line":172,"column":8},"end":{"line":172,"column":70}},"65":{"start":{"line":172,"column":52},"end":{"line":172,"column":70}},"66":{"start":{"line":173,"column":23},"end":{"line":173,"column":52}},"67":{"start":{"line":174,"column":8},"end":{"line":174,"column":19}},"68":{"start":{"line":175,"column":8},"end":{"line":193,"column":9}},"69":{"start":{"line":176,"column":10},"end":{"line":178,"column":11}},"70":{"start":{"line":177,"column":12},"end":{"line":177,"column":30}},"71":{"start":{"line":180,"column":23},"end":{"line":180,"column":67}},"72":{"start":{"line":181,"column":10},"end":{"line":181,"column":54}},"73":{"start":{"line":182,"column":21},"end":{"line":182,"column":51}},"74":{"start":{"line":183,"column":10},"end":{"line":183,"column":21}},"75":{"start":{"line":184,"column":10},"end":{"line":188,"column":11}},"76":{"start":{"line":184,"column":23},"end":{"line":184,"column":24}},"77":{"start":{"line":185,"column":12},"end":{"line":187,"column":13}},"78":{"start":{"line":186,"column":14},"end":{"line":186,"column":75}},"79":{"start":{"line":189,"column":15},"end":{"line":193,"column":9}},"80":{"start":{"line":190,"column":10},"end":{"line":190,"column":15}},"81":{"start":{"line":192,"column":10},"end":{"line":192,"column":49}},"82":{"start":{"line":195,"column":6},"end":{"line":195,"column":24}},"83":{"start":{"line":197,"column":4},"end":{"line":197,"column":37}},"84":{"start":{"line":197,"column":28},"end":{"line":197,"column":37}},"85":{"start":{"line":198,"column":4},"end":{"line":198,"column":34}},"86":{"start":{"line":209,"column":16},"end":{"line":209,"column":28}},"87":{"start":{"line":210,"column":17},"end":{"line":210,"column":30}},"88":{"start":{"line":211,"column":27},"end":{"line":211,"column":51}},"89":{"start":{"line":213,"column":23},"end":{"line":213,"column":115}},"90":{"start":{"line":215,"column":18},"end":{"line":215,"column":24}},"91":{"start":{"line":218,"column":2},"end":{"line":230,"column":3}},"92":{"start":{"line":219,"column":4},"end":{"line":219,"column":56}},"93":{"start":{"line":220,"column":4},"end":{"line":226,"column":5}},"94":{"start":{"line":221,"column":6},"end":{"line":221,"column":40}},"95":{"start":{"line":222,"column":6},"end":{"line":222,"column":60}},"96":{"start":{"line":224,"column":6},"end":{"line":224,"column":59}},"97":{"start":{"line":225,"column":6},"end":{"line":225,"column":41}},"98":{"start":{"line":227,"column":4},"end":{"line":227,"column":66}},"99":{"start":{"line":229,"column":4},"end":{"line":229,"column":31}},"100":{"start":{"line":232,"column":2},"end":{"line":232,"column":18}},"101":{"start":{"line":244,"column":16},"end":{"line":244,"column":28}},"102":{"start":{"line":245,"column":17},"end":{"line":245,"column":30}},"103":{"start":{"line":247,"column":27},"end":{"line":247,"column":60}},"104":{"start":{"line":250,"column":2},"end":{"line":256,"column":3}},"105":{"start":{"line":251,"column":4},"end":{"line":251,"column":28}},"106":{"start":{"line":252,"column":4},"end":{"line":252,"column":28}},"107":{"start":{"line":254,"column":4},"end":{"line":254,"column":27}},"108":{"start":{"line":255,"column":4},"end":{"line":255,"column":29}},"109":{"start":{"line":259,"column":2},"end":{"line":268,"column":3}},"110":{"start":{"line":260,"column":12},"end":{"line":260,"column":49}},"111":{"start":{"line":260,"column":50},"end":{"line":260,"column":56}},"112":{"start":{"line":261,"column":12},"end":{"line":261,"column":55}},"113":{"start":{"line":261,"column":56},"end":{"line":261,"column":62}},"114":{"start":{"line":262,"column":12},"end":{"line":262,"column":50}},"115":{"start":{"line":262,"column":51},"end":{"line":262,"column":57}},"116":{"start":{"line":263,"column":12},"end":{"line":263,"column":44}},"117":{"start":{"line":263,"column":45},"end":{"line":263,"column":51}},"118":{"start":{"line":264,"column":12},"end":{"line":264,"column":50}},"119":{"start":{"line":264,"column":51},"end":{"line":264,"column":57}},"120":{"start":{"line":265,"column":12},"end":{"line":265,"column":55}},"121":{"start":{"line":265,"column":56},"end":{"line":265,"column":62}},"122":{"start":{"line":266,"column":12},"end":{"line":266,"column":49}},"123":{"start":{"line":266,"column":50},"end":{"line":266,"column":56}},"124":{"start":{"line":267,"column":13},"end":{"line":267,"column":19}},"125":{"start":{"line":270,"column":2},"end":{"line":270,"column":44}},"126":{"start":{"line":272,"column":2},"end":{"line":272,"column":29}},"127":{"start":{"line":274,"column":2},"end":{"line":274,"column":18}},"128":{"start":{"line":286,"column":2},"end":{"line":295,"column":3}},"129":{"start":{"line":287,"column":4},"end":{"line":287,"column":47}},"130":{"start":{"line":288,"column":4},"end":{"line":288,"column":33}},"131":{"start":{"line":289,"column":4},"end":{"line":291,"column":5}},"132":{"start":{"line":290,"column":6},"end":{"line":290,"column":67}},"133":{"start":{"line":293,"column":4},"end":{"line":293,"column":45}},"134":{"start":{"line":294,"column":4},"end":{"line":294,"column":33}},"135":{"start":{"line":296,"column":2},"end":{"line":296,"column":22}},"136":{"start":{"line":297,"column":2},"end":{"line":297,"column":24}},"137":{"start":{"line":298,"column":2},"end":{"line":298,"column":22}},"138":{"start":{"line":307,"column":2},"end":{"line":307,"column":18}},"139":{"start":{"line":308,"column":2},"end":{"line":308,"column":19}},"140":{"start":{"line":311,"column":0},"end":{"line":318,"column":1}},"141":{"start":{"line":313,"column":2},"end":{"line":317,"column":3}},"142":{"start":{"line":314,"column":4},"end":{"line":316,"column":33}}},"fnMap":{"0":{"name":"(anonymous_0)","decl":{"start":{"line":10,"column":43},"end":{"line":10,"column":44}},"loc":{"start":{"line":10,"column":55},"end":{"line":30,"column":1}},"line":10},"1":{"name":"getDataUrlFromFile","decl":{"start":{"line":38,"column":16},"end":{"line":38,"column":34}},"loc":{"start":{"line":38,"column":42},"end":{"line":45,"column":1}},"line":38},"2":{"name":"(anonymous_2)","decl":{"start":{"line":39,"column":21},"end":{"line":39,"column":22}},"loc":{"start":{"line":39,"column":42},"end":{"line":44,"column":3}},"line":39},"3":{"name":"(anonymous_3)","decl":{"start":{"line":41,"column":20},"end":{"line":41,"column":21}},"loc":{"start":{"line":41,"column":26},"end":{"line":41,"column":48}},"line":41},"4":{"name":"(anonymous_4)","decl":{"start":{"line":42,"column":21},"end":{"line":42,"column":22}},"loc":{"start":{"line":42,"column":28},"end":{"line":42,"column":37}},"line":42},"5":{"name":"getFilefromDataUrl","decl":{"start":{"line":55,"column":16},"end":{"line":55,"column":34}},"loc":{"start":{"line":55,"column":82},"end":{"line":83,"column":1}},"line":55},"6":{"name":"(anonymous_6)","decl":{"start":{"line":56,"column":21},"end":{"line":56,"column":22}},"loc":{"start":{"line":56,"column":34},"end":{"line":82,"column":3}},"line":56},"7":{"name":"loadImage","decl":{"start":{"line":91,"column":16},"end":{"line":91,"column":25}},"loc":{"start":{"line":91,"column":32},"end":{"line":98,"column":1}},"line":91},"8":{"name":"(anonymous_8)","decl":{"start":{"line":92,"column":21},"end":{"line":92,"column":22}},"loc":{"start":{"line":92,"column":42},"end":{"line":97,"column":3}},"line":92},"9":{"name":"(anonymous_9)","decl":{"start":{"line":94,"column":17},"end":{"line":94,"column":18}},"loc":{"start":{"line":94,"column":23},"end":{"line":94,"column":35}},"line":94},"10":{"name":"(anonymous_10)","decl":{"start":{"line":95,"column":18},"end":{"line":95,"column":19}},"loc":{"start":{"line":95,"column":25},"end":{"line":95,"column":34}},"line":95},"11":{"name":"drawImageInCanvas","decl":{"start":{"line":106,"column":16},"end":{"line":106,"column":33}},"loc":{"start":{"line":106,"column":40},"end":{"line":110,"column":1}},"line":106},"12":{"name":"drawFileInCanvas","decl":{"start":{"line":118,"column":22},"end":{"line":118,"column":38}},"loc":{"start":{"line":118,"column":46},"end":{"line":128,"column":1}},"line":118},"13":{"name":"canvasToFile","decl":{"start":{"line":140,"column":22},"end":{"line":140,"column":34}},"loc":{"start":{"line":140,"column":95},"end":{"line":151,"column":1}},"line":140},"14":{"name":"getExifOrientation","decl":{"start":{"line":161,"column":16},"end":{"line":161,"column":34}},"loc":{"start":{"line":161,"column":42},"end":{"line":200,"column":1}},"line":161},"15":{"name":"(anonymous_15)","decl":{"start":{"line":162,"column":21},"end":{"line":162,"column":22}},"loc":{"start":{"line":162,"column":42},"end":{"line":199,"column":3}},"line":162},"16":{"name":"(anonymous_16)","decl":{"start":{"line":164,"column":20},"end":{"line":164,"column":21}},"loc":{"start":{"line":164,"column":27},"end":{"line":196,"column":5}},"line":164},"17":{"name":"(anonymous_17)","decl":{"start":{"line":197,"column":21},"end":{"line":197,"column":22}},"loc":{"start":{"line":197,"column":28},"end":{"line":197,"column":37}},"line":197},"18":{"name":"handleMaxWidthOrHeight","decl":{"start":{"line":208,"column":16},"end":{"line":208,"column":38}},"loc":{"start":{"line":208,"column":57},"end":{"line":233,"column":1}},"line":208},"19":{"name":"followExifOrientation","decl":{"start":{"line":243,"column":16},"end":{"line":243,"column":37}},"loc":{"start":{"line":243,"column":64},"end":{"line":275,"column":1}},"line":243},"20":{"name":"getNewCanvasAndCtx","decl":{"start":{"line":283,"column":16},"end":{"line":283,"column":34}},"loc":{"start":{"line":283,"column":51},"end":{"line":299,"column":1}},"line":283},"21":{"name":"cleanupCanvasMemory","decl":{"start":{"line":306,"column":16},"end":{"line":306,"column":35}},"loc":{"start":{"line":306,"column":45},"end":{"line":309,"column":1}},"line":306},"22":{"name":"(anonymous_22)","decl":{"start":{"line":313,"column":41},"end":{"line":313,"column":42}},"loc":{"start":{"line":313,"column":58},"end":{"line":317,"column":3}},"line":313}},"branchMap":{"0":{"loc":{"start":{"line":4,"column":21},"end":{"line":4,"column":124}},"type":"binary-expr","locations":[{"start":{"line":4,"column":21},"end":{"line":4,"column":30}},{"start":{"line":4,"column":34},"end":{"line":4,"column":48}},{"start":{"line":4,"column":52},"end":{"line":4,"column":74}},{"start":{"line":4,"column":78},"end":{"line":4,"column":124}}],"line":4},"1":{"loc":{"start":{"line":5,"column":26},"end":{"line":5,"column":113}},"type":"binary-expr","locations":[{"start":{"line":5,"column":26},"end":{"line":5,"column":35}},{"start":{"line":5,"column":41},"end":{"line":5,"column":53}},{"start":{"line":5,"column":57},"end":{"line":5,"column":103}},{"start":{"line":5,"column":108},"end":{"line":5,"column":112}}],"line":5},"2":{"loc":{"start":{"line":6,"column":32},"end":{"line":6,"column":131}},"type":"binary-expr","locations":[{"start":{"line":6,"column":32},"end":{"line":6,"column":41}},{"start":{"line":6,"column":47},"end":{"line":6,"column":59}},{"start":{"line":6,"column":63},"end":{"line":6,"column":115}},{"start":{"line":6,"column":120},"end":{"line":6,"column":130}}],"line":6},"3":{"loc":{"start":{"line":29,"column":9},"end":{"line":29,"column":44}},"type":"binary-expr","locations":[{"start":{"line":29,"column":9},"end":{"line":29,"column":24}},{"start":{"line":29,"column":28},"end":{"line":29,"column":44}}],"line":29},"4":{"loc":{"start":{"line":55,"column":55},"end":{"line":55,"column":80}},"type":"default-arg","locations":[{"start":{"line":55,"column":70},"end":{"line":55,"column":80}}],"line":55},"5":{"loc":{"start":{"line":140,"column":82},"end":{"line":140,"column":93}},"type":"default-arg","locations":[{"start":{"line":140,"column":92},"end":{"line":140,"column":93}}],"line":140},"6":{"loc":{"start":{"line":142,"column":2},"end":{"line":149,"column":3}},"type":"if","locations":[{"start":{"line":142,"column":2},"end":{"line":149,"column":3}},{"start":{"line":142,"column":2},"end":{"line":149,"column":3}}],"line":142},"7":{"loc":{"start":{"line":142,"column":6},"end":{"line":142,"column":80}},"type":"binary-expr","locations":[{"start":{"line":142,"column":6},"end":{"line":142,"column":43}},{"start":{"line":142,"column":47},"end":{"line":142,"column":80}}],"line":142},"8":{"loc":{"start":{"line":166,"column":6},"end":{"line":168,"column":7}},"type":"if","locations":[{"start":{"line":166,"column":6},"end":{"line":168,"column":7}},{"start":{"line":166,"column":6},"end":{"line":168,"column":7}}],"line":166},"9":{"loc":{"start":{"line":172,"column":8},"end":{"line":172,"column":70}},"type":"if","locations":[{"start":{"line":172,"column":8},"end":{"line":172,"column":70}},{"start":{"line":172,"column":8},"end":{"line":172,"column":70}}],"line":172},"10":{"loc":{"start":{"line":175,"column":8},"end":{"line":193,"column":9}},"type":"if","locations":[{"start":{"line":175,"column":8},"end":{"line":193,"column":9}},{"start":{"line":175,"column":8},"end":{"line":193,"column":9}}],"line":175},"11":{"loc":{"start":{"line":176,"column":10},"end":{"line":178,"column":11}},"type":"if","locations":[{"start":{"line":176,"column":10},"end":{"line":178,"column":11}},{"start":{"line":176,"column":10},"end":{"line":178,"column":11}}],"line":176},"12":{"loc":{"start":{"line":185,"column":12},"end":{"line":187,"column":13}},"type":"if","locations":[{"start":{"line":185,"column":12},"end":{"line":187,"column":13}},{"start":{"line":185,"column":12},"end":{"line":187,"column":13}}],"line":185},"13":{"loc":{"start":{"line":189,"column":15},"end":{"line":193,"column":9}},"type":"if","locations":[{"start":{"line":189,"column":15},"end":{"line":193,"column":9}},{"start":{"line":189,"column":15},"end":{"line":193,"column":9}}],"line":189},"14":{"loc":{"start":{"line":213,"column":23},"end":{"line":213,"column":115}},"type":"binary-expr","locations":[{"start":{"line":213,"column":23},"end":{"line":213,"column":56}},{"start":{"line":213,"column":61},"end":{"line":213,"column":85}},{"start":{"line":213,"column":89},"end":{"line":213,"column":114}}],"line":213},"15":{"loc":{"start":{"line":218,"column":2},"end":{"line":230,"column":3}},"type":"if","locations":[{"start":{"line":218,"column":2},"end":{"line":230,"column":3}},{"start":{"line":218,"column":2},"end":{"line":230,"column":3}}],"line":218},"16":{"loc":{"start":{"line":220,"column":4},"end":{"line":226,"column":5}},"type":"if","locations":[{"start":{"line":220,"column":4},"end":{"line":226,"column":5}},{"start":{"line":220,"column":4},"end":{"line":226,"column":5}}],"line":220},"17":{"loc":{"start":{"line":250,"column":2},"end":{"line":256,"column":3}},"type":"if","locations":[{"start":{"line":250,"column":2},"end":{"line":256,"column":3}},{"start":{"line":250,"column":2},"end":{"line":256,"column":3}}],"line":250},"18":{"loc":{"start":{"line":250,"column":6},"end":{"line":250,"column":48}},"type":"binary-expr","locations":[{"start":{"line":250,"column":6},"end":{"line":250,"column":25}},{"start":{"line":250,"column":29},"end":{"line":250,"column":48}}],"line":250},"19":{"loc":{"start":{"line":259,"column":2},"end":{"line":268,"column":3}},"type":"switch","locations":[{"start":{"line":260,"column":4},"end":{"line":260,"column":56}},{"start":{"line":261,"column":4},"end":{"line":261,"column":62}},{"start":{"line":262,"column":4},"end":{"line":262,"column":57}},{"start":{"line":263,"column":4},"end":{"line":263,"column":51}},{"start":{"line":264,"column":4},"end":{"line":264,"column":57}},{"start":{"line":265,"column":4},"end":{"line":265,"column":62}},{"start":{"line":266,"column":4},"end":{"line":266,"column":56}},{"start":{"line":267,"column":4},"end":{"line":267,"column":19}}],"line":259},"20":{"loc":{"start":{"line":289,"column":4},"end":{"line":291,"column":5}},"type":"if","locations":[{"start":{"line":289,"column":4},"end":{"line":291,"column":5}},{"start":{"line":289,"column":4},"end":{"line":291,"column":5}}],"line":289},"21":{"loc":{"start":{"line":311,"column":0},"end":{"line":318,"column":1}},"type":"if","locations":[{"start":{"line":311,"column":0},"end":{"line":318,"column":1}},{"start":{"line":311,"column":0},"end":{"line":318,"column":1}}],"line":311},"22":{"loc":{"start":{"line":313,"column":21},"end":{"line":317,"column":3}},"type":"binary-expr","locations":[{"start":{"line":313,"column":21},"end":{"line":313,"column":37}},{"start":{"line":313,"column":41},"end":{"line":317,"column":3}}],"line":313},"23":{"loc":{"start":{"line":314,"column":11},"end":{"line":316,"column":33}},"type":"binary-expr","locations":[{"start":{"line":314,"column":11},"end":{"line":314,"column":36}},{"start":{"line":315,"column":6},"end":{"line":315,"column":21}},{"start":{"line":316,"column":6},"end":{"line":316,"column":33}}],"line":314}},"s":{"0":1,"1":1,"2":1,"3":1,"4":1,"5":1,"6":1,"7":1,"8":1,"9":1,"10":1,"11":1,"12":1,"13":15,"14":15,"15":15,"16":15,"17":15,"18":0,"19":15,"20":19,"21":19,"22":19,"23":19,"24":19,"25":19,"26":19,"27":24015727,"28":19,"29":19,"30":19,"31":19,"32":15,"33":15,"34":15,"35":15,"36":15,"37":0,"38":15,"39":13,"40":13,"41":13,"42":12,"43":12,"44":12,"45":12,"46":12,"47":12,"48":16,"49":0,"50":0,"51":0,"52":16,"53":16,"54":16,"55":1,"56":1,"57":1,"58":1,"59":1,"60":1,"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":0,"75":0,"76":0,"77":0,"78":0,"79":0,"80":0,"81":0,"82":0,"83":1,"84":0,"85":1,"86":6,"87":6,"88":6,"89":6,"90":6,"91":6,"92":4,"93":4,"94":2,"95":2,"96":2,"97":2,"98":4,"99":4,"100":6,"101":6,"102":6,"103":6,"104":6,"105":0,"106":0,"107":6,"108":6,"109":6,"110":0,"111":0,"112":0,"113":0,"114":0,"115":0,"116":0,"117":0,"118":0,"119":0,"120":0,"121":0,"122":0,"123":0,"124":6,"125":6,"126":6,"127":6,"128":32,"129":32,"130":0,"131":0,"132":0,"133":32,"134":32,"135":32,"136":32,"137":32,"138":30,"139":30,"140":1,"141":1,"142":0},"f":{"0":1,"1":15,"2":15,"3":15,"4":0,"5":19,"6":19,"7":15,"8":15,"9":15,"10":0,"11":13,"12":12,"13":16,"14":1,"15":1,"16":1,"17":0,"18":6,"19":6,"20":32,"21":30,"22":0},"b":{"0":[1,1,0,0],"1":[1,1,0,1],"2":[1,1,0,1],"3":[1,0],"4":[2],"5":[1],"6":[0,16],"7":[16,0],"8":[1,0],"9":[0,0],"10":[0,0],"11":[0,0],"12":[0,0],"13":[0,0],"14":[6,4,0],"15":[4,2],"16":[2,2],"17":[0,6],"18":[6,0],"19":[0,0,0,0,0,0,0,6],"20":[0,0],"21":[1,0],"22":[1,0],"23":[0,0,0]},"_coverageSchema":"1a1c01bbd47fc00a2c39e90264f33305004495a9","hash":"026902591dcdc19bd376dd16dad500b05c2b3b1a","contentHash":"772227a6e2dd4c40952b6ec0c20b929c65ef4fc807f7d9565e6dbe69309fb225"} +,"D:\\Projects\\browser-image-compression\\lib\\index.js": {"path":"D:\\Projects\\browser-image-compression\\lib\\index.js","statementMap":{"0":{"start":{"line":35,"column":2},"end":{"line":35,"column":67}},"1":{"start":{"line":36,"column":23},"end":{"line":36,"column":94}},"2":{"start":{"line":37,"column":2},"end":{"line":37,"column":29}},"3":{"start":{"line":38,"column":2},"end":{"line":40,"column":3}},"4":{"start":{"line":39,"column":4},"end":{"line":39,"column":33}},"5":{"start":{"line":42,"column":2},"end":{"line":46,"column":3}},"6":{"start":{"line":43,"column":4},"end":{"line":43,"column":72}},"7":{"start":{"line":44,"column":9},"end":{"line":46,"column":3}},"8":{"start":{"line":45,"column":4},"end":{"line":45,"column":53}},"9":{"start":{"line":49,"column":22},"end":{"line":49,"column":99}},"10":{"start":{"line":57,"column":2},"end":{"line":70,"column":3}},"11":{"start":{"line":58,"column":4},"end":{"line":66,"column":5}},"12":{"start":{"line":61,"column":6},"end":{"line":61,"column":63}},"13":{"start":{"line":65,"column":6},"end":{"line":65,"column":52}},"14":{"start":{"line":69,"column":4},"end":{"line":69,"column":50}},"15":{"start":{"line":72,"column":2},"end":{"line":75,"column":16}},"16":{"start":{"line":73,"column":4},"end":{"line":73,"column":35}},"17":{"start":{"line":74,"column":4},"end":{"line":74,"column":51}},"18":{"start":{"line":77,"column":2},"end":{"line":77,"column":23}},"19":{"start":{"line":80,"column":0},"end":{"line":80,"column":56}},"20":{"start":{"line":81,"column":0},"end":{"line":81,"column":56}},"21":{"start":{"line":82,"column":0},"end":{"line":82,"column":38}},"22":{"start":{"line":83,"column":0},"end":{"line":83,"column":54}},"23":{"start":{"line":84,"column":0},"end":{"line":84,"column":52}},"24":{"start":{"line":85,"column":0},"end":{"line":85,"column":44}},"25":{"start":{"line":86,"column":0},"end":{"line":86,"column":56}},"26":{"start":{"line":88,"column":0},"end":{"line":88,"column":64}},"27":{"start":{"line":89,"column":0},"end":{"line":89,"column":62}},"28":{"start":{"line":90,"column":0},"end":{"line":90,"column":52}},"29":{"start":{"line":91,"column":0},"end":{"line":91,"column":35}}},"fnMap":{"0":{"name":"imageCompression","decl":{"start":{"line":31,"column":15},"end":{"line":31,"column":31}},"loc":{"start":{"line":31,"column":48},"end":{"line":78,"column":1}},"line":31},"1":{"name":"(anonymous_1)","decl":{"start":{"line":39,"column":25},"end":{"line":39,"column":26}},"loc":{"start":{"line":39,"column":31},"end":{"line":39,"column":33}},"line":39}},"branchMap":{"0":{"loc":{"start":{"line":35,"column":22},"end":{"line":35,"column":67}},"type":"binary-expr","locations":[{"start":{"line":35,"column":22},"end":{"line":35,"column":39}},{"start":{"line":35,"column":43},"end":{"line":35,"column":67}}],"line":35},"1":{"loc":{"start":{"line":36,"column":23},"end":{"line":36,"column":94}},"type":"cond-expr","locations":[{"start":{"line":36,"column":67},"end":{"line":36,"column":87}},{"start":{"line":36,"column":90},"end":{"line":36,"column":94}}],"line":36},"2":{"loc":{"start":{"line":38,"column":2},"end":{"line":40,"column":3}},"type":"if","locations":[{"start":{"line":38,"column":2},"end":{"line":40,"column":3}},{"start":{"line":38,"column":2},"end":{"line":40,"column":3}}],"line":38},"3":{"loc":{"start":{"line":42,"column":2},"end":{"line":46,"column":3}},"type":"if","locations":[{"start":{"line":42,"column":2},"end":{"line":46,"column":3}},{"start":{"line":42,"column":2},"end":{"line":46,"column":3}}],"line":42},"4":{"loc":{"start":{"line":42,"column":8},"end":{"line":42,"column":58}},"type":"binary-expr","locations":[{"start":{"line":42,"column":8},"end":{"line":42,"column":28}},{"start":{"line":42,"column":32},"end":{"line":42,"column":58}}],"line":42},"5":{"loc":{"start":{"line":44,"column":9},"end":{"line":46,"column":3}},"type":"if","locations":[{"start":{"line":44,"column":9},"end":{"line":46,"column":3}},{"start":{"line":44,"column":9},"end":{"line":46,"column":3}}],"line":44},"6":{"loc":{"start":{"line":49,"column":22},"end":{"line":49,"column":99}},"type":"binary-expr","locations":[{"start":{"line":49,"column":22},"end":{"line":49,"column":62}},{"start":{"line":49,"column":66},"end":{"line":49,"column":99}}],"line":49},"7":{"loc":{"start":{"line":57,"column":2},"end":{"line":70,"column":3}},"type":"if","locations":[{"start":{"line":57,"column":2},"end":{"line":70,"column":3}},{"start":{"line":57,"column":2},"end":{"line":70,"column":3}}],"line":57},"8":{"loc":{"start":{"line":57,"column":6},"end":{"line":57,"column":66}},"type":"binary-expr","locations":[{"start":{"line":57,"column":6},"end":{"line":57,"column":18}},{"start":{"line":57,"column":22},"end":{"line":57,"column":50}},{"start":{"line":57,"column":54},"end":{"line":57,"column":66}}],"line":57}},"s":{"0":9,"1":9,"2":9,"3":9,"4":9,"5":9,"6":2,"7":7,"8":1,"9":6,"10":6,"11":0,"12":0,"13":0,"14":6,"15":6,"16":6,"17":6,"18":6,"19":1,"20":1,"21":1,"22":1,"23":1,"24":1,"25":1,"26":1,"27":1,"28":1,"29":1},"f":{"0":9,"1":51},"b":{"0":[9,2],"1":[9,0],"2":[9,0],"3":[2,7],"4":[9,2],"5":[1,6],"6":[6,0],"7":[0,6],"8":[6,0,0]},"_coverageSchema":"1a1c01bbd47fc00a2c39e90264f33305004495a9","hash":"35e505470aaecc39feb4d81c5f2074412811d7bb","contentHash":"8d76e36ba9bd6a29fafd08cf6150584b38bd19801931f6791435e95349a1b33c"} +,"D:\\Projects\\browser-image-compression\\lib\\utils.js": {"path":"D:\\Projects\\browser-image-compression\\lib\\utils.js","statementMap":{"0":{"start":{"line":1,"column":18},"end":{"line":1,"column":47}},"1":{"start":{"line":4,"column":21},"end":{"line":4,"column":124}},"2":{"start":{"line":5,"column":26},"end":{"line":5,"column":113}},"3":{"start":{"line":6,"column":32},"end":{"line":6,"column":131}},"4":{"start":{"line":10,"column":42},"end":{"line":30,"column":4}},"5":{"start":{"line":14,"column":4},"end":{"line":19,"column":63}},"6":{"start":{"line":20,"column":24},"end":{"line":20,"column":86}},"7":{"start":{"line":22,"column":26},"end":{"line":22,"column":68}},"8":{"start":{"line":23,"column":25},"end":{"line":23,"column":128}},"9":{"start":{"line":24,"column":2},"end":{"line":24,"column":38}},"10":{"start":{"line":25,"column":24},"end":{"line":25,"column":64}},"11":{"start":{"line":26,"column":14},"end":{"line":26,"column":44}},"12":{"start":{"line":29,"column":2},"end":{"line":29,"column":44}},"13":{"start":{"line":39,"column":2},"end":{"line":44,"column":4}},"14":{"start":{"line":40,"column":19},"end":{"line":40,"column":41}},"15":{"start":{"line":41,"column":4},"end":{"line":41,"column":48}},"16":{"start":{"line":41,"column":26},"end":{"line":41,"column":48}},"17":{"start":{"line":42,"column":4},"end":{"line":42,"column":37}},"18":{"start":{"line":42,"column":28},"end":{"line":42,"column":37}},"19":{"start":{"line":43,"column":4},"end":{"line":43,"column":30}},"20":{"start":{"line":56,"column":2},"end":{"line":82,"column":4}},"21":{"start":{"line":57,"column":16},"end":{"line":57,"column":34}},"22":{"start":{"line":58,"column":17},"end":{"line":58,"column":43}},"23":{"start":{"line":59,"column":17},"end":{"line":59,"column":29}},"24":{"start":{"line":60,"column":12},"end":{"line":60,"column":23}},"25":{"start":{"line":61,"column":18},"end":{"line":61,"column":35}},"26":{"start":{"line":62,"column":4},"end":{"line":64,"column":5}},"27":{"start":{"line":63,"column":6},"end":{"line":63,"column":35}},"28":{"start":{"line":65,"column":17},"end":{"line":65,"column":50}},"29":{"start":{"line":66,"column":4},"end":{"line":66,"column":24}},"30":{"start":{"line":67,"column":4},"end":{"line":67,"column":36}},"31":{"start":{"line":68,"column":4},"end":{"line":68,"column":17}},"32":{"start":{"line":92,"column":2},"end":{"line":97,"column":4}},"33":{"start":{"line":93,"column":16},"end":{"line":93,"column":27}},"34":{"start":{"line":94,"column":4},"end":{"line":94,"column":35}},"35":{"start":{"line":94,"column":23},"end":{"line":94,"column":35}},"36":{"start":{"line":95,"column":4},"end":{"line":95,"column":34}},"37":{"start":{"line":95,"column":25},"end":{"line":95,"column":34}},"38":{"start":{"line":96,"column":4},"end":{"line":96,"column":17}},"39":{"start":{"line":107,"column":24},"end":{"line":107,"column":65}},"40":{"start":{"line":108,"column":2},"end":{"line":108,"column":55}},"41":{"start":{"line":109,"column":2},"end":{"line":109,"column":15}},"42":{"start":{"line":120,"column":2},"end":{"line":125,"column":3}},"43":{"start":{"line":121,"column":4},"end":{"line":121,"column":39}},"44":{"start":{"line":123,"column":20},"end":{"line":123,"column":50}},"45":{"start":{"line":124,"column":4},"end":{"line":124,"column":34}},"46":{"start":{"line":126,"column":17},"end":{"line":126,"column":39}},"47":{"start":{"line":127,"column":2},"end":{"line":127,"column":22}},"48":{"start":{"line":142,"column":2},"end":{"line":149,"column":3}},"49":{"start":{"line":143,"column":4},"end":{"line":143,"column":66}},"50":{"start":{"line":144,"column":4},"end":{"line":144,"column":24}},"51":{"start":{"line":145,"column":4},"end":{"line":145,"column":40}},"52":{"start":{"line":147,"column":20},"end":{"line":147,"column":55}},"53":{"start":{"line":148,"column":4},"end":{"line":148,"column":72}},"54":{"start":{"line":150,"column":2},"end":{"line":150,"column":13}},"55":{"start":{"line":162,"column":2},"end":{"line":199,"column":4}},"56":{"start":{"line":163,"column":19},"end":{"line":163,"column":41}},"57":{"start":{"line":164,"column":4},"end":{"line":196,"column":5}},"58":{"start":{"line":165,"column":19},"end":{"line":165,"column":48}},"59":{"start":{"line":166,"column":6},"end":{"line":168,"column":7}},"60":{"start":{"line":167,"column":8},"end":{"line":167,"column":26}},"61":{"start":{"line":169,"column":21},"end":{"line":169,"column":36}},"62":{"start":{"line":170,"column":19},"end":{"line":170,"column":20}},"63":{"start":{"line":171,"column":6},"end":{"line":194,"column":7}},"64":{"start":{"line":172,"column":8},"end":{"line":172,"column":70}},"65":{"start":{"line":172,"column":52},"end":{"line":172,"column":70}},"66":{"start":{"line":173,"column":23},"end":{"line":173,"column":52}},"67":{"start":{"line":174,"column":8},"end":{"line":174,"column":19}},"68":{"start":{"line":175,"column":8},"end":{"line":193,"column":9}},"69":{"start":{"line":176,"column":10},"end":{"line":178,"column":11}},"70":{"start":{"line":177,"column":12},"end":{"line":177,"column":30}},"71":{"start":{"line":180,"column":23},"end":{"line":180,"column":67}},"72":{"start":{"line":181,"column":10},"end":{"line":181,"column":54}},"73":{"start":{"line":182,"column":21},"end":{"line":182,"column":51}},"74":{"start":{"line":183,"column":10},"end":{"line":183,"column":21}},"75":{"start":{"line":184,"column":10},"end":{"line":188,"column":11}},"76":{"start":{"line":184,"column":23},"end":{"line":184,"column":24}},"77":{"start":{"line":185,"column":12},"end":{"line":187,"column":13}},"78":{"start":{"line":186,"column":14},"end":{"line":186,"column":75}},"79":{"start":{"line":189,"column":15},"end":{"line":193,"column":9}},"80":{"start":{"line":190,"column":10},"end":{"line":190,"column":15}},"81":{"start":{"line":192,"column":10},"end":{"line":192,"column":49}},"82":{"start":{"line":195,"column":6},"end":{"line":195,"column":24}},"83":{"start":{"line":197,"column":4},"end":{"line":197,"column":37}},"84":{"start":{"line":197,"column":28},"end":{"line":197,"column":37}},"85":{"start":{"line":198,"column":4},"end":{"line":198,"column":34}},"86":{"start":{"line":209,"column":16},"end":{"line":209,"column":28}},"87":{"start":{"line":210,"column":17},"end":{"line":210,"column":30}},"88":{"start":{"line":211,"column":27},"end":{"line":211,"column":51}},"89":{"start":{"line":213,"column":23},"end":{"line":213,"column":108}},"90":{"start":{"line":215,"column":18},"end":{"line":215,"column":24}},"91":{"start":{"line":218,"column":2},"end":{"line":230,"column":3}},"92":{"start":{"line":219,"column":4},"end":{"line":219,"column":56}},"93":{"start":{"line":220,"column":4},"end":{"line":226,"column":5}},"94":{"start":{"line":221,"column":6},"end":{"line":221,"column":40}},"95":{"start":{"line":222,"column":6},"end":{"line":222,"column":60}},"96":{"start":{"line":224,"column":6},"end":{"line":224,"column":59}},"97":{"start":{"line":225,"column":6},"end":{"line":225,"column":41}},"98":{"start":{"line":227,"column":4},"end":{"line":227,"column":66}},"99":{"start":{"line":229,"column":4},"end":{"line":229,"column":31}},"100":{"start":{"line":232,"column":2},"end":{"line":232,"column":18}},"101":{"start":{"line":244,"column":16},"end":{"line":244,"column":28}},"102":{"start":{"line":245,"column":17},"end":{"line":245,"column":30}},"103":{"start":{"line":247,"column":27},"end":{"line":247,"column":60}},"104":{"start":{"line":250,"column":2},"end":{"line":256,"column":3}},"105":{"start":{"line":251,"column":4},"end":{"line":251,"column":28}},"106":{"start":{"line":252,"column":4},"end":{"line":252,"column":28}},"107":{"start":{"line":254,"column":4},"end":{"line":254,"column":27}},"108":{"start":{"line":255,"column":4},"end":{"line":255,"column":29}},"109":{"start":{"line":259,"column":2},"end":{"line":268,"column":3}},"110":{"start":{"line":260,"column":12},"end":{"line":260,"column":49}},"111":{"start":{"line":260,"column":50},"end":{"line":260,"column":56}},"112":{"start":{"line":261,"column":12},"end":{"line":261,"column":55}},"113":{"start":{"line":261,"column":56},"end":{"line":261,"column":62}},"114":{"start":{"line":262,"column":12},"end":{"line":262,"column":50}},"115":{"start":{"line":262,"column":51},"end":{"line":262,"column":57}},"116":{"start":{"line":263,"column":12},"end":{"line":263,"column":44}},"117":{"start":{"line":263,"column":45},"end":{"line":263,"column":51}},"118":{"start":{"line":264,"column":12},"end":{"line":264,"column":50}},"119":{"start":{"line":264,"column":51},"end":{"line":264,"column":57}},"120":{"start":{"line":265,"column":12},"end":{"line":265,"column":55}},"121":{"start":{"line":265,"column":56},"end":{"line":265,"column":62}},"122":{"start":{"line":266,"column":12},"end":{"line":266,"column":49}},"123":{"start":{"line":266,"column":50},"end":{"line":266,"column":56}},"124":{"start":{"line":267,"column":13},"end":{"line":267,"column":19}},"125":{"start":{"line":270,"column":2},"end":{"line":270,"column":44}},"126":{"start":{"line":272,"column":2},"end":{"line":272,"column":29}},"127":{"start":{"line":274,"column":2},"end":{"line":274,"column":18}},"128":{"start":{"line":286,"column":2},"end":{"line":295,"column":3}},"129":{"start":{"line":287,"column":4},"end":{"line":287,"column":47}},"130":{"start":{"line":288,"column":4},"end":{"line":288,"column":33}},"131":{"start":{"line":289,"column":4},"end":{"line":291,"column":5}},"132":{"start":{"line":290,"column":6},"end":{"line":290,"column":67}},"133":{"start":{"line":293,"column":4},"end":{"line":293,"column":45}},"134":{"start":{"line":294,"column":4},"end":{"line":294,"column":33}},"135":{"start":{"line":296,"column":2},"end":{"line":296,"column":22}},"136":{"start":{"line":297,"column":2},"end":{"line":297,"column":24}},"137":{"start":{"line":298,"column":2},"end":{"line":298,"column":22}},"138":{"start":{"line":307,"column":2},"end":{"line":307,"column":18}},"139":{"start":{"line":308,"column":2},"end":{"line":308,"column":19}},"140":{"start":{"line":311,"column":0},"end":{"line":318,"column":1}},"141":{"start":{"line":313,"column":2},"end":{"line":317,"column":3}},"142":{"start":{"line":314,"column":4},"end":{"line":316,"column":33}}},"fnMap":{"0":{"name":"(anonymous_0)","decl":{"start":{"line":10,"column":43},"end":{"line":10,"column":44}},"loc":{"start":{"line":10,"column":55},"end":{"line":30,"column":1}},"line":10},"1":{"name":"getDataUrlFromFile","decl":{"start":{"line":38,"column":16},"end":{"line":38,"column":34}},"loc":{"start":{"line":38,"column":42},"end":{"line":45,"column":1}},"line":38},"2":{"name":"(anonymous_2)","decl":{"start":{"line":39,"column":21},"end":{"line":39,"column":22}},"loc":{"start":{"line":39,"column":42},"end":{"line":44,"column":3}},"line":39},"3":{"name":"(anonymous_3)","decl":{"start":{"line":41,"column":20},"end":{"line":41,"column":21}},"loc":{"start":{"line":41,"column":26},"end":{"line":41,"column":48}},"line":41},"4":{"name":"(anonymous_4)","decl":{"start":{"line":42,"column":21},"end":{"line":42,"column":22}},"loc":{"start":{"line":42,"column":28},"end":{"line":42,"column":37}},"line":42},"5":{"name":"getFilefromDataUrl","decl":{"start":{"line":55,"column":16},"end":{"line":55,"column":34}},"loc":{"start":{"line":55,"column":82},"end":{"line":83,"column":1}},"line":55},"6":{"name":"(anonymous_6)","decl":{"start":{"line":56,"column":21},"end":{"line":56,"column":22}},"loc":{"start":{"line":56,"column":34},"end":{"line":82,"column":3}},"line":56},"7":{"name":"loadImage","decl":{"start":{"line":91,"column":16},"end":{"line":91,"column":25}},"loc":{"start":{"line":91,"column":32},"end":{"line":98,"column":1}},"line":91},"8":{"name":"(anonymous_8)","decl":{"start":{"line":92,"column":21},"end":{"line":92,"column":22}},"loc":{"start":{"line":92,"column":42},"end":{"line":97,"column":3}},"line":92},"9":{"name":"(anonymous_9)","decl":{"start":{"line":94,"column":17},"end":{"line":94,"column":18}},"loc":{"start":{"line":94,"column":23},"end":{"line":94,"column":35}},"line":94},"10":{"name":"(anonymous_10)","decl":{"start":{"line":95,"column":18},"end":{"line":95,"column":19}},"loc":{"start":{"line":95,"column":25},"end":{"line":95,"column":34}},"line":95},"11":{"name":"drawImageInCanvas","decl":{"start":{"line":106,"column":16},"end":{"line":106,"column":33}},"loc":{"start":{"line":106,"column":40},"end":{"line":110,"column":1}},"line":106},"12":{"name":"drawFileInCanvas","decl":{"start":{"line":118,"column":22},"end":{"line":118,"column":38}},"loc":{"start":{"line":118,"column":46},"end":{"line":128,"column":1}},"line":118},"13":{"name":"canvasToFile","decl":{"start":{"line":140,"column":22},"end":{"line":140,"column":34}},"loc":{"start":{"line":140,"column":95},"end":{"line":151,"column":1}},"line":140},"14":{"name":"getExifOrientation","decl":{"start":{"line":161,"column":16},"end":{"line":161,"column":34}},"loc":{"start":{"line":161,"column":42},"end":{"line":200,"column":1}},"line":161},"15":{"name":"(anonymous_15)","decl":{"start":{"line":162,"column":21},"end":{"line":162,"column":22}},"loc":{"start":{"line":162,"column":42},"end":{"line":199,"column":3}},"line":162},"16":{"name":"(anonymous_16)","decl":{"start":{"line":164,"column":20},"end":{"line":164,"column":21}},"loc":{"start":{"line":164,"column":27},"end":{"line":196,"column":5}},"line":164},"17":{"name":"(anonymous_17)","decl":{"start":{"line":197,"column":21},"end":{"line":197,"column":22}},"loc":{"start":{"line":197,"column":28},"end":{"line":197,"column":37}},"line":197},"18":{"name":"handleMaxWidthOrHeight","decl":{"start":{"line":208,"column":16},"end":{"line":208,"column":38}},"loc":{"start":{"line":208,"column":57},"end":{"line":233,"column":1}},"line":208},"19":{"name":"followExifOrientation","decl":{"start":{"line":243,"column":16},"end":{"line":243,"column":37}},"loc":{"start":{"line":243,"column":64},"end":{"line":275,"column":1}},"line":243},"20":{"name":"getNewCanvasAndCtx","decl":{"start":{"line":283,"column":16},"end":{"line":283,"column":34}},"loc":{"start":{"line":283,"column":51},"end":{"line":299,"column":1}},"line":283},"21":{"name":"cleanupCanvasMemory","decl":{"start":{"line":306,"column":16},"end":{"line":306,"column":35}},"loc":{"start":{"line":306,"column":45},"end":{"line":309,"column":1}},"line":306},"22":{"name":"(anonymous_22)","decl":{"start":{"line":313,"column":41},"end":{"line":313,"column":42}},"loc":{"start":{"line":313,"column":58},"end":{"line":317,"column":3}},"line":313}},"branchMap":{"0":{"loc":{"start":{"line":4,"column":21},"end":{"line":4,"column":124}},"type":"binary-expr","locations":[{"start":{"line":4,"column":21},"end":{"line":4,"column":30}},{"start":{"line":4,"column":34},"end":{"line":4,"column":48}},{"start":{"line":4,"column":52},"end":{"line":4,"column":74}},{"start":{"line":4,"column":78},"end":{"line":4,"column":124}}],"line":4},"1":{"loc":{"start":{"line":5,"column":26},"end":{"line":5,"column":113}},"type":"binary-expr","locations":[{"start":{"line":5,"column":26},"end":{"line":5,"column":35}},{"start":{"line":5,"column":41},"end":{"line":5,"column":53}},{"start":{"line":5,"column":57},"end":{"line":5,"column":103}},{"start":{"line":5,"column":108},"end":{"line":5,"column":112}}],"line":5},"2":{"loc":{"start":{"line":6,"column":32},"end":{"line":6,"column":131}},"type":"binary-expr","locations":[{"start":{"line":6,"column":32},"end":{"line":6,"column":41}},{"start":{"line":6,"column":47},"end":{"line":6,"column":59}},{"start":{"line":6,"column":63},"end":{"line":6,"column":115}},{"start":{"line":6,"column":120},"end":{"line":6,"column":130}}],"line":6},"3":{"loc":{"start":{"line":29,"column":9},"end":{"line":29,"column":44}},"type":"binary-expr","locations":[{"start":{"line":29,"column":9},"end":{"line":29,"column":24}},{"start":{"line":29,"column":28},"end":{"line":29,"column":44}}],"line":29},"4":{"loc":{"start":{"line":55,"column":55},"end":{"line":55,"column":80}},"type":"default-arg","locations":[{"start":{"line":55,"column":70},"end":{"line":55,"column":80}}],"line":55},"5":{"loc":{"start":{"line":140,"column":82},"end":{"line":140,"column":93}},"type":"default-arg","locations":[{"start":{"line":140,"column":92},"end":{"line":140,"column":93}}],"line":140},"6":{"loc":{"start":{"line":142,"column":2},"end":{"line":149,"column":3}},"type":"if","locations":[{"start":{"line":142,"column":2},"end":{"line":149,"column":3}},{"start":{"line":142,"column":2},"end":{"line":149,"column":3}}],"line":142},"7":{"loc":{"start":{"line":142,"column":6},"end":{"line":142,"column":80}},"type":"binary-expr","locations":[{"start":{"line":142,"column":6},"end":{"line":142,"column":43}},{"start":{"line":142,"column":47},"end":{"line":142,"column":80}}],"line":142},"8":{"loc":{"start":{"line":166,"column":6},"end":{"line":168,"column":7}},"type":"if","locations":[{"start":{"line":166,"column":6},"end":{"line":168,"column":7}},{"start":{"line":166,"column":6},"end":{"line":168,"column":7}}],"line":166},"9":{"loc":{"start":{"line":172,"column":8},"end":{"line":172,"column":70}},"type":"if","locations":[{"start":{"line":172,"column":8},"end":{"line":172,"column":70}},{"start":{"line":172,"column":8},"end":{"line":172,"column":70}}],"line":172},"10":{"loc":{"start":{"line":175,"column":8},"end":{"line":193,"column":9}},"type":"if","locations":[{"start":{"line":175,"column":8},"end":{"line":193,"column":9}},{"start":{"line":175,"column":8},"end":{"line":193,"column":9}}],"line":175},"11":{"loc":{"start":{"line":176,"column":10},"end":{"line":178,"column":11}},"type":"if","locations":[{"start":{"line":176,"column":10},"end":{"line":178,"column":11}},{"start":{"line":176,"column":10},"end":{"line":178,"column":11}}],"line":176},"12":{"loc":{"start":{"line":185,"column":12},"end":{"line":187,"column":13}},"type":"if","locations":[{"start":{"line":185,"column":12},"end":{"line":187,"column":13}},{"start":{"line":185,"column":12},"end":{"line":187,"column":13}}],"line":185},"13":{"loc":{"start":{"line":189,"column":15},"end":{"line":193,"column":9}},"type":"if","locations":[{"start":{"line":189,"column":15},"end":{"line":193,"column":9}},{"start":{"line":189,"column":15},"end":{"line":193,"column":9}}],"line":189},"14":{"loc":{"start":{"line":213,"column":23},"end":{"line":213,"column":108}},"type":"binary-expr","locations":[{"start":{"line":213,"column":23},"end":{"line":213,"column":49}},{"start":{"line":213,"column":54},"end":{"line":213,"column":78}},{"start":{"line":213,"column":82},"end":{"line":213,"column":107}}],"line":213},"15":{"loc":{"start":{"line":218,"column":2},"end":{"line":230,"column":3}},"type":"if","locations":[{"start":{"line":218,"column":2},"end":{"line":230,"column":3}},{"start":{"line":218,"column":2},"end":{"line":230,"column":3}}],"line":218},"16":{"loc":{"start":{"line":220,"column":4},"end":{"line":226,"column":5}},"type":"if","locations":[{"start":{"line":220,"column":4},"end":{"line":226,"column":5}},{"start":{"line":220,"column":4},"end":{"line":226,"column":5}}],"line":220},"17":{"loc":{"start":{"line":250,"column":2},"end":{"line":256,"column":3}},"type":"if","locations":[{"start":{"line":250,"column":2},"end":{"line":256,"column":3}},{"start":{"line":250,"column":2},"end":{"line":256,"column":3}}],"line":250},"18":{"loc":{"start":{"line":250,"column":6},"end":{"line":250,"column":48}},"type":"binary-expr","locations":[{"start":{"line":250,"column":6},"end":{"line":250,"column":25}},{"start":{"line":250,"column":29},"end":{"line":250,"column":48}}],"line":250},"19":{"loc":{"start":{"line":259,"column":2},"end":{"line":268,"column":3}},"type":"switch","locations":[{"start":{"line":260,"column":4},"end":{"line":260,"column":56}},{"start":{"line":261,"column":4},"end":{"line":261,"column":62}},{"start":{"line":262,"column":4},"end":{"line":262,"column":57}},{"start":{"line":263,"column":4},"end":{"line":263,"column":51}},{"start":{"line":264,"column":4},"end":{"line":264,"column":57}},{"start":{"line":265,"column":4},"end":{"line":265,"column":62}},{"start":{"line":266,"column":4},"end":{"line":266,"column":56}},{"start":{"line":267,"column":4},"end":{"line":267,"column":19}}],"line":259},"20":{"loc":{"start":{"line":289,"column":4},"end":{"line":291,"column":5}},"type":"if","locations":[{"start":{"line":289,"column":4},"end":{"line":291,"column":5}},{"start":{"line":289,"column":4},"end":{"line":291,"column":5}}],"line":289},"21":{"loc":{"start":{"line":311,"column":0},"end":{"line":318,"column":1}},"type":"if","locations":[{"start":{"line":311,"column":0},"end":{"line":318,"column":1}},{"start":{"line":311,"column":0},"end":{"line":318,"column":1}}],"line":311},"22":{"loc":{"start":{"line":313,"column":21},"end":{"line":317,"column":3}},"type":"binary-expr","locations":[{"start":{"line":313,"column":21},"end":{"line":313,"column":37}},{"start":{"line":313,"column":41},"end":{"line":317,"column":3}}],"line":313},"23":{"loc":{"start":{"line":314,"column":11},"end":{"line":316,"column":33}},"type":"binary-expr","locations":[{"start":{"line":314,"column":11},"end":{"line":314,"column":36}},{"start":{"line":315,"column":6},"end":{"line":315,"column":21}},{"start":{"line":316,"column":6},"end":{"line":316,"column":33}}],"line":314}},"s":{"0":1,"1":1,"2":1,"3":1,"4":1,"5":1,"6":1,"7":1,"8":1,"9":1,"10":1,"11":1,"12":1,"13":15,"14":15,"15":15,"16":15,"17":15,"18":0,"19":15,"20":19,"21":19,"22":19,"23":19,"24":19,"25":19,"26":19,"27":24015727,"28":19,"29":19,"30":19,"31":19,"32":15,"33":15,"34":15,"35":15,"36":15,"37":0,"38":15,"39":13,"40":13,"41":13,"42":12,"43":12,"44":12,"45":12,"46":12,"47":12,"48":16,"49":0,"50":0,"51":0,"52":16,"53":16,"54":16,"55":1,"56":1,"57":1,"58":1,"59":1,"60":1,"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":0,"75":0,"76":0,"77":0,"78":0,"79":0,"80":0,"81":0,"82":0,"83":1,"84":0,"85":1,"86":6,"87":6,"88":6,"89":6,"90":6,"91":6,"92":4,"93":4,"94":2,"95":2,"96":2,"97":2,"98":4,"99":4,"100":6,"101":6,"102":6,"103":6,"104":6,"105":0,"106":0,"107":6,"108":6,"109":6,"110":0,"111":0,"112":0,"113":0,"114":0,"115":0,"116":0,"117":0,"118":0,"119":0,"120":0,"121":0,"122":0,"123":0,"124":6,"125":6,"126":6,"127":6,"128":32,"129":32,"130":0,"131":0,"132":0,"133":32,"134":32,"135":32,"136":32,"137":32,"138":30,"139":30,"140":1,"141":1,"142":0},"f":{"0":1,"1":15,"2":15,"3":15,"4":0,"5":19,"6":19,"7":15,"8":15,"9":15,"10":0,"11":13,"12":12,"13":16,"14":1,"15":1,"16":1,"17":0,"18":6,"19":6,"20":32,"21":30,"22":0},"b":{"0":[1,1,0,0],"1":[1,1,0,1],"2":[1,1,0,1],"3":[1,0],"4":[2],"5":[1],"6":[0,16],"7":[16,0],"8":[1,0],"9":[0,0],"10":[0,0],"11":[0,0],"12":[0,0],"13":[0,0],"14":[6,4,0],"15":[4,2],"16":[2,2],"17":[0,6],"18":[6,0],"19":[0,0,0,0,0,0,0,6],"20":[0,0],"21":[1,0],"22":[1,0],"23":[0,0,0]},"_coverageSchema":"1a1c01bbd47fc00a2c39e90264f33305004495a9","hash":"3ea65f352e8a792b5556835aafec24cdbbe0f09e","contentHash":"a24cdd962db05859305f4bae3b1496af7fe06bd47c7b46f3a9105a8f0ac522a3"} } diff --git a/coverage/image-compression.js.html b/coverage/image-compression.js.html index aa676b9..fcaf5bd 100644 --- a/coverage/image-compression.js.html +++ b/coverage/image-compression.js.html @@ -397,7 +397,7 @@

All files image-compression.js

diff --git a/coverage/index.html b/coverage/index.html index a8a9cd9..93ab4c0 100644 --- a/coverage/index.html +++ b/coverage/index.html @@ -125,7 +125,7 @@

All files

diff --git a/coverage/index.js.html b/coverage/index.js.html index 9363749..2a82349 100644 --- a/coverage/index.js.html +++ b/coverage/index.js.html @@ -333,7 +333,7 @@

All files index.js

imageCompression.handleMaxWidthOrHeight = handleMaxWidthOrHeight imageCompression.followExifOrientation = followExifOrientation imageCompression.cleanupMemory = cleanupCanvasMemory -imageCompression.version = '1.0.10' +imageCompression.version = '1.0.11'   export default imageCompression   @@ -343,7 +343,7 @@

All files index.js

diff --git a/coverage/lcov-report/image-compression.js.html b/coverage/lcov-report/image-compression.js.html index aa676b9..fcaf5bd 100644 --- a/coverage/lcov-report/image-compression.js.html +++ b/coverage/lcov-report/image-compression.js.html @@ -397,7 +397,7 @@

All files image-compression.js

diff --git a/coverage/lcov-report/index.html b/coverage/lcov-report/index.html index a8a9cd9..93ab4c0 100644 --- a/coverage/lcov-report/index.html +++ b/coverage/lcov-report/index.html @@ -125,7 +125,7 @@

All files

diff --git a/coverage/lcov-report/index.js.html b/coverage/lcov-report/index.js.html index 9363749..2a82349 100644 --- a/coverage/lcov-report/index.js.html +++ b/coverage/lcov-report/index.js.html @@ -333,7 +333,7 @@

All files index.js

imageCompression.handleMaxWidthOrHeight = handleMaxWidthOrHeight imageCompression.followExifOrientation = followExifOrientation imageCompression.cleanupMemory = cleanupCanvasMemory -imageCompression.version = '1.0.10' +imageCompression.version = '1.0.11'   export default imageCompression   @@ -343,7 +343,7 @@

All files index.js

diff --git a/coverage/lcov-report/utils.js.html b/coverage/lcov-report/utils.js.html index 24d4138..6754968 100644 --- a/coverage/lcov-report/utils.js.html +++ b/coverage/lcov-report/utils.js.html @@ -905,7 +905,7 @@

All files utils.js

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

All files utils.js

diff --git a/coverage/utils.js.html b/coverage/utils.js.html index 24d4138..6754968 100644 --- a/coverage/utils.js.html +++ b/coverage/utils.js.html @@ -905,7 +905,7 @@

All files utils.js

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

All files utils.js

diff --git a/dist/browser-image-compression.js b/dist/browser-image-compression.js index 7f07799..b680082 100644 --- a/dist/browser-image-compression.js +++ b/dist/browser-image-compression.js @@ -1,9 +1,9 @@ /** * Browser Image Compression - * v1.0.10 + * v1.0.11 * by Donald * https://github.com/Donaldcwl/browser-image-compression */ -!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r():"function"==typeof define&&define.amd?define(r):(e=e||self).imageCompression=r()}(this,(function(){"use strict";function _defineProperty(e,r,n){return r in e?Object.defineProperty(e,r,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[r]=n,e}function ownKeys(e,r){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var t=Object.getOwnPropertySymbols(e);r&&(t=t.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}))),n.push.apply(n,t)}return n}function _objectSpread2(e){for(var r=1;re.length)&&(r=e.length);for(var n=0,t=new Array(r);n2&&void 0!==arguments[2]?arguments[2]:Date.now();return new Promise((function(t){for(var i=e.split(","),o=i[0].match(/:(.*?);/)[1],a=atob(i[1]),s=a.length,c=new Uint8Array(s);s--;)c[s]=a.charCodeAt(s);var u=new Blob([c],{type:o});u.name=r,u.lastModified=n,t(u)}))}function loadImage(e){return new Promise((function(r,n){var t=new Image;t.onload=function(){return r(t)},t.onerror=function(e){return n(e)},t.src=e}))}function drawImageInCanvas(e){var r=_slicedToArray(getNewCanvasAndCtx(e.width,e.height),2),n=r[0];return r[1].drawImage(e,0,0,n.width,n.height),n}function drawFileInCanvas(e){return new Promise((function(r,n){var t,i,o=function $Try_1_Post(){try{return i=drawImageInCanvas(t),r([t,i])}catch(e){return n(e)}},a=function $Try_1_Catch(r){try{return getDataUrlFromFile(e).then((function(e){try{return loadImage(e).then((function(e){try{return t=e,o()}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 t=e,o()}catch(e){return a()}}),a)}catch(e){a()}}))}function canvasToFile(e,r,n,t){var i=arguments.length>4&&void 0!==arguments[4]?arguments[4]:1;return new Promise((function(o,a){var s;return"function"==typeof OffscreenCanvas&&e instanceof OffscreenCanvas?e.convertToBlob({type:r,quality:i}).then(function(e){try{return(s=e).name=n,s.lastModified=t,$If_4.call(this)}catch(e){return a(e)}}.bind(this),a):getFilefromDataUrl(e.toDataURL(r,i),n,t).then(function(e){try{return s=e,$If_4.call(this)}catch(e){return a(e)}}.bind(this),a);function $If_4(){return o(s)}}))}function getExifOrientation(e){return new Promise((function(r,n){var t=new CustomFileReader;t.onload=function(e){var n=new DataView(e.target.result);if(65496!=n.getUint16(0,!1))return r(-2);for(var t=n.byteLength,i=2;io||i>o)){var s=_slicedToArray(getNewCanvasAndCtx(t,i),2);a=s[0],n=s[1],t>i?(a.width=o,a.height=i/t*o):(a.width=t/i*o,a.height=o),n.drawImage(e,0,0,a.width,a.height),cleanupCanvasMemory(e)}return a}function followExifOrientation(e,r){var n=e.width,t=e.height,i=_slicedToArray(getNewCanvasAndCtx(n,t),2),o=i[0],a=i[1];switch(40&&void 0!==arguments[0]?arguments[0]:5;o+=e,r.onProgress(Math.min(o,100))}function setProgress(e){o=Math.min(Math.max(e,o),100),r.onProgress(o)}return o=0,a=r.maxIteration||10,s=1024*r.maxSizeMB*1024,incProgress(),drawFileInCanvas(e).then(function(o){try{var C=_slicedToArray(o,2);return C[0],c=C[1],incProgress(),u=handleMaxWidthOrHeight(c,r),incProgress(),new Promise((function(n,t){var i;if(!(i=r.exifOrientation))return getExifOrientation(e).then(function(e){try{return i=e,$If_2.call(this)}catch(e){return t(e)}}.bind(this),t);function $If_2(){return n(i)}return $If_2.call(this)})).then(function(o){try{return r.exifOrientation=o,incProgress(),n.then(function(n){try{return l=n?u:followExifOrientation(u,r.exifOrientation),incProgress(),f=1,canvasToFile(l,r.fileType||e.type,e.name,e.lastModified,f).then(function(n){try{{if(m=n,incProgress(),g=m.size>s,A=m.size>e.size,!g&&!A)return setProgress(100),t(m);var o;function $Loop_3(){if(a--&&(h>s||h>d)){var n,t,o=_slicedToArray(getNewCanvasAndCtx(n=g?.95*w.width:w.width,t=g?.95*w.height:w.height),2);return v=o[0],o[1].drawImage(w,0,0,n,t),"image/jpeg"===e.type&&(f*=.95),canvasToFile(v,r.fileType||e.type,e.name,e.lastModified,f).then((function(e){try{return y=e,cleanupCanvasMemory(w),w=v,h=y.size,setProgress(Math.min(99,Math.floor((p-h)/(p-s)*100))),$Loop_3}catch(e){return i(e)}}),i)}return[1]}return d=e.size,p=m.size,h=p,w=l,(o=function(e){for(;e;){if(e.then)return void e.then(o,i);try{if(e.pop){if(e.length)return e.pop()?$Loop_3_exit.call(this):e;e=$Loop_3}else e=e.call(this)}catch(e){return i(e)}}}.bind(this))($Loop_3);function $Loop_3_exit(){return cleanupCanvasMemory(w),cleanupCanvasMemory(v),cleanupCanvasMemory(u),cleanupCanvasMemory(l),cleanupCanvasMemory(c),setProgress(100),t(y)}}}catch(e){return i(e)}}.bind(this),i)}catch(e){return i(e)}}.bind(this),i)}catch(e){return i(e)}}.bind(this),i)}catch(e){return i(e)}}.bind(this),i)}))}e&&(Number.isInteger=Number.isInteger||function(e){return"number"==typeof e&&isFinite(e)&&Math.floor(e)===e});var t,i,o=0;function generateLib(){return function createSourceObject(e){return URL.createObjectURL(new Blob([e],{type:"application/javascript"}))}("\n function imageCompression (){return (".concat(imageCompression,").apply(null, arguments)}\n\n imageCompression.getDataUrlFromFile = ").concat(imageCompression.getDataUrlFromFile,"\n imageCompression.getFilefromDataUrl = ").concat(imageCompression.getFilefromDataUrl,"\n imageCompression.loadImage = ").concat(imageCompression.loadImage,"\n imageCompression.drawImageInCanvas = ").concat(imageCompression.drawImageInCanvas,"\n imageCompression.drawFileInCanvas = ").concat(imageCompression.drawFileInCanvas,"\n imageCompression.canvasToFile = ").concat(imageCompression.canvasToFile,"\n imageCompression.getExifOrientation = ").concat(imageCompression.getExifOrientation,"\n imageCompression.handleMaxWidthOrHeight = ").concat(imageCompression.handleMaxWidthOrHeight,"\n imageCompression.followExifOrientation = ").concat(imageCompression.followExifOrientation,"\n imageCompression.cleanupMemory = ").concat(imageCompression.cleanupMemory,"\n\n getDataUrlFromFile = imageCompression.getDataUrlFromFile\n getFilefromDataUrl = imageCompression.getFilefromDataUrl\n loadImage = imageCompression.loadImage\n drawImageInCanvas = imageCompression.drawImageInCanvas\n drawFileInCanvas = imageCompression.drawFileInCanvas\n canvasToFile = imageCompression.canvasToFile\n getExifOrientation = imageCompression.getExifOrientation\n handleMaxWidthOrHeight = imageCompression.handleMaxWidthOrHeight\n followExifOrientation = imageCompression.followExifOrientation\n cleanupMemory = imageCompression.cleanupMemory\n\n getNewCanvasAndCtx = ").concat(getNewCanvasAndCtx,"\n \n CustomFileReader = FileReader\n \n CustomFile = File\n \n function _slicedToArray(arr, n) { return arr }\n \n function _typeof(a) { return typeof a }\n\n function compress (){return (").concat(compress,").apply(null, arguments)}\n "))}function generateWorkerScript(){return function createWorker(e){return"function"==typeof e&&(e="(".concat(f,")()")),new Worker(URL.createObjectURL(new Blob([e])))}("\n let scriptImported = false\n self.addEventListener('message', async (e) => {\n const { file, id, imageCompressionLibUrl, options } = e.data\n options.onProgress = (progress) => self.postMessage({ progress, id })\n try {\n if (!scriptImported) {\n // console.log('[worker] importScripts', imageCompressionLibUrl)\n self.importScripts(imageCompressionLibUrl)\n scriptImported = true\n }\n // console.log('[worker] self', self)\n const compressedFile = await imageCompression(file, options)\n self.postMessage({ file: compressedFile, id })\n } catch (e) {\n // console.error('[worker] error', e)\n self.postMessage({ error: e.message + '\\n' + e.stack, id })\n }\n })\n ")}function imageCompression(e,r){return new Promise((function(n,a){var s,c,u;if(r.maxSizeMB=r.maxSizeMB||Number.POSITIVE_INFINITY,c="boolean"!=typeof r.useWebWorker||r.useWebWorker,delete r.useWebWorker,void 0===r.onProgress&&(r.onProgress=function(){}),!(e instanceof Blob||e instanceof CustomFile))return a(new Error("The file given is not an instance of Blob or File"));if(!/^image/.test(e.type))return a(new Error("The file given is not an image"));if(u="undefined"!=typeof WorkerGlobalScope&&self instanceof WorkerGlobalScope,!c||"function"!=typeof Worker||u)return compress(e,r).then(function(e){try{return s=e,$If_3.call(this)}catch(e){return a(e)}}.bind(this),a);var l=function(){try{return $If_3.call(this)}catch(e){return a(e)}}.bind(this),f=function $Try_1_Catch(n){try{return compress(e,r).then((function(e){try{return s=e,l()}catch(e){return a(e)}}),a)}catch(e){return a(e)}};try{return function compressOnWebWorker(e,r){return new Promise((function(n,a){return new Promise((function(s,c){var u=o++;return t||(t=generateLib()),i||(i=generateWorkerScript()),i.addEventListener("message",(function handler(e){if(e.data.id===u){if(void 0!==e.data.progress)return void r.onProgress(e.data.progress);i.removeEventListener("message",handler),e.data.error&&a(new Error(e.data.error)),n(e.data.file)}})),i.postMessage({file:e,id:u,imageCompressionLibUrl:t,options:_objectSpread2(_objectSpread2({},r),{},{onProgress:void 0})}),s()}))}))}(e,r).then((function(e){try{return s=e,l()}catch(e){return f()}}),f)}catch(e){f()}function $If_3(){try{s.name=e.name,s.lastModified=e.lastModified}catch(e){}return n(s)}}))}return imageCompression.getDataUrlFromFile=getDataUrlFromFile,imageCompression.getFilefromDataUrl=getFilefromDataUrl,imageCompression.loadImage=loadImage,imageCompression.drawImageInCanvas=drawImageInCanvas,imageCompression.drawFileInCanvas=drawFileInCanvas,imageCompression.canvasToFile=canvasToFile,imageCompression.getExifOrientation=getExifOrientation,imageCompression.handleMaxWidthOrHeight=handleMaxWidthOrHeight,imageCompression.followExifOrientation=followExifOrientation,imageCompression.cleanupMemory=cleanupCanvasMemory,imageCompression.version="1.0.10",imageCompression})); +!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r():"function"==typeof define&&define.amd?define(r):(e=e||self).imageCompression=r()}(this,(function(){"use strict";function _defineProperty(e,r,n){return r in e?Object.defineProperty(e,r,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[r]=n,e}function ownKeys(e,r){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var t=Object.getOwnPropertySymbols(e);r&&(t=t.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}))),n.push.apply(n,t)}return n}function _objectSpread2(e){for(var r=1;re.length)&&(r=e.length);for(var n=0,t=new Array(r);n2&&void 0!==arguments[2]?arguments[2]:Date.now();return new Promise((function(t){for(var i=e.split(","),o=i[0].match(/:(.*?);/)[1],a=atob(i[1]),s=a.length,c=new Uint8Array(s);s--;)c[s]=a.charCodeAt(s);var u=new Blob([c],{type:o});u.name=r,u.lastModified=n,t(u)}))}function loadImage(e){return new Promise((function(r,n){var t=new Image;t.onload=function(){return r(t)},t.onerror=function(e){return n(e)},t.src=e}))}function drawImageInCanvas(e){var r=_slicedToArray(getNewCanvasAndCtx(e.width,e.height),2),n=r[0];return r[1].drawImage(e,0,0,n.width,n.height),n}function drawFileInCanvas(e){return new Promise((function(r,n){var t,i,o=function $Try_1_Post(){try{return i=drawImageInCanvas(t),r([t,i])}catch(e){return n(e)}},a=function $Try_1_Catch(r){try{return getDataUrlFromFile(e).then((function(e){try{return loadImage(e).then((function(e){try{return t=e,o()}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 t=e,o()}catch(e){return a()}}),a)}catch(e){a()}}))}function canvasToFile(e,r,n,t){var i=arguments.length>4&&void 0!==arguments[4]?arguments[4]:1;return new Promise((function(o,a){var s;return"function"==typeof OffscreenCanvas&&e instanceof OffscreenCanvas?e.convertToBlob({type:r,quality:i}).then(function(e){try{return(s=e).name=n,s.lastModified=t,$If_4.call(this)}catch(e){return a(e)}}.bind(this),a):getFilefromDataUrl(e.toDataURL(r,i),n,t).then(function(e){try{return s=e,$If_4.call(this)}catch(e){return a(e)}}.bind(this),a);function $If_4(){return o(s)}}))}function getExifOrientation(e){return new Promise((function(r,n){var t=new CustomFileReader;t.onload=function(e){var n=new DataView(e.target.result);if(65496!=n.getUint16(0,!1))return r(-2);for(var t=n.byteLength,i=2;io||i>o)){var s=_slicedToArray(getNewCanvasAndCtx(t,i),2);a=s[0],n=s[1],t>i?(a.width=o,a.height=i/t*o):(a.width=t/i*o,a.height=o),n.drawImage(e,0,0,a.width,a.height),cleanupCanvasMemory(e)}return a}function followExifOrientation(e,r){var n=e.width,t=e.height,i=_slicedToArray(getNewCanvasAndCtx(n,t),2),o=i[0],a=i[1];switch(40&&void 0!==arguments[0]?arguments[0]:5;o+=e,r.onProgress(Math.min(o,100))}function setProgress(e){o=Math.min(Math.max(e,o),100),r.onProgress(o)}return o=0,a=r.maxIteration||10,s=1024*r.maxSizeMB*1024,incProgress(),drawFileInCanvas(e).then(function(o){try{var C=_slicedToArray(o,2);return C[0],c=C[1],incProgress(),u=handleMaxWidthOrHeight(c,r),incProgress(),new Promise((function(n,t){var i;if(!(i=r.exifOrientation))return getExifOrientation(e).then(function(e){try{return i=e,$If_2.call(this)}catch(e){return t(e)}}.bind(this),t);function $If_2(){return n(i)}return $If_2.call(this)})).then(function(o){try{return r.exifOrientation=o,incProgress(),n.then(function(n){try{return l=n?u:followExifOrientation(u,r.exifOrientation),incProgress(),f=1,canvasToFile(l,r.fileType||e.type,e.name,e.lastModified,f).then(function(n){try{{if(m=n,incProgress(),g=m.size>s,A=m.size>e.size,!g&&!A)return setProgress(100),t(m);var o;function $Loop_3(){if(a--&&(h>s||h>d)){var n,t,o=_slicedToArray(getNewCanvasAndCtx(n=g?.95*w.width:w.width,t=g?.95*w.height:w.height),2);return v=o[0],o[1].drawImage(w,0,0,n,t),"image/jpeg"===e.type&&(f*=.95),canvasToFile(v,r.fileType||e.type,e.name,e.lastModified,f).then((function(e){try{return y=e,cleanupCanvasMemory(w),w=v,h=y.size,setProgress(Math.min(99,Math.floor((p-h)/(p-s)*100))),$Loop_3}catch(e){return i(e)}}),i)}return[1]}return d=e.size,p=m.size,h=p,w=l,(o=function(e){for(;e;){if(e.then)return void e.then(o,i);try{if(e.pop){if(e.length)return e.pop()?$Loop_3_exit.call(this):e;e=$Loop_3}else e=e.call(this)}catch(e){return i(e)}}}.bind(this))($Loop_3);function $Loop_3_exit(){return cleanupCanvasMemory(w),cleanupCanvasMemory(v),cleanupCanvasMemory(u),cleanupCanvasMemory(l),cleanupCanvasMemory(c),setProgress(100),t(y)}}}catch(e){return i(e)}}.bind(this),i)}catch(e){return i(e)}}.bind(this),i)}catch(e){return i(e)}}.bind(this),i)}catch(e){return i(e)}}.bind(this),i)}))}e&&(Number.isInteger=Number.isInteger||function(e){return"number"==typeof e&&isFinite(e)&&Math.floor(e)===e});var t,i,o=0;function generateLib(){return function createSourceObject(e){return URL.createObjectURL(new Blob([e],{type:"application/javascript"}))}("\n function imageCompression (){return (".concat(imageCompression,").apply(null, arguments)}\n\n imageCompression.getDataUrlFromFile = ").concat(imageCompression.getDataUrlFromFile,"\n imageCompression.getFilefromDataUrl = ").concat(imageCompression.getFilefromDataUrl,"\n imageCompression.loadImage = ").concat(imageCompression.loadImage,"\n imageCompression.drawImageInCanvas = ").concat(imageCompression.drawImageInCanvas,"\n imageCompression.drawFileInCanvas = ").concat(imageCompression.drawFileInCanvas,"\n imageCompression.canvasToFile = ").concat(imageCompression.canvasToFile,"\n imageCompression.getExifOrientation = ").concat(imageCompression.getExifOrientation,"\n imageCompression.handleMaxWidthOrHeight = ").concat(imageCompression.handleMaxWidthOrHeight,"\n imageCompression.followExifOrientation = ").concat(imageCompression.followExifOrientation,"\n imageCompression.cleanupMemory = ").concat(imageCompression.cleanupMemory,"\n\n getDataUrlFromFile = imageCompression.getDataUrlFromFile\n getFilefromDataUrl = imageCompression.getFilefromDataUrl\n loadImage = imageCompression.loadImage\n drawImageInCanvas = imageCompression.drawImageInCanvas\n drawFileInCanvas = imageCompression.drawFileInCanvas\n canvasToFile = imageCompression.canvasToFile\n getExifOrientation = imageCompression.getExifOrientation\n handleMaxWidthOrHeight = imageCompression.handleMaxWidthOrHeight\n followExifOrientation = imageCompression.followExifOrientation\n cleanupMemory = imageCompression.cleanupMemory\n\n getNewCanvasAndCtx = ").concat(getNewCanvasAndCtx,"\n \n CustomFileReader = FileReader\n \n CustomFile = File\n \n function _slicedToArray(arr, n) { return arr }\n \n function _typeof(a) { return typeof a }\n\n function compress (){return (").concat(compress,").apply(null, arguments)}\n "))}function generateWorkerScript(){return function createWorker(e){return"function"==typeof e&&(e="(".concat(f,")()")),new Worker(URL.createObjectURL(new Blob([e])))}("\n let scriptImported = false\n self.addEventListener('message', async (e) => {\n const { file, id, imageCompressionLibUrl, options } = e.data\n options.onProgress = (progress) => self.postMessage({ progress, id })\n try {\n if (!scriptImported) {\n // console.log('[worker] importScripts', imageCompressionLibUrl)\n self.importScripts(imageCompressionLibUrl)\n scriptImported = true\n }\n // console.log('[worker] self', self)\n const compressedFile = await imageCompression(file, options)\n self.postMessage({ file: compressedFile, id })\n } catch (e) {\n // console.error('[worker] error', e)\n self.postMessage({ error: e.message + '\\n' + e.stack, id })\n }\n })\n ")}function imageCompression(e,r){return new Promise((function(n,a){var s,c,u;if(r.maxSizeMB=r.maxSizeMB||Number.POSITIVE_INFINITY,c="boolean"!=typeof r.useWebWorker||r.useWebWorker,delete r.useWebWorker,void 0===r.onProgress&&(r.onProgress=function(){}),!(e instanceof Blob||e instanceof CustomFile))return a(new Error("The file given is not an instance of Blob or File"));if(!/^image/.test(e.type))return a(new Error("The file given is not an image"));if(u="undefined"!=typeof WorkerGlobalScope&&self instanceof WorkerGlobalScope,!c||"function"!=typeof Worker||u)return compress(e,r).then(function(e){try{return s=e,$If_3.call(this)}catch(e){return a(e)}}.bind(this),a);var l=function(){try{return $If_3.call(this)}catch(e){return a(e)}}.bind(this),f=function $Try_1_Catch(n){try{return compress(e,r).then((function(e){try{return s=e,l()}catch(e){return a(e)}}),a)}catch(e){return a(e)}};try{return function compressOnWebWorker(e,r){return new Promise((function(n,a){return new Promise((function(s,c){var u=o++;return t||(t=generateLib()),i||(i=generateWorkerScript()),i.addEventListener("message",(function handler(e){if(e.data.id===u){if(void 0!==e.data.progress)return void r.onProgress(e.data.progress);i.removeEventListener("message",handler),e.data.error&&a(new Error(e.data.error)),n(e.data.file)}})),i.addEventListener("error",a),i.postMessage({file:e,id:u,imageCompressionLibUrl:t,options:_objectSpread2(_objectSpread2({},r),{},{onProgress:void 0})}),s()}))}))}(e,r).then((function(e){try{return s=e,l()}catch(e){return f()}}),f)}catch(e){f()}function $If_3(){try{s.name=e.name,s.lastModified=e.lastModified}catch(e){}return n(s)}}))}return imageCompression.getDataUrlFromFile=getDataUrlFromFile,imageCompression.getFilefromDataUrl=getFilefromDataUrl,imageCompression.loadImage=loadImage,imageCompression.drawImageInCanvas=drawImageInCanvas,imageCompression.drawFileInCanvas=drawFileInCanvas,imageCompression.canvasToFile=canvasToFile,imageCompression.getExifOrientation=getExifOrientation,imageCompression.handleMaxWidthOrHeight=handleMaxWidthOrHeight,imageCompression.followExifOrientation=followExifOrientation,imageCompression.cleanupMemory=cleanupCanvasMemory,imageCompression.version="1.0.11",imageCompression})); //# sourceMappingURL=browser-image-compression.js.map diff --git a/dist/browser-image-compression.js.map b/dist/browser-image-compression.js.map index 33fb541..092df2e 100644 --- a/dist/browser-image-compression.js.map +++ b/dist/browser-image-compression.js.map @@ -1 +1 @@ -{"version":3,"file":"browser-image-compression.js","sources":["../lib/utils.js","../lib/image-compression.js","../lib/web-worker.js","../lib/index.js"],"sourcesContent":["const isBrowser = typeof window !== 'undefined' // change browser environment to support SSR\r\n\r\n// add support for cordova-plugin-file\r\nconst moduleMapper = isBrowser && window.cordova && window.cordova.require && window.cordova.require('cordova/modulemapper')\r\nexport const CustomFile = isBrowser && ((moduleMapper && moduleMapper.getOriginalSymbol(window, 'File')) || File)\r\nexport const CustomFileReader = isBrowser && ((moduleMapper && moduleMapper.getOriginalSymbol(window, 'FileReader')) || FileReader)\r\n\r\n// Check if browser supports automatic image orientation\r\n// see https://github.com/blueimp/JavaScript-Load-Image/blob/1e4df707821a0afcc11ea0720ee403b8759f3881/js/load-image-orientation.js#L37-L53\r\nexport const isAutoOrientationInBrowser = (async () => {\r\n // black 2x1 JPEG, with the following meta information set:\r\n // EXIF Orientation: 6 (Rotated 90° CCW)\r\n const testImageURL =\r\n 'data:image/jpeg;base64,/9j/4QAiRXhpZgAATU0AKgAAAAgAAQESAAMAAAABAAYAAAA' +\r\n 'AAAD/2wCEAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBA' +\r\n 'QEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE' +\r\n 'BAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAf/AABEIAAEAAgMBEQACEQEDEQH/x' +\r\n 'ABKAAEAAAAAAAAAAAAAAAAAAAALEAEAAAAAAAAAAAAAAAAAAAAAAQEAAAAAAAAAAAAAAAA' +\r\n 'AAAAAEQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIRAxEAPwA/8H//2Q=='\r\n const testImageFile = await getFilefromDataUrl(testImageURL, 'test.jpg', Date.now())\r\n\r\n const testImageCanvas = (await drawFileInCanvas(testImageFile))[1]\r\n const testImageFile2 = await canvasToFile(testImageCanvas, testImageFile.type, testImageFile.name, testImageFile.lastModified)\r\n cleanupCanvasMemory(testImageCanvas)\r\n const testImageURL2 = await getDataUrlFromFile(testImageFile2)\r\n const img = await loadImage(testImageURL2)\r\n // console.log('img', img.width, img.height)\r\n\r\n return img.width === 1 && img.height === 2\r\n})()\r\n\r\n/**\r\n * getDataUrlFromFile\r\n *\r\n * @param {File | Blob} file\r\n * @returns {Promise}\r\n */\r\nexport function getDataUrlFromFile (file) {\r\n return new Promise((resolve, reject) => {\r\n const reader = new CustomFileReader()\r\n reader.onload = () => resolve(reader.result)\r\n reader.onerror = (e) => reject(e)\r\n reader.readAsDataURL(file)\r\n })\r\n}\r\n\r\n/**\r\n * getFilefromDataUrl\r\n *\r\n * @param {string} dataUrl\r\n * @param {string} filename\r\n * @param {number} [lastModified=Date.now()]\r\n * @returns {Promise}\r\n */\r\nexport function getFilefromDataUrl (dataUrl, filename, lastModified = Date.now()) {\r\n return new Promise((resolve) => {\r\n const arr = dataUrl.split(',')\r\n const mime = arr[0].match(/:(.*?);/)[1]\r\n const bstr = atob(arr[1])\r\n let n = bstr.length\r\n const u8arr = new Uint8Array(n)\r\n while (n--) {\r\n u8arr[n] = bstr.charCodeAt(n)\r\n }\r\n const file = new Blob([u8arr], { type: mime })\r\n file.name = filename\r\n file.lastModified = lastModified\r\n resolve(file)\r\n\r\n // Safari has issue with File constructor not being able to POST in FormData\r\n // https://github.com/Donaldcwl/browser-image-compression/issues/8\r\n // https://bugs.webkit.org/show_bug.cgi?id=165081\r\n // let file\r\n // try {\r\n // file = new File([u8arr], filename, { type: mime }) // Edge do not support File constructor\r\n // } catch (e) {\r\n // file = new Blob([u8arr], { type: mime })\r\n // file.name = filename\r\n // file.lastModified = lastModified\r\n // }\r\n // resolve(file)\r\n })\r\n}\r\n\r\n/**\r\n * loadImage\r\n *\r\n * @param {string} src\r\n * @returns {Promise}\r\n */\r\nexport function loadImage (src) {\r\n return new Promise((resolve, reject) => {\r\n const img = new Image()\r\n img.onload = () => resolve(img)\r\n img.onerror = (e) => reject(e)\r\n img.src = src\r\n })\r\n}\r\n\r\n/**\r\n * drawImageInCanvas\r\n *\r\n * @param {HTMLImageElement} img\r\n * @returns {HTMLCanvasElement | OffscreenCanvas}\r\n */\r\nexport function drawImageInCanvas (img) {\r\n const [canvas, ctx] = getNewCanvasAndCtx(img.width, img.height)\r\n ctx.drawImage(img, 0, 0, canvas.width, canvas.height)\r\n return canvas\r\n}\r\n\r\n/**\r\n * drawFileInCanvas\r\n *\r\n * @param {File | Blob} file\r\n * @returns {Promise<[ImageBitmap | HTMLImageElement, HTMLCanvasElement | OffscreenCanvas]>}\r\n */\r\nexport async function drawFileInCanvas (file) {\r\n let img\r\n try {\r\n img = await createImageBitmap(file)\r\n } catch (e) {\r\n const dataUrl = await getDataUrlFromFile(file)\r\n img = await loadImage(dataUrl)\r\n }\r\n const canvas = drawImageInCanvas(img)\r\n return [img, canvas]\r\n}\r\n\r\n/**\r\n * canvasToFile\r\n *\r\n * @param {HTMLCanvasElement | OffscreenCanvas} canvas\r\n * @param {string} fileType\r\n * @param {string} fileName\r\n * @param {number} fileLastModified\r\n * @param {number} [quality]\r\n * @returns {Promise}\r\n */\r\nexport async function canvasToFile (canvas, fileType, fileName, fileLastModified, quality = 1) {\r\n let file\r\n if (typeof OffscreenCanvas === 'function' && canvas instanceof OffscreenCanvas) {\r\n file = await canvas.convertToBlob({ type: fileType, quality })\r\n file.name = fileName\r\n file.lastModified = fileLastModified\r\n } else {\r\n const dataUrl = canvas.toDataURL(fileType, quality)\r\n file = await getFilefromDataUrl(dataUrl, fileName, fileLastModified)\r\n }\r\n return file\r\n}\r\n\r\n/**\r\n * getExifOrientation\r\n * get image exif orientation info\r\n * source: https://stackoverflow.com/a/32490603/10395024\r\n *\r\n * @param {File | Blob} file\r\n * @returns {Promise} - orientation id, see https://i.stack.imgur.com/VGsAj.gif\r\n */\r\nexport function getExifOrientation (file) {\r\n return new Promise((resolve, reject) => {\r\n const reader = new CustomFileReader()\r\n reader.onload = (e) => {\r\n const view = new DataView(e.target.result)\r\n if (view.getUint16(0, false) != 0xFFD8) {\r\n return resolve(-2)\r\n }\r\n const length = view.byteLength\r\n let offset = 2\r\n while (offset < length) {\r\n if (view.getUint16(offset + 2, false) <= 8) return resolve(-1)\r\n const marker = view.getUint16(offset, false)\r\n offset += 2\r\n if (marker == 0xFFE1) {\r\n if (view.getUint32(offset += 2, false) != 0x45786966) {\r\n return resolve(-1)\r\n }\r\n\r\n var little = view.getUint16(offset += 6, false) == 0x4949\r\n offset += view.getUint32(offset + 4, little)\r\n var tags = view.getUint16(offset, little)\r\n offset += 2\r\n for (var i = 0; i < tags; i++) {\r\n if (view.getUint16(offset + (i * 12), little) == 0x0112) {\r\n return resolve(view.getUint16(offset + (i * 12) + 8, little))\r\n }\r\n }\r\n } else if ((marker & 0xFF00) != 0xFF00) {\r\n break\r\n } else {\r\n offset += view.getUint16(offset, false)\r\n }\r\n }\r\n return resolve(-1)\r\n }\r\n reader.onerror = (e) => reject(e)\r\n reader.readAsArrayBuffer(file)\r\n })\r\n}\r\n\r\n/**\r\n *\r\n * @param {HTMLCanvasElement | OffscreenCanvas} canvas\r\n * @param options\r\n * @returns {HTMLCanvasElement | OffscreenCanvas}\r\n */\r\nexport function handleMaxWidthOrHeight (canvas, options) {\r\n const width = canvas.width\r\n const height = canvas.height\r\n const maxWidthOrHeight = options.maxWidthOrHeight\r\n\r\n const needToHandle = Number.isFinite(maxWidthOrHeight) && (width > maxWidthOrHeight || height > maxWidthOrHeight)\r\n\r\n let newCanvas = canvas\r\n let ctx\r\n\r\n if (needToHandle) {\r\n [newCanvas, ctx] = getNewCanvasAndCtx(width, height)\r\n if (width > height) {\r\n newCanvas.width = maxWidthOrHeight\r\n newCanvas.height = (height / width) * maxWidthOrHeight\r\n } else {\r\n newCanvas.width = (width / height) * maxWidthOrHeight\r\n newCanvas.height = maxWidthOrHeight\r\n }\r\n ctx.drawImage(canvas, 0, 0, newCanvas.width, newCanvas.height)\r\n\r\n cleanupCanvasMemory(canvas)\r\n }\r\n\r\n return newCanvas\r\n}\r\n\r\n/**\r\n * followExifOrientation\r\n * source: https://stackoverflow.com/a/40867559/10395024\r\n *\r\n * @param {HTMLCanvasElement | OffscreenCanvas} canvas\r\n * @param {number} exifOrientation\r\n * @returns {HTMLCanvasElement | OffscreenCanvas} canvas\r\n */\r\nexport function followExifOrientation (canvas, exifOrientation) {\r\n const width = canvas.width\r\n const height = canvas.height\r\n\r\n const [newCanvas, ctx] = getNewCanvasAndCtx(width, height)\r\n\r\n // set proper canvas dimensions before transform & export\r\n if (4 < exifOrientation && exifOrientation < 9) {\r\n newCanvas.width = height\r\n newCanvas.height = width\r\n } else {\r\n newCanvas.width = width\r\n newCanvas.height = height\r\n }\r\n\r\n // transform context before drawing image\r\n switch (exifOrientation) {\r\n case 2: ctx.transform(-1, 0, 0, 1, width, 0); break;\r\n case 3: ctx.transform(-1, 0, 0, -1, width, height); break;\r\n case 4: ctx.transform(1, 0, 0, -1, 0, height); break;\r\n case 5: ctx.transform(0, 1, 1, 0, 0, 0); break;\r\n case 6: ctx.transform(0, 1, -1, 0, height, 0); break;\r\n case 7: ctx.transform(0, -1, -1, 0, height, width); break;\r\n case 8: ctx.transform(0, -1, 1, 0, 0, width); break;\r\n default: break;\r\n }\r\n\r\n ctx.drawImage(canvas, 0, 0, width, height)\r\n\r\n cleanupCanvasMemory(canvas)\r\n\r\n return newCanvas\r\n}\r\n\r\n/**\r\n * get new Canvas and it's context\r\n * @param width\r\n * @param height\r\n * @returns {[HTMLCanvasElement | OffscreenCanvas, CanvasRenderingContext2D]}\r\n */\r\nexport function getNewCanvasAndCtx (width, height) {\r\n let canvas\r\n let ctx\r\n try {\r\n canvas = new OffscreenCanvas(width, height)\r\n ctx = canvas.getContext('2d')\r\n if (ctx === null) {\r\n throw new Error('getContext of OffscreenCanvas returns null')\r\n }\r\n } catch (e) {\r\n canvas = document.createElement('canvas')\r\n ctx = canvas.getContext('2d')\r\n }\r\n canvas.width = width\r\n canvas.height = height\r\n return [canvas, ctx]\r\n}\r\n\r\n/**\r\n * clear Canvas memory\r\n * @param canvas\r\n * @returns null\r\n */\r\nexport function cleanupCanvasMemory (canvas) {\r\n canvas.width = 0\r\n canvas.height = 0\r\n}\r\n\r\nif (isBrowser) {\r\n// Polyfill for Number.isInteger\r\n Number.isInteger = Number.isInteger || function (value) {\r\n return typeof value === 'number' &&\r\n isFinite(value) &&\r\n Math.floor(value) === value\r\n }\r\n}\r\n","import {\r\n canvasToFile,\r\n cleanupCanvasMemory,\r\n drawFileInCanvas,\r\n followExifOrientation,\r\n getExifOrientation,\r\n getNewCanvasAndCtx,\r\n handleMaxWidthOrHeight,\r\n isAutoOrientationInBrowser\r\n} from './utils'\r\n\r\n/**\r\n * Compress an image file.\r\n *\r\n * @param {File} file\r\n * @param {Object} options - { maxSizeMB=Number.POSITIVE_INFINITY, maxWidthOrHeight, useWebWorker=false, maxIteration = 10, exifOrientation, fileType }\r\n * @param {number} [options.maxSizeMB=Number.POSITIVE_INFINITY]\r\n * @param {number} [options.maxWidthOrHeight=undefined]\r\n * @param {number} [options.maxIteration=10]\r\n * @param {number} [options.exifOrientation] - default to be the exif orientation from the image file\r\n * @param {Function} [options.onProgress] - a function takes one progress argument (progress from 0 to 100)\r\n * @param {string} [options.fileType] - default to be the original mime type from the image file\r\n * @returns {Promise}\r\n */\r\nexport default async function compress (file, options) {\r\n let progress = 0\r\n\r\n function incProgress (inc = 5) {\r\n progress += inc\r\n options.onProgress(Math.min(progress, 100))\r\n }\r\n\r\n function setProgress (p) {\r\n progress = Math.min(Math.max(p, progress), 100)\r\n options.onProgress(progress)\r\n }\r\n\r\n let remainingTrials = options.maxIteration || 10\r\n\r\n const maxSizeByte = options.maxSizeMB * 1024 * 1024\r\n\r\n incProgress()\r\n\r\n // drawFileInCanvas\r\n let [img, origCanvas] = await drawFileInCanvas(file)\r\n\r\n incProgress()\r\n\r\n // handleMaxWidthOrHeight\r\n const maxWidthOrHeightFixedCanvas = handleMaxWidthOrHeight(origCanvas, options)\r\n\r\n incProgress()\r\n\r\n // exifOrientation\r\n options.exifOrientation = options.exifOrientation || await getExifOrientation(file)\r\n incProgress()\r\n const orientationFixedCanvas = (await isAutoOrientationInBrowser) ? maxWidthOrHeightFixedCanvas : followExifOrientation(maxWidthOrHeightFixedCanvas, options.exifOrientation)\r\n incProgress()\r\n\r\n let quality = 1\r\n\r\n let tempFile = await canvasToFile(orientationFixedCanvas, options.fileType || file.type, file.name, file.lastModified, quality)\r\n incProgress()\r\n\r\n const origExceedMaxSize = tempFile.size > maxSizeByte\r\n const sizeBecomeLarger = tempFile.size > file.size\r\n\r\n // check if we need to compress or resize\r\n if (!origExceedMaxSize && !sizeBecomeLarger) {\r\n // no need to compress\r\n setProgress(100)\r\n return tempFile\r\n }\r\n\r\n const sourceSize = file.size\r\n const renderedSize = tempFile.size\r\n let currentSize = renderedSize\r\n let compressedFile\r\n let newCanvas, ctx\r\n let canvas = orientationFixedCanvas\r\n while (remainingTrials-- && (currentSize > maxSizeByte || currentSize > sourceSize)) {\r\n const newWidth = origExceedMaxSize ? canvas.width * 0.95 : canvas.width\r\n const newHeight = origExceedMaxSize ? canvas.height * 0.95 : canvas.height;\r\n [newCanvas, ctx] = getNewCanvasAndCtx(newWidth, newHeight)\r\n\r\n ctx.drawImage(canvas, 0, 0, newWidth, newHeight)\r\n\r\n if (file.type === 'image/jpeg') {\r\n quality *= 0.95\r\n }\r\n compressedFile = await canvasToFile(newCanvas, options.fileType || file.type, file.name, file.lastModified, quality)\r\n\r\n cleanupCanvasMemory(canvas)\r\n\r\n canvas = newCanvas\r\n\r\n currentSize = compressedFile.size\r\n setProgress(Math.min(99, Math.floor((renderedSize - currentSize) / (renderedSize - maxSizeByte) * 100)))\r\n }\r\n\r\n // garbage clean canvas for safari\r\n // ref: https://bugs.webkit.org/show_bug.cgi?id=195325\r\n cleanupCanvasMemory(canvas)\r\n cleanupCanvasMemory(newCanvas)\r\n cleanupCanvasMemory(maxWidthOrHeightFixedCanvas)\r\n cleanupCanvasMemory(orientationFixedCanvas)\r\n cleanupCanvasMemory(origCanvas)\r\n\r\n setProgress(100)\r\n return compressedFile\r\n}\r\n","import lib from './index'\r\nimport compress from './image-compression'\r\nimport { getNewCanvasAndCtx } from './utils'\r\n\r\nlet cnt = 0\r\nlet imageCompressionLibUrl\r\nlet worker\r\n\r\nfunction createWorker (script) {\r\n if (typeof script === 'function') {\r\n script = `(${f})()`\r\n }\r\n return new Worker(URL.createObjectURL(new Blob([script])))\r\n}\r\n\r\nfunction createSourceObject (str) {\r\n return URL.createObjectURL(new Blob([str], { type: 'application/javascript' }))\r\n}\r\n\r\nfunction generateLib () {\r\n // prepare the lib to be used inside WebWorker\r\n return createSourceObject(`\r\n function imageCompression (){return (${lib}).apply(null, arguments)}\r\n\r\n imageCompression.getDataUrlFromFile = ${lib.getDataUrlFromFile}\r\n imageCompression.getFilefromDataUrl = ${lib.getFilefromDataUrl}\r\n imageCompression.loadImage = ${lib.loadImage}\r\n imageCompression.drawImageInCanvas = ${lib.drawImageInCanvas}\r\n imageCompression.drawFileInCanvas = ${lib.drawFileInCanvas}\r\n imageCompression.canvasToFile = ${lib.canvasToFile}\r\n imageCompression.getExifOrientation = ${lib.getExifOrientation}\r\n imageCompression.handleMaxWidthOrHeight = ${lib.handleMaxWidthOrHeight}\r\n imageCompression.followExifOrientation = ${lib.followExifOrientation}\r\n imageCompression.cleanupMemory = ${lib.cleanupMemory}\r\n\r\n getDataUrlFromFile = imageCompression.getDataUrlFromFile\r\n getFilefromDataUrl = imageCompression.getFilefromDataUrl\r\n loadImage = imageCompression.loadImage\r\n drawImageInCanvas = imageCompression.drawImageInCanvas\r\n drawFileInCanvas = imageCompression.drawFileInCanvas\r\n canvasToFile = imageCompression.canvasToFile\r\n getExifOrientation = imageCompression.getExifOrientation\r\n handleMaxWidthOrHeight = imageCompression.handleMaxWidthOrHeight\r\n followExifOrientation = imageCompression.followExifOrientation\r\n cleanupMemory = imageCompression.cleanupMemory\r\n\r\n getNewCanvasAndCtx = ${getNewCanvasAndCtx}\r\n \r\n CustomFileReader = FileReader\r\n \r\n CustomFile = File\r\n \r\n function _slicedToArray(arr, n) { return arr }\r\n \r\n function _typeof(a) { return typeof a }\r\n\r\n function compress (){return (${compress}).apply(null, arguments)}\r\n `)\r\n}\r\n\r\nfunction generateWorkerScript () {\r\n // code to be run in the WebWorker\r\n return createWorker(`\r\n let scriptImported = false\r\n self.addEventListener('message', async (e) => {\r\n const { file, id, imageCompressionLibUrl, options } = e.data\r\n options.onProgress = (progress) => self.postMessage({ progress, id })\r\n try {\r\n if (!scriptImported) {\r\n // console.log('[worker] importScripts', imageCompressionLibUrl)\r\n self.importScripts(imageCompressionLibUrl)\r\n scriptImported = true\r\n }\r\n // console.log('[worker] self', self)\r\n const compressedFile = await imageCompression(file, options)\r\n self.postMessage({ file: compressedFile, id })\r\n } catch (e) {\r\n // console.error('[worker] error', e)\r\n self.postMessage({ error: e.message + '\\\\n' + e.stack, id })\r\n }\r\n })\r\n `)\r\n}\r\n\r\nexport function compressOnWebWorker (file, options) {\r\n return new Promise(async (resolve, reject) => {\r\n let id = cnt++\r\n\r\n if (!imageCompressionLibUrl) {\r\n imageCompressionLibUrl = generateLib()\r\n }\r\n\r\n if (!worker) {\r\n worker = generateWorkerScript()\r\n }\r\n\r\n function handler (e) {\r\n if (e.data.id === id) {\r\n if (e.data.progress !== undefined) {\r\n options.onProgress(e.data.progress)\r\n return\r\n }\r\n worker.removeEventListener('message', handler)\r\n if (e.data.error) {\r\n reject(new Error(e.data.error))\r\n }\r\n resolve(e.data.file)\r\n }\r\n }\r\n\r\n worker.addEventListener('message', handler)\r\n worker.postMessage({\r\n file,\r\n id,\r\n imageCompressionLibUrl,\r\n options: { ...options, onProgress: undefined }\r\n })\r\n })\r\n}\r\n","import compress from './image-compression'\r\nimport {\r\n canvasToFile,\r\n drawFileInCanvas,\r\n drawImageInCanvas,\r\n getDataUrlFromFile,\r\n getFilefromDataUrl,\r\n loadImage,\r\n getExifOrientation,\r\n handleMaxWidthOrHeight,\r\n followExifOrientation,\r\n CustomFile,\r\n cleanupCanvasMemory\r\n} from './utils'\r\nimport { compressOnWebWorker } from './web-worker'\r\n\r\n/**\r\n * Compress an image file.\r\n *\r\n * @param {File} file\r\n * @param {Object} options - { maxSizeMB=Number.POSITIVE_INFINITY, maxWidthOrHeight, useWebWorker=false, maxIteration = 10, exifOrientation, fileType }\r\n * @param {number} [options.maxSizeMB=Number.POSITIVE_INFINITY]\r\n * @param {number} [options.maxWidthOrHeight=undefined]\r\n * @param {boolean} [options.useWebWorker=false]\r\n * @param {number} [options.maxIteration=10]\r\n * @param {number} [options.exifOrientation] - default to be the exif orientation from the image file\r\n * @param {Function} [options.onProgress] - a function takes one progress argument (progress from 0 to 100)\r\n * @param {string} [options.fileType] - default to be the original mime type from the image file\r\n * @returns {Promise}\r\n */\r\nasync function imageCompression (file, options) {\r\n\r\n let compressedFile\r\n\r\n options.maxSizeMB = options.maxSizeMB || Number.POSITIVE_INFINITY\r\n const useWebWorker = typeof options.useWebWorker === 'boolean' ? options.useWebWorker : true\r\n delete options.useWebWorker\r\n if (options.onProgress === undefined) {\r\n options.onProgress = () => {}\r\n }\r\n\r\n if (!(file instanceof Blob || file instanceof CustomFile)) {\r\n throw new Error('The file given is not an instance of Blob or File')\r\n } else if (!/^image/.test(file.type)) {\r\n throw new Error('The file given is not an image')\r\n }\r\n\r\n // try run in web worker, fall back to run in main thread\r\n const inWebWorker = typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope\r\n\r\n // if ((useWebWorker && typeof Worker === 'function') || inWebWorker) {\r\n // console.log('run compression in web worker')\r\n // } else {\r\n // console.log('run compression in main thread')\r\n // }\r\n\r\n if (useWebWorker && typeof Worker === 'function' && !inWebWorker) {\r\n try {\r\n // console.log(1)\r\n // \"compressOnWebWorker\" is kind of like a recursion to call \"imageCompression\" again inside web worker\r\n compressedFile = await compressOnWebWorker(file, options)\r\n } catch (e) {\r\n // console.warn('Run compression in web worker failed:', e, ', fall back to main thread')\r\n // console.log(1.5)\r\n compressedFile = await compress(file, options)\r\n }\r\n } else {\r\n // console.log(2)\r\n compressedFile = await compress(file, options)\r\n }\r\n\r\n try {\r\n compressedFile.name = file.name\r\n compressedFile.lastModified = file.lastModified\r\n } catch (e) {}\r\n\r\n return compressedFile\r\n}\r\n\r\nimageCompression.getDataUrlFromFile = getDataUrlFromFile\r\nimageCompression.getFilefromDataUrl = getFilefromDataUrl\r\nimageCompression.loadImage = loadImage\r\nimageCompression.drawImageInCanvas = drawImageInCanvas\r\nimageCompression.drawFileInCanvas = drawFileInCanvas\r\nimageCompression.canvasToFile = canvasToFile\r\nimageCompression.getExifOrientation = getExifOrientation\r\n\r\nimageCompression.handleMaxWidthOrHeight = handleMaxWidthOrHeight\r\nimageCompression.followExifOrientation = followExifOrientation\r\nimageCompression.cleanupMemory = cleanupCanvasMemory\r\nimageCompression.version = '1.0.10'\r\n\r\nexport default imageCompression\r\n"],"names":["isBrowser","window","CustomFile","getFilefromDataUrl","resolve","n","bstr","charCodeAt","file","e","img","type","fileName","length","offset","i","height","maxWidthOrHeight","options","ctx","width","getNewCanvasAndCtx","transform","Error","canvas","inc","then","$await_6","maxSizeByte","currentSize","imageCompressionLibUrl","cnt","generateLib","script","imageCompression","getDataUrlFromFile","loadImage","drawImageInCanvas","drawFileInCanvas","canvasToFile","getExifOrientation"],"mappings":";;;;;;;ghEAAA,IAAMA,EAA8B,oBAAlBC,2EAKoF,wBAIzFC,6vCA6CN,SAASC,oHACP,SAAAC,8DAGa,IAClBC,EAAIC,uCAGFA,EAAAC,uEAGFC,8IA6DMC,6FAHAC,4wBAmB4BC,yDAAEC,+aAOtCC,mPAoBI,mEAE8BC,EAAA,GAAxBC,kIAYOP,2DAYnBQ,WACIC,EAAiBC,iHAcnBC,+CAQEC,EAAAJ,gCAGE,4FA8BFI,sCAGmBC,iHAQXF,EAAAG,qCAAiCH,EAAAG,6EACjCH,EAAAG,UAAc,EAAG,EAAG,sBAAmBH,EAAAG,6EACvCH,EAAAG,UAAc,GAAI,EAAG,yEAsB7BD,mBAASD,oBAMT,QADJD,uDAEI,IAAAI,oDAEF,MAAOd,GAEPU,GADAK,kFAUAA,EAAAL,gLC5PqDM,kdAQrDC,KAAA,SAAAC,gBAAAA,8XAiBIC,6FAeFC,6FAGYb,88BC5FhB,IACIc,IADAC,IAcJ,SAASC,okEAPPC,mnDC4DwBzB,wzBAa1B0B,iBAAAC,mBAAsCA,mBAEtCD,iBAAA/B,sCACA+B,iBAAAE,oBACAF,iBAAAG,oCACAH,iBAAAI,kCAEAJ,iBAAAK,0BA5FAL,iBAAAM"} \ No newline at end of file +{"version":3,"file":"browser-image-compression.js","sources":["../lib/utils.js","../lib/image-compression.js","../lib/web-worker.js","../lib/index.js"],"sourcesContent":["const isBrowser = typeof window !== 'undefined' // change browser environment to support SSR\r\n\r\n// add support for cordova-plugin-file\r\nconst moduleMapper = isBrowser && window.cordova && window.cordova.require && window.cordova.require('cordova/modulemapper')\r\nexport const CustomFile = isBrowser && ((moduleMapper && moduleMapper.getOriginalSymbol(window, 'File')) || File)\r\nexport const CustomFileReader = isBrowser && ((moduleMapper && moduleMapper.getOriginalSymbol(window, 'FileReader')) || FileReader)\r\n\r\n// Check if browser supports automatic image orientation\r\n// see https://github.com/blueimp/JavaScript-Load-Image/blob/1e4df707821a0afcc11ea0720ee403b8759f3881/js/load-image-orientation.js#L37-L53\r\nexport const isAutoOrientationInBrowser = (async () => {\r\n // black 2x1 JPEG, with the following meta information set:\r\n // EXIF Orientation: 6 (Rotated 90° CCW)\r\n const testImageURL =\r\n 'data:image/jpeg;base64,/9j/4QAiRXhpZgAATU0AKgAAAAgAAQESAAMAAAABAAYAAAA' +\r\n 'AAAD/2wCEAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBA' +\r\n 'QEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE' +\r\n 'BAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAf/AABEIAAEAAgMBEQACEQEDEQH/x' +\r\n 'ABKAAEAAAAAAAAAAAAAAAAAAAALEAEAAAAAAAAAAAAAAAAAAAAAAQEAAAAAAAAAAAAAAAA' +\r\n 'AAAAAEQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIRAxEAPwA/8H//2Q=='\r\n const testImageFile = await getFilefromDataUrl(testImageURL, 'test.jpg', Date.now())\r\n\r\n const testImageCanvas = (await drawFileInCanvas(testImageFile))[1]\r\n const testImageFile2 = await canvasToFile(testImageCanvas, testImageFile.type, testImageFile.name, testImageFile.lastModified)\r\n cleanupCanvasMemory(testImageCanvas)\r\n const testImageURL2 = await getDataUrlFromFile(testImageFile2)\r\n const img = await loadImage(testImageURL2)\r\n // console.log('img', img.width, img.height)\r\n\r\n return img.width === 1 && img.height === 2\r\n})()\r\n\r\n/**\r\n * getDataUrlFromFile\r\n *\r\n * @param {File | Blob} file\r\n * @returns {Promise}\r\n */\r\nexport function getDataUrlFromFile (file) {\r\n return new Promise((resolve, reject) => {\r\n const reader = new CustomFileReader()\r\n reader.onload = () => resolve(reader.result)\r\n reader.onerror = (e) => reject(e)\r\n reader.readAsDataURL(file)\r\n })\r\n}\r\n\r\n/**\r\n * getFilefromDataUrl\r\n *\r\n * @param {string} dataUrl\r\n * @param {string} filename\r\n * @param {number} [lastModified=Date.now()]\r\n * @returns {Promise}\r\n */\r\nexport function getFilefromDataUrl (dataUrl, filename, lastModified = Date.now()) {\r\n return new Promise((resolve) => {\r\n const arr = dataUrl.split(',')\r\n const mime = arr[0].match(/:(.*?);/)[1]\r\n const bstr = atob(arr[1])\r\n let n = bstr.length\r\n const u8arr = new Uint8Array(n)\r\n while (n--) {\r\n u8arr[n] = bstr.charCodeAt(n)\r\n }\r\n const file = new Blob([u8arr], { type: mime })\r\n file.name = filename\r\n file.lastModified = lastModified\r\n resolve(file)\r\n\r\n // Safari has issue with File constructor not being able to POST in FormData\r\n // https://github.com/Donaldcwl/browser-image-compression/issues/8\r\n // https://bugs.webkit.org/show_bug.cgi?id=165081\r\n // let file\r\n // try {\r\n // file = new File([u8arr], filename, { type: mime }) // Edge do not support File constructor\r\n // } catch (e) {\r\n // file = new Blob([u8arr], { type: mime })\r\n // file.name = filename\r\n // file.lastModified = lastModified\r\n // }\r\n // resolve(file)\r\n })\r\n}\r\n\r\n/**\r\n * loadImage\r\n *\r\n * @param {string} src\r\n * @returns {Promise}\r\n */\r\nexport function loadImage (src) {\r\n return new Promise((resolve, reject) => {\r\n const img = new Image()\r\n img.onload = () => resolve(img)\r\n img.onerror = (e) => reject(e)\r\n img.src = src\r\n })\r\n}\r\n\r\n/**\r\n * drawImageInCanvas\r\n *\r\n * @param {HTMLImageElement} img\r\n * @returns {HTMLCanvasElement | OffscreenCanvas}\r\n */\r\nexport function drawImageInCanvas (img) {\r\n const [canvas, ctx] = getNewCanvasAndCtx(img.width, img.height)\r\n ctx.drawImage(img, 0, 0, canvas.width, canvas.height)\r\n return canvas\r\n}\r\n\r\n/**\r\n * drawFileInCanvas\r\n *\r\n * @param {File | Blob} file\r\n * @returns {Promise<[ImageBitmap | HTMLImageElement, HTMLCanvasElement | OffscreenCanvas]>}\r\n */\r\nexport async function drawFileInCanvas (file) {\r\n let img\r\n try {\r\n img = await createImageBitmap(file)\r\n } catch (e) {\r\n const dataUrl = await getDataUrlFromFile(file)\r\n img = await loadImage(dataUrl)\r\n }\r\n const canvas = drawImageInCanvas(img)\r\n return [img, canvas]\r\n}\r\n\r\n/**\r\n * canvasToFile\r\n *\r\n * @param {HTMLCanvasElement | OffscreenCanvas} canvas\r\n * @param {string} fileType\r\n * @param {string} fileName\r\n * @param {number} fileLastModified\r\n * @param {number} [quality]\r\n * @returns {Promise}\r\n */\r\nexport async function canvasToFile (canvas, fileType, fileName, fileLastModified, quality = 1) {\r\n let file\r\n if (typeof OffscreenCanvas === 'function' && canvas instanceof OffscreenCanvas) {\r\n file = await canvas.convertToBlob({ type: fileType, quality })\r\n file.name = fileName\r\n file.lastModified = fileLastModified\r\n } else {\r\n const dataUrl = canvas.toDataURL(fileType, quality)\r\n file = await getFilefromDataUrl(dataUrl, fileName, fileLastModified)\r\n }\r\n return file\r\n}\r\n\r\n/**\r\n * getExifOrientation\r\n * get image exif orientation info\r\n * source: https://stackoverflow.com/a/32490603/10395024\r\n *\r\n * @param {File | Blob} file\r\n * @returns {Promise} - orientation id, see https://i.stack.imgur.com/VGsAj.gif\r\n */\r\nexport function getExifOrientation (file) {\r\n return new Promise((resolve, reject) => {\r\n const reader = new CustomFileReader()\r\n reader.onload = (e) => {\r\n const view = new DataView(e.target.result)\r\n if (view.getUint16(0, false) != 0xFFD8) {\r\n return resolve(-2)\r\n }\r\n const length = view.byteLength\r\n let offset = 2\r\n while (offset < length) {\r\n if (view.getUint16(offset + 2, false) <= 8) return resolve(-1)\r\n const marker = view.getUint16(offset, false)\r\n offset += 2\r\n if (marker == 0xFFE1) {\r\n if (view.getUint32(offset += 2, false) != 0x45786966) {\r\n return resolve(-1)\r\n }\r\n\r\n var little = view.getUint16(offset += 6, false) == 0x4949\r\n offset += view.getUint32(offset + 4, little)\r\n var tags = view.getUint16(offset, little)\r\n offset += 2\r\n for (var i = 0; i < tags; i++) {\r\n if (view.getUint16(offset + (i * 12), little) == 0x0112) {\r\n return resolve(view.getUint16(offset + (i * 12) + 8, little))\r\n }\r\n }\r\n } else if ((marker & 0xFF00) != 0xFF00) {\r\n break\r\n } else {\r\n offset += view.getUint16(offset, false)\r\n }\r\n }\r\n return resolve(-1)\r\n }\r\n reader.onerror = (e) => reject(e)\r\n reader.readAsArrayBuffer(file)\r\n })\r\n}\r\n\r\n/**\r\n *\r\n * @param {HTMLCanvasElement | OffscreenCanvas} canvas\r\n * @param options\r\n * @returns {HTMLCanvasElement | OffscreenCanvas}\r\n */\r\nexport function handleMaxWidthOrHeight (canvas, options) {\r\n const width = canvas.width\r\n const height = canvas.height\r\n const maxWidthOrHeight = options.maxWidthOrHeight\r\n\r\n const needToHandle = isFinite(maxWidthOrHeight) && (width > maxWidthOrHeight || height > maxWidthOrHeight)\r\n\r\n let newCanvas = canvas\r\n let ctx\r\n\r\n if (needToHandle) {\r\n [newCanvas, ctx] = getNewCanvasAndCtx(width, height)\r\n if (width > height) {\r\n newCanvas.width = maxWidthOrHeight\r\n newCanvas.height = (height / width) * maxWidthOrHeight\r\n } else {\r\n newCanvas.width = (width / height) * maxWidthOrHeight\r\n newCanvas.height = maxWidthOrHeight\r\n }\r\n ctx.drawImage(canvas, 0, 0, newCanvas.width, newCanvas.height)\r\n\r\n cleanupCanvasMemory(canvas)\r\n }\r\n\r\n return newCanvas\r\n}\r\n\r\n/**\r\n * followExifOrientation\r\n * source: https://stackoverflow.com/a/40867559/10395024\r\n *\r\n * @param {HTMLCanvasElement | OffscreenCanvas} canvas\r\n * @param {number} exifOrientation\r\n * @returns {HTMLCanvasElement | OffscreenCanvas} canvas\r\n */\r\nexport function followExifOrientation (canvas, exifOrientation) {\r\n const width = canvas.width\r\n const height = canvas.height\r\n\r\n const [newCanvas, ctx] = getNewCanvasAndCtx(width, height)\r\n\r\n // set proper canvas dimensions before transform & export\r\n if (4 < exifOrientation && exifOrientation < 9) {\r\n newCanvas.width = height\r\n newCanvas.height = width\r\n } else {\r\n newCanvas.width = width\r\n newCanvas.height = height\r\n }\r\n\r\n // transform context before drawing image\r\n switch (exifOrientation) {\r\n case 2: ctx.transform(-1, 0, 0, 1, width, 0); break;\r\n case 3: ctx.transform(-1, 0, 0, -1, width, height); break;\r\n case 4: ctx.transform(1, 0, 0, -1, 0, height); break;\r\n case 5: ctx.transform(0, 1, 1, 0, 0, 0); break;\r\n case 6: ctx.transform(0, 1, -1, 0, height, 0); break;\r\n case 7: ctx.transform(0, -1, -1, 0, height, width); break;\r\n case 8: ctx.transform(0, -1, 1, 0, 0, width); break;\r\n default: break;\r\n }\r\n\r\n ctx.drawImage(canvas, 0, 0, width, height)\r\n\r\n cleanupCanvasMemory(canvas)\r\n\r\n return newCanvas\r\n}\r\n\r\n/**\r\n * get new Canvas and it's context\r\n * @param width\r\n * @param height\r\n * @returns {[HTMLCanvasElement | OffscreenCanvas, CanvasRenderingContext2D]}\r\n */\r\nexport function getNewCanvasAndCtx (width, height) {\r\n let canvas\r\n let ctx\r\n try {\r\n canvas = new OffscreenCanvas(width, height)\r\n ctx = canvas.getContext('2d')\r\n if (ctx === null) {\r\n throw new Error('getContext of OffscreenCanvas returns null')\r\n }\r\n } catch (e) {\r\n canvas = document.createElement('canvas')\r\n ctx = canvas.getContext('2d')\r\n }\r\n canvas.width = width\r\n canvas.height = height\r\n return [canvas, ctx]\r\n}\r\n\r\n/**\r\n * clear Canvas memory\r\n * @param canvas\r\n * @returns null\r\n */\r\nexport function cleanupCanvasMemory (canvas) {\r\n canvas.width = 0\r\n canvas.height = 0\r\n}\r\n\r\nif (isBrowser) {\r\n// Polyfill for Number.isInteger\r\n Number.isInteger = Number.isInteger || function (value) {\r\n return typeof value === 'number' &&\r\n isFinite(value) &&\r\n Math.floor(value) === value\r\n }\r\n}\r\n","import {\r\n canvasToFile,\r\n cleanupCanvasMemory,\r\n drawFileInCanvas,\r\n followExifOrientation,\r\n getExifOrientation,\r\n getNewCanvasAndCtx,\r\n handleMaxWidthOrHeight,\r\n isAutoOrientationInBrowser\r\n} from './utils'\r\n\r\n/**\r\n * Compress an image file.\r\n *\r\n * @param {File} file\r\n * @param {Object} options - { maxSizeMB=Number.POSITIVE_INFINITY, maxWidthOrHeight, useWebWorker=false, maxIteration = 10, exifOrientation, fileType }\r\n * @param {number} [options.maxSizeMB=Number.POSITIVE_INFINITY]\r\n * @param {number} [options.maxWidthOrHeight=undefined]\r\n * @param {number} [options.maxIteration=10]\r\n * @param {number} [options.exifOrientation] - default to be the exif orientation from the image file\r\n * @param {Function} [options.onProgress] - a function takes one progress argument (progress from 0 to 100)\r\n * @param {string} [options.fileType] - default to be the original mime type from the image file\r\n * @returns {Promise}\r\n */\r\nexport default async function compress (file, options) {\r\n let progress = 0\r\n\r\n function incProgress (inc = 5) {\r\n progress += inc\r\n options.onProgress(Math.min(progress, 100))\r\n }\r\n\r\n function setProgress (p) {\r\n progress = Math.min(Math.max(p, progress), 100)\r\n options.onProgress(progress)\r\n }\r\n\r\n let remainingTrials = options.maxIteration || 10\r\n\r\n const maxSizeByte = options.maxSizeMB * 1024 * 1024\r\n\r\n incProgress()\r\n\r\n // drawFileInCanvas\r\n let [img, origCanvas] = await drawFileInCanvas(file)\r\n\r\n incProgress()\r\n\r\n // handleMaxWidthOrHeight\r\n const maxWidthOrHeightFixedCanvas = handleMaxWidthOrHeight(origCanvas, options)\r\n\r\n incProgress()\r\n\r\n // exifOrientation\r\n options.exifOrientation = options.exifOrientation || await getExifOrientation(file)\r\n incProgress()\r\n const orientationFixedCanvas = (await isAutoOrientationInBrowser) ? maxWidthOrHeightFixedCanvas : followExifOrientation(maxWidthOrHeightFixedCanvas, options.exifOrientation)\r\n incProgress()\r\n\r\n let quality = 1\r\n\r\n let tempFile = await canvasToFile(orientationFixedCanvas, options.fileType || file.type, file.name, file.lastModified, quality)\r\n incProgress()\r\n\r\n const origExceedMaxSize = tempFile.size > maxSizeByte\r\n const sizeBecomeLarger = tempFile.size > file.size\r\n\r\n // check if we need to compress or resize\r\n if (!origExceedMaxSize && !sizeBecomeLarger) {\r\n // no need to compress\r\n setProgress(100)\r\n return tempFile\r\n }\r\n\r\n const sourceSize = file.size\r\n const renderedSize = tempFile.size\r\n let currentSize = renderedSize\r\n let compressedFile\r\n let newCanvas, ctx\r\n let canvas = orientationFixedCanvas\r\n while (remainingTrials-- && (currentSize > maxSizeByte || currentSize > sourceSize)) {\r\n const newWidth = origExceedMaxSize ? canvas.width * 0.95 : canvas.width\r\n const newHeight = origExceedMaxSize ? canvas.height * 0.95 : canvas.height;\r\n [newCanvas, ctx] = getNewCanvasAndCtx(newWidth, newHeight)\r\n\r\n ctx.drawImage(canvas, 0, 0, newWidth, newHeight)\r\n\r\n if (file.type === 'image/jpeg') {\r\n quality *= 0.95\r\n }\r\n compressedFile = await canvasToFile(newCanvas, options.fileType || file.type, file.name, file.lastModified, quality)\r\n\r\n cleanupCanvasMemory(canvas)\r\n\r\n canvas = newCanvas\r\n\r\n currentSize = compressedFile.size\r\n setProgress(Math.min(99, Math.floor((renderedSize - currentSize) / (renderedSize - maxSizeByte) * 100)))\r\n }\r\n\r\n // garbage clean canvas for safari\r\n // ref: https://bugs.webkit.org/show_bug.cgi?id=195325\r\n cleanupCanvasMemory(canvas)\r\n cleanupCanvasMemory(newCanvas)\r\n cleanupCanvasMemory(maxWidthOrHeightFixedCanvas)\r\n cleanupCanvasMemory(orientationFixedCanvas)\r\n cleanupCanvasMemory(origCanvas)\r\n\r\n setProgress(100)\r\n return compressedFile\r\n}\r\n","import lib from './index'\r\nimport compress from './image-compression'\r\nimport { getNewCanvasAndCtx } from './utils'\r\n\r\nlet cnt = 0\r\nlet imageCompressionLibUrl\r\nlet worker\r\n\r\nfunction createWorker (script) {\r\n if (typeof script === 'function') {\r\n script = `(${f})()`\r\n }\r\n return new Worker(URL.createObjectURL(new Blob([script])))\r\n}\r\n\r\nfunction createSourceObject (str) {\r\n return URL.createObjectURL(new Blob([str], { type: 'application/javascript' }))\r\n}\r\n\r\nfunction generateLib () {\r\n // prepare the lib to be used inside WebWorker\r\n return createSourceObject(`\r\n function imageCompression (){return (${lib}).apply(null, arguments)}\r\n\r\n imageCompression.getDataUrlFromFile = ${lib.getDataUrlFromFile}\r\n imageCompression.getFilefromDataUrl = ${lib.getFilefromDataUrl}\r\n imageCompression.loadImage = ${lib.loadImage}\r\n imageCompression.drawImageInCanvas = ${lib.drawImageInCanvas}\r\n imageCompression.drawFileInCanvas = ${lib.drawFileInCanvas}\r\n imageCompression.canvasToFile = ${lib.canvasToFile}\r\n imageCompression.getExifOrientation = ${lib.getExifOrientation}\r\n imageCompression.handleMaxWidthOrHeight = ${lib.handleMaxWidthOrHeight}\r\n imageCompression.followExifOrientation = ${lib.followExifOrientation}\r\n imageCompression.cleanupMemory = ${lib.cleanupMemory}\r\n\r\n getDataUrlFromFile = imageCompression.getDataUrlFromFile\r\n getFilefromDataUrl = imageCompression.getFilefromDataUrl\r\n loadImage = imageCompression.loadImage\r\n drawImageInCanvas = imageCompression.drawImageInCanvas\r\n drawFileInCanvas = imageCompression.drawFileInCanvas\r\n canvasToFile = imageCompression.canvasToFile\r\n getExifOrientation = imageCompression.getExifOrientation\r\n handleMaxWidthOrHeight = imageCompression.handleMaxWidthOrHeight\r\n followExifOrientation = imageCompression.followExifOrientation\r\n cleanupMemory = imageCompression.cleanupMemory\r\n\r\n getNewCanvasAndCtx = ${getNewCanvasAndCtx}\r\n \r\n CustomFileReader = FileReader\r\n \r\n CustomFile = File\r\n \r\n function _slicedToArray(arr, n) { return arr }\r\n \r\n function _typeof(a) { return typeof a }\r\n\r\n function compress (){return (${compress}).apply(null, arguments)}\r\n `)\r\n}\r\n\r\nfunction generateWorkerScript () {\r\n // code to be run in the WebWorker\r\n return createWorker(`\r\n let scriptImported = false\r\n self.addEventListener('message', async (e) => {\r\n const { file, id, imageCompressionLibUrl, options } = e.data\r\n options.onProgress = (progress) => self.postMessage({ progress, id })\r\n try {\r\n if (!scriptImported) {\r\n // console.log('[worker] importScripts', imageCompressionLibUrl)\r\n self.importScripts(imageCompressionLibUrl)\r\n scriptImported = true\r\n }\r\n // console.log('[worker] self', self)\r\n const compressedFile = await imageCompression(file, options)\r\n self.postMessage({ file: compressedFile, id })\r\n } catch (e) {\r\n // console.error('[worker] error', e)\r\n self.postMessage({ error: e.message + '\\\\n' + e.stack, id })\r\n }\r\n })\r\n `)\r\n}\r\n\r\nexport function compressOnWebWorker (file, options) {\r\n return new Promise(async (resolve, reject) => {\r\n let id = cnt++\r\n\r\n if (!imageCompressionLibUrl) {\r\n imageCompressionLibUrl = generateLib()\r\n }\r\n\r\n if (!worker) {\r\n worker = generateWorkerScript()\r\n }\r\n\r\n function handler (e) {\r\n if (e.data.id === id) {\r\n if (e.data.progress !== undefined) {\r\n options.onProgress(e.data.progress)\r\n return\r\n }\r\n worker.removeEventListener('message', handler)\r\n if (e.data.error) {\r\n reject(new Error(e.data.error))\r\n }\r\n resolve(e.data.file)\r\n }\r\n }\r\n\r\n worker.addEventListener('message', handler)\r\n worker.addEventListener('error', reject)\r\n\r\n worker.postMessage({\r\n file,\r\n id,\r\n imageCompressionLibUrl,\r\n options: { ...options, onProgress: undefined }\r\n })\r\n })\r\n}\r\n","import compress from './image-compression'\r\nimport {\r\n canvasToFile,\r\n drawFileInCanvas,\r\n drawImageInCanvas,\r\n getDataUrlFromFile,\r\n getFilefromDataUrl,\r\n loadImage,\r\n getExifOrientation,\r\n handleMaxWidthOrHeight,\r\n followExifOrientation,\r\n CustomFile,\r\n cleanupCanvasMemory\r\n} from './utils'\r\nimport { compressOnWebWorker } from './web-worker'\r\n\r\n/**\r\n * Compress an image file.\r\n *\r\n * @param {File} file\r\n * @param {Object} options - { maxSizeMB=Number.POSITIVE_INFINITY, maxWidthOrHeight, useWebWorker=false, maxIteration = 10, exifOrientation, fileType }\r\n * @param {number} [options.maxSizeMB=Number.POSITIVE_INFINITY]\r\n * @param {number} [options.maxWidthOrHeight=undefined]\r\n * @param {boolean} [options.useWebWorker=false]\r\n * @param {number} [options.maxIteration=10]\r\n * @param {number} [options.exifOrientation] - default to be the exif orientation from the image file\r\n * @param {Function} [options.onProgress] - a function takes one progress argument (progress from 0 to 100)\r\n * @param {string} [options.fileType] - default to be the original mime type from the image file\r\n * @returns {Promise}\r\n */\r\nasync function imageCompression (file, options) {\r\n\r\n let compressedFile\r\n\r\n options.maxSizeMB = options.maxSizeMB || Number.POSITIVE_INFINITY\r\n const useWebWorker = typeof options.useWebWorker === 'boolean' ? options.useWebWorker : true\r\n delete options.useWebWorker\r\n if (options.onProgress === undefined) {\r\n options.onProgress = () => {}\r\n }\r\n\r\n if (!(file instanceof Blob || file instanceof CustomFile)) {\r\n throw new Error('The file given is not an instance of Blob or File')\r\n } else if (!/^image/.test(file.type)) {\r\n throw new Error('The file given is not an image')\r\n }\r\n\r\n // try run in web worker, fall back to run in main thread\r\n const inWebWorker = typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope\r\n\r\n // if ((useWebWorker && typeof Worker === 'function') || inWebWorker) {\r\n // console.log('run compression in web worker')\r\n // } else {\r\n // console.log('run compression in main thread')\r\n // }\r\n\r\n if (useWebWorker && typeof Worker === 'function' && !inWebWorker) {\r\n try {\r\n // console.log(1)\r\n // \"compressOnWebWorker\" is kind of like a recursion to call \"imageCompression\" again inside web worker\r\n compressedFile = await compressOnWebWorker(file, options)\r\n } catch (e) {\r\n // console.warn('Run compression in web worker failed:', e, ', fall back to main thread')\r\n // console.log(1.5)\r\n compressedFile = await compress(file, options)\r\n }\r\n } else {\r\n // console.log(2)\r\n compressedFile = await compress(file, options)\r\n }\r\n\r\n try {\r\n compressedFile.name = file.name\r\n compressedFile.lastModified = file.lastModified\r\n } catch (e) {}\r\n\r\n return compressedFile\r\n}\r\n\r\nimageCompression.getDataUrlFromFile = getDataUrlFromFile\r\nimageCompression.getFilefromDataUrl = getFilefromDataUrl\r\nimageCompression.loadImage = loadImage\r\nimageCompression.drawImageInCanvas = drawImageInCanvas\r\nimageCompression.drawFileInCanvas = drawFileInCanvas\r\nimageCompression.canvasToFile = canvasToFile\r\nimageCompression.getExifOrientation = getExifOrientation\r\n\r\nimageCompression.handleMaxWidthOrHeight = handleMaxWidthOrHeight\r\nimageCompression.followExifOrientation = followExifOrientation\r\nimageCompression.cleanupMemory = cleanupCanvasMemory\r\nimageCompression.version = '1.0.11'\r\n\r\nexport default imageCompression\r\n"],"names":["isBrowser","window","CustomFile","getFilefromDataUrl","resolve","n","bstr","charCodeAt","file","e","img","type","fileName","length","offset","i","height","maxWidthOrHeight","options","ctx","width","getNewCanvasAndCtx","transform","Error","canvas","inc","then","$await_6","maxSizeByte","currentSize","imageCompressionLibUrl","cnt","generateLib","script","imageCompression","getDataUrlFromFile","loadImage","drawImageInCanvas","drawFileInCanvas","canvasToFile","getExifOrientation"],"mappings":";;;;;;;ghEAAA,IAAMA,EAA8B,oBAAlBC,2EAKoF,wBAIzFC,6vCA6CN,SAASC,oHACP,SAAAC,8DAGa,IAClBC,EAAIC,uCAGFA,EAAAC,uEAGFC,8IA6DMC,6FAHAC,4wBAmB4BC,yDAAEC,+aAOtCC,mPAoBI,mEAE8BC,EAAA,GAAxBC,kIAYOP,2DAYnBQ,WACIC,EAAiBC,0GAcnBC,+CAQEC,EAAAJ,gCAGE,4FA8BFI,sCAGmBC,iHAQXF,EAAAG,qCAAiCH,EAAAG,6EACjCH,EAAAG,UAAc,EAAG,EAAG,sBAAmBH,EAAAG,6EACvCH,EAAAG,UAAc,GAAI,EAAG,yEAsB7BD,mBAASD,oBAMT,QADJD,uDAEI,IAAAI,oDAEF,MAAOd,GAEPU,GADAK,kFAUAA,EAAAL,gLC5PqDM,kdAQrDC,KAAA,SAAAC,gBAAAA,8XAiBIC,6FAeFC,6FAGYb,88BC5FhB,IACIc,IADAC,IAcJ,SAASC,okEAPPC,mnDC4DwBzB,s1BAa1B0B,iBAAAC,mBAAsCA,mBAEtCD,iBAAA/B,sCACA+B,iBAAAE,oBACAF,iBAAAG,oCACAH,iBAAAI,kCAEAJ,iBAAAK,0BA5FAL,iBAAAM"} \ No newline at end of file diff --git a/dist/browser-image-compression.mjs b/dist/browser-image-compression.mjs index 72727be..ba84118 100644 --- a/dist/browser-image-compression.mjs +++ b/dist/browser-image-compression.mjs @@ -1,9 +1,9 @@ /** * Browser Image Compression - * v1.0.10 + * v1.0.11 * by Donald * https://github.com/Donaldcwl/browser-image-compression */ -function _defineProperty(e,r,n){return r in e?Object.defineProperty(e,r,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[r]=n,e}function ownKeys(e,r){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var t=Object.getOwnPropertySymbols(e);r&&(t=t.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}))),n.push.apply(n,t)}return n}function _objectSpread2(e){for(var r=1;re.length)&&(r=e.length);for(var n=0,t=new Array(r);n2&&void 0!==arguments[2]?arguments[2]:Date.now();return new Promise((function(t){for(var a=e.split(","),i=a[0].match(/:(.*?);/)[1],o=atob(a[1]),s=o.length,c=new Uint8Array(s);s--;)c[s]=o.charCodeAt(s);var u=new Blob([c],{type:i});u.name=r,u.lastModified=n,t(u)}))}function loadImage(e){return new Promise((function(r,n){var t=new Image;t.onload=function(){return r(t)},t.onerror=function(e){return n(e)},t.src=e}))}function drawImageInCanvas(e){var r=_slicedToArray(getNewCanvasAndCtx(e.width,e.height),2),n=r[0];return r[1].drawImage(e,0,0,n.width,n.height),n}function drawFileInCanvas(e){return new Promise((function(r,n){var t,a,i=function $Try_1_Post(){try{return a=drawImageInCanvas(t),r([t,a])}catch(e){return n(e)}},o=function $Try_1_Catch(r){try{return getDataUrlFromFile(e).then((function(e){try{return loadImage(e).then((function(e){try{return t=e,i()}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 t=e,i()}catch(e){return o()}}),o)}catch(e){o()}}))}function canvasToFile(e,r,n,t){var a=arguments.length>4&&void 0!==arguments[4]?arguments[4]:1;return new Promise((function(i,o){var s;return"function"==typeof OffscreenCanvas&&e instanceof OffscreenCanvas?e.convertToBlob({type:r,quality:a}).then(function(e){try{return(s=e).name=n,s.lastModified=t,$If_4.call(this)}catch(e){return o(e)}}.bind(this),o):getFilefromDataUrl(e.toDataURL(r,a),n,t).then(function(e){try{return s=e,$If_4.call(this)}catch(e){return o(e)}}.bind(this),o);function $If_4(){return i(s)}}))}function getExifOrientation(e){return new Promise((function(r,n){var t=new CustomFileReader;t.onload=function(e){var n=new DataView(e.target.result);if(65496!=n.getUint16(0,!1))return r(-2);for(var t=n.byteLength,a=2;ai||a>i)){var s=_slicedToArray(getNewCanvasAndCtx(t,a),2);o=s[0],n=s[1],t>a?(o.width=i,o.height=a/t*i):(o.width=t/a*i,o.height=i),n.drawImage(e,0,0,o.width,o.height),cleanupCanvasMemory(e)}return o}function followExifOrientation(e,r){var n=e.width,t=e.height,a=_slicedToArray(getNewCanvasAndCtx(n,t),2),i=a[0],o=a[1];switch(40&&void 0!==arguments[0]?arguments[0]:5;i+=e,r.onProgress(Math.min(i,100))}function setProgress(e){i=Math.min(Math.max(e,i),100),r.onProgress(i)}return i=0,o=r.maxIteration||10,s=1024*r.maxSizeMB*1024,incProgress(),drawFileInCanvas(e).then(function(i){try{var C=_slicedToArray(i,2);return C[0],c=C[1],incProgress(),u=handleMaxWidthOrHeight(c,r),incProgress(),new Promise((function(n,t){var a;if(!(a=r.exifOrientation))return getExifOrientation(e).then(function(e){try{return a=e,$If_2.call(this)}catch(e){return t(e)}}.bind(this),t);function $If_2(){return n(a)}return $If_2.call(this)})).then(function(i){try{return r.exifOrientation=i,incProgress(),n.then(function(n){try{return l=n?u:followExifOrientation(u,r.exifOrientation),incProgress(),f=1,canvasToFile(l,r.fileType||e.type,e.name,e.lastModified,f).then(function(n){try{{if(m=n,incProgress(),g=m.size>s,A=m.size>e.size,!g&&!A)return setProgress(100),t(m);var i;function $Loop_3(){if(o--&&(h>s||h>d)){var n,t,i=_slicedToArray(getNewCanvasAndCtx(n=g?.95*w.width:w.width,t=g?.95*w.height:w.height),2);return v=i[0],i[1].drawImage(w,0,0,n,t),"image/jpeg"===e.type&&(f*=.95),canvasToFile(v,r.fileType||e.type,e.name,e.lastModified,f).then((function(e){try{return y=e,cleanupCanvasMemory(w),w=v,h=y.size,setProgress(Math.min(99,Math.floor((p-h)/(p-s)*100))),$Loop_3}catch(e){return a(e)}}),a)}return[1]}return d=e.size,p=m.size,h=p,w=l,(i=function(e){for(;e;){if(e.then)return void e.then(i,a);try{if(e.pop){if(e.length)return e.pop()?$Loop_3_exit.call(this):e;e=$Loop_3}else e=e.call(this)}catch(e){return a(e)}}}.bind(this))($Loop_3);function $Loop_3_exit(){return cleanupCanvasMemory(w),cleanupCanvasMemory(v),cleanupCanvasMemory(u),cleanupCanvasMemory(l),cleanupCanvasMemory(c),setProgress(100),t(y)}}}catch(e){return a(e)}}.bind(this),a)}catch(e){return a(e)}}.bind(this),a)}catch(e){return a(e)}}.bind(this),a)}catch(e){return a(e)}}.bind(this),a)}))}e&&(Number.isInteger=Number.isInteger||function(e){return"number"==typeof e&&isFinite(e)&&Math.floor(e)===e});var t,a,i=0;function generateLib(){return function createSourceObject(e){return URL.createObjectURL(new Blob([e],{type:"application/javascript"}))}("\n function imageCompression (){return (".concat(imageCompression,").apply(null, arguments)}\n\n imageCompression.getDataUrlFromFile = ").concat(imageCompression.getDataUrlFromFile,"\n imageCompression.getFilefromDataUrl = ").concat(imageCompression.getFilefromDataUrl,"\n imageCompression.loadImage = ").concat(imageCompression.loadImage,"\n imageCompression.drawImageInCanvas = ").concat(imageCompression.drawImageInCanvas,"\n imageCompression.drawFileInCanvas = ").concat(imageCompression.drawFileInCanvas,"\n imageCompression.canvasToFile = ").concat(imageCompression.canvasToFile,"\n imageCompression.getExifOrientation = ").concat(imageCompression.getExifOrientation,"\n imageCompression.handleMaxWidthOrHeight = ").concat(imageCompression.handleMaxWidthOrHeight,"\n imageCompression.followExifOrientation = ").concat(imageCompression.followExifOrientation,"\n imageCompression.cleanupMemory = ").concat(imageCompression.cleanupMemory,"\n\n getDataUrlFromFile = imageCompression.getDataUrlFromFile\n getFilefromDataUrl = imageCompression.getFilefromDataUrl\n loadImage = imageCompression.loadImage\n drawImageInCanvas = imageCompression.drawImageInCanvas\n drawFileInCanvas = imageCompression.drawFileInCanvas\n canvasToFile = imageCompression.canvasToFile\n getExifOrientation = imageCompression.getExifOrientation\n handleMaxWidthOrHeight = imageCompression.handleMaxWidthOrHeight\n followExifOrientation = imageCompression.followExifOrientation\n cleanupMemory = imageCompression.cleanupMemory\n\n getNewCanvasAndCtx = ").concat(getNewCanvasAndCtx,"\n \n CustomFileReader = FileReader\n \n CustomFile = File\n \n function _slicedToArray(arr, n) { return arr }\n \n function _typeof(a) { return typeof a }\n\n function compress (){return (").concat(compress,").apply(null, arguments)}\n "))}function generateWorkerScript(){return function createWorker(e){return"function"==typeof e&&(e="(".concat(f,")()")),new Worker(URL.createObjectURL(new Blob([e])))}("\n let scriptImported = false\n self.addEventListener('message', async (e) => {\n const { file, id, imageCompressionLibUrl, options } = e.data\n options.onProgress = (progress) => self.postMessage({ progress, id })\n try {\n if (!scriptImported) {\n // console.log('[worker] importScripts', imageCompressionLibUrl)\n self.importScripts(imageCompressionLibUrl)\n scriptImported = true\n }\n // console.log('[worker] self', self)\n const compressedFile = await imageCompression(file, options)\n self.postMessage({ file: compressedFile, id })\n } catch (e) {\n // console.error('[worker] error', e)\n self.postMessage({ error: e.message + '\\n' + e.stack, id })\n }\n })\n ")}function imageCompression(e,r){return new Promise((function(n,o){var s,c,u;if(r.maxSizeMB=r.maxSizeMB||Number.POSITIVE_INFINITY,c="boolean"!=typeof r.useWebWorker||r.useWebWorker,delete r.useWebWorker,void 0===r.onProgress&&(r.onProgress=function(){}),!(e instanceof Blob||e instanceof CustomFile))return o(new Error("The file given is not an instance of Blob or File"));if(!/^image/.test(e.type))return o(new Error("The file given is not an image"));if(u="undefined"!=typeof WorkerGlobalScope&&self instanceof WorkerGlobalScope,!c||"function"!=typeof Worker||u)return compress(e,r).then(function(e){try{return s=e,$If_3.call(this)}catch(e){return o(e)}}.bind(this),o);var l=function(){try{return $If_3.call(this)}catch(e){return o(e)}}.bind(this),f=function $Try_1_Catch(n){try{return compress(e,r).then((function(e){try{return s=e,l()}catch(e){return o(e)}}),o)}catch(e){return o(e)}};try{return function compressOnWebWorker(e,r){return new Promise((function(n,o){return new Promise((function(s,c){var u=i++;return t||(t=generateLib()),a||(a=generateWorkerScript()),a.addEventListener("message",(function handler(e){if(e.data.id===u){if(void 0!==e.data.progress)return void r.onProgress(e.data.progress);a.removeEventListener("message",handler),e.data.error&&o(new Error(e.data.error)),n(e.data.file)}})),a.postMessage({file:e,id:u,imageCompressionLibUrl:t,options:_objectSpread2(_objectSpread2({},r),{},{onProgress:void 0})}),s()}))}))}(e,r).then((function(e){try{return s=e,l()}catch(e){return f()}}),f)}catch(e){f()}function $If_3(){try{s.name=e.name,s.lastModified=e.lastModified}catch(e){}return n(s)}}))}imageCompression.getDataUrlFromFile=getDataUrlFromFile,imageCompression.getFilefromDataUrl=getFilefromDataUrl,imageCompression.loadImage=loadImage,imageCompression.drawImageInCanvas=drawImageInCanvas,imageCompression.drawFileInCanvas=drawFileInCanvas,imageCompression.canvasToFile=canvasToFile,imageCompression.getExifOrientation=getExifOrientation,imageCompression.handleMaxWidthOrHeight=handleMaxWidthOrHeight,imageCompression.followExifOrientation=followExifOrientation,imageCompression.cleanupMemory=cleanupCanvasMemory,imageCompression.version="1.0.10";export default imageCompression; +function _defineProperty(e,r,n){return r in e?Object.defineProperty(e,r,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[r]=n,e}function ownKeys(e,r){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var t=Object.getOwnPropertySymbols(e);r&&(t=t.filter((function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable}))),n.push.apply(n,t)}return n}function _objectSpread2(e){for(var r=1;re.length)&&(r=e.length);for(var n=0,t=new Array(r);n2&&void 0!==arguments[2]?arguments[2]:Date.now();return new Promise((function(t){for(var a=e.split(","),i=a[0].match(/:(.*?);/)[1],o=atob(a[1]),s=o.length,c=new Uint8Array(s);s--;)c[s]=o.charCodeAt(s);var u=new Blob([c],{type:i});u.name=r,u.lastModified=n,t(u)}))}function loadImage(e){return new Promise((function(r,n){var t=new Image;t.onload=function(){return r(t)},t.onerror=function(e){return n(e)},t.src=e}))}function drawImageInCanvas(e){var r=_slicedToArray(getNewCanvasAndCtx(e.width,e.height),2),n=r[0];return r[1].drawImage(e,0,0,n.width,n.height),n}function drawFileInCanvas(e){return new Promise((function(r,n){var t,a,i=function $Try_1_Post(){try{return a=drawImageInCanvas(t),r([t,a])}catch(e){return n(e)}},o=function $Try_1_Catch(r){try{return getDataUrlFromFile(e).then((function(e){try{return loadImage(e).then((function(e){try{return t=e,i()}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 t=e,i()}catch(e){return o()}}),o)}catch(e){o()}}))}function canvasToFile(e,r,n,t){var a=arguments.length>4&&void 0!==arguments[4]?arguments[4]:1;return new Promise((function(i,o){var s;return"function"==typeof OffscreenCanvas&&e instanceof OffscreenCanvas?e.convertToBlob({type:r,quality:a}).then(function(e){try{return(s=e).name=n,s.lastModified=t,$If_4.call(this)}catch(e){return o(e)}}.bind(this),o):getFilefromDataUrl(e.toDataURL(r,a),n,t).then(function(e){try{return s=e,$If_4.call(this)}catch(e){return o(e)}}.bind(this),o);function $If_4(){return i(s)}}))}function getExifOrientation(e){return new Promise((function(r,n){var t=new CustomFileReader;t.onload=function(e){var n=new DataView(e.target.result);if(65496!=n.getUint16(0,!1))return r(-2);for(var t=n.byteLength,a=2;ai||a>i)){var s=_slicedToArray(getNewCanvasAndCtx(t,a),2);o=s[0],n=s[1],t>a?(o.width=i,o.height=a/t*i):(o.width=t/a*i,o.height=i),n.drawImage(e,0,0,o.width,o.height),cleanupCanvasMemory(e)}return o}function followExifOrientation(e,r){var n=e.width,t=e.height,a=_slicedToArray(getNewCanvasAndCtx(n,t),2),i=a[0],o=a[1];switch(40&&void 0!==arguments[0]?arguments[0]:5;i+=e,r.onProgress(Math.min(i,100))}function setProgress(e){i=Math.min(Math.max(e,i),100),r.onProgress(i)}return i=0,o=r.maxIteration||10,s=1024*r.maxSizeMB*1024,incProgress(),drawFileInCanvas(e).then(function(i){try{var C=_slicedToArray(i,2);return C[0],c=C[1],incProgress(),u=handleMaxWidthOrHeight(c,r),incProgress(),new Promise((function(n,t){var a;if(!(a=r.exifOrientation))return getExifOrientation(e).then(function(e){try{return a=e,$If_2.call(this)}catch(e){return t(e)}}.bind(this),t);function $If_2(){return n(a)}return $If_2.call(this)})).then(function(i){try{return r.exifOrientation=i,incProgress(),n.then(function(n){try{return l=n?u:followExifOrientation(u,r.exifOrientation),incProgress(),f=1,canvasToFile(l,r.fileType||e.type,e.name,e.lastModified,f).then(function(n){try{{if(m=n,incProgress(),g=m.size>s,A=m.size>e.size,!g&&!A)return setProgress(100),t(m);var i;function $Loop_3(){if(o--&&(h>s||h>d)){var n,t,i=_slicedToArray(getNewCanvasAndCtx(n=g?.95*w.width:w.width,t=g?.95*w.height:w.height),2);return v=i[0],i[1].drawImage(w,0,0,n,t),"image/jpeg"===e.type&&(f*=.95),canvasToFile(v,r.fileType||e.type,e.name,e.lastModified,f).then((function(e){try{return y=e,cleanupCanvasMemory(w),w=v,h=y.size,setProgress(Math.min(99,Math.floor((p-h)/(p-s)*100))),$Loop_3}catch(e){return a(e)}}),a)}return[1]}return d=e.size,p=m.size,h=p,w=l,(i=function(e){for(;e;){if(e.then)return void e.then(i,a);try{if(e.pop){if(e.length)return e.pop()?$Loop_3_exit.call(this):e;e=$Loop_3}else e=e.call(this)}catch(e){return a(e)}}}.bind(this))($Loop_3);function $Loop_3_exit(){return cleanupCanvasMemory(w),cleanupCanvasMemory(v),cleanupCanvasMemory(u),cleanupCanvasMemory(l),cleanupCanvasMemory(c),setProgress(100),t(y)}}}catch(e){return a(e)}}.bind(this),a)}catch(e){return a(e)}}.bind(this),a)}catch(e){return a(e)}}.bind(this),a)}catch(e){return a(e)}}.bind(this),a)}))}e&&(Number.isInteger=Number.isInteger||function(e){return"number"==typeof e&&isFinite(e)&&Math.floor(e)===e});var t,a,i=0;function generateLib(){return function createSourceObject(e){return URL.createObjectURL(new Blob([e],{type:"application/javascript"}))}("\n function imageCompression (){return (".concat(imageCompression,").apply(null, arguments)}\n\n imageCompression.getDataUrlFromFile = ").concat(imageCompression.getDataUrlFromFile,"\n imageCompression.getFilefromDataUrl = ").concat(imageCompression.getFilefromDataUrl,"\n imageCompression.loadImage = ").concat(imageCompression.loadImage,"\n imageCompression.drawImageInCanvas = ").concat(imageCompression.drawImageInCanvas,"\n imageCompression.drawFileInCanvas = ").concat(imageCompression.drawFileInCanvas,"\n imageCompression.canvasToFile = ").concat(imageCompression.canvasToFile,"\n imageCompression.getExifOrientation = ").concat(imageCompression.getExifOrientation,"\n imageCompression.handleMaxWidthOrHeight = ").concat(imageCompression.handleMaxWidthOrHeight,"\n imageCompression.followExifOrientation = ").concat(imageCompression.followExifOrientation,"\n imageCompression.cleanupMemory = ").concat(imageCompression.cleanupMemory,"\n\n getDataUrlFromFile = imageCompression.getDataUrlFromFile\n getFilefromDataUrl = imageCompression.getFilefromDataUrl\n loadImage = imageCompression.loadImage\n drawImageInCanvas = imageCompression.drawImageInCanvas\n drawFileInCanvas = imageCompression.drawFileInCanvas\n canvasToFile = imageCompression.canvasToFile\n getExifOrientation = imageCompression.getExifOrientation\n handleMaxWidthOrHeight = imageCompression.handleMaxWidthOrHeight\n followExifOrientation = imageCompression.followExifOrientation\n cleanupMemory = imageCompression.cleanupMemory\n\n getNewCanvasAndCtx = ").concat(getNewCanvasAndCtx,"\n \n CustomFileReader = FileReader\n \n CustomFile = File\n \n function _slicedToArray(arr, n) { return arr }\n \n function _typeof(a) { return typeof a }\n\n function compress (){return (").concat(compress,").apply(null, arguments)}\n "))}function generateWorkerScript(){return function createWorker(e){return"function"==typeof e&&(e="(".concat(f,")()")),new Worker(URL.createObjectURL(new Blob([e])))}("\n let scriptImported = false\n self.addEventListener('message', async (e) => {\n const { file, id, imageCompressionLibUrl, options } = e.data\n options.onProgress = (progress) => self.postMessage({ progress, id })\n try {\n if (!scriptImported) {\n // console.log('[worker] importScripts', imageCompressionLibUrl)\n self.importScripts(imageCompressionLibUrl)\n scriptImported = true\n }\n // console.log('[worker] self', self)\n const compressedFile = await imageCompression(file, options)\n self.postMessage({ file: compressedFile, id })\n } catch (e) {\n // console.error('[worker] error', e)\n self.postMessage({ error: e.message + '\\n' + e.stack, id })\n }\n })\n ")}function imageCompression(e,r){return new Promise((function(n,o){var s,c,u;if(r.maxSizeMB=r.maxSizeMB||Number.POSITIVE_INFINITY,c="boolean"!=typeof r.useWebWorker||r.useWebWorker,delete r.useWebWorker,void 0===r.onProgress&&(r.onProgress=function(){}),!(e instanceof Blob||e instanceof CustomFile))return o(new Error("The file given is not an instance of Blob or File"));if(!/^image/.test(e.type))return o(new Error("The file given is not an image"));if(u="undefined"!=typeof WorkerGlobalScope&&self instanceof WorkerGlobalScope,!c||"function"!=typeof Worker||u)return compress(e,r).then(function(e){try{return s=e,$If_3.call(this)}catch(e){return o(e)}}.bind(this),o);var l=function(){try{return $If_3.call(this)}catch(e){return o(e)}}.bind(this),f=function $Try_1_Catch(n){try{return compress(e,r).then((function(e){try{return s=e,l()}catch(e){return o(e)}}),o)}catch(e){return o(e)}};try{return function compressOnWebWorker(e,r){return new Promise((function(n,o){return new Promise((function(s,c){var u=i++;return t||(t=generateLib()),a||(a=generateWorkerScript()),a.addEventListener("message",(function handler(e){if(e.data.id===u){if(void 0!==e.data.progress)return void r.onProgress(e.data.progress);a.removeEventListener("message",handler),e.data.error&&o(new Error(e.data.error)),n(e.data.file)}})),a.addEventListener("error",o),a.postMessage({file:e,id:u,imageCompressionLibUrl:t,options:_objectSpread2(_objectSpread2({},r),{},{onProgress:void 0})}),s()}))}))}(e,r).then((function(e){try{return s=e,l()}catch(e){return f()}}),f)}catch(e){f()}function $If_3(){try{s.name=e.name,s.lastModified=e.lastModified}catch(e){}return n(s)}}))}imageCompression.getDataUrlFromFile=getDataUrlFromFile,imageCompression.getFilefromDataUrl=getFilefromDataUrl,imageCompression.loadImage=loadImage,imageCompression.drawImageInCanvas=drawImageInCanvas,imageCompression.drawFileInCanvas=drawFileInCanvas,imageCompression.canvasToFile=canvasToFile,imageCompression.getExifOrientation=getExifOrientation,imageCompression.handleMaxWidthOrHeight=handleMaxWidthOrHeight,imageCompression.followExifOrientation=followExifOrientation,imageCompression.cleanupMemory=cleanupCanvasMemory,imageCompression.version="1.0.11";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 2d2a0f2..f3b6fc3 100644 --- a/dist/browser-image-compression.mjs.map +++ b/dist/browser-image-compression.mjs.map @@ -1 +1 @@ -{"version":3,"file":"browser-image-compression.mjs","sources":["../lib/utils.js","../lib/image-compression.js","../lib/web-worker.js","../lib/index.js"],"sourcesContent":["const isBrowser = typeof window !== 'undefined' // change browser environment to support SSR\r\n\r\n// add support for cordova-plugin-file\r\nconst moduleMapper = isBrowser && window.cordova && window.cordova.require && window.cordova.require('cordova/modulemapper')\r\nexport const CustomFile = isBrowser && ((moduleMapper && moduleMapper.getOriginalSymbol(window, 'File')) || File)\r\nexport const CustomFileReader = isBrowser && ((moduleMapper && moduleMapper.getOriginalSymbol(window, 'FileReader')) || FileReader)\r\n\r\n// Check if browser supports automatic image orientation\r\n// see https://github.com/blueimp/JavaScript-Load-Image/blob/1e4df707821a0afcc11ea0720ee403b8759f3881/js/load-image-orientation.js#L37-L53\r\nexport const isAutoOrientationInBrowser = (async () => {\r\n // black 2x1 JPEG, with the following meta information set:\r\n // EXIF Orientation: 6 (Rotated 90° CCW)\r\n const testImageURL =\r\n 'data:image/jpeg;base64,/9j/4QAiRXhpZgAATU0AKgAAAAgAAQESAAMAAAABAAYAAAA' +\r\n 'AAAD/2wCEAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBA' +\r\n 'QEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE' +\r\n 'BAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAf/AABEIAAEAAgMBEQACEQEDEQH/x' +\r\n 'ABKAAEAAAAAAAAAAAAAAAAAAAALEAEAAAAAAAAAAAAAAAAAAAAAAQEAAAAAAAAAAAAAAAA' +\r\n 'AAAAAEQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIRAxEAPwA/8H//2Q=='\r\n const testImageFile = await getFilefromDataUrl(testImageURL, 'test.jpg', Date.now())\r\n\r\n const testImageCanvas = (await drawFileInCanvas(testImageFile))[1]\r\n const testImageFile2 = await canvasToFile(testImageCanvas, testImageFile.type, testImageFile.name, testImageFile.lastModified)\r\n cleanupCanvasMemory(testImageCanvas)\r\n const testImageURL2 = await getDataUrlFromFile(testImageFile2)\r\n const img = await loadImage(testImageURL2)\r\n // console.log('img', img.width, img.height)\r\n\r\n return img.width === 1 && img.height === 2\r\n})()\r\n\r\n/**\r\n * getDataUrlFromFile\r\n *\r\n * @param {File | Blob} file\r\n * @returns {Promise}\r\n */\r\nexport function getDataUrlFromFile (file) {\r\n return new Promise((resolve, reject) => {\r\n const reader = new CustomFileReader()\r\n reader.onload = () => resolve(reader.result)\r\n reader.onerror = (e) => reject(e)\r\n reader.readAsDataURL(file)\r\n })\r\n}\r\n\r\n/**\r\n * getFilefromDataUrl\r\n *\r\n * @param {string} dataUrl\r\n * @param {string} filename\r\n * @param {number} [lastModified=Date.now()]\r\n * @returns {Promise}\r\n */\r\nexport function getFilefromDataUrl (dataUrl, filename, lastModified = Date.now()) {\r\n return new Promise((resolve) => {\r\n const arr = dataUrl.split(',')\r\n const mime = arr[0].match(/:(.*?);/)[1]\r\n const bstr = atob(arr[1])\r\n let n = bstr.length\r\n const u8arr = new Uint8Array(n)\r\n while (n--) {\r\n u8arr[n] = bstr.charCodeAt(n)\r\n }\r\n const file = new Blob([u8arr], { type: mime })\r\n file.name = filename\r\n file.lastModified = lastModified\r\n resolve(file)\r\n\r\n // Safari has issue with File constructor not being able to POST in FormData\r\n // https://github.com/Donaldcwl/browser-image-compression/issues/8\r\n // https://bugs.webkit.org/show_bug.cgi?id=165081\r\n // let file\r\n // try {\r\n // file = new File([u8arr], filename, { type: mime }) // Edge do not support File constructor\r\n // } catch (e) {\r\n // file = new Blob([u8arr], { type: mime })\r\n // file.name = filename\r\n // file.lastModified = lastModified\r\n // }\r\n // resolve(file)\r\n })\r\n}\r\n\r\n/**\r\n * loadImage\r\n *\r\n * @param {string} src\r\n * @returns {Promise}\r\n */\r\nexport function loadImage (src) {\r\n return new Promise((resolve, reject) => {\r\n const img = new Image()\r\n img.onload = () => resolve(img)\r\n img.onerror = (e) => reject(e)\r\n img.src = src\r\n })\r\n}\r\n\r\n/**\r\n * drawImageInCanvas\r\n *\r\n * @param {HTMLImageElement} img\r\n * @returns {HTMLCanvasElement | OffscreenCanvas}\r\n */\r\nexport function drawImageInCanvas (img) {\r\n const [canvas, ctx] = getNewCanvasAndCtx(img.width, img.height)\r\n ctx.drawImage(img, 0, 0, canvas.width, canvas.height)\r\n return canvas\r\n}\r\n\r\n/**\r\n * drawFileInCanvas\r\n *\r\n * @param {File | Blob} file\r\n * @returns {Promise<[ImageBitmap | HTMLImageElement, HTMLCanvasElement | OffscreenCanvas]>}\r\n */\r\nexport async function drawFileInCanvas (file) {\r\n let img\r\n try {\r\n img = await createImageBitmap(file)\r\n } catch (e) {\r\n const dataUrl = await getDataUrlFromFile(file)\r\n img = await loadImage(dataUrl)\r\n }\r\n const canvas = drawImageInCanvas(img)\r\n return [img, canvas]\r\n}\r\n\r\n/**\r\n * canvasToFile\r\n *\r\n * @param {HTMLCanvasElement | OffscreenCanvas} canvas\r\n * @param {string} fileType\r\n * @param {string} fileName\r\n * @param {number} fileLastModified\r\n * @param {number} [quality]\r\n * @returns {Promise}\r\n */\r\nexport async function canvasToFile (canvas, fileType, fileName, fileLastModified, quality = 1) {\r\n let file\r\n if (typeof OffscreenCanvas === 'function' && canvas instanceof OffscreenCanvas) {\r\n file = await canvas.convertToBlob({ type: fileType, quality })\r\n file.name = fileName\r\n file.lastModified = fileLastModified\r\n } else {\r\n const dataUrl = canvas.toDataURL(fileType, quality)\r\n file = await getFilefromDataUrl(dataUrl, fileName, fileLastModified)\r\n }\r\n return file\r\n}\r\n\r\n/**\r\n * getExifOrientation\r\n * get image exif orientation info\r\n * source: https://stackoverflow.com/a/32490603/10395024\r\n *\r\n * @param {File | Blob} file\r\n * @returns {Promise} - orientation id, see https://i.stack.imgur.com/VGsAj.gif\r\n */\r\nexport function getExifOrientation (file) {\r\n return new Promise((resolve, reject) => {\r\n const reader = new CustomFileReader()\r\n reader.onload = (e) => {\r\n const view = new DataView(e.target.result)\r\n if (view.getUint16(0, false) != 0xFFD8) {\r\n return resolve(-2)\r\n }\r\n const length = view.byteLength\r\n let offset = 2\r\n while (offset < length) {\r\n if (view.getUint16(offset + 2, false) <= 8) return resolve(-1)\r\n const marker = view.getUint16(offset, false)\r\n offset += 2\r\n if (marker == 0xFFE1) {\r\n if (view.getUint32(offset += 2, false) != 0x45786966) {\r\n return resolve(-1)\r\n }\r\n\r\n var little = view.getUint16(offset += 6, false) == 0x4949\r\n offset += view.getUint32(offset + 4, little)\r\n var tags = view.getUint16(offset, little)\r\n offset += 2\r\n for (var i = 0; i < tags; i++) {\r\n if (view.getUint16(offset + (i * 12), little) == 0x0112) {\r\n return resolve(view.getUint16(offset + (i * 12) + 8, little))\r\n }\r\n }\r\n } else if ((marker & 0xFF00) != 0xFF00) {\r\n break\r\n } else {\r\n offset += view.getUint16(offset, false)\r\n }\r\n }\r\n return resolve(-1)\r\n }\r\n reader.onerror = (e) => reject(e)\r\n reader.readAsArrayBuffer(file)\r\n })\r\n}\r\n\r\n/**\r\n *\r\n * @param {HTMLCanvasElement | OffscreenCanvas} canvas\r\n * @param options\r\n * @returns {HTMLCanvasElement | OffscreenCanvas}\r\n */\r\nexport function handleMaxWidthOrHeight (canvas, options) {\r\n const width = canvas.width\r\n const height = canvas.height\r\n const maxWidthOrHeight = options.maxWidthOrHeight\r\n\r\n const needToHandle = Number.isFinite(maxWidthOrHeight) && (width > maxWidthOrHeight || height > maxWidthOrHeight)\r\n\r\n let newCanvas = canvas\r\n let ctx\r\n\r\n if (needToHandle) {\r\n [newCanvas, ctx] = getNewCanvasAndCtx(width, height)\r\n if (width > height) {\r\n newCanvas.width = maxWidthOrHeight\r\n newCanvas.height = (height / width) * maxWidthOrHeight\r\n } else {\r\n newCanvas.width = (width / height) * maxWidthOrHeight\r\n newCanvas.height = maxWidthOrHeight\r\n }\r\n ctx.drawImage(canvas, 0, 0, newCanvas.width, newCanvas.height)\r\n\r\n cleanupCanvasMemory(canvas)\r\n }\r\n\r\n return newCanvas\r\n}\r\n\r\n/**\r\n * followExifOrientation\r\n * source: https://stackoverflow.com/a/40867559/10395024\r\n *\r\n * @param {HTMLCanvasElement | OffscreenCanvas} canvas\r\n * @param {number} exifOrientation\r\n * @returns {HTMLCanvasElement | OffscreenCanvas} canvas\r\n */\r\nexport function followExifOrientation (canvas, exifOrientation) {\r\n const width = canvas.width\r\n const height = canvas.height\r\n\r\n const [newCanvas, ctx] = getNewCanvasAndCtx(width, height)\r\n\r\n // set proper canvas dimensions before transform & export\r\n if (4 < exifOrientation && exifOrientation < 9) {\r\n newCanvas.width = height\r\n newCanvas.height = width\r\n } else {\r\n newCanvas.width = width\r\n newCanvas.height = height\r\n }\r\n\r\n // transform context before drawing image\r\n switch (exifOrientation) {\r\n case 2: ctx.transform(-1, 0, 0, 1, width, 0); break;\r\n case 3: ctx.transform(-1, 0, 0, -1, width, height); break;\r\n case 4: ctx.transform(1, 0, 0, -1, 0, height); break;\r\n case 5: ctx.transform(0, 1, 1, 0, 0, 0); break;\r\n case 6: ctx.transform(0, 1, -1, 0, height, 0); break;\r\n case 7: ctx.transform(0, -1, -1, 0, height, width); break;\r\n case 8: ctx.transform(0, -1, 1, 0, 0, width); break;\r\n default: break;\r\n }\r\n\r\n ctx.drawImage(canvas, 0, 0, width, height)\r\n\r\n cleanupCanvasMemory(canvas)\r\n\r\n return newCanvas\r\n}\r\n\r\n/**\r\n * get new Canvas and it's context\r\n * @param width\r\n * @param height\r\n * @returns {[HTMLCanvasElement | OffscreenCanvas, CanvasRenderingContext2D]}\r\n */\r\nexport function getNewCanvasAndCtx (width, height) {\r\n let canvas\r\n let ctx\r\n try {\r\n canvas = new OffscreenCanvas(width, height)\r\n ctx = canvas.getContext('2d')\r\n if (ctx === null) {\r\n throw new Error('getContext of OffscreenCanvas returns null')\r\n }\r\n } catch (e) {\r\n canvas = document.createElement('canvas')\r\n ctx = canvas.getContext('2d')\r\n }\r\n canvas.width = width\r\n canvas.height = height\r\n return [canvas, ctx]\r\n}\r\n\r\n/**\r\n * clear Canvas memory\r\n * @param canvas\r\n * @returns null\r\n */\r\nexport function cleanupCanvasMemory (canvas) {\r\n canvas.width = 0\r\n canvas.height = 0\r\n}\r\n\r\nif (isBrowser) {\r\n// Polyfill for Number.isInteger\r\n Number.isInteger = Number.isInteger || function (value) {\r\n return typeof value === 'number' &&\r\n isFinite(value) &&\r\n Math.floor(value) === value\r\n }\r\n}\r\n","import {\r\n canvasToFile,\r\n cleanupCanvasMemory,\r\n drawFileInCanvas,\r\n followExifOrientation,\r\n getExifOrientation,\r\n getNewCanvasAndCtx,\r\n handleMaxWidthOrHeight,\r\n isAutoOrientationInBrowser\r\n} from './utils'\r\n\r\n/**\r\n * Compress an image file.\r\n *\r\n * @param {File} file\r\n * @param {Object} options - { maxSizeMB=Number.POSITIVE_INFINITY, maxWidthOrHeight, useWebWorker=false, maxIteration = 10, exifOrientation, fileType }\r\n * @param {number} [options.maxSizeMB=Number.POSITIVE_INFINITY]\r\n * @param {number} [options.maxWidthOrHeight=undefined]\r\n * @param {number} [options.maxIteration=10]\r\n * @param {number} [options.exifOrientation] - default to be the exif orientation from the image file\r\n * @param {Function} [options.onProgress] - a function takes one progress argument (progress from 0 to 100)\r\n * @param {string} [options.fileType] - default to be the original mime type from the image file\r\n * @returns {Promise}\r\n */\r\nexport default async function compress (file, options) {\r\n let progress = 0\r\n\r\n function incProgress (inc = 5) {\r\n progress += inc\r\n options.onProgress(Math.min(progress, 100))\r\n }\r\n\r\n function setProgress (p) {\r\n progress = Math.min(Math.max(p, progress), 100)\r\n options.onProgress(progress)\r\n }\r\n\r\n let remainingTrials = options.maxIteration || 10\r\n\r\n const maxSizeByte = options.maxSizeMB * 1024 * 1024\r\n\r\n incProgress()\r\n\r\n // drawFileInCanvas\r\n let [img, origCanvas] = await drawFileInCanvas(file)\r\n\r\n incProgress()\r\n\r\n // handleMaxWidthOrHeight\r\n const maxWidthOrHeightFixedCanvas = handleMaxWidthOrHeight(origCanvas, options)\r\n\r\n incProgress()\r\n\r\n // exifOrientation\r\n options.exifOrientation = options.exifOrientation || await getExifOrientation(file)\r\n incProgress()\r\n const orientationFixedCanvas = (await isAutoOrientationInBrowser) ? maxWidthOrHeightFixedCanvas : followExifOrientation(maxWidthOrHeightFixedCanvas, options.exifOrientation)\r\n incProgress()\r\n\r\n let quality = 1\r\n\r\n let tempFile = await canvasToFile(orientationFixedCanvas, options.fileType || file.type, file.name, file.lastModified, quality)\r\n incProgress()\r\n\r\n const origExceedMaxSize = tempFile.size > maxSizeByte\r\n const sizeBecomeLarger = tempFile.size > file.size\r\n\r\n // check if we need to compress or resize\r\n if (!origExceedMaxSize && !sizeBecomeLarger) {\r\n // no need to compress\r\n setProgress(100)\r\n return tempFile\r\n }\r\n\r\n const sourceSize = file.size\r\n const renderedSize = tempFile.size\r\n let currentSize = renderedSize\r\n let compressedFile\r\n let newCanvas, ctx\r\n let canvas = orientationFixedCanvas\r\n while (remainingTrials-- && (currentSize > maxSizeByte || currentSize > sourceSize)) {\r\n const newWidth = origExceedMaxSize ? canvas.width * 0.95 : canvas.width\r\n const newHeight = origExceedMaxSize ? canvas.height * 0.95 : canvas.height;\r\n [newCanvas, ctx] = getNewCanvasAndCtx(newWidth, newHeight)\r\n\r\n ctx.drawImage(canvas, 0, 0, newWidth, newHeight)\r\n\r\n if (file.type === 'image/jpeg') {\r\n quality *= 0.95\r\n }\r\n compressedFile = await canvasToFile(newCanvas, options.fileType || file.type, file.name, file.lastModified, quality)\r\n\r\n cleanupCanvasMemory(canvas)\r\n\r\n canvas = newCanvas\r\n\r\n currentSize = compressedFile.size\r\n setProgress(Math.min(99, Math.floor((renderedSize - currentSize) / (renderedSize - maxSizeByte) * 100)))\r\n }\r\n\r\n // garbage clean canvas for safari\r\n // ref: https://bugs.webkit.org/show_bug.cgi?id=195325\r\n cleanupCanvasMemory(canvas)\r\n cleanupCanvasMemory(newCanvas)\r\n cleanupCanvasMemory(maxWidthOrHeightFixedCanvas)\r\n cleanupCanvasMemory(orientationFixedCanvas)\r\n cleanupCanvasMemory(origCanvas)\r\n\r\n setProgress(100)\r\n return compressedFile\r\n}\r\n","import lib from './index'\r\nimport compress from './image-compression'\r\nimport { getNewCanvasAndCtx } from './utils'\r\n\r\nlet cnt = 0\r\nlet imageCompressionLibUrl\r\nlet worker\r\n\r\nfunction createWorker (script) {\r\n if (typeof script === 'function') {\r\n script = `(${f})()`\r\n }\r\n return new Worker(URL.createObjectURL(new Blob([script])))\r\n}\r\n\r\nfunction createSourceObject (str) {\r\n return URL.createObjectURL(new Blob([str], { type: 'application/javascript' }))\r\n}\r\n\r\nfunction generateLib () {\r\n // prepare the lib to be used inside WebWorker\r\n return createSourceObject(`\r\n function imageCompression (){return (${lib}).apply(null, arguments)}\r\n\r\n imageCompression.getDataUrlFromFile = ${lib.getDataUrlFromFile}\r\n imageCompression.getFilefromDataUrl = ${lib.getFilefromDataUrl}\r\n imageCompression.loadImage = ${lib.loadImage}\r\n imageCompression.drawImageInCanvas = ${lib.drawImageInCanvas}\r\n imageCompression.drawFileInCanvas = ${lib.drawFileInCanvas}\r\n imageCompression.canvasToFile = ${lib.canvasToFile}\r\n imageCompression.getExifOrientation = ${lib.getExifOrientation}\r\n imageCompression.handleMaxWidthOrHeight = ${lib.handleMaxWidthOrHeight}\r\n imageCompression.followExifOrientation = ${lib.followExifOrientation}\r\n imageCompression.cleanupMemory = ${lib.cleanupMemory}\r\n\r\n getDataUrlFromFile = imageCompression.getDataUrlFromFile\r\n getFilefromDataUrl = imageCompression.getFilefromDataUrl\r\n loadImage = imageCompression.loadImage\r\n drawImageInCanvas = imageCompression.drawImageInCanvas\r\n drawFileInCanvas = imageCompression.drawFileInCanvas\r\n canvasToFile = imageCompression.canvasToFile\r\n getExifOrientation = imageCompression.getExifOrientation\r\n handleMaxWidthOrHeight = imageCompression.handleMaxWidthOrHeight\r\n followExifOrientation = imageCompression.followExifOrientation\r\n cleanupMemory = imageCompression.cleanupMemory\r\n\r\n getNewCanvasAndCtx = ${getNewCanvasAndCtx}\r\n \r\n CustomFileReader = FileReader\r\n \r\n CustomFile = File\r\n \r\n function _slicedToArray(arr, n) { return arr }\r\n \r\n function _typeof(a) { return typeof a }\r\n\r\n function compress (){return (${compress}).apply(null, arguments)}\r\n `)\r\n}\r\n\r\nfunction generateWorkerScript () {\r\n // code to be run in the WebWorker\r\n return createWorker(`\r\n let scriptImported = false\r\n self.addEventListener('message', async (e) => {\r\n const { file, id, imageCompressionLibUrl, options } = e.data\r\n options.onProgress = (progress) => self.postMessage({ progress, id })\r\n try {\r\n if (!scriptImported) {\r\n // console.log('[worker] importScripts', imageCompressionLibUrl)\r\n self.importScripts(imageCompressionLibUrl)\r\n scriptImported = true\r\n }\r\n // console.log('[worker] self', self)\r\n const compressedFile = await imageCompression(file, options)\r\n self.postMessage({ file: compressedFile, id })\r\n } catch (e) {\r\n // console.error('[worker] error', e)\r\n self.postMessage({ error: e.message + '\\\\n' + e.stack, id })\r\n }\r\n })\r\n `)\r\n}\r\n\r\nexport function compressOnWebWorker (file, options) {\r\n return new Promise(async (resolve, reject) => {\r\n let id = cnt++\r\n\r\n if (!imageCompressionLibUrl) {\r\n imageCompressionLibUrl = generateLib()\r\n }\r\n\r\n if (!worker) {\r\n worker = generateWorkerScript()\r\n }\r\n\r\n function handler (e) {\r\n if (e.data.id === id) {\r\n if (e.data.progress !== undefined) {\r\n options.onProgress(e.data.progress)\r\n return\r\n }\r\n worker.removeEventListener('message', handler)\r\n if (e.data.error) {\r\n reject(new Error(e.data.error))\r\n }\r\n resolve(e.data.file)\r\n }\r\n }\r\n\r\n worker.addEventListener('message', handler)\r\n worker.postMessage({\r\n file,\r\n id,\r\n imageCompressionLibUrl,\r\n options: { ...options, onProgress: undefined }\r\n })\r\n })\r\n}\r\n","import compress from './image-compression'\r\nimport {\r\n canvasToFile,\r\n drawFileInCanvas,\r\n drawImageInCanvas,\r\n getDataUrlFromFile,\r\n getFilefromDataUrl,\r\n loadImage,\r\n getExifOrientation,\r\n handleMaxWidthOrHeight,\r\n followExifOrientation,\r\n CustomFile,\r\n cleanupCanvasMemory\r\n} from './utils'\r\nimport { compressOnWebWorker } from './web-worker'\r\n\r\n/**\r\n * Compress an image file.\r\n *\r\n * @param {File} file\r\n * @param {Object} options - { maxSizeMB=Number.POSITIVE_INFINITY, maxWidthOrHeight, useWebWorker=false, maxIteration = 10, exifOrientation, fileType }\r\n * @param {number} [options.maxSizeMB=Number.POSITIVE_INFINITY]\r\n * @param {number} [options.maxWidthOrHeight=undefined]\r\n * @param {boolean} [options.useWebWorker=false]\r\n * @param {number} [options.maxIteration=10]\r\n * @param {number} [options.exifOrientation] - default to be the exif orientation from the image file\r\n * @param {Function} [options.onProgress] - a function takes one progress argument (progress from 0 to 100)\r\n * @param {string} [options.fileType] - default to be the original mime type from the image file\r\n * @returns {Promise}\r\n */\r\nasync function imageCompression (file, options) {\r\n\r\n let compressedFile\r\n\r\n options.maxSizeMB = options.maxSizeMB || Number.POSITIVE_INFINITY\r\n const useWebWorker = typeof options.useWebWorker === 'boolean' ? options.useWebWorker : true\r\n delete options.useWebWorker\r\n if (options.onProgress === undefined) {\r\n options.onProgress = () => {}\r\n }\r\n\r\n if (!(file instanceof Blob || file instanceof CustomFile)) {\r\n throw new Error('The file given is not an instance of Blob or File')\r\n } else if (!/^image/.test(file.type)) {\r\n throw new Error('The file given is not an image')\r\n }\r\n\r\n // try run in web worker, fall back to run in main thread\r\n const inWebWorker = typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope\r\n\r\n // if ((useWebWorker && typeof Worker === 'function') || inWebWorker) {\r\n // console.log('run compression in web worker')\r\n // } else {\r\n // console.log('run compression in main thread')\r\n // }\r\n\r\n if (useWebWorker && typeof Worker === 'function' && !inWebWorker) {\r\n try {\r\n // console.log(1)\r\n // \"compressOnWebWorker\" is kind of like a recursion to call \"imageCompression\" again inside web worker\r\n compressedFile = await compressOnWebWorker(file, options)\r\n } catch (e) {\r\n // console.warn('Run compression in web worker failed:', e, ', fall back to main thread')\r\n // console.log(1.5)\r\n compressedFile = await compress(file, options)\r\n }\r\n } else {\r\n // console.log(2)\r\n compressedFile = await compress(file, options)\r\n }\r\n\r\n try {\r\n compressedFile.name = file.name\r\n compressedFile.lastModified = file.lastModified\r\n } catch (e) {}\r\n\r\n return compressedFile\r\n}\r\n\r\nimageCompression.getDataUrlFromFile = getDataUrlFromFile\r\nimageCompression.getFilefromDataUrl = getFilefromDataUrl\r\nimageCompression.loadImage = loadImage\r\nimageCompression.drawImageInCanvas = drawImageInCanvas\r\nimageCompression.drawFileInCanvas = drawFileInCanvas\r\nimageCompression.canvasToFile = canvasToFile\r\nimageCompression.getExifOrientation = getExifOrientation\r\n\r\nimageCompression.handleMaxWidthOrHeight = handleMaxWidthOrHeight\r\nimageCompression.followExifOrientation = followExifOrientation\r\nimageCompression.cleanupMemory = cleanupCanvasMemory\r\nimageCompression.version = '1.0.10'\r\n\r\nexport default imageCompression\r\n"],"names":["isBrowser","window","CustomFile","getFilefromDataUrl","resolve","n","bstr","charCodeAt","file","e","img","type","fileName","length","offset","i","height","maxWidthOrHeight","options","ctx","width","getNewCanvasAndCtx","transform","Error","canvas","inc","then","$await_6","maxSizeByte","currentSize","imageCompressionLibUrl","cnt","generateLib","script","imageCompression","getDataUrlFromFile","loadImage","drawImageInCanvas","drawFileInCanvas","canvasToFile","getExifOrientation"],"mappings":";;;;;;;y0DAAA,IAAMA,EAA8B,oBAAlBC,2EAKoF,wBAIzFC,6vCA6CN,SAASC,oHACP,SAAAC,8DAGa,IAClBC,EAAIC,uCAGFA,EAAAC,uEAGFC,8IA6DMC,6FAHAC,4wBAmB4BC,yDAAEC,+aAOtCC,mPAoBI,mEAE8BC,EAAA,GAAxBC,kIAYOP,2DAYnBQ,WACIC,EAAiBC,iHAcnBC,+CAQEC,EAAAJ,gCAGE,4FA8BFI,sCAGmBC,iHAQXF,EAAAG,qCAAiCH,EAAAG,6EACjCH,EAAAG,UAAc,EAAG,EAAG,sBAAmBH,EAAAG,6EACvCH,EAAAG,UAAc,GAAI,EAAG,yEAsB7BD,mBAASD,oBAMT,QADJD,uDAEI,IAAAI,oDAEF,MAAOd,GAEPU,GADAK,kFAUAA,EAAAL,gLC5PqDM,kdAQrDC,KAAA,SAAAC,gBAAAA,8XAiBIC,6FAeFC,6FAGYb,88BC5FhB,IACIc,IADAC,IAcJ,SAASC,okEAPPC,mnDC4DwBzB,izBAa1B0B,iBAAAC,mBAAsCA,mBAEtCD,iBAAA/B,sCACA+B,iBAAAE,oBACAF,iBAAAG,oCACAH,iBAAAI,kCAEAJ,iBAAAK,0BA5FAL,iBAAAM"} \ No newline at end of file +{"version":3,"file":"browser-image-compression.mjs","sources":["../lib/utils.js","../lib/image-compression.js","../lib/web-worker.js","../lib/index.js"],"sourcesContent":["const isBrowser = typeof window !== 'undefined' // change browser environment to support SSR\r\n\r\n// add support for cordova-plugin-file\r\nconst moduleMapper = isBrowser && window.cordova && window.cordova.require && window.cordova.require('cordova/modulemapper')\r\nexport const CustomFile = isBrowser && ((moduleMapper && moduleMapper.getOriginalSymbol(window, 'File')) || File)\r\nexport const CustomFileReader = isBrowser && ((moduleMapper && moduleMapper.getOriginalSymbol(window, 'FileReader')) || FileReader)\r\n\r\n// Check if browser supports automatic image orientation\r\n// see https://github.com/blueimp/JavaScript-Load-Image/blob/1e4df707821a0afcc11ea0720ee403b8759f3881/js/load-image-orientation.js#L37-L53\r\nexport const isAutoOrientationInBrowser = (async () => {\r\n // black 2x1 JPEG, with the following meta information set:\r\n // EXIF Orientation: 6 (Rotated 90° CCW)\r\n const testImageURL =\r\n 'data:image/jpeg;base64,/9j/4QAiRXhpZgAATU0AKgAAAAgAAQESAAMAAAABAAYAAAA' +\r\n 'AAAD/2wCEAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBA' +\r\n 'QEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE' +\r\n 'BAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAf/AABEIAAEAAgMBEQACEQEDEQH/x' +\r\n 'ABKAAEAAAAAAAAAAAAAAAAAAAALEAEAAAAAAAAAAAAAAAAAAAAAAQEAAAAAAAAAAAAAAAA' +\r\n 'AAAAAEQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIRAxEAPwA/8H//2Q=='\r\n const testImageFile = await getFilefromDataUrl(testImageURL, 'test.jpg', Date.now())\r\n\r\n const testImageCanvas = (await drawFileInCanvas(testImageFile))[1]\r\n const testImageFile2 = await canvasToFile(testImageCanvas, testImageFile.type, testImageFile.name, testImageFile.lastModified)\r\n cleanupCanvasMemory(testImageCanvas)\r\n const testImageURL2 = await getDataUrlFromFile(testImageFile2)\r\n const img = await loadImage(testImageURL2)\r\n // console.log('img', img.width, img.height)\r\n\r\n return img.width === 1 && img.height === 2\r\n})()\r\n\r\n/**\r\n * getDataUrlFromFile\r\n *\r\n * @param {File | Blob} file\r\n * @returns {Promise}\r\n */\r\nexport function getDataUrlFromFile (file) {\r\n return new Promise((resolve, reject) => {\r\n const reader = new CustomFileReader()\r\n reader.onload = () => resolve(reader.result)\r\n reader.onerror = (e) => reject(e)\r\n reader.readAsDataURL(file)\r\n })\r\n}\r\n\r\n/**\r\n * getFilefromDataUrl\r\n *\r\n * @param {string} dataUrl\r\n * @param {string} filename\r\n * @param {number} [lastModified=Date.now()]\r\n * @returns {Promise}\r\n */\r\nexport function getFilefromDataUrl (dataUrl, filename, lastModified = Date.now()) {\r\n return new Promise((resolve) => {\r\n const arr = dataUrl.split(',')\r\n const mime = arr[0].match(/:(.*?);/)[1]\r\n const bstr = atob(arr[1])\r\n let n = bstr.length\r\n const u8arr = new Uint8Array(n)\r\n while (n--) {\r\n u8arr[n] = bstr.charCodeAt(n)\r\n }\r\n const file = new Blob([u8arr], { type: mime })\r\n file.name = filename\r\n file.lastModified = lastModified\r\n resolve(file)\r\n\r\n // Safari has issue with File constructor not being able to POST in FormData\r\n // https://github.com/Donaldcwl/browser-image-compression/issues/8\r\n // https://bugs.webkit.org/show_bug.cgi?id=165081\r\n // let file\r\n // try {\r\n // file = new File([u8arr], filename, { type: mime }) // Edge do not support File constructor\r\n // } catch (e) {\r\n // file = new Blob([u8arr], { type: mime })\r\n // file.name = filename\r\n // file.lastModified = lastModified\r\n // }\r\n // resolve(file)\r\n })\r\n}\r\n\r\n/**\r\n * loadImage\r\n *\r\n * @param {string} src\r\n * @returns {Promise}\r\n */\r\nexport function loadImage (src) {\r\n return new Promise((resolve, reject) => {\r\n const img = new Image()\r\n img.onload = () => resolve(img)\r\n img.onerror = (e) => reject(e)\r\n img.src = src\r\n })\r\n}\r\n\r\n/**\r\n * drawImageInCanvas\r\n *\r\n * @param {HTMLImageElement} img\r\n * @returns {HTMLCanvasElement | OffscreenCanvas}\r\n */\r\nexport function drawImageInCanvas (img) {\r\n const [canvas, ctx] = getNewCanvasAndCtx(img.width, img.height)\r\n ctx.drawImage(img, 0, 0, canvas.width, canvas.height)\r\n return canvas\r\n}\r\n\r\n/**\r\n * drawFileInCanvas\r\n *\r\n * @param {File | Blob} file\r\n * @returns {Promise<[ImageBitmap | HTMLImageElement, HTMLCanvasElement | OffscreenCanvas]>}\r\n */\r\nexport async function drawFileInCanvas (file) {\r\n let img\r\n try {\r\n img = await createImageBitmap(file)\r\n } catch (e) {\r\n const dataUrl = await getDataUrlFromFile(file)\r\n img = await loadImage(dataUrl)\r\n }\r\n const canvas = drawImageInCanvas(img)\r\n return [img, canvas]\r\n}\r\n\r\n/**\r\n * canvasToFile\r\n *\r\n * @param {HTMLCanvasElement | OffscreenCanvas} canvas\r\n * @param {string} fileType\r\n * @param {string} fileName\r\n * @param {number} fileLastModified\r\n * @param {number} [quality]\r\n * @returns {Promise}\r\n */\r\nexport async function canvasToFile (canvas, fileType, fileName, fileLastModified, quality = 1) {\r\n let file\r\n if (typeof OffscreenCanvas === 'function' && canvas instanceof OffscreenCanvas) {\r\n file = await canvas.convertToBlob({ type: fileType, quality })\r\n file.name = fileName\r\n file.lastModified = fileLastModified\r\n } else {\r\n const dataUrl = canvas.toDataURL(fileType, quality)\r\n file = await getFilefromDataUrl(dataUrl, fileName, fileLastModified)\r\n }\r\n return file\r\n}\r\n\r\n/**\r\n * getExifOrientation\r\n * get image exif orientation info\r\n * source: https://stackoverflow.com/a/32490603/10395024\r\n *\r\n * @param {File | Blob} file\r\n * @returns {Promise} - orientation id, see https://i.stack.imgur.com/VGsAj.gif\r\n */\r\nexport function getExifOrientation (file) {\r\n return new Promise((resolve, reject) => {\r\n const reader = new CustomFileReader()\r\n reader.onload = (e) => {\r\n const view = new DataView(e.target.result)\r\n if (view.getUint16(0, false) != 0xFFD8) {\r\n return resolve(-2)\r\n }\r\n const length = view.byteLength\r\n let offset = 2\r\n while (offset < length) {\r\n if (view.getUint16(offset + 2, false) <= 8) return resolve(-1)\r\n const marker = view.getUint16(offset, false)\r\n offset += 2\r\n if (marker == 0xFFE1) {\r\n if (view.getUint32(offset += 2, false) != 0x45786966) {\r\n return resolve(-1)\r\n }\r\n\r\n var little = view.getUint16(offset += 6, false) == 0x4949\r\n offset += view.getUint32(offset + 4, little)\r\n var tags = view.getUint16(offset, little)\r\n offset += 2\r\n for (var i = 0; i < tags; i++) {\r\n if (view.getUint16(offset + (i * 12), little) == 0x0112) {\r\n return resolve(view.getUint16(offset + (i * 12) + 8, little))\r\n }\r\n }\r\n } else if ((marker & 0xFF00) != 0xFF00) {\r\n break\r\n } else {\r\n offset += view.getUint16(offset, false)\r\n }\r\n }\r\n return resolve(-1)\r\n }\r\n reader.onerror = (e) => reject(e)\r\n reader.readAsArrayBuffer(file)\r\n })\r\n}\r\n\r\n/**\r\n *\r\n * @param {HTMLCanvasElement | OffscreenCanvas} canvas\r\n * @param options\r\n * @returns {HTMLCanvasElement | OffscreenCanvas}\r\n */\r\nexport function handleMaxWidthOrHeight (canvas, options) {\r\n const width = canvas.width\r\n const height = canvas.height\r\n const maxWidthOrHeight = options.maxWidthOrHeight\r\n\r\n const needToHandle = isFinite(maxWidthOrHeight) && (width > maxWidthOrHeight || height > maxWidthOrHeight)\r\n\r\n let newCanvas = canvas\r\n let ctx\r\n\r\n if (needToHandle) {\r\n [newCanvas, ctx] = getNewCanvasAndCtx(width, height)\r\n if (width > height) {\r\n newCanvas.width = maxWidthOrHeight\r\n newCanvas.height = (height / width) * maxWidthOrHeight\r\n } else {\r\n newCanvas.width = (width / height) * maxWidthOrHeight\r\n newCanvas.height = maxWidthOrHeight\r\n }\r\n ctx.drawImage(canvas, 0, 0, newCanvas.width, newCanvas.height)\r\n\r\n cleanupCanvasMemory(canvas)\r\n }\r\n\r\n return newCanvas\r\n}\r\n\r\n/**\r\n * followExifOrientation\r\n * source: https://stackoverflow.com/a/40867559/10395024\r\n *\r\n * @param {HTMLCanvasElement | OffscreenCanvas} canvas\r\n * @param {number} exifOrientation\r\n * @returns {HTMLCanvasElement | OffscreenCanvas} canvas\r\n */\r\nexport function followExifOrientation (canvas, exifOrientation) {\r\n const width = canvas.width\r\n const height = canvas.height\r\n\r\n const [newCanvas, ctx] = getNewCanvasAndCtx(width, height)\r\n\r\n // set proper canvas dimensions before transform & export\r\n if (4 < exifOrientation && exifOrientation < 9) {\r\n newCanvas.width = height\r\n newCanvas.height = width\r\n } else {\r\n newCanvas.width = width\r\n newCanvas.height = height\r\n }\r\n\r\n // transform context before drawing image\r\n switch (exifOrientation) {\r\n case 2: ctx.transform(-1, 0, 0, 1, width, 0); break;\r\n case 3: ctx.transform(-1, 0, 0, -1, width, height); break;\r\n case 4: ctx.transform(1, 0, 0, -1, 0, height); break;\r\n case 5: ctx.transform(0, 1, 1, 0, 0, 0); break;\r\n case 6: ctx.transform(0, 1, -1, 0, height, 0); break;\r\n case 7: ctx.transform(0, -1, -1, 0, height, width); break;\r\n case 8: ctx.transform(0, -1, 1, 0, 0, width); break;\r\n default: break;\r\n }\r\n\r\n ctx.drawImage(canvas, 0, 0, width, height)\r\n\r\n cleanupCanvasMemory(canvas)\r\n\r\n return newCanvas\r\n}\r\n\r\n/**\r\n * get new Canvas and it's context\r\n * @param width\r\n * @param height\r\n * @returns {[HTMLCanvasElement | OffscreenCanvas, CanvasRenderingContext2D]}\r\n */\r\nexport function getNewCanvasAndCtx (width, height) {\r\n let canvas\r\n let ctx\r\n try {\r\n canvas = new OffscreenCanvas(width, height)\r\n ctx = canvas.getContext('2d')\r\n if (ctx === null) {\r\n throw new Error('getContext of OffscreenCanvas returns null')\r\n }\r\n } catch (e) {\r\n canvas = document.createElement('canvas')\r\n ctx = canvas.getContext('2d')\r\n }\r\n canvas.width = width\r\n canvas.height = height\r\n return [canvas, ctx]\r\n}\r\n\r\n/**\r\n * clear Canvas memory\r\n * @param canvas\r\n * @returns null\r\n */\r\nexport function cleanupCanvasMemory (canvas) {\r\n canvas.width = 0\r\n canvas.height = 0\r\n}\r\n\r\nif (isBrowser) {\r\n// Polyfill for Number.isInteger\r\n Number.isInteger = Number.isInteger || function (value) {\r\n return typeof value === 'number' &&\r\n isFinite(value) &&\r\n Math.floor(value) === value\r\n }\r\n}\r\n","import {\r\n canvasToFile,\r\n cleanupCanvasMemory,\r\n drawFileInCanvas,\r\n followExifOrientation,\r\n getExifOrientation,\r\n getNewCanvasAndCtx,\r\n handleMaxWidthOrHeight,\r\n isAutoOrientationInBrowser\r\n} from './utils'\r\n\r\n/**\r\n * Compress an image file.\r\n *\r\n * @param {File} file\r\n * @param {Object} options - { maxSizeMB=Number.POSITIVE_INFINITY, maxWidthOrHeight, useWebWorker=false, maxIteration = 10, exifOrientation, fileType }\r\n * @param {number} [options.maxSizeMB=Number.POSITIVE_INFINITY]\r\n * @param {number} [options.maxWidthOrHeight=undefined]\r\n * @param {number} [options.maxIteration=10]\r\n * @param {number} [options.exifOrientation] - default to be the exif orientation from the image file\r\n * @param {Function} [options.onProgress] - a function takes one progress argument (progress from 0 to 100)\r\n * @param {string} [options.fileType] - default to be the original mime type from the image file\r\n * @returns {Promise}\r\n */\r\nexport default async function compress (file, options) {\r\n let progress = 0\r\n\r\n function incProgress (inc = 5) {\r\n progress += inc\r\n options.onProgress(Math.min(progress, 100))\r\n }\r\n\r\n function setProgress (p) {\r\n progress = Math.min(Math.max(p, progress), 100)\r\n options.onProgress(progress)\r\n }\r\n\r\n let remainingTrials = options.maxIteration || 10\r\n\r\n const maxSizeByte = options.maxSizeMB * 1024 * 1024\r\n\r\n incProgress()\r\n\r\n // drawFileInCanvas\r\n let [img, origCanvas] = await drawFileInCanvas(file)\r\n\r\n incProgress()\r\n\r\n // handleMaxWidthOrHeight\r\n const maxWidthOrHeightFixedCanvas = handleMaxWidthOrHeight(origCanvas, options)\r\n\r\n incProgress()\r\n\r\n // exifOrientation\r\n options.exifOrientation = options.exifOrientation || await getExifOrientation(file)\r\n incProgress()\r\n const orientationFixedCanvas = (await isAutoOrientationInBrowser) ? maxWidthOrHeightFixedCanvas : followExifOrientation(maxWidthOrHeightFixedCanvas, options.exifOrientation)\r\n incProgress()\r\n\r\n let quality = 1\r\n\r\n let tempFile = await canvasToFile(orientationFixedCanvas, options.fileType || file.type, file.name, file.lastModified, quality)\r\n incProgress()\r\n\r\n const origExceedMaxSize = tempFile.size > maxSizeByte\r\n const sizeBecomeLarger = tempFile.size > file.size\r\n\r\n // check if we need to compress or resize\r\n if (!origExceedMaxSize && !sizeBecomeLarger) {\r\n // no need to compress\r\n setProgress(100)\r\n return tempFile\r\n }\r\n\r\n const sourceSize = file.size\r\n const renderedSize = tempFile.size\r\n let currentSize = renderedSize\r\n let compressedFile\r\n let newCanvas, ctx\r\n let canvas = orientationFixedCanvas\r\n while (remainingTrials-- && (currentSize > maxSizeByte || currentSize > sourceSize)) {\r\n const newWidth = origExceedMaxSize ? canvas.width * 0.95 : canvas.width\r\n const newHeight = origExceedMaxSize ? canvas.height * 0.95 : canvas.height;\r\n [newCanvas, ctx] = getNewCanvasAndCtx(newWidth, newHeight)\r\n\r\n ctx.drawImage(canvas, 0, 0, newWidth, newHeight)\r\n\r\n if (file.type === 'image/jpeg') {\r\n quality *= 0.95\r\n }\r\n compressedFile = await canvasToFile(newCanvas, options.fileType || file.type, file.name, file.lastModified, quality)\r\n\r\n cleanupCanvasMemory(canvas)\r\n\r\n canvas = newCanvas\r\n\r\n currentSize = compressedFile.size\r\n setProgress(Math.min(99, Math.floor((renderedSize - currentSize) / (renderedSize - maxSizeByte) * 100)))\r\n }\r\n\r\n // garbage clean canvas for safari\r\n // ref: https://bugs.webkit.org/show_bug.cgi?id=195325\r\n cleanupCanvasMemory(canvas)\r\n cleanupCanvasMemory(newCanvas)\r\n cleanupCanvasMemory(maxWidthOrHeightFixedCanvas)\r\n cleanupCanvasMemory(orientationFixedCanvas)\r\n cleanupCanvasMemory(origCanvas)\r\n\r\n setProgress(100)\r\n return compressedFile\r\n}\r\n","import lib from './index'\r\nimport compress from './image-compression'\r\nimport { getNewCanvasAndCtx } from './utils'\r\n\r\nlet cnt = 0\r\nlet imageCompressionLibUrl\r\nlet worker\r\n\r\nfunction createWorker (script) {\r\n if (typeof script === 'function') {\r\n script = `(${f})()`\r\n }\r\n return new Worker(URL.createObjectURL(new Blob([script])))\r\n}\r\n\r\nfunction createSourceObject (str) {\r\n return URL.createObjectURL(new Blob([str], { type: 'application/javascript' }))\r\n}\r\n\r\nfunction generateLib () {\r\n // prepare the lib to be used inside WebWorker\r\n return createSourceObject(`\r\n function imageCompression (){return (${lib}).apply(null, arguments)}\r\n\r\n imageCompression.getDataUrlFromFile = ${lib.getDataUrlFromFile}\r\n imageCompression.getFilefromDataUrl = ${lib.getFilefromDataUrl}\r\n imageCompression.loadImage = ${lib.loadImage}\r\n imageCompression.drawImageInCanvas = ${lib.drawImageInCanvas}\r\n imageCompression.drawFileInCanvas = ${lib.drawFileInCanvas}\r\n imageCompression.canvasToFile = ${lib.canvasToFile}\r\n imageCompression.getExifOrientation = ${lib.getExifOrientation}\r\n imageCompression.handleMaxWidthOrHeight = ${lib.handleMaxWidthOrHeight}\r\n imageCompression.followExifOrientation = ${lib.followExifOrientation}\r\n imageCompression.cleanupMemory = ${lib.cleanupMemory}\r\n\r\n getDataUrlFromFile = imageCompression.getDataUrlFromFile\r\n getFilefromDataUrl = imageCompression.getFilefromDataUrl\r\n loadImage = imageCompression.loadImage\r\n drawImageInCanvas = imageCompression.drawImageInCanvas\r\n drawFileInCanvas = imageCompression.drawFileInCanvas\r\n canvasToFile = imageCompression.canvasToFile\r\n getExifOrientation = imageCompression.getExifOrientation\r\n handleMaxWidthOrHeight = imageCompression.handleMaxWidthOrHeight\r\n followExifOrientation = imageCompression.followExifOrientation\r\n cleanupMemory = imageCompression.cleanupMemory\r\n\r\n getNewCanvasAndCtx = ${getNewCanvasAndCtx}\r\n \r\n CustomFileReader = FileReader\r\n \r\n CustomFile = File\r\n \r\n function _slicedToArray(arr, n) { return arr }\r\n \r\n function _typeof(a) { return typeof a }\r\n\r\n function compress (){return (${compress}).apply(null, arguments)}\r\n `)\r\n}\r\n\r\nfunction generateWorkerScript () {\r\n // code to be run in the WebWorker\r\n return createWorker(`\r\n let scriptImported = false\r\n self.addEventListener('message', async (e) => {\r\n const { file, id, imageCompressionLibUrl, options } = e.data\r\n options.onProgress = (progress) => self.postMessage({ progress, id })\r\n try {\r\n if (!scriptImported) {\r\n // console.log('[worker] importScripts', imageCompressionLibUrl)\r\n self.importScripts(imageCompressionLibUrl)\r\n scriptImported = true\r\n }\r\n // console.log('[worker] self', self)\r\n const compressedFile = await imageCompression(file, options)\r\n self.postMessage({ file: compressedFile, id })\r\n } catch (e) {\r\n // console.error('[worker] error', e)\r\n self.postMessage({ error: e.message + '\\\\n' + e.stack, id })\r\n }\r\n })\r\n `)\r\n}\r\n\r\nexport function compressOnWebWorker (file, options) {\r\n return new Promise(async (resolve, reject) => {\r\n let id = cnt++\r\n\r\n if (!imageCompressionLibUrl) {\r\n imageCompressionLibUrl = generateLib()\r\n }\r\n\r\n if (!worker) {\r\n worker = generateWorkerScript()\r\n }\r\n\r\n function handler (e) {\r\n if (e.data.id === id) {\r\n if (e.data.progress !== undefined) {\r\n options.onProgress(e.data.progress)\r\n return\r\n }\r\n worker.removeEventListener('message', handler)\r\n if (e.data.error) {\r\n reject(new Error(e.data.error))\r\n }\r\n resolve(e.data.file)\r\n }\r\n }\r\n\r\n worker.addEventListener('message', handler)\r\n worker.addEventListener('error', reject)\r\n\r\n worker.postMessage({\r\n file,\r\n id,\r\n imageCompressionLibUrl,\r\n options: { ...options, onProgress: undefined }\r\n })\r\n })\r\n}\r\n","import compress from './image-compression'\r\nimport {\r\n canvasToFile,\r\n drawFileInCanvas,\r\n drawImageInCanvas,\r\n getDataUrlFromFile,\r\n getFilefromDataUrl,\r\n loadImage,\r\n getExifOrientation,\r\n handleMaxWidthOrHeight,\r\n followExifOrientation,\r\n CustomFile,\r\n cleanupCanvasMemory\r\n} from './utils'\r\nimport { compressOnWebWorker } from './web-worker'\r\n\r\n/**\r\n * Compress an image file.\r\n *\r\n * @param {File} file\r\n * @param {Object} options - { maxSizeMB=Number.POSITIVE_INFINITY, maxWidthOrHeight, useWebWorker=false, maxIteration = 10, exifOrientation, fileType }\r\n * @param {number} [options.maxSizeMB=Number.POSITIVE_INFINITY]\r\n * @param {number} [options.maxWidthOrHeight=undefined]\r\n * @param {boolean} [options.useWebWorker=false]\r\n * @param {number} [options.maxIteration=10]\r\n * @param {number} [options.exifOrientation] - default to be the exif orientation from the image file\r\n * @param {Function} [options.onProgress] - a function takes one progress argument (progress from 0 to 100)\r\n * @param {string} [options.fileType] - default to be the original mime type from the image file\r\n * @returns {Promise}\r\n */\r\nasync function imageCompression (file, options) {\r\n\r\n let compressedFile\r\n\r\n options.maxSizeMB = options.maxSizeMB || Number.POSITIVE_INFINITY\r\n const useWebWorker = typeof options.useWebWorker === 'boolean' ? options.useWebWorker : true\r\n delete options.useWebWorker\r\n if (options.onProgress === undefined) {\r\n options.onProgress = () => {}\r\n }\r\n\r\n if (!(file instanceof Blob || file instanceof CustomFile)) {\r\n throw new Error('The file given is not an instance of Blob or File')\r\n } else if (!/^image/.test(file.type)) {\r\n throw new Error('The file given is not an image')\r\n }\r\n\r\n // try run in web worker, fall back to run in main thread\r\n const inWebWorker = typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope\r\n\r\n // if ((useWebWorker && typeof Worker === 'function') || inWebWorker) {\r\n // console.log('run compression in web worker')\r\n // } else {\r\n // console.log('run compression in main thread')\r\n // }\r\n\r\n if (useWebWorker && typeof Worker === 'function' && !inWebWorker) {\r\n try {\r\n // console.log(1)\r\n // \"compressOnWebWorker\" is kind of like a recursion to call \"imageCompression\" again inside web worker\r\n compressedFile = await compressOnWebWorker(file, options)\r\n } catch (e) {\r\n // console.warn('Run compression in web worker failed:', e, ', fall back to main thread')\r\n // console.log(1.5)\r\n compressedFile = await compress(file, options)\r\n }\r\n } else {\r\n // console.log(2)\r\n compressedFile = await compress(file, options)\r\n }\r\n\r\n try {\r\n compressedFile.name = file.name\r\n compressedFile.lastModified = file.lastModified\r\n } catch (e) {}\r\n\r\n return compressedFile\r\n}\r\n\r\nimageCompression.getDataUrlFromFile = getDataUrlFromFile\r\nimageCompression.getFilefromDataUrl = getFilefromDataUrl\r\nimageCompression.loadImage = loadImage\r\nimageCompression.drawImageInCanvas = drawImageInCanvas\r\nimageCompression.drawFileInCanvas = drawFileInCanvas\r\nimageCompression.canvasToFile = canvasToFile\r\nimageCompression.getExifOrientation = getExifOrientation\r\n\r\nimageCompression.handleMaxWidthOrHeight = handleMaxWidthOrHeight\r\nimageCompression.followExifOrientation = followExifOrientation\r\nimageCompression.cleanupMemory = cleanupCanvasMemory\r\nimageCompression.version = '1.0.11'\r\n\r\nexport default imageCompression\r\n"],"names":["isBrowser","window","CustomFile","getFilefromDataUrl","resolve","n","bstr","charCodeAt","file","e","img","type","fileName","length","offset","i","height","maxWidthOrHeight","options","ctx","width","getNewCanvasAndCtx","transform","Error","canvas","inc","then","$await_6","maxSizeByte","currentSize","imageCompressionLibUrl","cnt","generateLib","script","imageCompression","getDataUrlFromFile","loadImage","drawImageInCanvas","drawFileInCanvas","canvasToFile","getExifOrientation"],"mappings":";;;;;;;y0DAAA,IAAMA,EAA8B,oBAAlBC,2EAKoF,wBAIzFC,6vCA6CN,SAASC,oHACP,SAAAC,8DAGa,IAClBC,EAAIC,uCAGFA,EAAAC,uEAGFC,8IA6DMC,6FAHAC,4wBAmB4BC,yDAAEC,+aAOtCC,mPAoBI,mEAE8BC,EAAA,GAAxBC,kIAYOP,2DAYnBQ,WACIC,EAAiBC,0GAcnBC,+CAQEC,EAAAJ,gCAGE,4FA8BFI,sCAGmBC,iHAQXF,EAAAG,qCAAiCH,EAAAG,6EACjCH,EAAAG,UAAc,EAAG,EAAG,sBAAmBH,EAAAG,6EACvCH,EAAAG,UAAc,GAAI,EAAG,yEAsB7BD,mBAASD,oBAMT,QADJD,uDAEI,IAAAI,oDAEF,MAAOd,GAEPU,GADAK,kFAUAA,EAAAL,gLC5PqDM,kdAQrDC,KAAA,SAAAC,gBAAAA,8XAiBIC,6FAeFC,6FAGYb,88BC5FhB,IACIc,IADAC,IAcJ,SAASC,okEAPPC,mnDC4DwBzB,+0BAa1B0B,iBAAAC,mBAAsCA,mBAEtCD,iBAAA/B,sCACA+B,iBAAAE,oBACAF,iBAAAG,oCACAH,iBAAAI,kCAEAJ,iBAAAK,0BA5FAL,iBAAAM"} \ No newline at end of file diff --git a/example/basic.html b/example/basic.html index 17fb023..1649ffb 100644 --- a/example/basic.html +++ b/example/basic.html @@ -100,6 +100,6 @@ } - + diff --git a/lib/index.js b/lib/index.js index da93ea4..2747a2c 100644 --- a/lib/index.js +++ b/lib/index.js @@ -88,6 +88,6 @@ imageCompression.getExifOrientation = getExifOrientation imageCompression.handleMaxWidthOrHeight = handleMaxWidthOrHeight imageCompression.followExifOrientation = followExifOrientation imageCompression.cleanupMemory = cleanupCanvasMemory -imageCompression.version = '1.0.10' +imageCompression.version = '1.0.11' export default imageCompression diff --git a/package.json b/package.json index 6cff4ab..aa92504 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "browser-image-compression", - "version": "1.0.10", + "version": "1.0.11", "description": "Compress images in the browser", "main": "dist/browser-image-compression.js", "module": "dist/browser-image-compression.mjs",