react-native-compressor icon indicating copy to clipboard operation
react-native-compressor copied to clipboard

[Error: width and height must be > 0] On image compression

Open wwessel01 opened this issue 3 years ago • 7 comments

Current behavior

When trying to compress an image from URI or base64 an error is thrown. [Error: width and height must be > 0]

Expected behavior

To be able to compress an image using base64 and throw a more elaborate error.

Platform

  • [X] Android
  • [ ] iOS (Not tested)

Code

This will throw the error

try {
    const compressedSource = await compressFile(
        CompressFileType.IMAGE,
        'https://picsum.photos/500'
    );
    console.log(compressedSource);
} catch (error) {
    console.error(error);
}

And this is where the compression is handled

import { Image, Video, Audio } from 'react-native-compressor';
import { CompressFileType } from '../../enums/CompressFileType';

const compressFile = async (fileType: CompressFileType, value: string) => {
  console.log('Compressing ' + fileType + ' with value ' + value);
  switch (fileType) {
    case CompressFileType.IMAGE:
      console.log('IMAGE');
      const imageResult = await Image.compress(value, {
        compressionMethod: 'auto',
        // input: 'base64',
        maxHeight: 50,
        maxWidth: 50,
      });
      return imageResult;
    case CompressFileType.VIDEO:
      console.log('VIDEO');
      const videoResult = await Video.compress(value, {
        compressionMethod: 'auto',
      });
      return videoResult;
    case CompressFileType.AUDIO:
      console.log('AUDIO');
      const audioResult = await Audio.compress(value, {
        quality: 'medium',
      });
      return audioResult;
    default:
      throw new Error(`Unknown file type '${fileType}'`);
  }
};

export { compressFile };

wwessel01 avatar May 16 '22 21:05 wwessel01

👋 @wwessel01 Thanks for opening your issue here! If you find this package useful hit the star🌟!

github-actions[bot] avatar May 16 '22 21:05 github-actions[bot]

same issue here

saif-o99 avatar May 19 '22 12:05 saif-o99

On Anroid same error "width and height must be > 0", while on iOS promise never resolve, I tried example code with auto compression.

import { Image } from 'react-native-compressor';

const result = await Image.compress('...', {
  compressionMethod: 'auto',
});
System:
    OS: macOS 12.3.1
    CPU: (8) x64 Intel(R) Core(TM) i5-1038NG7 CPU @ 2.00GHz
    Memory: 932.83 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.13.2 - /usr/local/bin/node
    Yarn: 1.22.17 - /usr/local/bin/yarn
    npm: 8.1.2 - /usr/local/bin/npm
    Watchman: 2022.03.21.00 - /usr/local/bin/watchman
  Managers:
    CocoaPods: 1.11.3 - /Users/kristijan/.rbenv/shims/pod
  SDKs:
    iOS SDK:
      Platforms: DriverKit 21.4, iOS 15.5, macOS 12.3, tvOS 15.4, watchOS 8.5
    Android SDK:
      API Levels: 23, 28, 29, 30, 31
      Build Tools: 28.0.3, 29.0.2, 29.0.3, 30.0.2, 31.0.0, 31.0.0
      System Images: android-27 | Google APIs Intel x86 Atom, android-29 | Google Play Intel x86 Atom, android-30 | Google APIs Intel x86 Atom, android-30 | Google Play Intel x86 Atom
      Android NDK: 22.1.7171670
  IDEs:
    Android Studio: 2021.1 AI-211.7628.21.2111.8309675
    Xcode: 13.4/13F17a - /usr/bin/xcodebuild
  Languages:
    Java: 1.8.0_332 - /usr/bin/javac
  npmPackages:
    react: 17.0.2 => 17.0.2
    react-native: 0.68.2 => 0.68.2
    react-native-macos: Not Found

kristijantomic avatar May 22 '22 05:05 kristijantomic

same issue on android

brianso123 avatar Jun 10 '22 08:06 brianso123

found a work around by removing compressionMethod: 'auto'

brianso123 avatar Jun 10 '22 13:06 brianso123

I had this issue working with URI returned by react-native-document-picker, I solved it by using react-native-fs copying the file into a temporary path, like this:

import { Image } from 'react-native-compressor'
import RNFS from 'react-native-fs'

...

const tempPath = RNFS.TemporaryDirectoryPath + '/' + fileName

await RNFS.copyFile(uri, tempPath)
const compressedImage = await Image.compress(tempPath, {
	compressionMethod: 'auto',
})

josmmv avatar Jun 13 '22 15:06 josmmv

For me the error occurs if I compress base64 string on Android

anhnch avatar Aug 05 '22 09:08 anhnch

Anyone find a solution to this?

pontustengroth avatar Feb 11 '23 18:02 pontustengroth

Solved it by providing a file:// path instead of a content:// path

pontustengroth avatar Feb 12 '23 18:02 pontustengroth

@pontustengroth how did you convert from content:// to file:// ?

vineethsagar avatar Mar 28 '23 04:03 vineethsagar

@pontustengroth how did you convert from content:// to file:// ?

const uri = FileSystem.cacheDirectory + `file-${makeid(16)}`;
await FileSystem.copyAsync({
  from: "content://path",
  to: uri,
});

Then sending uri for compression

pontustengroth avatar Mar 28 '23 06:03 pontustengroth

remove compressionMethod: 'auto', this willl work but image did'not compressed.

Yasa000 avatar Jun 19 '23 13:06 Yasa000

The only thing that has worked for me is removing compressionMethod: 'auto', but that disables the compression, so is not really a solution. I have tried other suggestions like saving the file in a temp file and adding file:// prefix, but no luck. Are the maintainers of this library looking into this?

dmsierra11 avatar Jun 29 '23 08:06 dmsierra11

🙏 sorry for a long ago I was not maintaining due to the huge office workload, hopefully, I am going to start fixing bugs on a weekend basis, I will fix at least 2 issues on the weekend, for priority bugs you can join the discord channel https://discord.gg/dtYzk8sp where we can discuss which bug is most important

numandev1 avatar Aug 19 '23 08:08 numandev1

@dmsierra11 @wwessel01 @kristijantomic @saif-o99 should I add support for compressing images from the server like this https://picsum.photos/500👀

numandev1 avatar Aug 24 '23 06:08 numandev1

@dmsierra11 @wwessel01 @kristijantomic @saif-o99 should I add support for compressing images from the server like this https://picsum.photos/500👀

This is what i require.

suleymanbasbug avatar Aug 24 '23 07:08 suleymanbasbug

@suleymanbasbug @dmsierra11 @kristijantomic @saif-o99 released in 1.7.1, can you check compression for remote images/videos?

numandev1 avatar Sep 03 '23 09:09 numandev1