์ด๋ฏธ์ง€ ์••์ถ•์˜ ํ•„์š”์„ฑ๊ณผ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ ํƒ

์ด๋ฏธ์ง€ ์••์ถ•์˜ ํ•„์š”์„ฑ

๋ฆฌ๋ทฐ ์ž‘์„ฑ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ œ์ž‘ํ•˜๋ฉฐ ์ตœ์ ํ™”๋ฅผ ์–ด๋–ป๊ฒŒ ํ• ์ง€ ๊ณ ๋ฏผํ–ˆ๋‹ค.

 

์šฐ์„ , ์ด๋ฏธ์ง€ ํŒŒ์ผ ๊ฐœ์ˆ˜ ์ œํ•œ๊ณผ ํŒŒ์ผ ํฌ๊ธฐ ์ œํ•œ์— ๋Œ€ํ•œ ๊ธฐ์ค€์„ ์„ค์ •ํ–ˆ๋‹ค. ๋ฆฌ๋ทฐ ํŽ˜์ด์ง€๊ฐ€ ์ธ์Šคํƒ€ ํ”ผ๋“œ ํ˜•์‹์ด๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฏธ์ง€ ํŒŒ์ผ ๊ฐœ์ˆ˜๋Š” ์ตœ๋Œ€ 5๊ฐœ๋กœ ์ œํ•œํ•˜๊ณ , ํŒŒ์ผ ํฌ๊ธฐ๋Š” ๋ฐฑ์—”๋“œ ๊ฐœ๋ฐœ์ž์™€ ํ˜‘์˜ํ•˜์—ฌ ์ž„์‹œ๋กœ 10MB๊นŒ์ง€ ์—…๋กœ๋“œํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ค์ •ํ–ˆ๋‹ค.

 

 

๋˜ํ•œ, ํด๋ผ์ด์–ธํŠธ ์ธก์—์„œ ํŒŒ์ผ ํฌ๊ธฐ๋ฅผ ์ค„์ด๊ณ  WebP ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋ฉด ์„œ๋ฒ„ ์ €์žฅ ๊ณต๊ฐ„์„ ์ ˆ์•ฝํ•˜๊ณ  ์ „์†ก ์†๋„๋ฅผ ๊ฐœ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ํŒ๋‹จํ–ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์ด๋ฏธ์ง€๋ฅผ ๋ถˆ๋Ÿฌ์˜ฌ ๋•Œ ๋” ๊ฐ€๋ณ๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์–ด ์‚ฌ์šฉ์ž ๊ฒฝํ—˜์„ ํฌ๊ฒŒ ํ–ฅ์ƒ์‹œํ‚ฌ ๊ฒƒ์œผ๋กœ ๊ธฐ๋Œ€๋œ๋‹ค.

 

 

์ด๋ฏธ์ง€ ์••์ถ• ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

์–ด๋–ค ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ• ์ง€ ๊ณ ๋ฏผํ•œ ๋์— browser-image-compression์„ ์„ ํƒํ–ˆ๋‹ค.

 

์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ๋‹ค์šด๋กœ๋“œ ์ˆ˜๊ฐ€ ๋งŽ๊ณ , ๋ฌธ์„œ๊ฐ€ ์ž˜ ์ •๋ฆฌ๋˜์–ด ์žˆ์œผ๋ฉฐ, ๋งŽ์€ ์‚ฌ์šฉ ํ›„๊ธฐ๊ฐ€ ์žˆ์–ด ์‹ ๋ขฐํ•  ์ˆ˜ ์žˆ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ผ๊ณ  ํŒ๋‹จํ–ˆ๋‹ค.

ํ•˜์ง€๋งŒ ๋น„๊ต์  ๋ฒˆ๋“ค ํฌ๊ธฐ๊ฐ€ ํฌ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋Š”๋ฐ, ์ด ๋ฌธ์ œ๋ฅผ ์–ด๋–ป๊ฒŒ ํ•ด๊ฒฐํ•˜๋ฉด ์ข‹์„์ง€ ๊ณ ๋ฏผํ–ˆ๋‹ค.

 

์ด๋Š” ๋น„๋™๊ธฐ์ ์œผ๋กœ imageCompression์„ ๋กœ๋“œํ•˜์—ฌ ํ•ด๊ฒฐ ํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค. ์‚ฌ์šฉ์ž๊ฐ€ ์ด๋ฏธ์ง€๋ฅผ ์—…๋กœ๋“œํ•˜๋ ค๊ณ  ์‹œ๋„ํ•  ๋•Œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ๋กœ๋“œ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ดˆ๊ธฐ ๋กœ๋”ฉ ์†๋„๊ฐ€ ๊ฐœ์„ ๋  ๊ฒƒ์ด๋ผ ์ƒ๊ฐํ•œ๋‹ค.

export const compressImage = async (file: File): Promise<File | null> => {
  try {
    const { default: imageCompression } = await import(
      'browser-image-compression'
    );
    const options = {
      maxSizeMB: 2, // ์ตœ๋Œ€ ํฌ๊ธฐ (MB)
      maxWidthOrHeight: 800, // ์ตœ๋Œ€ ๋„ˆ๋น„๋‚˜ ๋†’์ด (ํ”ฝ์…€)
      useWebWorker: true, // ์›น ์›Œ์ปค ์‚ฌ์šฉ ์—ฌ๋ถ€
      fileType: 'image/webp',
    };
    const convertedFile = await imageCompression(file, options);
    return convertedFile;
  } catch (error) {
    console.error('ํŒŒ์ผ ๋ณ€ํ™˜ ์ค‘ ์˜ค๋ฅ˜:', error);
    return null;
  }
};

 

๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ ์šฉ ๊ฒฐ๊ณผ

  • ์••์ถ• ์ „ ํŒŒ์ผ ํฌ๊ธฐ: 144003, ์ด๋ฏธ์ง€ ํƒ€์ž…: “image/png”
  • ์••์ถ• ํ›„ ํŒŒ์ผ ํฌ๊ธฐ: 33872, ์ด๋ฏธ์ง€ ํƒ€์ž…: "image/webp"

 

https://www.npmjs.com/package/browser-image-compression

 

browser-image-compression

Compress images in the browser. Latest version: 2.0.2, last published: 2 years ago. Start using browser-image-compression in your project by running `npm i browser-image-compression`. There are 182 other projects in the npm registry using browser-image-com

www.npmjs.com