[React] ํŒŒ์ด์–ด๋ฒ ์ด์Šค ํ”„๋กœํ•„ ์ด๋ฏธ์ง€ ์šฉ๋Ÿ‰ ์ตœ์ ํ™”
728x90

Today, What I learned?

๋ผ์ดํŠธํ•˜์šฐ์Šค์˜ ์ง€ํ‘œ๋ฅผ ๋ณด๊ณ  ํ”„๋กœํ•„ ์ด๋ฏธ์ง€์˜ ํ™•์žฅ์ž๋ฅผ webp๋กœ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ์ด ์ข‹๊ฒ ๋‹ค๊ณ  ํŒ๋‹จ๋˜์–ด์„œ..

๋‹ค๋ฅธ ์ด๋ฏธ์ง€ ํ™•์žฅ์ž๋ฅผ ๋ฐ”๊พธ๋Š” ๊ณผ์ •์—์„œ ํ”„๋กœํ•„ ์ด๋ฏธ์ง€ ํ™•์žฅ์ž๋„ ํ™”๋ฉด๊ณต์œ ๋ฅผ ํ†ตํ•ด ํŒ€์› ๋ถ„์ด ๋ณ€๊ฒฝํ•ด ์ฃผ์…จ์ง€๋งŒ,

๊ทธ ๊ณผ์ •์—์„œ ๋‚ด๊ฐ€ ๋˜ ์‹ค์ˆ˜ํ•œ(!) ์ฝ”๋“œ๊ฐ€ ์žˆ์–ด์„œ ์ •๋ฆฌํ•ด๋ณด๋ ค ํ•œ๋‹ค ^^ใ…Ž

 

์šฐ์„  ํ˜„์žฌ ๋ผ์ดํŠธ ํ•˜์šฐ์Šค๋Š” ์ด๋ ‡๋‹ค.

 

 

 

์ด๋ฏธ์ง€ ๋ฆฌ์‚ฌ์ด์ฆˆ

์šฐ๋ฆฌ ํ”„๋กœ์ ํŠธ์—์„œ๋Š” ์ด๋ฏธ์ง€ ์‚ฌ์ด์ฆˆ ์ตœ์ ํ™”๋ฅผ ์œ„ํ•ด react-image-file-resizer ๋ผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ด์šฉํ•˜๊ณ  ์žˆ๋‹ค.
์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ํ™•์žฅ์ž๋ฅผ jpg, png ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ webp๋„ ํ•จ๊ป˜ ์ œ๊ณตํ•œ๋‹ค.

๊ธฐ๋ณธ์ ์œผ๋กœ ์ ์šฉ๋˜์–ด ์žˆ๋˜ ๋ฆฌ์‚ฌ์ด์ฆˆ ํฌ๋งท์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

const resizeFile = (file: File) =>
  new Promise((resolve) => {
    Resizer.imageFileResizer(
      file,
      230,
      230,
      'JPEG',
      70,
      0,
      (uri) => {
        resolve(uri);
      },
      'base64',
    );
  });

 

 

ํŒŒ์ผ ๊ฐ์ฒด๋ฅผ ์˜ฌ๋ฆฌ๋ฉด ํŒŒ์ผ์„ ๋ฆฌ์‚ฌ์ด์ฆˆํ•ด์„œ ํŒŒ์ด์–ด๋ฒ ์ด์Šค ์Šคํ† ๋ฆฌ์ง€์— ์˜ฌ๋ฆฌ๊ณ , ๋‚ด๋ ค๋ฐ›์€ ์ด๋ฏธ์ง€ url์„ ๋ฆฌํ„ด ๋ฐ›์•„ state์— ์ €์žฅํ•ด ์ฃผ๋Š” ๋ฐฉ์‹์ด๋‹ค.

...
  const file = e.target.files[0];
  const reader = new FileReader();
  reader.readAsDataURL(file as Blob);

  const resizedImage = await resizeFile(file);

  // ์ด๋ฏธ์ง€๋ฅผ ์Šคํ† ๋ฆฌ์ง€์— ์—…๋กœ๋“œ ํ›„ ๋ฐ˜ํ™˜๋ฐ›์€ ์ด๋ฏธ์ง€ url์„ ์ƒํƒœ(profileImg)๋กœ ์ €์žฅ
  uploadProfileImg(resizedImage, uid).then((res) =>
    setProfileImg(resizedImage as string), 
  );

 

 

