react-image-file-resizer
Advanced tools
Comparing version 0.4.2 to 0.4.3
@@ -1,1 +0,1 @@ | ||
module.exports=(()=>{"use strict";var e={352:(e,t,r)=>{function a(e,t){for(var r=0;r<t.length;r++){var a=t[r];a.enumerable=a.enumerable||!1,a.configurable=!0,"value"in a&&(a.writable=!0),Object.defineProperty(e,a.key,a)}}r.r(t),r.d(t,{default:()=>o});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,a,n,o){return r>a&&(e=Math.round(e*a/r),r=a),e>t&&(r=Math.round(r*t/e),e=t),n&&r<n&&(e=Math.round(e*n/r),r=n),o&&e<o&&(r=Math.round(r*o/e),e=o),{height:e,width:r}}},{key:"resizeAndRotateImage",value:function(e,t,r,a,n){var o=arguments.length>5&&void 0!==arguments[5]?arguments[5]:"jpeg",i=arguments.length>6&&void 0!==arguments[6]?arguments[6]:100,l=arguments.length>7&&void 0!==arguments[7]?arguments[7]:0,h=i/100,u=document.createElement("canvas"),d=e.width,g=e.height,c=this.changeHeightWidth(g,r,d,t,a,n);!l||90!==l&&270!==l?(u.width=c.width,u.height=c.height):(u.width=c.height,u.height=c.width),d=c.width,g=c.height;var s=u.getContext("2d");return l&&(s.rotate(l*Math.PI/180),90===l?s.translate(0,-u.width):180===l?s.translate(-u.width,-u.height):270===l?s.translate(-u.height,0):0!==l&&360!==l||s.translate(0,0)),s.drawImage(e,0,0,d,g),u.toDataURL("image/".concat(o),h)}},{key:"b64toByteArrays",value:function(e,t){t=t||"image/jpeg";for(var r=atob(e.toString().replace(/^data:image\/(png|jpeg|jpg|webp);base64,/,"")),a=[],n=0;n<r.length;n+=512){for(var o=r.slice(n,n+512),i=new Array(o.length),l=0;l<o.length;l++)i[l]=o.charCodeAt(l);var h=new Uint8Array(i);a.push(h)}return a}},{key:"b64toBlob",value:function(e,t){var r=this.b64toByteArrays(e,t);return new Blob(r,{type:t})}},{key:"b64toFile",value:function(e,t,r){var a=this.b64toByteArrays(e,r);return new File(a,t,{type:r})}},{key:"createResizedImage",value:function(t,r,a,n,o,i,l){var h=arguments.length>7&&void 0!==arguments[7]?arguments[7]:"base64",u=arguments.length>8&&void 0!==arguments[8]?arguments[8]:null,d=arguments.length>9&&void 0!==arguments[9]?arguments[9]:null,g=new FileReader;if(!t)throw Error("File Not Found!");if(t.type&&!t.type.includes("image"))throw Error("File Is NOT Image!");g.readAsDataURL(t),g.onload=function(){var c=new Image;c.src=g.result,c.onload=function(){var g=e.resizeAndRotateImage(c,r,a,u,d,n,o,i),s="image/".concat(n);switch(h){case"blob":var f=e.b64toBlob(g,s);l(f);break;case"base64":l(g);break;case"file":var v=e.b64toFile(g,t.name,s);l(v);break;default:l(g)}}},g.onerror=function(e){throw Error(e)}}}],null&&a(t.prototype,null),r&&a(t,r),e}();const o={imageFileResizer:function(e,t,r,a,o,i,l,h,u,d){return n.createResizedImage(e,t,r,a,o,i,l,h,u,d)}}}},t={};function r(a){if(t[a])return t[a].exports;var n=t[a]={exports:{}};return e[a](n,n.exports,r),n.exports}return r.d=(e,t)=>{for(var a in t)r.o(t,a)&&!r.o(e,a)&&Object.defineProperty(e,a,{enumerable:!0,get:t[a]})},r.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r(352)})(); | ||
(()=>{"use strict";var e={d:(t,a)=>{for(var r in a)e.o(a,r)&&!e.o(t,r)&&Object.defineProperty(t,r,{enumerable:!0,get:a[r]})},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 a(e,t){for(var a=0;a<t.length;a++){var r=t[a];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}e.r(t),e.d(t,{default:()=>n});var r=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,a,r,n,o){return a>r&&(e=Math.round(e*r/a),a=r),e>t&&(a=Math.round(a*t/e),e=t),n&&a<n&&(e=Math.round(e*n/a),a=n),o&&e<o&&(a=Math.round(a*o/e),e=o),{height:e,width:a}}},{key:"resizeAndRotateImage",value:function(e,t,a,r,n){var o=arguments.length>5&&void 0!==arguments[5]?arguments[5]:"jpeg",i=arguments.length>6&&void 0!==arguments[6]?arguments[6]:100,l=arguments.length>7&&void 0!==arguments[7]?arguments[7]:0,h=i/100,u=document.createElement("canvas"),g=e.width,d=e.height,c=this.changeHeightWidth(d,a,g,t,r,n);!l||90!==l&&270!==l?(u.width=c.width,u.height=c.height):(u.width=c.height,u.height=c.width),g=c.width,d=c.height;var s=u.getContext("2d");return l&&(s.rotate(l*Math.PI/180),90===l?s.translate(0,-u.width):180===l?s.translate(-u.width,-u.height):270===l?s.translate(-u.height,0):0!==l&&360!==l||s.translate(0,0)),s.drawImage(e,0,0,g,d),u.toDataURL("image/".concat(o),h)}},{key:"b64toByteArrays",value:function(e,t){t=t||"image/jpeg";for(var a=atob(e.toString().replace(/^data:image\/(png|jpeg|jpg|webp);base64,/,"")),r=[],n=0;n<a.length;n+=512){for(var o=a.slice(n,n+512),i=new Array(o.length),l=0;l<o.length;l++)i[l]=o.charCodeAt(l);var h=new Uint8Array(i);r.push(h)}return r}},{key:"b64toBlob",value:function(e,t){var a=this.b64toByteArrays(e,t);return new Blob(a,{type:t,lastModified:new Date})}},{key:"b64toFile",value:function(e,t,a){var r=this.b64toByteArrays(e,a);return new File(r,t,{type:a,lastModified:new Date})}},{key:"createResizedImage",value:function(t,a,r,n,o,i,l){var h=arguments.length>7&&void 0!==arguments[7]?arguments[7]:"base64",u=arguments.length>8&&void 0!==arguments[8]?arguments[8]:null,g=arguments.length>9&&void 0!==arguments[9]?arguments[9]:null,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 c=new Image;c.src=d.result,c.onload=function(){var d=e.resizeAndRotateImage(c,a,r,u,g,n,o,i),s="image/".concat(n);switch(h){case"blob":var f=e.b64toBlob(d,s);l(f);break;case"base64":l(d);break;case"file":var b=t.name.toString().replace(/(png|jpeg|jpg|webp)/,"").concat(n.toString()),v=e.b64toFile(d,b,s);l(v);break;default:l(d)}}},d.onerror=function(e){throw Error(e)}}}],null&&a(t.prototype,null),r&&a(t,r),e}();const n={imageFileResizer:function(e,t,a,n,o,i,l,h,u,g){return r.createResizedImage(e,t,a,n,o,i,l,h,u,g)}};module.exports=t})(); |
{ | ||
"name": "react-image-file-resizer", | ||
"version": "0.4.2", | ||
"version": "0.4.3", | ||
"description": "React module that can rescaled local images. You can change image's width, height, format, rotation and quality. It returns resized image's new base64 URI or Blob. The URI can be used as the source of an <Image> component.", | ||
@@ -37,3 +37,3 @@ "main": "build/index.js", | ||
"email": "zorlueronur@gmail.com", | ||
"url": "https://onurzorluer.com" | ||
"url": "https://zorluer.com" | ||
}, | ||
@@ -51,3 +51,3 @@ "license": "MIT", | ||
"all-contributors-cli": "^6.19.0", | ||
"babel-cli": "^6.26.0", | ||
"babel-cli": "^6.18.0", | ||
"babel-core": "^6.26.3", | ||
@@ -59,5 +59,5 @@ "babel-jest": "^26.6.3", | ||
"jest": "^26.6.3", | ||
"webpack": "^5.11.1", | ||
"webpack-cli": "^3.3.12" | ||
"webpack": "^5.31.2", | ||
"webpack-cli": "^4.6.0" | ||
} | ||
} |
187
README.md
# React Image File Resizer | ||
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section --> | ||
[![All Contributors](https://img.shields.io/badge/all_contributors-7-orange.svg?style=flat-square)](#contributors-) | ||
<!-- ALL-CONTRIBUTORS-BADGE:END --> | ||
[![Build Status](https://travis-ci.org/onurzorluer/react-image-file-resizer.svg?branch=master)](https://travis-ci.org/onurzorluer/react-image-file-resizer.svg?branch=master) | ||
[![Build Status](https://travis-ci.org/onurzorluer/react-image-file-resizer.svg?branch=master)](https://travis-ci.org/onurzorluer/react-image-file-resizer.svg?branch=master) [![npm version](https://badge.fury.io/js/react-image-file-resizer.svg)](https://badge.fury.io/js/react-image-file-resizer) | ||
@@ -16,6 +19,9 @@ `react-image-file-resizer` is a react module that can rescaled local images. | ||
Install the package: | ||
``` | ||
npm i react-image-file-resizer | ||
``` | ||
or | ||
``` | ||
@@ -28,43 +34,54 @@ yarn add react-image-file-resizer | ||
```javascript | ||
import Resizer from 'react-image-file-resizer'; | ||
import Resizer from "react-image-file-resizer"; | ||
Resizer.imageFileResizer( | ||
file, // Is the file of the image which will resized. | ||
maxWidth, // Is the maxWidth of the resized new image. | ||
maxHeight, // Is the maxHeight of the resized new image. | ||
compressFormat, // Is the compressFormat of the resized new image. | ||
quality, // Is the quality of the resized new image. | ||
rotation, // Is the degree of clockwise rotation to apply to uploaded image. | ||
responseUriFunc, // Is the callBack function of the resized new image URI. | ||
outputType, // Is the output type of the resized new image. | ||
minWidth, // Is the minWidth of the resized new image. | ||
minHeight, // Is the minHeight of the resized new image. | ||
); | ||
file, // Is the file of the image which will resized. | ||
maxWidth, // Is the maxWidth of the resized new image. | ||
maxHeight, // Is the maxHeight of the resized new image. | ||
compressFormat, // Is the compressFormat of the resized new image. | ||
quality, // Is the quality of the resized new image. | ||
rotation, // Is the degree of clockwise rotation to apply to uploaded image. | ||
responseUriFunc, // Is the callBack function of the resized new image URI. | ||
outputType, // Is the output type of the resized new image. | ||
minWidth, // Is the minWidth of the resized new image. | ||
minHeight // Is the minHeight of the resized new image. | ||
); | ||
``` | ||
## Example 1 | ||
First, wrap this resizer: | ||
First, wrap the resizer: | ||
```javascript | ||
const resizeFile = (file) => new Promise(resolve => { | ||
Resizer.imageFileResizer(file, 300, 300, 'JPEG', 100, 0, | ||
uri => { | ||
resolve(uri); | ||
}, | ||
'base64' | ||
import Resizer from "react-image-file-resizer"; | ||
const resizeFile = (file) => | ||
new Promise((resolve) => { | ||
Resizer.imageFileResizer( | ||
file, | ||
300, | ||
300, | ||
"JPEG", | ||
100, | ||
0, | ||
(uri) => { | ||
resolve(uri); | ||
}, | ||
"base64" | ||
); | ||
}); | ||
}); | ||
``` | ||
And then use it in your async function: | ||
```javascript | ||
const onChange = async (event) => { | ||
try { | ||
const file = event.target.files[0]; | ||
const image = await resizeFile(file); | ||
console.log(image); | ||
} catch(err) { | ||
console.log(err); | ||
} | ||
} | ||
try { | ||
const file = event.target.files[0]; | ||
const image = await resizeFile(file); | ||
console.log(image); | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
}; | ||
``` | ||
@@ -75,50 +92,50 @@ | ||
```javascript | ||
import React, { Component } from 'react'; | ||
import Resizer from 'react-image-file-resizer'; | ||
import React, { Component } from "react"; | ||
import Resizer from "react-image-file-resizer"; | ||
class App extends Component { | ||
constructor(props) { | ||
super(props); | ||
this.fileChangedHandler = this.fileChangedHandler.bind(this); | ||
this.state = { | ||
newImage: '' | ||
} | ||
} | ||
constructor(props) { | ||
super(props); | ||
this.fileChangedHandler = this.fileChangedHandler.bind(this); | ||
this.state = { | ||
newImage: "", | ||
}; | ||
} | ||
fileChangedHandler(event) { | ||
var fileInput = false | ||
if(event.target.files[0]) { | ||
fileInput = true | ||
} | ||
if(fileInput) { | ||
try { | ||
Resizer.imageFileResizer( | ||
event.target.files[0], | ||
300, | ||
300, | ||
'JPEG', | ||
100, | ||
0, | ||
uri => { | ||
console.log(uri) | ||
this.setState({ newImage: uri }) | ||
}, | ||
'base64', | ||
200, | ||
200, | ||
); | ||
} catch(err) { | ||
console.log(err) | ||
} | ||
} | ||
fileChangedHandler(event) { | ||
var fileInput = false; | ||
if (event.target.files[0]) { | ||
fileInput = true; | ||
} | ||
render() { | ||
return ( | ||
<div className="App"> | ||
<input type="file" onChange={this.fileChangedHandler}/> | ||
<img src={this.state.newImage} alt=''/> | ||
</div> | ||
if (fileInput) { | ||
try { | ||
Resizer.imageFileResizer( | ||
event.target.files[0], | ||
300, | ||
300, | ||
"JPEG", | ||
100, | ||
0, | ||
(uri) => { | ||
console.log(uri); | ||
this.setState({ newImage: uri }); | ||
}, | ||
"base64", | ||
200, | ||
200 | ||
); | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
} | ||
} | ||
render() { | ||
return ( | ||
<div className="App"> | ||
<input type="file" onChange={this.fileChangedHandler} /> | ||
<img src={this.state.newImage} alt="" /> | ||
</div> | ||
); | ||
} | ||
} | ||
@@ -129,14 +146,14 @@ | ||
Option | Description | Type | Required | ||
------ | ----------- | ---- | -------- | ||
`file` | Path of image file | `object` | Yes | ||
`maxWidth` | New image max width (ratio is preserved) | `number` | Yes | ||
`maxHeight` | New image max height (ratio is preserved) | `number` | Yes | ||
`compressFormat` | Can be either **JPEG**, **PNG** or **WEBP**. | `string` | Yes | ||
`quality` | A number between 0 and 100. Used for the JPEG compression.(if no compress is needed, just set it to 100) | `number` | Yes | ||
`rotation` | Degree of clockwise rotation to apply to the image. Rotation is limited to multiples of 90 degrees.(if no rotation is needed, just set it to 0) (0, 90, 180, 270, 360) | `number` | Yes | ||
`responseUriFunc` | Callback function of URI. Returns URI of resized image's base64 format. ex: `uri => {console.log(uri)});` | `function` | Yes | ||
`outputType` | Can be either **base64**, **blob** or **file**.(Default type is base64) | `string` | No | ||
`minWidth` | New image min width (ratio is preserved, defaults to null) | `number` | No | ||
`minHeight` | New image min height (ratio is preserved, defaults to null) | `number` | No | ||
| Option | Description | Type | Required | | ||
| ----------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------- | -------- | | ||
| `file` | Path of image file | `object` | Yes | | ||
| `maxWidth` | New image max width (ratio is preserved) | `number` | Yes | | ||
| `maxHeight` | New image max height (ratio is preserved) | `number` | Yes | | ||
| `compressFormat` | Can be either **JPEG**, **PNG** or **WEBP**. | `string` | Yes | | ||
| `quality` | A number between 0 and 100. Used for the JPEG compression.(if no compress is needed, just set it to 100) | `number` | Yes | | ||
| `rotation` | Degree of clockwise rotation to apply to the image. Rotation is limited to multiples of 90 degrees.(if no rotation is needed, just set it to 0) (0, 90, 180, 270, 360) | `number` | Yes | | ||
| `responseUriFunc` | Callback function of URI. Returns URI of resized image's base64 format. ex: `uri => {console.log(uri)});` | `function` | Yes | | ||
| `outputType` | Can be either **base64**, **blob** or **file**.(Default type is base64) | `string` | No | | ||
| `minWidth` | New image min width (ratio is preserved, defaults to null) | `number` | No | | ||
| `minHeight` | New image min height (ratio is preserved, defaults to null) | `number` | No | | ||
@@ -150,2 +167,3 @@ ## License | ||
Thanks goes to these wonderful people ([emoji key](https://github.com/all-contributors/all-contributors#emoji-key)): | ||
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section --> | ||
@@ -172,2 +190,1 @@ <!-- prettier-ignore-start --> | ||
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome! | ||
16188
185