UNPKG

image-rotation

Version:

Help the image complete compression and support rotation operations at any angle.

16 lines (13 loc) 5.33 kB
/*! ***************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ var t=function(){return(t=Object.assign||function(t){for(var e,n=1,a=arguments.length;n<a;n++)for(var r in e=arguments[n])Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r]);return t}).apply(this,arguments)};function e(t,e,n,a){return new(n||(n=Promise))((function(r,o){function i(t){try{u(a.next(t))}catch(t){o(t)}}function s(t){try{u(a.throw(t))}catch(t){o(t)}}function u(t){var e;t.done?r(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(i,s)}u((a=a.apply(t,e||[])).next())}))}function n(t,e){var n,a,r,o,i={label:0,sent:function(){if(1&r[0])throw r[1];return r[1]},trys:[],ops:[]};return o={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function s(o){return function(s){return function(o){if(n)throw new TypeError("Generator is already executing.");for(;i;)try{if(n=1,a&&(r=2&o[0]?a.return:o[0]?a.throw||((r=a.return)&&r.call(a),0):a.next)&&!(r=r.call(a,o[1])).done)return r;switch(a=0,r&&(o=[2&o[0],r.value]),o[0]){case 0:case 1:r=o;break;case 4:return i.label++,{value:o[1],done:!1};case 5:i.label++,a=o[1],o=[0];continue;case 7:o=i.ops.pop(),i.trys.pop();continue;default:if(!(r=i.trys,(r=r.length>0&&r[r.length-1])||6!==o[0]&&2!==o[0])){i=0;continue}if(3===o[0]&&(!r||o[1]>r[0]&&o[1]<r[3])){i.label=o[1];break}if(6===o[0]&&i.label<r[1]){i.label=r[1],r=o;break}if(r&&i.label<r[2]){i.label=r[2],i.ops.push(o);break}r[2]&&i.ops.pop(),i.trys.pop();continue}o=e.call(t,i)}catch(t){o=[6,t],a=0}finally{n=r=0}if(5&o[0])throw o[1];return{value:o[0]?o[1]:void 0,done:!0}}([o,s])}}}function a(t,e,n,a,r){var o=r*Math.PI/180;return{x:(n-t)*Math.cos(o)-(a-e)*Math.sin(o)+t,y:(n-t)*Math.sin(o)+(a-e)*Math.cos(o)+e}}var r=function(t){var e=t.split(",")[0].split(":")[1].split(";")[0],n=function(t){for(var e=atob(t.split(",")[1]),n=new ArrayBuffer(e.length),a=new Uint8Array(n),r=0;r<e.length;r++)a[r]=e.charCodeAt(r);return n}(t);return new Blob([n],{type:e})},o=function(){function o(e,n){this.file=e,this.options=t({isDrawPngBg:!1,quality:.92},n),this.deg=0,this.downloadType="image/jpeg"}return o.prototype.generate=function(t,a){var r=this;void 0===t&&(t=0),void 0===a&&(a="image/jpeg"),["image/jpeg","image/jpeg","image/webp"].includes(a)?this.downloadType=a:this.downloadType="image/jpeg",this.downloadType=a;var o=function(t){for(var e=t;e>=360;)e-=360;return e}(Math.abs(t));return this.deg=o<=360&&o>=0?t>=0?o:360-o:0,new Promise((function(t,a){return e(r,void 0,void 0,(function(){var r,o,i,s=this;return n(this,(function(u){switch(u.label){case 0:return u.trys.push([0,2,,3]),[4,this.loadImage()];case 1:return r=u.sent(),[3,3];case 2:return o=u.sent(),a(o),[2,!1];case 3:return(i=new Image).crossOrigin="anonymous",i.src=r,i.onload=function(){return e(s,void 0,void 0,(function(){var e,a;return n(this,(function(n){return e=this.getCanvasData(i),a=this.downLoadFile(e),t(a),[2]}))}))},[2]}}))}))}))},o.prototype.getCanvasData=function(t){var e,n,r,o,i,s,u,l,h=document.createElement("canvas"),c=h.getContext("2d"),p=this.options.maxLength,f=void 0===p?0:p,d=t.width,g=t.height,y=(e=d,n=g,r=this.deg,s=[a(o=e/2,i=n/2,0,n,180-r),a(o,i,e,n,180-r),a(o,i,e,0,180-r),a(o,i,0,0,180-r)],u=[],l=[],s.map((function(t){u.push(t.x),l.push(t.y)})),{x:Math.round(Math.max.apply(Math,u)-Math.min.apply(Math,u)),y:Math.round(Math.max.apply(Math,l)-Math.min.apply(Math,l))}),w=y.x,v=y.y,m=w,b=v;return f&&Math.max(w,v)>f&&(w<v?(m=f*w/v,b=f):(m=f,b=f*v/w)),m=Math.ceil(m),b=Math.ceil(b),m===w&&b===v||(d=Math.ceil(d*(m/w)),g=Math.ceil(g*(b/v))),h.width=m,h.height=b,this.options.background&&("image/png"!==this.downloadType||"image/png"===this.downloadType&&!0===this.options.isDrawPngBg)&&(c.fillStyle=this.options.background,c.fillRect(0,0,h.width,h.height)),c.translate(h.width/2,h.height/2),c.rotate(this.deg*Math.PI/180),c.drawImage(t,-d/2,-g/2,d,g),h},o.prototype.loadImage=function(){var t=this;return new Promise((function(e,n){if("string"==typeof t.file){if(!/^\s*data:([a-z]+\/[a-z0-9-+.]+(;[a-z-]+=[a-z0-9-]+)?)?(;base64)?,([a-z0-9!$&',()*+;=\-._~:@\/?%\s]*?)\s*$/i.test(t.file))return n("Image cannot be correctly interpreted.");e(t.file)}else{var a=new FileReader;a.readAsDataURL(t.file),a.onload=function(){return e(this.result)},a.onerror=function(){return n("Image cannot be correctly interpreted.")}}}))},o.prototype.downLoadFile=function(t){return"image/png"===this.downloadType?t.toDataURL(this.downloadType):t.toDataURL(this.downloadType,this.options.quality)},o.prototype.toBlob=function(t){return r(t)},o}();export default o;