Class Thumbnails


  • public final class Thumbnails
    extends java.lang.Object

    Provides a fluent interface to create thumbnails.

    This is the main entry point for creating thumbnails with Thumbnailator.

    By using the Thumbnailator's fluent interface, it is possible to write thumbnail generation code which resembles written English.

    Usage:

    The following example code demonstrates how to use the fluent interface to create a thumbnail from multiple files from a directory, resizing them to a maximum of 200 pixels by 200 pixels while preserving the aspect ratio of the original, then saving the resulting thumbnails as JPEG images with file names having thumbnail. appended to the beginning of the file name.

    Thumbnails.of(directory.listFiles())
        .size(200, 200)
        .outputFormat("jpeg")
        .asFiles(Rename.PREFIX_DOT_THUMBNAIL);
    
    // English: "Make thumbnails of files in the directory, with a size of 200x200,
                 with output format of JPEG, and save them as files while renaming
                 the files to be prefixed with a 'thumbnail.'."
     

    For more examples, please visit the Thumbnailator project page.

    Important Implementation Notes

    Upon calling one of the Thumbnails.of(...) methods, in the current implementation, an instance of an inner class of this class is returned. In most cases, the returned instance should not be used by storing it in a local variable, as changes in the internal implementation could break code in the future.

    As a rule of thumb, always method chain from the Thumbnails.of all the way until the output method (e.g. toFile, asBufferedImage, etc.) is called without breaking them down into single statements. See the "Usage" section above for the intended use of the Thumbnailator's fluent interface.

    Unintended Use:
    // Unintended use - not recommended!
    Builder<File> instance = Thumbnails.of("path/to/image");
    instance.size(200, 200);
    instance.asFiles("path/to/thumbnail");
     
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  Thumbnails.Builder<T>
      The builder interface for Thumbnailator to set up the thumbnail generation task.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private Thumbnails()
      This class is not intended to be instantiated.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      private static void checkForEmpty​(java.lang.Iterable<?> o, java.lang.String message)  
      private static void checkForEmpty​(java.lang.Object[] o, java.lang.String message)  
      private static void checkForNull​(java.lang.Object o, java.lang.String message)  
      static Thumbnails.Builder<java.io.File> fromFilenames​(java.lang.Iterable<java.lang.String> files)
      Indicate to make thumbnails for images with the specified filenames.
      static Thumbnails.Builder<java.io.File> fromFiles​(java.lang.Iterable<java.io.File> files)
      Indicate to make thumbnails from the specified Files.
      static Thumbnails.Builder<java.awt.image.BufferedImage> fromImages​(java.lang.Iterable<java.awt.image.BufferedImage> images)
      Indicate to make thumbnails from the specified BufferedImages.
      static Thumbnails.Builder<java.io.InputStream> fromInputStreams​(java.lang.Iterable<? extends java.io.InputStream> inputStreams)
      Indicate to make thumbnails for images obtained from the specified InputStreams.
      static Thumbnails.Builder<java.net.URL> fromURLs​(java.lang.Iterable<java.net.URL> urls)
      Indicate to make thumbnails for images with the specified URLs.
      static Thumbnails.Builder<java.awt.image.BufferedImage> of​(java.awt.image.BufferedImage... images)
      Indicate to make thumbnails from the specified BufferedImages.
      static Thumbnails.Builder<java.io.File> of​(java.io.File... files)
      Indicate to make thumbnails from the specified Files.
      static Thumbnails.Builder<? extends java.io.InputStream> of​(java.io.InputStream... inputStreams)
      Indicate to make thumbnails from the specified InputStreams.
      static Thumbnails.Builder<java.io.File> of​(java.lang.String... files)
      Indicate to make thumbnails for images with the specified filenames.
      static Thumbnails.Builder<java.net.URL> of​(java.net.URL... urls)
      Indicate to make thumbnails from the specified URLs.
      private static void validateDimensions​(int width, int height)
      Performs validation on the specified dimensions.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Thumbnails

        private Thumbnails()
        This class is not intended to be instantiated.
    • Method Detail

      • validateDimensions

        private static void validateDimensions​(int width,
                                               int height)
        Performs validation on the specified dimensions.

        If any of the dimensions are less than or equal to 0, an IllegalArgumentException is thrown with an message specifying the reason for the exception.

        Parameters:
        width - The width to validate.
        height - The height to validate.
      • checkForNull

        private static void checkForNull​(java.lang.Object o,
                                         java.lang.String message)
      • checkForEmpty

        private static void checkForEmpty​(java.lang.Object[] o,
                                          java.lang.String message)
      • checkForEmpty

        private static void checkForEmpty​(java.lang.Iterable<?> o,
                                          java.lang.String message)
      • of

        public static Thumbnails.Builder<java.io.File> of​(java.lang.String... files)
        Indicate to make thumbnails for images with the specified filenames.
        Parameters:
        files - File names of image files for which thumbnails are to be produced for.
        Returns:
        Reference to a builder object which is used to specify the parameters for creating the thumbnail.
        Throws:
        java.lang.NullPointerException - If the argument is null.
        java.lang.IllegalArgumentException - If the argument is an empty array.
      • of

        public static Thumbnails.Builder<java.io.File> of​(java.io.File... files)
        Indicate to make thumbnails from the specified Files.
        Parameters:
        files - File objects of image files for which thumbnails are to be produced for.
        Returns:
        Reference to a builder object which is used to specify the parameters for creating the thumbnail.
        Throws:
        java.lang.NullPointerException - If the argument is null.
        java.lang.IllegalArgumentException - If the argument is an empty array.
      • of

        public static Thumbnails.Builder<java.net.URL> of​(java.net.URL... urls)
        Indicate to make thumbnails from the specified URLs.
        Parameters:
        urls - URL objects of image files for which thumbnails are to be produced for.
        Returns:
        Reference to a builder object which is used to specify the parameters for creating the thumbnail.
        Throws:
        java.lang.NullPointerException - If the argument is null.
        java.lang.IllegalArgumentException - If the argument is an empty array.
      • of

        public static Thumbnails.Builder<? extends java.io.InputStream> of​(java.io.InputStream... inputStreams)
        Indicate to make thumbnails from the specified InputStreams.

        Note that the InputStream.close() method will not be called upon reading the source image from the InputStream.

        Parameters:
        inputStreams - InputStreams which provide the images for which thumbnails are to be produced for.
        Returns:
        Reference to a builder object which is used to specify the parameters for creating the thumbnail.
        Throws:
        java.lang.NullPointerException - If the argument is null.
        java.lang.IllegalArgumentException - If the argument is an empty array.
      • of

        public static Thumbnails.Builder<java.awt.image.BufferedImage> of​(java.awt.image.BufferedImage... images)
        Indicate to make thumbnails from the specified BufferedImages.
        Parameters:
        images - BufferedImages for which thumbnails are to be produced for.
        Returns:
        Reference to a builder object which is used to specify the parameters for creating the thumbnail.
        Throws:
        java.lang.NullPointerException - If the argument is null.
        java.lang.IllegalArgumentException - If the argument is an empty array.
      • fromFilenames

        public static Thumbnails.Builder<java.io.File> fromFilenames​(java.lang.Iterable<java.lang.String> files)
        Indicate to make thumbnails for images with the specified filenames.
        Parameters:
        files - File names of image files for which thumbnails are to be produced for.
        Returns:
        Reference to a builder object which is used to specify the parameters for creating the thumbnail.
        Throws:
        java.lang.NullPointerException - If the argument is null.
        java.lang.IllegalArgumentException - If the argument is an empty collection.
        Since:
        0.3.1
      • fromFiles

        public static Thumbnails.Builder<java.io.File> fromFiles​(java.lang.Iterable<java.io.File> files)
        Indicate to make thumbnails from the specified Files.
        Parameters:
        files - File objects of image files for which thumbnails are to be produced for.
        Returns:
        Reference to a builder object which is used to specify the parameters for creating the thumbnail.
        Throws:
        java.lang.NullPointerException - If the argument is null.
        java.lang.IllegalArgumentException - If the argument is an empty collection.
        Since:
        0.3.1
      • fromURLs

        public static Thumbnails.Builder<java.net.URL> fromURLs​(java.lang.Iterable<java.net.URL> urls)
        Indicate to make thumbnails for images with the specified URLs.
        Parameters:
        urls - URLs of the images for which thumbnails are to be produced.
        Returns:
        Reference to a builder object which is used to specify the parameters for creating the thumbnail.
        Throws:
        java.lang.NullPointerException - If the argument is null.
        java.lang.IllegalArgumentException - If the argument is an empty collection.
        Since:
        0.3.1
      • fromInputStreams

        public static Thumbnails.Builder<java.io.InputStream> fromInputStreams​(java.lang.Iterable<? extends java.io.InputStream> inputStreams)
        Indicate to make thumbnails for images obtained from the specified InputStreams.

        Note that the InputStream.close() method will not be called upon reading the source image from the InputStream.

        Parameters:
        inputStreams - InputStreams which provide images for which thumbnails are to be produced.
        Returns:
        Reference to a builder object which is used to specify the parameters for creating the thumbnail.
        Throws:
        java.lang.NullPointerException - If the argument is null.
        java.lang.IllegalArgumentException - If the argument is an empty collection.
        Since:
        0.3.1
      • fromImages

        public static Thumbnails.Builder<java.awt.image.BufferedImage> fromImages​(java.lang.Iterable<java.awt.image.BufferedImage> images)
        Indicate to make thumbnails from the specified BufferedImages.
        Parameters:
        images - BufferedImages for which thumbnails are to be produced for.
        Returns:
        Reference to a builder object which is used to specify the parameters for creating the thumbnail.
        Throws:
        java.lang.NullPointerException - If the argument is null.
        java.lang.IllegalArgumentException - If the argument is an empty collection.
        Since:
        0.3.1