varPWImageResizer=function(config){this.setConfig(config)};PWImageResizer.prototype.resize=function(file,completionCallback){varimg=document.createElement("img");this.currentFile=file;varreader=newFileReader;varThis=this;varcontentType=file.type.toString();reader.onload=function(e){img.src=e.target.result;img.onload=function(){if(!This.needsResize(img,contentType)){completionCallback(false);return}This.scaleImage(img,completionCallback)}};reader.readAsDataURL(file)};PWImageResizer.prototype.needsResize=function(img,contentType){varneedsResize=false;varwhy="n/a";if(contentType!="image/jpeg"&&contentType!="image/png"&&contentType!="image/gif"){why="unsupported image content-type: "+contentType}elseif(this.config.scaleRatio>0){needsResize=true;why="scaleRatio specified"}elseif(this.config.maxWidth>0||this.config.maxHeight>0){if(this.config.maxWidth>0&&img.width>this.config.maxWidth)needsResize=true;if(this.config.maxHeight>0&&img.height>this.config.maxHeight)needsResize=true;why=needsResize?"dimensions exceed max allowed":"dimensions do not require resize"}if(!needsResize&&this.config.maxSize>0){if(this.config.maxSize<img.width*img.height/1e6)needsResize=true;why=(needsResize?"megapixels exceeds ":"megapixels below ")+this.config.maxSize}if(this.config.debug){this.consoleLog("needsResize="+(needsResize?"Yes":"No")+" ("+why+")")}returnneedsResize};PWImageResizer.prototype.drawImage=function(context,img,x,y,width,height,deg,flip,flop,center){context.save();if(typeofwidth==="undefined")width=img.width;if(typeofheight==="undefined")height=img.height;if(typeofcenter==="undefined")center=false;if(center){x-=width/2;y-=height/2}context.translate(x+width/2,y+height/2);varrad=2*Math.PI-deg*Math.PI/180;context.rotate(rad);if(flip)flipScale=-1;elseflipScale=1;if(flop)flopScale=-1;elseflopScale=1;context.scale(flipScale,flopScale);context.drawImage(img,-width/2,-height/2,width,height);context.restore()};PWImageResizer.prototype.scaleImage=function(img,completionCallback){varcanvas=document.createElement("canvas");canvas.width=img.width;canvas.height=img.height;varctx=canvas.getContext("2d");ctx.save();varwidth=canvas.width;varstyleWidth=canvas.style.width;varheight=canvas.height;varstyleHeight=canvas.style.height;ctx.drawImage(img,0,0);ctx.restore();varratio=canvas.width/canvas.height;varmWidth=0;varresizeType="";if(this.config.maxWidth>0||this.config.maxHeight>0){mWidth=Math.min(this.config.maxWidth,ratio*this.config.maxHeight);resizeType="max width/height of "+this.config.maxWidth+"x"+this.config.maxHeight}if(this.config.maxSize>0&&this.config.maxSize<canvas.width*canvas.height/1e6){varmSize=Math.floor(Math.sqrt(this.config.maxSize*ratio)*1e3);mWidth=mWidth>0?Math.min(mWidth,mSize):mSize;if(mSize===mWidth)resizeType="max megapixels of "+this.config.maxSize}if(this.config.scaleRatio){varmScale=Math.floor(this.config.scaleRatio*canvas.width);mWidth=mWidth>0?Math.min(mWidth,mScale):mScale;if(mScale==mWidth)resizeType="scale ratio of "+this.config.scaleRatio}if(mWidth<=0){this.consoleLog("image size is too small to resize");completionCallback(false);return}if(this.config.debug){this.consoleLog("original image size: "+canvas.width+"x"+canvas.height+" px");this.consoleLog("scaled image size: "+mWidth+"x"+Math.floor(mWidth/ratio)+" px via "+resizeType)}while(canvas.width>=2*mWidth){canvas=this.getHalfScaleCanvas(canvas)}if(canvas.width>mWidth){canvas=this.scaleCanvasWithAlgorithm(canvas,mWidth)}varquality=this.config.quality;if(this.currentFile.type!="image/jpeg")quality=1;varimageData=canvas.toDataURL(this.currentFile.type,quality);if(typeofthis.config.onScale==="function"){this.config.onScale(imageData)}if(this.currentFile.type==="image/jpeg"){try{varexifObj=piexif.load(img.src);varorientation=exifObj["0th"][piexif.ImageIFD.Orientation];if(orientation>4&&img.height>img.width){exifObj["0th"][piexif.ImageIFD.Orientation]=1}try{varexifStr=piexif.dump(exifObj);try{imageData=piexif.insert(exifStr,imageData)}catch(error){console.error(error)}}catch(error){console.error(error)}}catch(error){console.error(error)