๋ฌธ์ œ๋Š” webp ํ™•์žฅ์ž๋กœ ๋ฐ”๊พผ ์ดํ›„์ธ๋ฐ, ํŒŒ์ด์–ด๋ฒ ์ด์Šค์—๋Š” base64 ๋ฐฉ์‹์œผ๋กœ webp ์ด๋ฏธ์ง€๋ฅผ ์˜ฌ๋ฆด ์ˆ˜ ์—†์–ด blob์œผ๋กœ ๋ณ€๊ฒฝํ–ˆ๋Š”๋ฐ ํ”„๋กœํ•„ ์ˆ˜์ • ๋’ค ๋ฐ”๋กœ ๋ณ€๊ฒฝ๋œ ์ด๋ฏธ์ง€๊ฐ€ ๋ฐ˜์˜์ด ๋˜์ง€ ์•Š์•˜๋‹ค.

const resizeFile = (file: File) =>
  new Promise((resolve) => {
    Resizer.imageFileResizer(
      file,
      230,
      230,
      'WEBP',
      100,
      0,
      (uri) => {
        resolve(uri);
      },
      'blob',
    );
  });

 

 

์ด์œ ๋Š”.. ์ฝ˜์†”์„ ์ฐ์–ด๋ณด๋‹ˆ ์•„๋ž˜์ฒ˜๋Ÿผ blob ๊ฐ์ฒด๋ฅผ ์ด๋ฏธ์ง€ํƒœ๊ทธ src์— ๋„ฃ๊ณ  ์žˆ์–ด์„œ์˜€๋Š”๋ฐ,

 

 

์ด ๋ถ€๋ถ„์—์„œ setState์— blob ๊ฐ์ฒด๋ฅผ string ํƒ€์ž…์œผ๋กœ ๋„ฃ์–ด์ฃผ๊ณ  ์žˆ์—ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค..

  // ์ด๋ฏธ์ง€๋ฅผ ์Šคํ† ๋ฆฌ์ง€์— ์—…๋กœ๋“œ ํ›„ ๋ฐ˜ํ™˜๋ฐ›์€ ์ด๋ฏธ์ง€ url์„ ์ƒํƒœ(profileImg)๋กœ ์ €์žฅ
  uploadProfileImg(resizedImage, uid).then((res) =>
    setProfileImg(resizedImage as string), 
  );

 

๊ทธ๋ž˜์„œ ๊ตณ์ด string์œผ๋กœ ํƒ€์ž…์„ ์ง€์ •ํ•ด์ฃผ์ง€ ์•Š๊ณ  promise์˜ ๋ฐ˜ํ™˜๊ฐ’์„ setState์— ๋„ฃ์–ด์„œ ์ œ๋Œ€๋กœ ์ ์šฉ ์™„๋ฃŒ!..

// ์ด๋ฏธ์ง€๋ฅผ ์Šคํ† ๋ฆฌ์ง€์— ์—…๋กœ๋“œ ํ›„ ๋ฐ˜ํ™˜๋ฐ›์€ ์ด๋ฏธ์ง€ url์„ ์ƒํƒœ(profileImg)๋กœ ์ €์žฅ
uploadProfileImg(resizedImage, uid).then((res) => setProfileImg(res));

 

 

์ด๋ ‡๊ฒŒ ์ด๋ฏธ์ง€ ์ˆ˜์ •์ด ๋ฐ”๋กœ ๋œ๋‹ค.

 

 

 

์ตœ์ ํ™”๊ฐ€ ์–ผ๋งˆ๋‚˜ ๋˜์—ˆ๋Š”์ง€? ์ด๋ฏธ์ง€ ์šฉ๋Ÿ‰ ๋น„๊ต

๋™์ผํ•œ ์ด๋ฏธ์ง€๋ฅผ ๋น„๊ตํ•ด ๋ณด๋‹ˆ ์•„์ฃผ ํฐ ์ฐจ์ด๊ฐ€ ์žˆ์—ˆ๋‹ค.

(์ขŒ) jpeg (์šฐ) webp

์›๋ž˜ 2.54MB์˜€๋˜ ํŒŒ์ผ์ด 52.58KB๊นŒ์ง€ ์ค„์–ด๋“ค์—ˆ๋‹ค. ๊ฑฐ์˜ 98%์— ๊ฐ€๊นŒ์šด ๊ฐ์†Œ์œจ?!...

 

 

 

์ด๋ ‡๊ฒŒ ์ด๋ฏธ์ง€ ์ตœ์ ํ™”๋ฅผ ์™„๋ฃŒํ–ˆ๋‹ค. ๐Ÿ˜Š

728x90