Class Thumbnailator


  • public final class Thumbnailator
    extends java.lang.Object
    This class provides static utility methods which perform generation of thumbnails using Thumbnailator.

    When images are resized, the aspect ratio of the images are preserved.

    • Constructor Summary

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

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.awt.image.BufferedImage createThumbnail​(java.awt.image.BufferedImage img, int width, int height)
      Creates a thumbnail.
      static java.awt.Image createThumbnail​(java.awt.Image img, int width, int height)
      Creates a thumbnail from an Image.
      static java.awt.image.BufferedImage createThumbnail​(java.io.File f, int width, int height)
      Creates a thumbnail from an image file, and returns as a BufferedImage.
      static void createThumbnail​(java.io.File inFile, java.io.File outFile, int width, int height)
      Creates a thumbnail from an source image and writes the thumbnail to a destination file.
      static void createThumbnail​(java.io.InputStream is, java.io.OutputStream os, int width, int height)
      Creates a thumbnail from image data streamed from an InputStream and streams the data out to an OutputStream.
      static void createThumbnail​(java.io.InputStream is, java.io.OutputStream os, java.lang.String format, int width, int height)
      Creates a thumbnail from image data streamed from an InputStream and streams the data out to an OutputStream, with the specified format for the output data.
      static void createThumbnail​(ThumbnailTask<?,​?> task)
      Creates a thumbnail from parameters specified in a ThumbnailTask.
      static void createThumbnails​(java.util.Collection<? extends java.io.File> files, Rename rename, int width, int height)
      Creates thumbnails from a specified Collection of Files.
      static java.util.Collection<java.io.File> createThumbnailsAsCollection​(java.util.Collection<? extends java.io.File> files, Rename rename, int width, int height)
      Creates thumbnails from a specified Collection of Files.
      private static boolean hasSwapDimensionsFilter​(java.util.List<ImageFilter> imageFilters)  
      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

      • Thumbnailator

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

      • createThumbnail

        public static void createThumbnail​(ThumbnailTask<?,​?> task)
                                    throws java.io.IOException
        Creates a thumbnail from parameters specified in a ThumbnailTask.
        Parameters:
        task - A ThumbnailTask to execute.
        Throws:
        java.io.IOException - Thrown when a problem occurs when creating a thumbnail.
      • hasSwapDimensionsFilter

        private static boolean hasSwapDimensionsFilter​(java.util.List<ImageFilter> imageFilters)
      • createThumbnail

        public static java.awt.image.BufferedImage createThumbnail​(java.awt.image.BufferedImage img,
                                                                   int width,
                                                                   int height)
        Creates a thumbnail.

        The resulting thumbnail uses the default image type.

        When the image is resized, the aspect ratio will be preserved.

        When the specified dimensions does not have the same aspect ratio as the source image, the specified dimensions will be used as the absolute boundary of the thumbnail.

        For example, if the source image of 100 pixels by 100 pixels, and the desired thumbnail size is 50 pixels by 100 pixels, then the resulting thumbnail will be 50 pixels by 50 pixels, as the constraint will be 50 pixels for the width, and therefore, by preserving the aspect ratio, the height will be required to be 50 pixels.

        Parameters:
        img - The source image.
        width - The width of the thumbnail.
        height - The height of the thumbnail.
        Returns:
        Resulting thumbnail.
      • createThumbnail

        public static void createThumbnail​(java.io.File inFile,
                                           java.io.File outFile,
                                           int width,
                                           int height)
                                    throws java.io.IOException
        Creates a thumbnail from an source image and writes the thumbnail to a destination file.

        The image format to use for the thumbnail will be determined from the file extension. However, if the image format cannot be determined, then, the same image format as the original image will be used when writing the thumbnail.

        Parameters:
        inFile - The File from which image data is read.
        outFile - The File to which thumbnail is written.
        width - The width of the thumbnail.
        height - The height of the thumbnail.
        Throws:
        java.io.IOException - Thrown when a problem occurs when reading from File representing an image file.
      • createThumbnail

        public static java.awt.image.BufferedImage createThumbnail​(java.io.File f,
                                                                   int width,
                                                                   int height)
                                                            throws java.io.IOException
        Creates a thumbnail from an image file, and returns as a BufferedImage.
        Parameters:
        f - The File from which image data is read.
        width - The width of the thumbnail.
        height - The height of the thumbnail.
        Returns:
        The thumbnail image as a BufferedImage.
        Throws:
        java.io.IOException - Thrown when a problem occurs when reading from File representing an image file.
      • createThumbnail

        public static java.awt.Image createThumbnail​(java.awt.Image img,
                                                     int width,
                                                     int height)
        Creates a thumbnail from an Image.

        The resulting BufferedImage uses the default image type.

        When the image is resized, the aspect ratio will be preserved.

        When the specified dimensions does not have the same aspect ratio as the source image, the specified dimensions will be used as the absolute boundary of the thumbnail.

        Parameters:
        img - The source image.
        width - The width of the thumbnail.
        height - The height of the thumbnail.
        Returns:
        The thumbnail image as an Image.
      • createThumbnail

        public static void createThumbnail​(java.io.InputStream is,
                                           java.io.OutputStream os,
                                           int width,
                                           int height)
                                    throws java.io.IOException
        Creates a thumbnail from image data streamed from an InputStream and streams the data out to an OutputStream.

        The thumbnail will be stored in the same format as the original image.

        Parameters:
        is - The InputStream from which to obtain image data.
        os - The OutputStream to send thumbnail data to.
        width - The width of the thumbnail.
        height - The height of the thumbnail.
        Throws:
        java.io.IOException - Thrown when a problem occurs when reading from File representing an image file.
      • createThumbnail

        public static void createThumbnail​(java.io.InputStream is,
                                           java.io.OutputStream os,
                                           java.lang.String format,
                                           int width,
                                           int height)
                                    throws java.io.IOException
        Creates a thumbnail from image data streamed from an InputStream and streams the data out to an OutputStream, with the specified format for the output data.
        Parameters:
        is - The InputStream from which to obtain image data.
        os - The OutputStream to send thumbnail data to.
        format - The image format to use to store the thumbnail data.
        width - The width of the thumbnail.
        height - The height of the thumbnail.
        Throws:
        java.io.IOException - Thrown when a problem occurs when reading from File representing an image file.
        java.lang.IllegalArgumentException - If the specified output format is not supported.
      • createThumbnailsAsCollection

        public static java.util.Collection<java.io.File> createThumbnailsAsCollection​(java.util.Collection<? extends java.io.File> files,
                                                                                      Rename rename,
                                                                                      int width,
                                                                                      int height)
                                                                               throws java.io.IOException
        Creates thumbnails from a specified Collection of Files. The filenames of the resulting thumbnails are determined by applying the specified Rename.

        The order of the thumbnail Files in the returned Collection will be the same as the order as the source list.

        Parameters:
        files - A Collection containing File objects of image files.
        rename - The renaming function to use.
        width - The width of the thumbnail.
        height - The height of the thumbnail.
        Returns:
        A collection of Files to the thumbnails.
        Throws:
        java.io.IOException - Thrown when a problem occurs when reading from File representing an image file.
      • createThumbnails

        public static void createThumbnails​(java.util.Collection<? extends java.io.File> files,
                                            Rename rename,
                                            int width,
                                            int height)
                                     throws java.io.IOException
        Creates thumbnails from a specified Collection of Files. The filenames of the resulting thumbnails are determined by applying the specified Rename function.
        Parameters:
        files - A Collection containing File objects of image files.
        rename - The renaming function to use.
        width - The width of the thumbnail.
        height - The height of the thumbnail.
        Throws:
        java.io.IOException - Thrown when a problem occurs when reading from File representing an image file.
      • 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.

        This method is used to perform a check on the output dimensions of a thumbnail for the createThumbnail(net.coobird.thumbnailator.tasks.ThumbnailTask<?, ?>) methods.

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