UNPKG

compressorjs-global

Version:
10 lines 9.22 kB
/*! * Compressor.js v1.1.1 * https://fengyuanchen.github.io/compressorjs * * Copyright 2018-present Chen Fengyuan * Released under the MIT license * * Date: 2022-03-22T15:13:43.699Z */ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).Compressor=t()}(this,function(){"use strict";function t(t,e){var i,r=Object.keys(t);return Object.getOwnPropertySymbols&&(i=Object.getOwnPropertySymbols(t),e&&(i=i.filter(function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable})),r.push.apply(r,i)),r}function a(r){for(var e=1;e<arguments.length;e++){var a=null!=arguments[e]?arguments[e]:{};e%2?t(Object(a),!0).forEach(function(e){var t,i;t=r,i=a[e=e],e in t?Object.defineProperty(t,e,{value:i,enumerable:!0,configurable:!0,writable:!0}):t[e]=i}):Object.getOwnPropertyDescriptors?Object.defineProperties(r,Object.getOwnPropertyDescriptors(a)):t(Object(a)).forEach(function(e){Object.defineProperty(r,e,Object.getOwnPropertyDescriptor(a,e))})}return r}function n(e,t){for(var i=0;i<t.length;i++){var r=t[i];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}function s(){return(s=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var i,r=arguments[t];for(i in r)Object.prototype.hasOwnProperty.call(r,i)&&(e[i]=r[i])}return e}).apply(this,arguments)}function U(e){return 0<e&&e<1/0}var e,i,o,r,l,h,c,u,f,d={exports:{}},B=(e=d,"undefined"!=typeof window&&(i=globalThis||window,o=i.HTMLCanvasElement&&i.HTMLCanvasElement.prototype,r=!!o,l=i.Blob&&function(){try{return Boolean(new Blob)}catch(e){return!1}}(),h=l&&i.Uint8Array&&function(){try{return 100===new Blob([new Uint8Array(100)]).size}catch(e){return!1}}(),c=i.BlobBuilder||i.WebKitBlobBuilder||i.MozBlobBuilder||i.MSBlobBuilder,u=/^data:((.*?)(;charset=.*?)?)(;base64)?,/,f=(l||c)&&i.atob&&i.ArrayBuffer&&i.Uint8Array&&function(e){var t,i,r,a,n,o=e.match(u);if(!o)throw new Error("invalid data URI");for(t=o[2]?o[1]:"text/plain"+(o[3]||";charset=US-ASCII"),n=!!o[4],e=e.slice(o[0].length),i=(n?atob:decodeURIComponent)(e),o=new ArrayBuffer(i.length),r=new Uint8Array(o),a=0;a<i.length;a+=1)r[a]=i.charCodeAt(a);return l?new Blob([h?r:o],{type:t}):((n=new c).append(o),n.getBlob(t))},r&&i.HTMLCanvasElement&&!o.toBlob&&(o.mozGetAsFile?o.toBlob=function(e,t,i){var r=this;setTimeout(function(){i&&o.toDataURL&&f?e(f(r.toDataURL(t,i))):e(r.mozGetAsFile("blob",t))})}:o.toDataURL&&f&&(o.msToBlob?o.toBlob=function(e,t,i){var r=this;setTimeout(function(){(t&&"image/png"!==t||i)&&o.toDataURL&&f?e(f(r.toDataURL(t,i))):e(r.msToBlob(t))})}:o.toBlob=function(e,t,i){var r=this;setTimeout(function(){e(f(r.toDataURL(t,i)))})})),e.exports?e.exports=f:i.dataURLtoBlob=f),d.exports),p={strict:!0,checkOrientation:!0,maxWidth:1/0,maxHeight:1/0,minWidth:0,minHeight:0,width:void 0,height:void 0,resize:"none",quality:.8,mimeType:"auto",convertTypes:["image/png"],convertSize:5e6,beforeDraw:null,drew:null,success:null,error:null},b="undefined"!=typeof window&&void 0!==window.document?window:{},m=Array.prototype.slice;var g=/^image\/.+$/;function j(e){return g.test(e)}var y=String.fromCharCode;var w=b.btoa;function v(e,t){for(var i,r=[],a=new Uint8Array(e);0<a.length;)r.push(y.apply(null,(i=a.subarray(0,8192),Array.from?Array.from(i):m.call(i)))),a=a.subarray(8192);return"data:".concat(t,";base64,").concat(w(r.join("")))}function O(e){var t,i,r,a,n,o,s,l=new DataView(e);try{if(255===l.getUint8(0)&&216===l.getUint8(1))for(var h=l.byteLength,c=2;c+1<h;){if(255===l.getUint8(c)&&225===l.getUint8(c+1)){i=c;break}c+=1}if(i&&(a=i+10,"Exif"!==function(e,t,i){var r,a="";for(i+=t,r=t;r<i;r+=1)a+=y(e.getUint8(r));return a}(l,i+4,4)||!(s=18761===(n=l.getUint16(a)))&&19789!==n||42===l.getUint16(a+2,s)&&8<=(o=l.getUint32(a+4,s))&&(r=a+o)),r)for(var u,f=l.getUint16(r,s),d=0;d<f;d+=1)if(u=r+12*d+2,274===l.getUint16(u,s)){u+=8,t=l.getUint16(u,s),l.setUint16(u,1,s);break}}catch(e){t=1}return t}var T=/\.\d*(?:0|9){12}\d*$/;function R(e,t){t=1<arguments.length&&void 0!==t?t:1e11;return T.test(e)?Math.round(e*t)/t:e}function k(e,t){var i,r=e.aspectRatio,a=e.height,e=e.width,t=1<arguments.length&&void 0!==t?t:"none",n=U(e),o=U(a);return n&&o?(i=a*r,("contain"===t||"none"===t)&&e<i||"cover"===t&&i<e?a=e/r:e=a*r):n?a=e/r:o&&(e=a*r),{width:e,height:a}}var x=b.ArrayBuffer,A=b.FileReader,M=b.URL||b.webkitURL,D=/\.\w+$/,L=b.Compressor;return function(){function i(e,t){if(!(this instanceof i))throw new TypeError("Cannot call a class as a function");this.file=e,this.image=new Image,this.options=a(a({},p),t),this.aborted=!1,this.result=null,this.init()}var e,t,r;return e=i,r=[{key:"noConflict",value:function(){return window.Compressor=L,i}},{key:"setDefaults",value:function(e){s(p,e)}}],(t=[{key:"init",value:function(){var r,e,a,n=this,o=this.file,t=this.options;e=o,"undefined"!=typeof Blob&&(e instanceof Blob||"[object Blob]"===Object.prototype.toString.call(e))?j(r=o.type)?M&&A?(x||(t.checkOrientation=!1),M&&!t.checkOrientation?this.load({url:M.createObjectURL(o)}):(e=new A,a=t.checkOrientation&&"image/jpeg"===r,(this.reader=e).onload=function(e){var t,e=e.target.result,i={};a?1<(t=O(e))||!M?(i.url=v(e,r),1<t&&s(i,function(e){var t=0,i=1,r=1;switch(e){case 2:i=-1;break;case 3:t=-180;break;case 4:r=-1;break;case 5:t=90,r=-1;break;case 6:t=90;break;case 7:t=90,i=-1;break;case 8:t=-90}return{rotate:t,scaleX:i,scaleY:r}}(t))):i.url=M.createObjectURL(o):i.url=e,n.load(i)},e.onabort=function(){n.fail(new Error("Aborted to read the image with FileReader."))},e.onerror=function(){n.fail(new Error("Failed to read the image with FileReader."))},e.onloadend=function(){n.reader=null},a?e.readAsArrayBuffer(o):e.readAsDataURL(o))):this.fail(new Error("The current browser does not support image compression.")):this.fail(new Error("The first argument must be an image File or Blob object.")):this.fail(new Error("The first argument must be a File or Blob object."))}},{key:"load",value:function(e){var t=this,i=this.file,r=this.image;r.onload=function(){t.draw(a(a({},e),{},{naturalWidth:r.naturalWidth,naturalHeight:r.naturalHeight}))},r.onabort=function(){t.fail(new Error("Aborted to load the image."))},r.onerror=function(){t.fail(new Error("Failed to load the image."))},b.navigator&&/(?:iPad|iPhone|iPod).*?AppleWebKit/i.test(b.navigator.userAgent)&&(r.crossOrigin="anonymous"),r.alt=i.name,r.src=e.url}},{key:"draw",value:function(e){var t=this,i=e.naturalWidth,r=e.naturalHeight,a=e.rotate,a=void 0===a?0:a,n=e.scaleX,n=void 0===n?1:n,e=e.scaleY,e=void 0===e?1:e,o=this.file,s=this.image,l=this.options,h=document.createElement("canvas"),c=h.getContext("2d"),u=Math.abs(a)%180==90,f=("contain"===l.resize||"cover"===l.resize)&&U(l.width)&&U(l.height),d=Math.max(l.maxWidth,0)||1/0,p=Math.max(l.maxHeight,0)||1/0,b=Math.max(l.minWidth,0)||0,m=Math.max(l.minHeight,0)||0,g=i/r,y=l.width,w=l.height,v=(u&&(d=(v=[p,d])[0],p=v[1],b=(v=[m,b])[0],m=v[1],y=(v=[w,y])[0],w=v[1]),k({aspectRatio:g=f?y/w:g,width:d,height:p},"contain")),v=(d=v.width,p=v.height,k({aspectRatio:g,width:b,height:m},"cover")),v=(b=v.width,m=v.height,w=f?(y=(v=k({aspectRatio:g,width:y,height:w},l.resize)).width,v.height):(y=void 0===(O=(v=k({aspectRatio:g,width:y,height:w})).width)?i:O,void 0===(O=v.height)?r:O),-(y=Math.floor(R(Math.min(Math.max(y,b),d))))/2),O=-(w=Math.floor(R(Math.min(Math.max(w,m),p))))/2,b=y,d=w,m=[],p=(f&&(0,p=(g=k({aspectRatio:g,width:p=i,height:f=r},{contain:"cover",cover:"contain"}[l.resize])).width,f=g.height,m.push((i-p)/2,(r-f)/2,p,f)),m.push(v,O,b,d),u&&(y=(g=[w,y])[0],w=g[1]),h.width=y,h.height=w,j(l.mimeType)||(l.mimeType=o.type),"transparent");o.size>l.convertSize&&0<=l.convertTypes.indexOf(l.mimeType)&&(l.mimeType="image/jpeg"),"image/jpeg"===l.mimeType&&(p="#fff"),c.fillStyle=p,c.fillRect(0,0,y,w),l.beforeDraw&&l.beforeDraw.call(this,c,h),this.aborted||(c.save(),c.translate(y/2,w/2),c.rotate(a*Math.PI/180),c.scale(n,e),c.drawImage.apply(c,[s].concat(m)),c.restore(),l.drew&&l.drew.call(this,c,h),this.aborted||(f=function(e){t.aborted||t.done({naturalWidth:i,naturalHeight:r,result:e})},h.toBlob?h.toBlob(f,l.mimeType,l.quality):f(B(h.toDataURL(l.mimeType,l.quality)))))}},{key:"done",value:function(e){var t=e.naturalWidth,i=e.naturalHeight,e=e.result,r=this.file,a=this.image,n=this.options;M&&!n.checkOrientation&&M.revokeObjectURL(a.src),!e||n.strict&&e.size>r.size&&n.mimeType===r.type&&!(n.width>t||n.height>i||n.minWidth>t||n.minHeight>i||n.maxWidth<t||n.maxHeight<i)?e=r:(a=new Date,e.lastModified=a.getTime(),e.lastModifiedDate=a,e.name=r.name,e.name&&e.type!==r.type&&(e.name=e.name.replace(D,(t=j(t=e.type)?t.substr(6):"",".".concat(t="jpeg"===t?"jpg":t))))),this.result=e,n.success&&n.success.call(this,e)}},{key:"fail",value:function(e){var t=this.options;if(!t.error)throw e;t.error.call(this,e)}},{key:"abort",value:function(){this.aborted||(this.aborted=!0,this.reader?this.reader.abort():this.image.complete?this.fail(new Error("The compression process has been aborted.")):(this.image.onload=null,this.image.onabort()))}}])&&n(e.prototype,t),r&&n(e,r),Object.defineProperty(e,"prototype",{writable:!1}),i}()});