kleisauke / net-vips

.NET binding for libvips.
https://kleisauke.github.io/net-vips/
MIT License
396 stars 31 forks source link

Thumbnail width for portrait images #71

Open boris612 opened 4 years ago

boris612 commented 4 years ago

ThumbnailImage method allows a user to specify only width, but it does not work correctly for portrait images (or it is not correctly documented) For example, if you write the following code

var image = Image.NewFromFile(filename);
Image thumbnail = image.ThumbnailImage(300);

for the image with dimensions 600x900 then it would produce 200x300 thumbnail instead of 300x450. It appears that it translates to vipsthumbnail.exe -s 300 instead to vipsthumbnail.exe -s 300x

A workaround is to calculate height or to give some large value for height Image thumbnail = image.ThumbnailImage(300, height: a big enough number)

kleisauke commented 4 years ago

This is a known issue in libvips. See https://github.com/libvips/libvips/issues/709 for a future possible enhancement.

A workaround is to calculate height or to give some large value for height Image thumbnail = image.ThumbnailImage(300, height: a big enough number)

Indeed, you can set the target resize height to VIPS_MAX_COORD (which is currently 10000000) to prevent reduction or enlargement in the vertical axis and vice versa for the target resize width (that's what vipsthumbnail does).

var image = Image.NewFromFile(filename);
Image thumbnail = image.ThumbnailImage(300);

You could use Image.Thumbnail instead. Because the image has already been opened, it can't do any of the shrink tricks that makes thumbnailing fast. https://github.com/kleisauke/net-vips/issues/64 might be relevant here.

boris612 commented 4 years ago

Thanks for the info, it solves the problem. Just a suggestion. Maybe you could write a similar note in the method summary, and/or define public constant VIPS_MAX_COORD and set default height value to it instead to null.

kleisauke commented 4 years ago

See https://github.com/libvips/libvips/pull/1639 for a draft that changes the vips_thumbnail into a resize based on a specific axis. Any feedback is welcome!