forked from FBerthelot/angular-images-resizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular-images-resizer.js
1 lines (1 loc) · 4.54 KB
/
angular-images-resizer.js
1
"use strict";angular.module("images-resizer",[]),angular.module("images-resizer").service("resizeService",["$q","$document","$window",function(e,r,i){var t,a=this,o=!(!r[0].createElement("canvas").getContext||!r[0].createElement("canvas").getContext("2d"));this.createImage=function(r,t){var a=e.defer(),o=new i.Image;return t&&(o.crossOrigin=t),o.onload=function(){a.resolve(o)},o.onabort=function(){a.reject("image creation was aborted")},o.onerror=function(e){a.reject(e)},o.src=r,a.promise},this.resizeCanvas=function(e,i,t){if(!i||!t)return e;var a=r[0].createElement("canvas");a.width=i,a.height=t;var o=a.getContext("2d");return o.drawImage(e,0,0,a.width,a.height),a},this.resizeImage=function(r,i){if(!o)return e.reject("Canvas is not supported on your browser");if(!i||!r)return e.reject("Missing argument when calling resizeImage function");var t=e.defer();return i={height:i.height?i.height:i.width?null:i.size?null:1024,width:i.width?i.width:i.height?null:i.size?null:1024,size:i.size?i.size:500,sizeScale:i.sizeScale?i.sizeScale:"kb",step:i.step?i.step:3,outputFormat:i.outputFormat?i.outputFormat:"image/jpeg",blobCallback:i.blobCallback?i.blobCallback:null,crossOrigin:i.crossOrigin?i.crossOrigin:null},a.createImage(r,i.crossOrigin).then(function(e){if(i.height||i.width)t.resolve(a.resizeImageWidthHeight(e,i.width,i.height,i.step,i.outputFormat,i.blobCallback));else if(i.size){if(angular.isString(i.sizeScale))switch(i.sizeScale.toLowerCase()){case"kb":i.size*=1024;break;case"mb":i.size*=1048576;break;case"gb":i.size*=1073741824}t.resolve(a.resizeImageBySize(e,i.size,i.outputFormat,i.blobCallback))}else t.reject("Missing option to resize the image")})["catch"](t.reject),t.promise},this.resizeImageWidthHeight=function(e,i,a,o,n,s){if(!e)throw new Error("No image provided");if(n||(n="image/jpeg"),"blob"===n&&"function"!=typeof s)throw new Error("No callback function provided");t=r[0].createElement("canvas"),i||a?!i&&a?i=a/e.height*e.width:i&&!a&&(a=i/e.width*e.height):(i=e.width,a=e.height);var l=e.width!==i&&o?(e.width-i)/o:0,c=e.height!==a&&o?(e.height-a)/o:0;t.width=e.width,t.height=e.height,t.getContext("2d").drawImage(e,0,0,t.width,t.height);for(var h=1;h<o;h++){var g=e.width-l*h,u=e.height-c*h;t=this.resizeCanvas(t,g,u)}return t=this.resizeCanvas(t,i,a),"blob"===n?(t.toBlob(s),t):t.toDataURL(n)},this.resizeImageBySize=function(e,i,o,n){if(!e)throw new Error("No image provided");if(o||(o="image/jpeg"),"blob"===o&&"function"!=typeof n)throw new Error("No callback function provided");t=r[0].createElement("canvas"),t.width=e.width,t.height=e.height,t.getContext("2d").drawImage(e,0,0,t.width,t.height);var s;"blob"===o?t.toBlob(function(e){s=e}):s=t.toDataURL(o);for(var l=s,c=a.calulateImageSize(s,o),h=Math.max(1,Math.min(c/i,200)),g=20;c>i&&0!==g;){g--;var u={width:t.width/h,height:t.height/h},E=this.resizeCanvas(t,u.width,u.height);"blob"===o?E.toBlob(function(e){s=e}):s=E.toDataURL(o);var d=a.calulateImageSize(s,o);d/i<.5||0===d?(h/=2,h<1&&(g=0)):(t=E,l=s,c=d),t=E}return l},this.calulateImageSize=function(e,r){switch(r){case"image/jpg":r="image/jpeg";break;case"image/png":r="image/png";break;default:r="image/jpeg"}return Math.max(0,Math.round(3*(e.length-("data:"+r+";base64,").length)/4))}}]),angular.module("images-resizer").service("readLocalPicService",["$q","$window",function(e,r){function i(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(t){var a=e.defer();if(t.files&&t.files[0]){r.File&&r.FileReader&&r.FileList&&r.Blob||a.reject("Your browser do not support reading file");var o=new r.FileReader;o.onload=function(e){a.resolve(e.target.result)},o.onabort=function(e){a.reject("Fail to convert file in base64img, aborded: "+i(e))},o.onerror=function(e){a.reject("Fail to convert file in base64img, error: "+i(e))},o.readAsDataURL(t.files[0])}else a.reject("No file selected");return a.promise}}]);