Zeynab Rostami
Zeynab Rostami

Reputation: 433

How to compress image only when it's too large with react-native-image-picker

I'm using react-native-image-picker and I want to compress images in order to send them faster to my server.

Right now setting the option "quality" to 0.5 works just fine, but I don't want to compress small images too. I rather keep them untouched indeed.

How can I provide a condition which considers the current size of the file, then set the "quality" option to 0.5 , only if the size is larger than a specified amount (5MB for example)?

import ImagePicker from 'react-native-image-picker';

onPressGallery() {
            const option = {
                multiple: false,
                width: 1000,
                height: 500,
                quality: 0.5,
            };
            ImagePicker.launchImageLibrary(option, response => {
                {
                    console.log('onPressGallery', response);
                    const source = {uri: response.path};
                }
            });
        }

I really appreciate it if anyone could help.

Upvotes: 3

Views: 6554

Answers (2)

JorgeW
JorgeW

Reputation: 21

I am using the showImagePicker function returned from 'react-native-image-picker' and having the same problem. What i did was add the (maxHeight: 600, maxWidth: 800) and it works.

enter image description here

Upvotes: 2

Egizas
Egizas

Reputation: 317

Response is returning bytes now, after that you can feed the image to ImageManipulator and apply the wished quality (compress prop from https://docs.expo.io/versions/v35.0.0/sdk/imagemanipulator/)

Upvotes: -1

Related Questions