Donaldcwl / browser-image-compression

Image compression in web browser
MIT License
1.31k stars 161 forks source link

Cannot select image from Gdrive #135

Closed ekobayu closed 2 years ago

ekobayu commented 2 years ago

Hello, I try select image from google drive in my phone but the browser-image-compression plugin cannot compress the file. I try using png, jpg and jpeg but all it's failed. Any suggestion ? Thanks

Donaldcwl commented 2 years ago

This is a bug with Android Chrome, please refer to: https://bugs.chromium.org/p/chromium/issues/detail?id=1063576 A workable solution is to reconstruct the File object:

<body>
    <input type="file" accept="image/*" onchange="compressImage(event);" />
</body>
<script>
async function compressImage(event) {
  const originalFile = event.target.files[0];
  const buffer = await originalFile.arrayBuffer();
  const imageFile= new File([buffer], file.name, { type: file.type });
  const compressedFile = await imageCompression(imageFile, options);
  // ...
}
</script>
ekobayu commented 2 years ago

This is a bug with Android Chrome, please refer to: https://bugs.chromium.org/p/chromium/issues/detail?id=1063576 A workable solution is to reconstruct the File object:

<body>
    <input type="file" accept="image/*" onchange="compressImage(event);" />
</body>
<script>
async function compressImage(event) {
  const originalFile = event.target.files[0];
  const buffer = await originalFile.arrayBuffer();
  const imageFile= new File([buffer], file.name, { type: file.type });
  const compressedFile = await imageCompression(imageFile, options);
  // ...
}
</script>

Thanks bro, it's works!