disintegration / imaging

Imaging is a simple image processing package for Go
MIT License
5.3k stars 443 forks source link

Question about upscaling while maintaining aspect ratio #107

Closed vincentmarck closed 4 years ago

vincentmarck commented 4 years ago

Hi,

As the subject already gave away, I'm trying to implement code which resizes images while maintaining aspect ratio. Right now I'm using the Fit function, since that takes care of all the aspect ratio logic for me. Unfortunately, this does not allow me to upscale the image. For upscaling I have to use Resize, but that means I have to introduce logic in my own code to figure out if I should send the width or height while keeping the other one at 0, otherwise my images will get stretched.

I've been looking at the Fit function and figured out that removing these three lines make it work exactly how I want it to, upscaling the image but always keeping the aspect ratio intact.

if srcW <= maxW && srcH <= maxH {
    return imaging.Clone(img)
}

But that is not ideal of course. Does Imaging offer a better way to do this?

disintegration commented 4 years ago

Hi,

There's no better way. If the Fit and Fill helper functions do not meet your needs, the best way is to calculate the desired image size and use Resize.