Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the file format using browser-image-compression library in reactjs?

In this example I am using the filetype option as .png but its not working as expected.

import imageCompression from 'browser-image-compression';

const compressImage = async (pic) => {
  const options = {
    maxSizeMB: 1,
    maxWidthOrHeight: 1024,
    useWebWorker: true,
    fileType: '.png',
  };

  const compressedPic = await imageCompression(pic, options);
  console.log('compressed', compressedPic.size);
  console.log('compressd pic title', compressedPic.name);
};

like image 843
Shivam Rawat Avatar asked Oct 18 '25 14:10

Shivam Rawat


1 Answers

In 'browser-image-compression' sources you can see that

@param {string} [options.fileType] - default to be the original mime type from the image file

You should use file mime type rather than .png extension, so try to use image/png instead.

like image 82
vicacid Avatar answered Oct 21 '25 05:10

vicacid