ngx-pica
Version:
Angular 5 module to resize images files in browser
1 lines • 6.29 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@angular/core"),require("rxjs/Subject"),require("exif-js"),require("pica/dist/pica")):"function"==typeof define&&define.amd?define(["exports","@angular/core","rxjs/Subject","exif-js","pica/dist/pica"],t):t(e["ngx-pica"]={},e.ng.core,e.Rx,e.EXIF,e.pica)}(this,function(e,t,r,n,i){"use strict";i=i&&i.hasOwnProperty("default")?i.default:i;var a;!function(e){e.NO_FILES_RECEIVED="NO_FILES_RECEIVED",e.CANVAS_CONTEXT_IDENTIFIER_NOT_SUPPORTED="CANVAS_CONTEXT_IDENTIFIER_NOT_SUPPORTED",e.NOT_BE_ABLE_TO_COMPRESS_ENOUGH="NOT_BE_ABLE_TO_COMPRESS_ENOUGH"}(a||(a={}));var o=function(){function e(){}return e.prototype.getExifOrientedImage=function(e){return new Promise(function(t,r){n.getData(e,function(){var r=n.getAllTags(e).Orientation;if(r){var i=document.createElement("canvas"),a=i.getContext("2d"),o=e.width,c=e.height;if([5,6,7,8].indexOf(r)>-1?(i.width=c,i.height=o):(i.width=o,i.height=c),a){switch(r){case 2:a.transform(-1,0,0,1,o,0);break;case 3:a.transform(-1,0,0,-1,o,c);break;case 4:a.transform(1,0,0,-1,0,c);break;case 5:a.transform(0,1,1,0,0,0);break;case 6:a.transform(0,1,-1,0,c,0);break;case 7:a.transform(0,-1,-1,0,c,o);break;case 8:a.transform(0,-1,1,0,0,o);break;default:a.transform(1,0,0,1,0,0)}a.drawImage(e,0,0,o,c);var s=new Image;s.width=o,s.height=c,s.onload=function(){t(s)},s.src=i.toDataURL()}}else t(e)})})},e.decorators=[{type:t.Injectable}],e.ctorParameters=function(){return[]},e}(),c=function(){function e(e){this._ngxPicaExifService=e,this.picaResizer=new i,this.MAX_STEPS=20,this.picaResizer&&this.picaResizer.resize||(this.picaResizer=new window.pica)}return e.prototype.resizeImages=function(e,t,n,i){var o=this,c=new r.Subject,s=e.length;if(s>0){var u=new r.Subject,f=0,p=u.subscribe(function(r){o.resizeImage(r,t,n,i).subscribe(function(t){f++,c.next(t),f<s?u.next(e[f]):(c.complete(),p.unsubscribe())},function(e){var t={file:r,err:e};c.error(t)})});u.next(e[f])}else{var m={err:a.NO_FILES_RECEIVED};c.error(m),c.complete()}return c.asObservable()},e.prototype.resizeImage=function(e,t,n,i){var o=this,c=new r.Subject,s=document.createElement("canvas"),u=s.getContext("2d"),f=new Image;return u?(f.onload=function(){o._ngxPicaExifService.getExifOrientedImage(f).then(function(r){window.URL.revokeObjectURL(f.src),s.width=r.width,s.height=r.height,u.drawImage(r,0,0);var a=u.getImageData(0,0,r.width,r.height);if(i&&i.aspectRatio&&i.aspectRatio.keepAspectRatio){var p=0;p=i.aspectRatio.forceMinDimensions?Math.max(t/a.width,n/a.height):Math.min(t/a.width,n/a.height),t=Math.round(a.width*p),n=Math.round(a.height*p)}var m=document.createElement("canvas");m.width=t,m.height=n,o.picaResize(e,s,m,i).catch(function(e){return c.error(e)}).then(function(e){c.next(e)})})},f.src=window.URL.createObjectURL(e)):c.error(a.CANVAS_CONTEXT_IDENTIFIER_NOT_SUPPORTED),c.asObservable()},e.prototype.compressImages=function(e,t){var n=this,i=new r.Subject,o=e.length;if(o>0){var c=new r.Subject,s=0,u=c.subscribe(function(r){n.compressImage(r,t).subscribe(function(t){s++,i.next(t),s<o?c.next(e[s]):(i.complete(),u.unsubscribe())},function(e){var t={file:r,err:e};i.error(t)})});c.next(e[s])}else{var f={err:a.NO_FILES_RECEIVED};i.error(f),i.complete()}return i.asObservable()},e.prototype.compressImage=function(e,t){var n=this,i=new r.Subject;if(this.bytesToMB(e.size)<=t)setTimeout(function(){i.next(e)});else{var o=document.createElement("canvas"),c=o.getContext("2d"),s=new Image;c?(s.onload=function(){n._ngxPicaExifService.getExifOrientedImage(s).then(function(r){window.URL.revokeObjectURL(s.src),o.width=r.width,o.height=r.height,c.drawImage(r,0,0),n.getCompressedImage(o,e.type,1,t,0).catch(function(e){return i.error(e)}).then(function(t){var r=n.blobToFile(t,e.name,e.type,(new Date).getTime());i.next(r)})})},s.src=window.URL.createObjectURL(e)):i.error(a.CANVAS_CONTEXT_IDENTIFIER_NOT_SUPPORTED)}return i.asObservable()},e.prototype.getCompressedImage=function(e,t,r,n,i){var a=this;return new Promise(function(o,c){a.picaResizer.toBlob(e,t,r).catch(function(e){return c(e)}).then(function(t){a.checkCompressedImageSize(e,t,r,n,i).catch(function(e){return c(e)}).then(function(e){o(e)})})})},e.prototype.checkCompressedImageSize=function(e,t,r,n,i){var o=this;return new Promise(function(c,s){if(i>o.MAX_STEPS&&s(a.NOT_BE_ABLE_TO_COMPRESS_ENOUGH),o.bytesToMB(t.size)<n)c(t);else{var u=r-.1*r,f=i+1;c(o.getCompressedImage(e,t.type,u,n,f))}})},e.prototype.picaResize=function(e,t,r,n){var i=this;return new Promise(function(a,o){i.picaResizer.resize(t,r,n).catch(function(e){return o(e)}).then(function(t){return i.picaResizer.toBlob(t,e.type)}).then(function(t){var r=i.blobToFile(t,e.name,e.type,(new Date).getTime());a(r)})})},e.prototype.blobToFile=function(e,t,r,n){return new File([e],t,{type:r,lastModified:n})},e.prototype.bytesToMB=function(e){return e/1048576},e.decorators=[{type:t.Injectable}],e.ctorParameters=function(){return[{type:o}]},e}(),s=function(){function e(){this.imageExtensions=["ase","art","bmp","blp","cd5","cit","cpt","cr2","cut","dds","dib","djvu","egt","exif","gif","gpl","grf","icns","ico","iff","jng","jpeg","jpg","jfif","jp2","jps","lbm","max","miff","mng","msp","nitf","ota","pbm","pc1","pc2","pc3","pcf","pcx","pdn","pgm","PI1","PI2","PI3","pict","pct","pnm","pns","ppm","psb","psd","pdd","psp","px","pxm","pxr","qfx","raw","rle","sct","sgi","rgb","int","bw","tga","tiff","tif","vtf","xbm","xcf","xpm","3dv","amf","ai","awg","cgm","cdr","cmx","dxf","e2d","egt","eps","fs","gbr","odg","svg","stl","vrml","x3d","sxd","v2d","vnd","wmf","emf","art","xar","png","webp","jxr","hdp","wdp","cur","ecw","iff","lbm","liff","nrrd","pam","pcx","pgf","sgi","rgb","rgba","bw","int","inta","sid","ras","sun","tga"]}return e.prototype.isImage=function(e){var t=e.name.toLowerCase().substr(e.name.lastIndexOf(".")+1);return-1!==this.imageExtensions.indexOf(t)},e.decorators=[{type:t.Injectable}],e.ctorParameters=function(){return[]},e}(),u=function(){function e(){}return e.decorators=[{type:t.NgModule,args:[{providers:[{provide:c,useClass:c},{provide:o,useClass:o},{provide:s,useClass:s}]}]}],e.ctorParameters=function(){return[]},e}();e.NgxPicaModule=u,e.NgxPicaService=c,e.NgxPicaImageService=s,Object.defineProperty(e,"__esModule",{value:!0})});