728x90
๋ฐ์ํ
์ด๋ฏธ์งํ์ผ์ ๋ง์ด ๋ค๋ฃจ๋ ์ ๋ฌด๋ฅผ ๋ณด๊ณ ์๋ค. ๊ฐ์๊ธฐ ์ถ๊ฐ๋ ์ด๋ค ์ด๋ฏธ์ง์ ๋ํด์ ํ๋ก ํธ์ชฝ์์ ์ฌ์ด์ฆ๋ฅผ ๋ฐ๊ฟ์ ์ ๋ฌํด์ผํ ์ผ์ด ์๊ฒผ๋ค. ๊ทธ๋์ ์ฐพ์์ ์ฐ๊ฒ ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ
React Image File Resizer
๋๋ ๋์ ๋ฆฌ์กํธ ์ํ๊ณ์์ ์๋๊ฒ ์๋ค.
์ด๋ฏธ์ง์ width, height, format ๋ฑ์ ๋ฐ๊ฟ ์ ์๊ฒ ๋์์ฃผ๋ ํจํค์ง์ด๋ค.
์ค์นํ๊ธฐ
npm i react-image-file-resizer
or
yarn add react-image-file-resizer
์ฌ์ฉํ๊ธฐ
//๋ถ๋ฌ์ค๊ธฐ
import Resizer from "react-image-file-resizer";
//์ด๋ป๊ฒ ๋ณ๊ฒฝํ ๊ฑด์ง์ ๋ํ ๋ด์ฉ์ ์ฐจ๋ก ์ฐจ๋ก ๋ฃ์ด์ฃผ๋ฉด ๋๋ค.
Resizer.imageFileResizer(
file, //๋ฐ๊ฟ ์ค ํ์ผ
maxWidth, // resize ํ ์ต๋ ๊ฐ๋ก
maxHeight, // resize ํ ์ต๋ ์ธ๋ก
compressFormat, // ํฌ๋งท
quality, // ํ๋ฆฌํฐ
rotation, // ์
๋ก๋๋ ํ์ผ์ ๋ฐฉํฅ๋ ๋ณ๊ฒฝํ ์ ์๋ค , ํด๋น์ฌํญ์์ผ๋ฉด 0
responseUriFunc, // Is the callBack function of the resized new image URI.
outputType, // Is the output type of the resized new image.
minWidth, // ์ต์ ๊ฐ๋ก
minHeight // ์ต์ ๋์ด
);
๐์ ์ฉํด๋ณด๊ธฐ
const resizeFile = (file) =>
new Promise((resolve) => {
Resizer.imageFileResizer(
file,
400,
865,
"PNG",
100,
0,
(uri) => {
resolve(uri);
},
"blob"
);
});
promise๋ฅผ ์ฌ์ฉํด์ ์์ฑํด๋ณด์์ต๋๋ค. ์ฌ์ฉํ์ค ๋ file์์ ๋ณ๊ฒฝ์ ์ํ๋ ํ์ผ์ ๋ฃ์ด์ค๋๋ค.
Option ์ ๋ํ ์ค๋ช ์ ๋๋ค.
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 |
728x90
'๊ฐ๋ฐ > React' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฆฌ์กํธ] React-Query - 2. CacheTime , StaleTime (feat.ReactQueryDevtools) (0) | 2023.05.09 |
---|---|
[๋ฆฌ์กํธ] React-Query - 1. data fetch & error (0) | 2023.05.08 |
[๋ฆฌ์กํธ] useRef ์ฌ์ฉํ๊ธฐ - ๋ฆฌ์กํธ์์ ํน์ DOM์ ์ ๊ทผํ๊ธฐ (0) | 2022.11.15 |
React Testing Library ์ฌ์ฉํ๊ธฐ (from Scratch) - 1 (0) | 2022.04.29 |
[React] ๋ค๊ตญ์ด ์ฒ๋ฆฌํ๊ธฐ - i18n ์ฌ์ฉํ๊ธฐ (0) | 2022.04.02 |