thumbnailator icon indicating copy to clipboard operation
thumbnailator copied to clipboard

Add 'scaleUp' option

Open thiago-negri opened this issue 8 years ago • 2 comments

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

thiago-negri avatar Jun 08 '16 15:06 thiago-negri

@coobird What do you think about this change? What should I do to get it accepted?

thiago-negri avatar Jun 13 '16 20:06 thiago-negri

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);

nqzero avatar Feb 18 '18 03:02 nqzero

Thank you for your interest! Pull requests are currently not being accepted.

coobird avatar Nov 21 '22 16:11 coobird