UNPKG

5.35 kBJavaScriptView Raw
1var daycaca=function(t){function e(i){if(r[i])return r[i].exports;var a=r[i]={i:i,l:!1,exports:{}};return t[i].call(a.exports,a,a.exports,e),a.l=!0,a.exports}var r={};return e.m=t,e.c=r,e.d=function(t,r,i){e.o(t,r)||Object.defineProperty(t,r,{configurable:!1,enumerable:!0,get:i})},e.n=function(t){var r=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(r,"a",r),r},e.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},e.p="",e(e.s=0)}([function(t,e,r){"use strict";var i="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},a=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var i in r)Object.prototype.hasOwnProperty.call(r,i)&&(t[i]=r[i])}return t},n=function(t){return"number"==typeof t},o=/[.\/](png|jpeg|jpg|gif|bmp)/,s={ratio:1,compress:80,enableWebWorker:!1};t.exports={setConfig:function(t){this._config=a(s,t)},init:function(t,e){var r=this,i=t.split(";"),a=null,n=this._createImage(t);i.length>1&&(a=i[0].replace("data:","")),n.onload=function(){var t=r._getCanvas(n.naturalWidth,n.naturalHeight);t.getContext("2d").drawImage(n,0,0);var i=t.toDataURL(a);e(i)}},base64:function(t,e){var r=this._getSrc(t),i=r.src,a=r.type;if("file"===a)return this._readFile(i,e);if("video"===a){var n=t,o=this._getCanvas(n.videoWidth,n.videoHeight);o.getContext("2d").drawImage(n,0,0);e(o.toDataURL(),o)}return this.init(i,e)},compress:function(t,e,r){var i=this,a=this._getSrc(t),n=a.src;if("file"===a.type)return this._readFile(n,function(a){i._compress(a,t,e,r)});this._compress(n,t,e,r)},_compress:function(t,e,r,i){var a=this;this._loadImage(t,function(t){var n=a._getImageType(e),o=a._getCanvas(t.naturalWidth,t.naturalHeight);o.getContext("2d").drawImage(t,0,0);var s=o.toDataURL(n,r/100);i(s)})},crop:function(t,e,r){var i=this,a=this._getSrc(t),n=a.src;if("file"===a.type)return this._readFile(n,function(a){i._crop(a,t,e,r)});this._crop(n,t,e,r)},_crop:function(t,e,r,i){var a=this;this._loadImage(t,function(t){if(!r.w&&r.width&&(r.w=r.width,r.h=r.height),n(r.x)&&n(r.y)&&r.w>0&&r.h>0){var o=r.w,s=r.h;r.maxWidth&&r.maxWidth<o&&(o=r.maxWidth,s=r.h*o/r.w),r.maxHeight&&r.maxHeight<s&&(s=r.maxHeight),r.fixedWidth&&r.fixedHeight&&(o=r.fixedWidth,s=r.fixedHeight);var c=a._getCanvas(o,s);c.getContext("2d").drawImage(t,r.x,r.y,r.w,r.h,0,0,o,s);var h=a._getImageType(e),u=c.toDataURL(h,r.compress/100);i(u)}})},resize:function(t,e,r){var a=this,n=this._getSrc(t),o=n.src,c=n.type,h={};if("number"!=typeof e&&"string"!=typeof e||(h={ratio:e,compress:s.compress}),"object"===(void 0===e?"undefined":i(e))&&(h=e),"file"===c)return this._readFile(o,function(e){a._resize(e,t,h,r)});this._resize(o,t,h,r)},_resize:function(t,e,r,i){function a(t,e,r,i){return t/e==r/i}var n=this;this._loadImage(t,function(o){var s=o.naturalWidth,c=o.naturalHeight,h=s/c;if(r.ratio>0)s=Math.floor(o.naturalWidth*r.ratio),c=Math.floor(o.naturalHeight*r.ratio);else if("number"==typeof r.width&&"number"==typeof r.height)if(a(r.width,r.height,s,c)){if(s>r.width||c>r.height)return r.x=(s-r.width)/2,r.y=(c-r.height)/2,n._crop(t,e,r,i);if(s<r.width)return s=r.width,c=s/h,r.x=0,r.y=(c-r.height)/2,n._crop(t,e,r,i);if(c<r.height)return c=r.height,s=c*h,r.y=0,r.x=(s-r.width)/2,n._crop(t,e,r,i)}else s=Math.floor(r.width),c=Math.floor(r.height);var u=n._getCanvas(s,c);u.getContext("2d").drawImage(o,0,0,o.naturalWidth,o.naturalHeight,0,0,s,c);var f=n._getImageType(e),l=u.toDataURL(f,r.compress/100);i(l)})},rotate:function(t,e,r){var i=this,a=this._getSrc(t),n=a.src;return"file"===a.type?this._readFile(n,function(){i._rotate(n,t,e,r)}):e%360==0?r(n):void this._rotate(n,t,e,r)},_rotate:function(t,e,r,i){var a=this;this._loadImage(t,function(t){var n=t.naturalWidth,o=t.naturalHeight;90!==(r%=360)&&270!==r||(n=t.naturalHeight,o=t.naturalWidth);var s=a._getCanvas(n,o),c=s.getContext("2d");c.clearRect(0,0,n,o),c.fillStyle="white",c.fillRect(0,0,n,o),c.translate(n/2,o/2),c.rotate(r*Math.PI/180),c.drawImage(t,-t.naturalWidth/2,-t.naturalHeight/2);var h=a._getImageType(e),u=s.toDataURL(h,1);i(u,n,o),s=null,c=null})},_loadImage:function(t,e){var r=this._createImage(t);r.onload=function(){e(r)}},_readFile:function(t,e){var r=new FileReader;r.onload=function(t){var r=t.target.result;e(r)},r.readAsDataURL(t)},_getCanvas:function(t,e){var r=document.createElement("canvas");return r.width=t,r.height=e,r},_createImage:function(t){var e=new Image;return e.src=t,e.crossOrigin="anonymous",e},_getSrc:function(t){var e=t,r="url";if(this._isImageElement(t)){var i=t.src;if(!i)throw new Error("Element must hava src");e=i,r="element"}else this._isVideoElement(t)?(e=t,r="video"):this._isFileObject(t)&&(e=t,r="file");return{src:e,type:r}},_isFileObject:function(t){return"object"===(void 0===t?"undefined":i(t))&&t.type&&t.size>0},_isImageElement:function(t){return"object"===(void 0===t?"undefined":i(t))&&"IMG"===t.tagName},_isVideoElement:function(t){return"object"===(void 0===t?"undefined":i(t))&&"VIDEO"===t.tagName},_getImageType:function(t){var e=this._getSrc(t),r=e.src,i=e.type,a="image/jpeg";if("file"===i){var n=t.type,s=n.match(/(image\/[\w]+)\.*/)[0];void 0!==s&&(a=s)}else{var c=o.exec(r);c&&c[1]&&(a="image/"+c[1])}return a}}}]);
\No newline at end of file