UNPKG

ng7-pica

Version:

Angular module to resize images files in browser

2 lines 6.07 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("rxjs"),require("exif-js"),require("pica"),require("@angular/core")):"function"==typeof define&&define.amd?define("ng7-pica",["exports","rxjs","exif-js","pica","@angular/core"],t):t(e["ng7-pica"]={},e.rxjs,e.EXIF,e.Pica,e.ng.core)}(this,function(e,d,f,t,r){"use strict";t=t&&t.hasOwnProperty("default")?t["default"]:t;var g="NO_FILES_RECEIVED",h="CANVAS_CONTEXT_IDENTIFIER_NOT_SUPPORTED",p="NOT_BE_ABLE_TO_COMPRESS_ENOUGH",n=function(){function e(){}return e.prototype.getExifOrientedImage=function(p){return new Promise(function(u,e){f.getData(p,function(){var e=f.getAllTags(p).Orientation;if(e){if(!/^[1-8]$/.test(e))throw new Error("orientation should be [1-8]");var t=document.createElement("canvas"),r=t.getContext("2d"),n=0,i=0,a=0,o=p.width,c=p.height;switch(-1<[5,6,7,8].indexOf(e)&&(o=p.height,c=p.width),t.width=o,t.height=c,e){case 3:case 4:i=-p.width,a=-p.height,n=180;break;case 5:case 6:a=-p.height,n=90;break;case 7:case 8:i=-p.width,n=270}-1<[2,4,5,7].indexOf(e)&&(r.translate(o,0),r.scale(-1,1)),r.rotate(n/180*Math.PI),r.drawImage(p,i,a);var s=new Image;s.width=o,s.height=c,s.onload=function(){u(s)},s.src=t.toDataURL()}else u(p)})})},e.decorators=[{type:r.Injectable}],e}(),i=function(){function e(e){this._ngxPicaExifService=e,this.picaResizer=new t,this.MAX_STEPS=20,this.picaResizer&&this.picaResizer.resize||(this.picaResizer=new window.Pica)}return e.prototype.resizeImages=function(t,e,n,i){var a=this,o=new d.Subject,c=t.length;if(0<c){var s=new d.Subject,u=0,p=s.subscribe(function(r){a.resizeImage(r,e,n,i).subscribe(function(e){u++,o.next(e),u<c?s.next(t[u]):(o.complete(),p.unsubscribe())},function(e){var t={file:r,err:e};o.error(t)})});s.next(t[u])}else{var r={err:g};o.error(r),o.complete()}return o.asObservable()},e.prototype.resizeImage=function(i,a,o,c){var s=this,u=new d.Subject,p=document.createElement("canvas"),f=p.getContext("2d"),g=new Image;return f?(g.onload=function(){s._ngxPicaExifService.getExifOrientedImage(g).then(function(e){window.URL.revokeObjectURL(g.src),p.width=e.width,p.height=e.height,f.drawImage(e,0,0);var t=f.getImageData(0,0,e.width,e.height);if(c&&c.aspectRatio&&c.aspectRatio.keepAspectRatio){var r=0;r=c.aspectRatio.forceMinDimensions?Math.max(a/t.width,o/t.height):Math.min(a/t.width,o/t.height),a=Math.round(t.width*r),o=Math.round(t.height*r)}var n=document.createElement("canvas");n.width=a,n.height=o,s.picaResize(i,p,n,c)["catch"](function(e){return u.error(e)}).then(function(e){u.next(e)})})},g.src=window.URL.createObjectURL(i)):u.error(h),u.asObservable()},e.prototype.compressImages=function(t,e){var n=this,i=new d.Subject,a=t.length;if(0<a){var o=new d.Subject,c=0,s=o.subscribe(function(r){n.compressImage(r,e).subscribe(function(e){c++,i.next(e),c<a?o.next(t[c]):(i.complete(),s.unsubscribe())},function(e){var t={file:r,err:e};i.error(t)})});o.next(t[c])}else{var r={err:g};i.error(r),i.complete()}return i.asObservable()},e.prototype.compressImage=function(r,t){var n=this,i=new d.Subject,a=document.createElement("canvas"),o=a.getContext("2d"),c=new Image;return o?(c.onload=function(){n._ngxPicaExifService.getExifOrientedImage(c).then(function(e){window.URL.revokeObjectURL(c.src),a.width=e.width,a.height=e.height,o.drawImage(e,0,0),n.bytesToMB(r.size)<=t?n.picaResizer.toBlob(a,r.type,1)["catch"](function(e){return i.error(e)}).then(function(e){var t=n.blobToFile(e,r.name,r.type,(new Date).getTime());i.next(t)}):n.getCompressedImage(a,r.type,1,t,0)["catch"](function(e){return i.error(e)}).then(function(e){var t=n.blobToFile(e,r.name,r.type,(new Date).getTime());i.next(t)})})},c.src=window.URL.createObjectURL(r)):i.error(h),i.asObservable()},e.prototype.getCompressedImage=function(n,e,i,a,o){var c=this;return new Promise(function(t,r){c.picaResizer.toBlob(n,e,i)["catch"](function(e){return r(e)}).then(function(e){c.checkCompressedImageSize(n,e,i,a,o)["catch"](function(e){return r(e)}).then(function(e){t(e)})})})},e.prototype.checkCompressedImageSize=function(i,a,o,c,s){var u=this;return new Promise(function(e,t){if(s>u.MAX_STEPS)t(p);else if(u.bytesToMB(a.size)<c)e(a);else{var r=o-.1*o,n=s+1;e(u.getCompressedImage(i,a.type,r,c,n))}})},e.prototype.picaResize=function(n,e,i,a){var o=this;return new Promise(function(r,t){o.picaResizer.resize(e,i,a)["catch"](function(e){return t(e)}).then(function(e){return o.picaResizer.toBlob(e,n.type)}).then(function(e){var t=o.blobToFile(e,n.name,n.type,(new Date).getTime());r(t)})})},e.prototype.blobToFile=function(e,t,r,n){var i=new Blob([e],{type:r});return i.name=t,i.lastModifiedDate=n,i},e.prototype.bytesToMB=function(e){return e/1048576},e.decorators=[{type:r.Injectable}],e.ctorParameters=function(){return[{type:n}]},e}(),a=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:r.Injectable}],e}(),o=function(){function e(){}return e.decorators=[{type:r.NgModule,args:[{providers:[{provide:i,useClass:i},{provide:n,useClass:n},{provide:a,useClass:a}]}]}],e}();e.NgxPicaModule=o,e.NgxPicaService=i,e.NgxPicaImageService=a,e.NgxPicaExifService=n,Object.defineProperty(e,"__esModule",{value:!0})}); //# sourceMappingURL=ng7-pica.umd.min.js.map