thumbnailator
thumbnailator copied to clipboard
Add 'scaleUp' option
This option lets client select whether to generate a thumbnail when it would scale up the image instead.
It is enabled by default (current behaviour), if client wants to generate a thumbnail only unless the image is going to scale up, just set 'scaleUp' to false and the image size won't change.
See #87, #85
@coobird What do you think about this change? What should I do to get it accepted?
since @coobird doesn't appear to be actively working on this anymore, here's a work-around for the scaleUp
public static class Shrinker {
static Factory shrinkFactory = new Factory();
static DefaultResizerFactory drf = (DefaultResizerFactory) DefaultResizerFactory.getInstance();
static class Factory implements ResizerFactory {
public Resizer getResizer() { return null; }
public Resizer getResizer(Dimension src,Dimension dst) {
if (src.height < dst.height | src.width < dst.width)
throw new BiggerException();
return drf.getResizer(src,dst);
}
}
static class BiggerException extends RuntimeException {}
public interface Saver<TT> {
void exec(TT obj) throws IOException;
}
/**
* render a source image to a size that will never be larger than the source
* @param <TT> the builder type
* @param producer the config portion of the builder chain
* @param sizer the sizing portion of the builder chain, eg size(100,100)
* @param saver the termination of the builder chain, eg toFile
* @param always if the source is smaller than the sizer size, scale by 1.0 and save anyway
* @throws IOException */
public static <TT> void resize(
Supplier<Builder<TT>> producer,
Function<Builder<TT>,Builder<TT>> sizer,
Saver<Builder<TT>> saver,
boolean always) throws IOException {
try {
saver.exec(sizer.apply(producer.get()).resizerFactory(shrinkFactory));
}
catch (BiggerException ex) {
if (always) saver.exec(producer.get().scale(1.0));
}
}
}
to use it call: Shrinker.resize(() -> Thumbnails.of("brad.jpg"),x -> x.size(1000,1000),x -> x.toFile("b1"),true);
Thank you for your interest! Pull requests are currently not being accepted.