Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can I set max-size instead of just size? (Only scale down) #152

Open
ILyaCyclone opened this issue Apr 13, 2020 · 5 comments
Open

Can I set max-size instead of just size? (Only scale down) #152

ILyaCyclone opened this issue Apr 13, 2020 · 5 comments

Comments

@ILyaCyclone
Copy link

ILyaCyclone commented Apr 13, 2020

First of all, really glad to see new Thumbnailator releases in 2020 after ~6 years of silence. Cheers!

I'd like to know if there is an option to restrict upscaling of images and only resize them down.
We plan to use Thumbnailator as dynamic on-fly resizer rather than for making static thumbnails, so in rare circumstances original image may be smaller than requested size.
What happens now is that image is scaled UP to requested size and becomes very messy. I haven't found any method to tell Thumbnailator not to make images larger than they already are (even if requested size says so), only make them smaller. Is there such method?

Example:
Image original size is 150*150. For some internal reasons we call resize(200, 200).
I want result image to stay 150*150.

void resize(InputStream is, OutputStream os, int width, int height) {
    Thumbnails.of(is).size(width, height).toOutputStream(os);
}

Consider this as CSS max-width attribute instead of width.

@leoviveiros
Copy link

I've tried a ResizerFactory returning a NullResizer without success, it still upscales the image.

@coobird
Copy link
Owner

coobird commented May 31, 2020

@ILyaCyclone, unfortunately, the quick answer to your question is "no", not yet.

It's been brought up in the past as well, as issue #11.

I'll actually keep this ticket open, and supersede #11 with this ticket.

@coobird coobird changed the title Can I set max-size instead of just size? Can I set max-size instead of just size? (Only scale down) May 31, 2020
@ILyaCyclone
Copy link
Author

Gotcha, thanks for your attention.

@simon1867
Copy link

This has been open for a while.
Any plans to add support for this?
Or at least to disable upscaling?

@qiezhengyuan
Copy link

One solution:

public class NoScaleUpResizer implements ImageFilter {
    private final int maxWidth;
    private final int maxHeight;

    public NoScaleUpResizer(int maxWidth, int maxHeight) {
        this.maxWidth = maxWidth;
        this.maxHeight = maxHeight;
    }

    @Override
    public BufferedImage apply(BufferedImage img) {
        if (img.getWidth() <= maxWidth && img.getHeight() <= maxHeight) {
            return img;
        }
        try {
            return Thumbnails.of(img).size(maxWidth, maxHeight).asBufferedImage(); 
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

And then:

Thumbnails.of(inputFile)
    .scale(1) // do not resize
    .addFilter(new NoScaleUpResizer(maxWidth, maxHeight)) // then resize only if larger
    .toFile(outputFile);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants