-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpetit.min.js
1 lines (1 loc) · 2.28 KB
/
petit.min.js
1
'use strict';var _createClass=function(){function a(b,c){for(var f,d=0;d<c.length;d++)f=c[d],f.enumerable=f.enumerable||!1,f.configurable=!0,'value'in f&&(f.writable=!0),Object.defineProperty(b,f.key,f)}return function(b,c,d){return c&&a(b.prototype,c),d&&a(b,d),b}}();function _classCallCheck(a,b){if(!(a instanceof b))throw new TypeError('Cannot call a class as a function')}HTMLCanvasElement.prototype.toBlob||Object.defineProperty(HTMLCanvasElement.prototype,'toBlob',{value:function value(a,b,c){var d=this;setTimeout(function(){for(var f=atob(d.toDataURL(b,c).split(',')[1]),g=f.length,h=new Uint8Array(g),j=0;j<g;j++)h[j]=f.charCodeAt(j);a(new Blob([h],{type:b||'image/png'}))})}});var Petit=function(){function a(_ref){var _ref$fileType=_ref.fileType,b=void 0===_ref$fileType?'jpeg':_ref$fileType,_ref$quality=_ref.quality,c=void 0===_ref$quality?.88:_ref$quality,_ref$maxHeight=_ref.maxHeight,_ref$maxWidth=_ref.maxWidth,_ref$scaling=_ref.scaling,_ref$onError=_ref.onError,h=void 0===_ref$onError?function(k){return console.error(k)}:_ref$onError,_ref$onSuccess=_ref.onSuccess,j=void 0===_ref$onSuccess?null:_ref$onSuccess;_classCallCheck(this,a),this.onSuccess=j,this.fileType='image/'+b,this.quality=c,this.maxHeight=void 0!==_ref$maxHeight&&_ref$maxHeight,this.maxWidth=void 0!==_ref$maxWidth&&_ref$maxWidth,this.scaling=void 0!==_ref$scaling&&_ref$scaling,this.onError=h}return _createClass(a,[{key:'loadFile',value:function loadFile(_ref2){var b=_ref2.file,c=new FileReader,d=this.quality,f=this.maxHeight,g=this.maxWidth,h=this.fileType,j=this.onSuccess,k=this.scaling,l=this.onError;try{c.onload=function(m){var n=new Image;n.onload=function(){var o=null;if(!f&&g)o=g/n.width;else if(f&&!g)o=f/n.height;else if(f&&g){var p=g/n.width;p=n.height*p;var q=f/n.height;q=n.width*q,p>f?o=f/n.height:q>g&&(o=g/n.width)}else o=1;1<o&&!k&&(o=1);var r=document.createElement('canvas');r.width=n.width*o,r.height=n.height*o;var s=r.getContext('2d');s.drawImage(n,0,0,r.width,r.height),r.toBlob(function(t){return b.size<t.size&&1==o?void j(b,0):void j(t,1)},h,d)},n.src=m.target.result},c.readAsDataURL(b)}catch(m){l(m)}}}],[{key:'returnFileSize',value:function returnFileSize(b){return 1024>b?b+' bytes':1024<b&&1048576>b?(b/1024).toFixed(1)+' <span>KB</span>':1048576<b?(b/1048576).toFixed(1)+' <span>MB</span>':void 0}}]),a}();