๊ฐœ๋ฐœ/React

[๋ฆฌ์•กํŠธ] React image file Resize ํ•˜๊ธฐ / react-image-file-resizer

๋ฐ(Ming) ๐Ÿˆ‍โฌ› 2022. 11. 28. 15:25
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

 

 

react-image-file-resizer

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.. Latest version: 0.4.8, last publ

www.npmjs.com

 

 

 

 

 

728x90