question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:7
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
QieZhengyuancommented, Nov 11, 2022

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);
2reactions
leoviveiroscommented, May 13, 2020

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

html - CSS scale down image to fit in containing div, without ...
Pixels or percentages work for max size. With both the height and width set to auto , the aspect ratio of the original...
Read more >
Scale included graphics to the higher ratio instead of the lower ...
1. Your package is just amazing! · 2. Yes, indeed, it doesn't work for large images directly, but you can use first max...
Read more >
How Do You Do max-font-size in CSS?
One solution is to use a media query at a certain screen size breakpoint that sets the font size in a non-relative unit....
Read more >
Resizing designs and design size limits - Canva Help Center
If you really need to create a design that's outside of the allowed dimensions (e.g. a large banner), you can scale the dimensions...
Read more >
Facebook Image Sizes 2022: Everything You Need to Know
It's how people will come to know you, so you want it to be good. Down and dirty dimensions: Max size: 2048px by...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found