Class FixedSizeThumbnailMaker
- java.lang.Object
-
- net.coobird.thumbnailator.makers.ThumbnailMaker
-
- net.coobird.thumbnailator.makers.FixedSizeThumbnailMaker
-
public final class FixedSizeThumbnailMaker extends ThumbnailMaker
A
ThumbnailMakerwhich resizes an image to a specified dimension when producing a thumbnail.Optionally, if the aspect ratio of the thumbnail is to be maintained the same as the original image (by calling the
keepAspectRatio(boolean)method with the valuetrue), then the dimensions specified by thesize(int, int)method,FixedSizeThumbnailMaker(int, int)orFixedSizeThumbnailMaker(int, int, boolean)constructor will be used as the maximum constraint of dimensions of the thumbnail.In other words, when the aspect ratio is to be kept constant, then thumbnails which are created will be sized to fit inside the dimensions specified by the size parameter.
Upon calculating the size of the thumbnail, if any of the dimensions are
0, then that dimension will be promoted to1, regardless of whether the aspect ratio of the original image is to be maintained. This will lead to some thumbnails not preserving the aspect ratio of the original image, even ifkeepAspectRatio(boolean)has beentrue.- Usage:
-
The following example demonstrates how to create a thumbnail which fits
within 200 pixels by 200 pixels, while preserving the aspect ratio of the
source image:
BufferedImage img = ImageIO.read(new File("sourceImage.jpg")); BufferedImage thumbnail = new FixedSizeThumbnailMaker() .size(200, 200) .keepAspectRatio(true) .make(img);
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class net.coobird.thumbnailator.makers.ThumbnailMaker
ThumbnailMaker.ReadinessTracker
-
-
Field Summary
Fields Modifier and Type Field Description private booleanfitWithinDimensionsprivate intheightprivate booleankeepRatioprivate static java.lang.StringPARAM_FIT_WITHINprivate static java.lang.StringPARAM_KEEP_RATIOprivate static java.lang.StringPARAM_SIZEprivate intwidth-
Fields inherited from class net.coobird.thumbnailator.makers.ThumbnailMaker
imageType, ready, resizerFactory
-
-
Constructor Summary
Constructors Constructor Description FixedSizeThumbnailMaker()Creates aFixedSizeThumbnailMaker.FixedSizeThumbnailMaker(int width, int height)Creates aFixedSizeThumbnailMakerwhich creates thumbnails with the specified size.FixedSizeThumbnailMaker(int width, int height, boolean aspectRatio)Creates aFixedSizeThumbnailMakerwhich creates thumbnails with the specified size.FixedSizeThumbnailMaker(int width, int height, boolean aspectRatio, boolean fit)Creates aFixedSizeThumbnailMakerwhich creates thumbnails with the specified size.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description FixedSizeThumbnailMakerfitWithinDimensions(boolean fit)Sets whether or not the thumbnail should fit within the specified dimensions.FixedSizeThumbnailMakerkeepAspectRatio(boolean keep)Sets whether or not the thumbnail is to maintain the aspect ratio of the original image.java.awt.image.BufferedImagemake(java.awt.image.BufferedImage img)Makes a thumbnail.FixedSizeThumbnailMakersize(int width, int height)Sets the size of the thumbnail to produce.-
Methods inherited from class net.coobird.thumbnailator.makers.ThumbnailMaker
defaultImageType, defaultResizer, defaultResizerFactory, imageType, makeThumbnail, resizer, resizerFactory
-
-
-
-
Field Detail
-
PARAM_SIZE
private static final java.lang.String PARAM_SIZE
- See Also:
- Constant Field Values
-
PARAM_KEEP_RATIO
private static final java.lang.String PARAM_KEEP_RATIO
- See Also:
- Constant Field Values
-
PARAM_FIT_WITHIN
private static final java.lang.String PARAM_FIT_WITHIN
- See Also:
- Constant Field Values
-
width
private int width
-
height
private int height
-
keepRatio
private boolean keepRatio
-
fitWithinDimensions
private boolean fitWithinDimensions
-
-
Constructor Detail
-
FixedSizeThumbnailMaker
public FixedSizeThumbnailMaker()
Creates aFixedSizeThumbnailMaker.The size of the resulting thumbnail, and whether or not the aspect ratio of the original image should be maintained in the thumbnail must be set before this instance is able to produce thumbnails.
-
FixedSizeThumbnailMaker
public FixedSizeThumbnailMaker(int width, int height)Creates aFixedSizeThumbnailMakerwhich creates thumbnails with the specified size.Before this instance is able to produce thumbnails, whether or not the aspect ratio of the original image should be maintained in the thumbnail must be specified by calling the
keepAspectRatio(boolean)method.- Parameters:
width- The width of the thumbnail to produce.height- The height of the thumbnails to produce.
-
FixedSizeThumbnailMaker
public FixedSizeThumbnailMaker(int width, int height, boolean aspectRatio)Creates aFixedSizeThumbnailMakerwhich creates thumbnails with the specified size. Whether or not the aspect ratio of the original image should be preserved by the thumbnail is also specified at instantiation.- Parameters:
width- The width of the thumbnail to produce.height- The height of the thumbnails to produce.aspectRatio- Whether or not to maintain the aspect ratio in the thumbnail the same as the original image.If
trueis specified, then the thumbnail image will have the same aspect ratio as the original image.
-
FixedSizeThumbnailMaker
public FixedSizeThumbnailMaker(int width, int height, boolean aspectRatio, boolean fit)Creates aFixedSizeThumbnailMakerwhich creates thumbnails with the specified size. Whether or not the aspect ratio of the original image should be preserved by the thumbnail, and whether to fit the thumbnail within the given dimensions is also specified at instantiation.- Parameters:
width- The width of the thumbnail to produce.height- The height of the thumbnails to produce.aspectRatio- Whether or not to maintain the aspect ratio in the thumbnail the same as the original image.If
trueis specified, then the thumbnail image will have the same aspect ratio as the original image.fit- Whether or not to fit the thumbnail within the specified dimensions.If
trueis specified, then the thumbnail will be sized to fit within the specifiedwidthandheight.
-
-
Method Detail
-
size
public FixedSizeThumbnailMaker size(int width, int height)
Sets the size of the thumbnail to produce.- Parameters:
width- The width of the thumbnail to produce.height- The height of the thumbnails to produce.- Returns:
- A reference to this object.
- Throws:
java.lang.IllegalStateException- If the size has already been previously set, or if thewidthorheightis less than or equal to zero.
-
keepAspectRatio
public FixedSizeThumbnailMaker keepAspectRatio(boolean keep)
Sets whether or not the thumbnail is to maintain the aspect ratio of the original image.- Parameters:
keep- Whether or not to maintain the aspect ratio in the thumbnail the same as the original image.If
trueis specified, then the thumbnail image will have the same aspect ratio as the original image.- Returns:
- A reference to this object.
- Throws:
java.lang.IllegalStateException- If whether to keep the aspect ratio has already been previously set.
-
fitWithinDimensions
public FixedSizeThumbnailMaker fitWithinDimensions(boolean fit)
Sets whether or not the thumbnail should fit within the specified dimensions.When the dimensions of a thumbnail will exceed the specified dimensions, with the aspect ratio of the original being preserved, then if this method was called with
false, then the resulting thumbnail will have the larger dimension align with the specified dimension, and the other will exceed the given dimension.When
keepAspectRatio(boolean)isfalse, then calling this method withtrueorfalsemakes no difference, as the thumbnail dimensions will be exactly the given dimensions.- Parameters:
fit- Whether or not to maintain the aspect ratio in the thumbnail the same as the original image.If
trueis specified, then the thumbnail image will have the same aspect ratio as the original image.- Returns:
- A reference to this object.
- Throws:
java.lang.IllegalStateException- If whether to keep the aspect ratio has already been previously set.- Since:
- 0.4.0
-
make
public java.awt.image.BufferedImage make(java.awt.image.BufferedImage img)
Description copied from class:ThumbnailMakerMakes a thumbnail.- Specified by:
makein classThumbnailMaker- Parameters:
img- The source image.- Returns:
- The thumbnail created from the source image, using the
parameters set by the
ThumbnailMaker.
-
-