Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

imageCompression func alters options object #70

Closed
kltchnko opened this issue Jun 16, 2020 · 1 comment
Closed

imageCompression func alters options object #70

kltchnko opened this issue Jun 16, 2020 · 1 comment

Comments

@kltchnko
Copy link

kltchnko commented Jun 16, 2020

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: #71

@mystoryjar
Copy link

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

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

Donaldcwl added a commit that referenced this issue Nov 8, 2020
Fix #70: options object being altered by compress func
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants