Donaldcwl / browser-image-compression

Image compression in web browser
MIT License
1.3k stars 160 forks source link

imageCompression func alters options object #70

Closed kltchnko closed 4 years ago

kltchnko commented 4 years ago

Hello! Thank you for this module. There is an issue if imageCompression function is used in a "for" loop. If options object is defined before the loop then on each iteration function imageCompression changes options' properties:

async function compressFiles(uncompressedFiles) {
    const compressedFiles = [];
    const options = {
          maxSizeMB: 0.3,
          maxWidthOrHeight: 1280,
          useWebWorker: true,
    };
    for (let i = 0; i < uncompressedFiles.length; i++) {
      try {
        const compressedFile = await imageCompression(uncompressedFiles[i], options);
        compressedFiles.push(compressedFile);
      } catch (error) {
        console.log(error);
      }
    }
    return compressedFiles;
  }

For example, if I try to upload 3 photos and the first one has exifOrientation = 6 then the rest of the photos will be rotated as well, because exifOrientation will be in the options object after the first iteration. I guess it is because of this line: options.exifOrientation = options.exifOrientation || await getExifOrientation(file)

I think compress function should use a copy of options object. I've opened PR for this issue: https://github.com/Donaldcwl/browser-image-compression/pull/71

mystoryjar commented 4 years ago

I encountered the same issue and fixed it by inlining options:

const compressedFile = await imageCompression(uncompressedFiles[i], {
  maxSizeMB: 0.3,
  maxWidthOrHeight: 1280,
  useWebWorker: true,
});