var PWImageResizer=function(config){this.setConfig(config)};PWImageResizer.prototype.resize=function(file,completionCallback){var img=document.createElement("img");this.currentFile=file;var reader=new FileReader;var This=this;var contentType=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){var needsResize=false;var why="n/a";if(contentType!="image/jpeg"&&contentType!="image/png"&&contentType!="image/gif"){why="unsupported image content-type: "+contentType}else if(this.config.scaleRatio>0){needsResize=true;why="scaleRatio specified"}else if(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.maxSize0||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.maxSize0?Math.min(mWidth,mSize):mSize;if(mSize===mWidth)resizeType="max megapixels of "+this.config.maxSize}if(this.config.scaleRatio){var mScale=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)}var quality=this.config.quality;if(this.currentFile.type!="image/jpeg")quality=1;var imageData=canvas.toDataURL(this.currentFile.type,quality);if(typeof this.config.onScale==="function"){this.config.onScale(imageData)}if(this.currentFile.type==="image/jpeg"){try{var exifObj=piexif.load(img.src);var orientation=exifObj["0th"][piexif.ImageIFD.Orientation];if(orientation>4&&img.height>img.width){exifObj["0th"][piexif.ImageIFD.Orientation]=1}try{var exifStr=piexif.dump(exifObj);try{imageData=piexif.insert(exifStr,imageData)}catch(error){console.error(error)}}catch(error){console.error(error)}}catch(error){console.error(error)}}completionCallback(this.imageDataToBlob(imageData))};PWImageResizer.prototype.imageDataToBlob=function(imageData){var base64Marker=";base64,";if(imageData.indexOf(base64Marker)==-1){var parts=imageData.split(",");var contentType=parts[0].split(":")[1];var raw=parts[1];return new Blob([raw],{type:contentType})}var parts=imageData.split(base64Marker);var contentType=parts[0].split(":")[1];var raw=window.atob(parts[1]);var rawLength=raw.length;var uInt8Array=new Uint8Array(rawLength);for(var i=0;isrcCanvasData.height-1?srcCanvasData.height-1:Math.ceil(iyv);for(j=0;jsrcCanvasData.width-1?srcCanvasData.width-1:Math.ceil(ixv);idxD=(j+destCanvasData.width*i)*4;idxS00=(ix0+srcCanvasData.width*iy0)*4;idxS10=(ix1+srcCanvasData.width*iy0)*4;idxS01=(ix0+srcCanvasData.width*iy1)*4;idxS11=(ix1+srcCanvasData.width*iy1)*4;dx=ixv-ix0;dy=iyv-iy0;r=inner(srcCanvasData.data[idxS00],srcCanvasData.data[idxS10],srcCanvasData.data[idxS01],srcCanvasData.data[idxS11],dx,dy);destCanvasData.data[idxD]=r;g=inner(srcCanvasData.data[idxS00+1],srcCanvasData.data[idxS10+1],srcCanvasData.data[idxS01+1],srcCanvasData.data[idxS11+1],dx,dy);destCanvasData.data[idxD+1]=g;b=inner(srcCanvasData.data[idxS00+2],srcCanvasData.data[idxS10+2],srcCanvasData.data[idxS01+2],srcCanvasData.data[idxS11+2],dx,dy);destCanvasData.data[idxD+2]=b;a=inner(srcCanvasData.data[idxS00+3],srcCanvasData.data[idxS10+3],srcCanvasData.data[idxS01+3],srcCanvasData.data[idxS11+3],dx,dy);destCanvasData.data[idxD+3]=a}}};PWImageResizer.prototype.setConfig=function(customConfig){this.config=customConfig;this.config.debug=this.config.debug||false;if(typeof customConfig.quality=="undefined")customConfig.quality=1;if(customConfig.quality<.1)customConfig.quality=.1;if(customConfig.quality>1)customConfig.quality=1;this.config.quality=customConfig.quality;if(!this.config.maxWidth||this.config.maxWidth<0){this.config.maxWidth=0}if(!this.config.maxHeight||this.config.maxHeight<0){this.config.maxHeight=0}if(!this.config.maxSize||this.config.maxSize<0){this.config.maxSize=null}if(!this.config.scaleRatio||this.config.scaleRatio<=0||this.config.scaleRatio>=1){this.config.scaleRatio=null}if(this.config.maxWidth&&!this.config.maxHeight){this.config.maxHeight=this.config.maxWidth}else if(this.config.maxHeight&&!this.config.maxWidth){this.config.maxWidth=this.config.maxHeight}else if(!this.config.maxWidth&&!this.config.maxHeight){}};PWImageResizer.prototype.consoleLog=function(msg){if(this.config.debug)console.log("PWImageResizer: "+msg)};