bamlab / react-native-image-resizer

🗻 Resize local images with React Native
MIT License
1.6k stars 342 forks source link

Not possible to resize an image into a square #345

Closed aymeric75 closed 1 year ago

aymeric75 commented 1 year ago

Hello,

I want to resize an image so that its height and width are equal (I want to squarify the image say).

But:

  ImageResizer.createResizedImage(
    result.uri,
    100,
    100,
    'PNG',
    100,
    0,
    null,
    false,
    'stretch'
  ).then((response) => {
      console.log(response);
  ....................

shows:

{"height": 100, "name": "09d283d5-2e22-464d-946a-47a2ae5c7a20.PNG", "path": "/data/user/0/com.bareproject/cache/09d283d5-2e22-464d-946a-47a2ae5c7a20.PNG", "size": 6988, "uri": "file:///data/user/0/com.bareproject/cache/09d283d5-2e22-464d-946a-47a2ae5c7a20.PNG", "width": 46}

I have tried all values for the options.mode parameter, i.e. contain, cover and stretch but still the same...

Should I modify a function in the code ?

Thank you

stale[bot] commented 1 year 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.

emmnlala commented 1 year ago

in node_modules android>src>>> ImageResizer.java making these changes worked for me on ANDROID when using resizeMode "cover"


private static Bitmap resizeImage(Bitmap image, int newWidth, int newHeight,
                                      String mode, boolean onlyScaleDown) {

       ...

            try {
                newImage = Bitmap.createScaledBitmap(image, finalWidth, finalHeight, true);

                if(mode.equals("cover")){       // <---- ADD FOR COVER
                    // Bitmap.createBitmap(Bitmap source, int x, int y, int width, int height)
                    int toWidth =  Math.min(finalWidth, newWidth);
                    int toHeight = Math.min(finalHeight, newHeight);
                    newImage = Bitmap.createBitmap(
                                newImage, 
                                finalWidth/2 - toWidth/2, 
                                finalHeight/2 - toHeight/2, 
                                toWidth, 
                                toHeight
                                );
                }

            } 

        ...

       return newImage;
 }
taboulot commented 1 year ago

Hi @aymeric75 ,

I think the problem is that you do not respect the createResizedImage interface. I tried with the following example:

let result = await ImageResizer.createResizedImage(
        image.uri,
        100,
        100,
        'PNG',
        100,
        0,
        undefined,
        false,
        {
          mode: 'stretch', // <- mode option needs to be specified in an object. You should have a ts error.
        }
);

console.log(result);

And I have the following correct output:

{"height": 100, "name": "fdb4bc30-2e7e-46ad-9e8e-9aeb0ae5379e.PNG", "path": "/data/user/0/com.example.reactnativeimageresizer/cache/fdb4bc30-2e7e-46ad-9e8e-9aeb0ae5379e.PNG", "size": 9188, "uri": "file:///data/user/0/com.example.reactnativeimageresizer/cache/fdb4bc30-2e7e-46ad-9e8e-9aeb0ae5379e.PNG", "width": 100}

Can you try again and tell me if you still have the issue? If yes, can you provide me a reproducible example ?

stale[bot] commented 1 year 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 1 year ago

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