-
Notifications
You must be signed in to change notification settings - Fork 18
/
angular-images-resizer.js
1 lines (1 loc) · 4.1 KB
/
angular-images-resizer.js
1
"use strict";angular.module("images-resizer",[]),angular.module("images-resizer").service("resizeService",["$q","$document","$window",function(e,r,t){var i,a=this,s=!(!r[0].createElement("canvas").getContext||!r[0].createElement("canvas").getContext("2d"));this.createImage=function(r,i){var a=e.defer(),s=new t.Image;return i&&(s.crossOrigin=i),s.onload=function(){a.resolve(s)},s.onabort=function(){a.reject("image creation was aborted")},s.onerror=function(e){a.reject(e)},s.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 s=a.getContext("2d");return s.drawImage(e,0,0,a.width,a.height),a},this.resizeImage=function(r,t){if(!s)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,s,n){if(!e)return null;n||(n="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&&s?(e.width-t)/s:0,h=e.height!==a&&s?(e.height-a)/s:0;i.width=e.width,i.height=e.height,i.getContext("2d").drawImage(e,0,0,i.width,i.height);for(var c=1;s>c;c++){var g=e.width-o*c,l=e.height-h*c;i=this.resizeCanvas(i,g,l)}return i=this.resizeCanvas(i,t,a),i.toDataURL(n)},this.resizeImageBySize=function(e,t,s){if(!e)return null;s||(s="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 n=i.toDataURL(s),o=n,h=a.calulateImageSize(n,s),c=Math.max(1,Math.min(h/t,200)),g=20;h>t&&0!==g;){g--;var l={width:i.width/c,height:i.height/c},u=this.resizeCanvas(i,l.width,l.height);n=u.toDataURL(s);var E=a.calulateImageSize(n,s);.5>E/t||0===E?(c/=2,1>c&&(g=0)):(i=u,o=n,h=E),i=u}return o},this.calulateImageSize=function(e,r){switch(r){case"image/jpeg":r="image/jpg";break;default:r="image/jpg"}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 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 s=new r.FileReader;s.onload=function(e){a.resolve(e.target.result)},s.onabort=function(e){a.reject("Fail to convert file in base64img, aborded: "+t(e))},s.onerror=function(e){a.reject("Fail to convert file in base64img, error: "+t(e))},s.readAsDataURL(i.files[0])}else a.reject("No file selected");return a.promise}}]);