Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native- Resize Image and convert to base64

So I'm using react-native-signature-capture to capture a signature, but I wan't to cut down on the image size before encoding it. I used https://github.com/bamlab/react-native-image-resizer to resize the image, but now I don't know how to convert it to base64. I tried using RN's ImageStore but I get an error with the filepath of the image. See below for the code:

ImageResizer.createResizedImage(encoded.pathName, 200, 100, 'PNG', 80, null, encoded.pathName)
  .then((resizedImageUrl) => {
    ImageStore.getBase64ForTag(resizedImageUrl, (data) => {
      console.log(data);
    }, (err) => console.log(err));
  })
  .catch((err) => console.log('failed to resize: ' + err));
like image 864
Matt Aft Avatar asked Dec 15 '16 19:12

Matt Aft


1 Answers

Solved this a while ago and forgot to put what I did until now, I basically used the library above and react-native-fs to resize and retrieve the image as a base64:

handleBase64 = async (path) => {
  const resizedImageUrl = await ImageResizer.createResizedImage(path, 200, 80, 'PNG', 80, 0, RNFS.DocumentDirectoryPath);
  const base64 = await RNFS.readFile(resizedImageUrl, 'base64');
  return base64;
}
like image 67
Matt Aft Avatar answered Oct 11 '22 12:10

Matt Aft