bamlab / react-native-image-resizer

🗻 Resize local images with React Native
MIT License
1.61k stars 346 forks source link

problem to load the source and recover the width and height ? If width or height === 0, there is no output #290

Closed Esoriak closed 2 years ago

Esoriak commented 3 years ago

Hi,

We use this package to resize an image before sending it to a visual search engine.

The source is still existing but sometimes the function does not execute and returns an error 'Can't resize the image', when the maxwidth or maxheight are at 0.

Is there a reason why these variables are at 0 then that the source of the image seems ok? Could it be from a corrupted link, a cache problem, or bad connection ? We tried to modify the input variables to see when an error occurs. When the source is not good (ex: empty string), when the width or height are at 0, there is no output. Impossible to go back further and find out what is causing a problem on these 3 parameters. Do you have any ideas ?

This my code :

if (takenUri) CameraRoll.saveToCameraRoll(takenUri);

  const originalUri = loadedUri || takenUri;

  const uri = croppedUri ? croppedUri : originalUri;
  Image.getSize(uri, async (width, height) => {
    let loadWidth = width;
    let loadHeight = height;
    if (width > 800) {
      loadWidth = 800;
      loadHeight = (height * 800) / width;
    }
    const uri64 = await toBase64(uri);
    const image = Platform.OS === 'ios' ? uri : 'data:image/jpeg;base64,' + uri64;

     if ((Platform.OS === 'android' && uri64 !== undefined) || (Platform.OS === 'ios' && uri !== undefined )) {
        ImageResizer.createResizedImage(image, loadWidth, loadHeight, 'JPEG', 100)
        .then(async ({uri}) => {
          const image64 = await toBase64(uri);
          const userInfos = await getUserInfos();
          const data = await searchStyle({...userInfos, imageBase64: image64});
          setIsLoading(false);
          if (data.results) {
            navigation.goBack();
            navigation.navigate('Results', {
              search: uri,
              dataProducts: data.results[0],
              sentence: data.sentence,
              requestId: data.request_id,
            });
          }
          if (!data.results || data.results === undefined) setLoadError(true);
        })
        .catch(err => {
          if (err === "Can't resize the image." || "Can't retrieve the file from the path.") {
            setGetSizeImgFailed(true);
          }
           else if (err.message === 'Network request failed' || "Failed to decode Bitmap"){
             setRequestFailed(true);
          } 
        });
      }
  }, (error) => {
    console.error(`Couldn't get the image size: ${error.message}`)
  });
PierreCapo commented 3 years ago

Hello @Esoriak,

Thanks for opening the issue! Sorry I don't understand 100% your problem yet.

The source is still existing but sometimes the function does not execute and returns an error 'Can't resize the image', when the maxwidth or maxheight are at 0.

When you say that maxWidth or maxHeight are at 0, you mean that you are calling the function like that : createResizedImage("some path", 0, 200, ... ? And this is when you have your bug right ?

Do you have any particular reason why would you like resize an image with either maxwidth=0 or maxheight=0 ?

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] commented 2 years ago

This issue has been automatically closed. Thank you for your contributions.