UNPKG

@meghoshpritam/react-image-file-resizer

Version:

Elevate your React application with a dynamic module that empowers you to **transform and finesse** local images at will. With this enchanting tool, you wield the power to effortlessly adjust image width, height, format, rotation, and quality. Watch in aw

1 lines 4.17 kB
(()=>{"use strict";var e={d:(t,r)=>{for(var o in r)e.o(r,o)&&!e.o(t,o)&&Object.defineProperty(t,o,{enumerable:!0,get:r[o]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},t={};function r(e){return r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},r(e)}function o(e,t){for(var o=0;o<t.length;o++){var n=t[o];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,(void 0,i=function(e,t){if("object"!==r(e)||null===e)return e;var o=e[Symbol.toPrimitive];if(void 0!==o){var n=o.call(e,"string");if("object"!==r(n))return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(n.key),"symbol"===r(i)?i:String(i)),n)}var i}e.r(t),e.d(t,{default:()=>i});var n=function(){function e(){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e)}var t,r;return t=e,r=[{key:"changeHeightWidth",value:function(e,t,r,o,n,i,a){return a?(r>o&&(e=Math.round(e*o/r),r=o),e>t&&(r=Math.round(r*t/e),e=t),n&&r<n&&(e=Math.round(e*n/r),r=n),i&&e<i&&(r=Math.round(r*i/e),e=i)):(r>o&&(r=o),e>t&&(e=t),n&&r<n&&(r=n),i&&e<i&&(e=i)),{height:e,width:r}}},{key:"resizeAndRotateImage",value:function(e,t,r,o,n){var i=arguments.length>5&&void 0!==arguments[5]?arguments[5]:"jpeg",a=arguments.length>7&&void 0!==arguments[7]?arguments[7]:0,l=!(arguments.length>8&&void 0!==arguments[8])||arguments[8],u=(arguments.length>6&&void 0!==arguments[6]?arguments[6]:100)/100,h=document.createElement("canvas"),g=e.width,c=e.height,d=this.changeHeightWidth(c,r,g,t,o,n,l);!a||90!==a&&270!==a?(h.width=d.width,h.height=d.height):(h.width=d.height,h.height=d.width),g=d.width,c=d.height,r&&(h.height=r,c=r),t&&(h.width=t,g=t);var s=h.getContext("2d");return s.fillStyle="rgba(0, 0, 0, 0)",s.fillRect(0,0,g,c),s.imageSmoothingEnabled&&s.imageSmoothingQuality&&(s.imageSmoothingQuality="high"),a&&(s.rotate(a*Math.PI/180),90===a?s.translate(0,-h.width):180===a?s.translate(-h.width,-h.height):270===a?s.translate(-h.height,0):0!==a&&360!==a||s.translate(0,0)),s.drawImage(e,0,0,g,c),h.toDataURL("image/".concat(i),u)}},{key:"b64toByteArrays",value:function(e){for(var t=atob(e.toString().replace(/^data:image\/(png|jpeg|jpg|webp);base64,/,"")),r=[],o=0;o<t.length;o+=512){for(var n=t.slice(o,o+512),i=new Array(n.length),a=0;a<n.length;a+=1)i[a]=n.charCodeAt(a);var l=new Uint8Array(i);r.push(l)}return r}},{key:"b64toBlob",value:function(e,t){var r=this.b64toByteArrays(e);return new Blob(r,{type:t,lastModified:new Date})}},{key:"b64toFile",value:function(e,t,r){var o=this.b64toByteArrays(e);return new File(o,t,{type:r,lastModified:new Date})}},{key:"createResizedImage",value:function(t,r,o,n,i,a,l){var u=arguments.length>7&&void 0!==arguments[7]?arguments[7]:"base64",h=arguments.length>8&&void 0!==arguments[8]?arguments[8]:null,g=arguments.length>9&&void 0!==arguments[9]?arguments[9]:null,c=!(arguments.length>10&&void 0!==arguments[10])||arguments[10],d=new FileReader;if(!t)throw Error("File Not Found!");if(t.type&&!t.type.includes("image"))throw Error("File Is NOT Image!");d.readAsDataURL(t),d.onload=function(){var s=new Image;s.src=d.result,s.onload=function(){var d=e.resizeAndRotateImage(s,r,o,h,g,n,i,a,c),y="image/".concat(n);switch(u){case"blob":var f=e.b64toBlob(d,y);l(f);break;case"base64":default:l(d);break;case"file":var b=t.name.toString().replace(/(png|jpeg|jpg|webp)$/i,"").concat(n.toString()),m=e.b64toFile(d,b,y);l(m)}}},d.onerror=function(e){throw Error(e)}}}],null&&o(t.prototype,null),r&&o(t,r),Object.defineProperty(t,"prototype",{writable:!1}),e}();const i={imageFileResizer:function(e){var t=e.file,r=e.maxWidth,o=e.maxHeight,i=e.compressFormat,a=e.quality,l=e.rotation,u=e.keepAspectRatio,h=e.responseUriFunc,g=e.outputType,c=e.minWidth,d=e.minHeight;return n.createResizedImage(t,r,o,i,a,l,h,g,c,d,u)}};module.exports=t})();