"use strict";angular.module("images-resizer",[]),angular.module("images-resizer").service("resizeService",["$q","$document","$window",function(e,r,t){var i,a=this,n=!(!r[0].createElement("canvas").getContext||!r[0].createElement("canvas").getContext("2d"));this.createImage=function(r,i){var a=e.defer(),n=new t.Image;return i&&(n.crossOrigin=i),n.onload=function(){a.resolve(n)},n.onabort=function(){a.reject("image creation was aborted")},n.onerror=function(e){a.reject(e)},n.src=r,a.promise},this.resizeCanvas=function(e,t,i){if(!t||!i)return e;var a=r[0].createElement("canvas");a.width=t,a.height=i;var n=a.getContext("2d");return n.drawImage(e,0,0,a.width,a.height),a},this.resizeImage=function(r,t){if(!n)return e.reject("Canvas is not supported on your browser");if(!t||!r)return e.reject("Missing argument when calling resizeImage function");var i=e.defer();return t={height:t.height?t.height:t.width?null:t.size?null:1024,width:t.width?t.width:t.height?null:t.size?null:1024,size:t.size?t.size:500,sizeScale:t.sizeScale?t.sizeScale:"ko",step:t.step?t.step:3,outputFormat:t.outputFormat?t.outputFormat:"image/jpeg",crossOrigin:t.crossOrigin?t.crossOrigin:null},a.createImage(r,t.crossOrigin).then(function(e){if(t.height||t.width)i.resolve(a.resizeImageWidthHeight(e,t.width,t.height,t.step,t.outputFormat));else if(t.size){if(angular.isString(t.sizeScale))switch(t.sizeScale.toLowerCase()){case"ko":t.size*=1024;break;case"mo":t.size*=1048576;break;case"go":t.size*=1073741824}i.resolve(a.resizeImageBySize(e,t.size,t.outputFormat))}else i.reject("Missing option to resize the image")})["catch"](i.reject),i.promise},this.resizeImageWidthHeight=function(e,t,a,n,s){if(!e)return null;s||(s="image/jpeg"),i=r[0].createElement("canvas"),t||a?!t&&a?t=a/e.height*e.width:t&&!a&&(a=t/e.width*e.height):(t=e.width,a=e.height);var o=e.width!==t&&n?(e.width-t)/n:0,c=e.height!==a&&n?(e.height-a)/n:0;i.width=e.width,i.height=e.height,i.getContext("2d").drawImage(e,0,0,i.width,i.height);for(var h=1;n>h;h++){var g=e.width-o*h,l=e.height-c*h;i=this.resizeCanvas(i,g,l)}return i=this.resizeCanvas(i,t,a),i.toDataURL(s)},this.resizeImageBySize=function(e,t,n){if(!e)return null;n||(n="image/jpeg"),i=r[0].createElement("canvas"),i.width=e.width,i.height=e.height,i.getContext("2d").drawImage(e,0,0,i.width,i.height);for(var s=i.toDataURL(n),o=s,c=a.calulateImageSize(s,n),h=1>=c/(2*t)?.9:c/(2.3*t);c>t;){var g=r[0].createElement("canvas");g.width=i.width/h,g.height=i.height/h,g.getContext("2d").drawImage(i,0,0,g.width,g.height),s=i.toDataURL(n);var l=a.calulateImageSize(s,n);.5>l/t?h=1>=c/(2*t)?.9:c/(2.3*t):(i=g,o=s,c=a.calulateImageSize(s,n)),i=g}return o},this.calulateImageSize=function(e,r){switch(r){case"image/jpeg":r="image/jpg";break;default:r="image/jpg"}return Math.round(3*(e.length-("data:"+r+";base64,").length)/4)}}]),angular.module("images-resizer").service("readLocalPicService",["$q","$window",function(e,r){function t(e){var r=null;switch(e.target.error.code){case FileError.NOT_FOUND_ERR:r="NOT_FOUND_ERR";break;case FileError.SECURITY_ERR:r="SECURITY_ERR";break;case FileError.ABORT_ERR:r="ABORT_ERR";break;case FileError.NOT_READABLE_ERR:r="NOT_READABLE_ERR";break;case FileError.ENCODING_ERR:r="ENCODING_ERR";break;case FileError.NO_MODIFICATION_ALLOWED_ERR:r="NO_MODIFICATION_ALLOWED_ERR";break;case FileError.INVALID_STATE_ERR:r="INVALID_STATE_ERR";break;case FileError.SYNTAX_ERR:r="SYNTAX_ERR";break;case FileError.INVALID_MODIFICATION_ERR:r="INVALID_MODIFICATION_ERR";break;case FileError.QUOTA_EXCEEDED_ERR:r="QUOTA_EXCEEDED_ERR";break;case FileError.TYPE_MISMATCH_ERR:r="TYPE_MISMATCH_ERR";break;case FileError.PATH_EXISTS_ERR:r="PATH_EXISTS_ERR";break;default:r="Unknown Error: "+e.target.error.code}return r}this.readFileInput=function(i){var a=e.defer();if(i.files&&i.files[0]){r.File&&r.FileReader&&r.FileList&&r.Blob||a.reject("Your browser do not support reading file");var n=new r.FileReader;n.onload=function(e){a.resolve(e.target.result)},n.onabort=function(e){a.reject("Fail to convert file in base64img, aborded: "+t(e))},n.onerror=function(e){a.reject("Fail to convert file in base64img, error: "+t(e))},n.readAsDataURL(i.files[0])}else a.reject("No file selected");return a.promise}}]);