h2non / imaginary

Fast, simple, scalable, Docker-ready HTTP microservice for high-level image processing
https://fly.io/docs/app-guides/run-a-global-image-service/
MIT License
5.57k stars 456 forks source link

Aspect ratio for Resize operation #139

Open saravanakumar38 opened 7 years ago

saravanakumar38 commented 7 years ago

Hi,

I tried to resize the image (640x480) to 185x110, but resize image is missing portion of the original image. I tried with thumbnail, but images is stretched. How can we resize the image automatically based on aspect ratio. example if original image size (640x480) and resize parameter is (185x110), imaginary should automatically convert to (146x110).

var ratioX = (double)requestedResizeWidth / OriginalWidth; var ratioY = (double)requestedResizeHeight / OriginalHeight; var ratio = Math.Min(ratioX, ratioY);

                        finalResizeWidth = (int)(OriginalWidth * ratio);
                        finalResizeHeight = (int)(OriginalHeight * ratio);

I have attached sample image for reference. Is there best method to get resized image without missing image content?

Thanks

thumbnail_185x110 original_640x480 resize_146x110 resize_185x110

Bessonov commented 7 years ago

We are looking for alternatives for ngx_small_light and we have same requirement.

Dynom commented 7 years ago

The work-around is by doing this client-side. If you know the dimensions you can decide what to do, this also empowers you to deal with the side you want to crop.

Imaginary supports maintaining an aspect ratio, by not specifying one of the sides. If you specify both width & height, you're telling Imaginary that you know what you're doing. I think you get the result you want by only specifying the longest side.

See also: https://github.com/h2non/imaginary/issues/135#issuecomment-302696540

Bessonov commented 7 years ago

Just a side note. This is possible with gm and sharp. Both can be used to get dimensions of an image to use it with imaginary, but they can resize it too. We stick with sharp (which use libvips too), because it meets all our current requirements, partially resizing with keeping aspect ratio, performance and memory footprint (important for lambda on aws).