Skip to content

Add functionality to generate thumbnails with a fixed-size frame #218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.20</version>
<version>0.4.21</version>
<packaging>jar</packaging>
<name>thumbnailator</name>
<description>Thumbnailator - a thumbnail generation library for Java</description>
Expand Down
87 changes: 77 additions & 10 deletions src/main/java/net/coobird/thumbnailator/ThumbnailParameter.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@

package net.coobird.thumbnailator;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;

import net.coobird.thumbnailator.filters.ImageFilter;
import net.coobird.thumbnailator.resizers.FixedResizerFactory;
import net.coobird.thumbnailator.geometry.Region;
import net.coobird.thumbnailator.resizers.FixedResizerFactory;
import net.coobird.thumbnailator.resizers.Resizer;
import net.coobird.thumbnailator.resizers.ResizerFactory;

Expand Down Expand Up @@ -193,6 +194,16 @@ public class ThumbnailParameter {
* thumbnails.
*/
private final boolean useExifOrientation;

/**
* Indicated whether or not the thumbnail should be scaled to fit a fixed size (frame) background.
*/
private final boolean useFrame;

/**
* The background color of the frame, if thumbnail will be produced with frame.
*/
private final Color frameColor;

/**
* Private constructor which sets all the required fields, and performs
Expand Down Expand Up @@ -270,6 +281,9 @@ public class ThumbnailParameter {
* If {@code true} is specified, then the
* Exif metadata will be used to determine
* the orientation of the thumbnail.
* @param useFrame Whether or not, the thumbnail should be scaled to
* fit a fixed size (frame) background.
* @param frameColor The background color of the frame.
*
* @throws IllegalArgumentException If the scaling factor is not a
* rational number or is less than or
Expand All @@ -289,7 +303,9 @@ private ThumbnailParameter(
List<ImageFilter> filters,
ResizerFactory resizerFactory,
boolean fitWithinDimensions,
boolean useExifOrientation
boolean useExifOrientation,
boolean useFrame,
Color frameColor
) {
// The following 2 fields are set by the public constructors.
this.thumbnailSize = thumbnailSize;
Expand Down Expand Up @@ -332,6 +348,8 @@ private ThumbnailParameter(
this.resizerFactory = resizerFactory;
this.fitWithinDimensions = fitWithinDimensions;
this.useExifOrientation = useExifOrientation;
this.useFrame = useFrame;
this.frameColor = frameColor;
}

/**
Expand Down Expand Up @@ -428,6 +446,9 @@ private void validateScalingFactor() {
* If {@code true} is specified, then the
* Exif metadata will be used to determine
* the orientation of the thumbnail.
* @param useFrame Whether or not, the thumbnail should be scaled to
* fit a fixed size (frame) background.
* @param frameColor The background color of the frame.
*
* @throws IllegalArgumentException If size is {@code null} or if the
* dimensions are negative, or if the
Expand All @@ -445,7 +466,9 @@ public ThumbnailParameter(
List<ImageFilter> filters,
Resizer resizer,
boolean fitWithinDimensions,
boolean useExifOrientation
boolean useExifOrientation,
boolean useFrame,
Color frameColor
) {
this(
thumbnailSize,
Expand All @@ -460,7 +483,9 @@ public ThumbnailParameter(
filters,
new FixedResizerFactory(resizer),
fitWithinDimensions,
useExifOrientation
useExifOrientation,
useFrame,
frameColor
);

validateThumbnailSize();
Expand Down Expand Up @@ -538,6 +563,9 @@ public ThumbnailParameter(
* If {@code true} is specified, then the
* Exif metadata will be used to determine
* the orientation of the thumbnail.
* @param useFrame Whether or not, the thumbnail should be scaled to
* fit a fixed size (frame) background.
* @param frameColor The background color of the frame.
*
* @throws IllegalArgumentException If the scaling factor is not a
* rational number or is less than or
Expand All @@ -557,7 +585,9 @@ public ThumbnailParameter(
List<ImageFilter> filters,
Resizer resizer,
boolean fitWithinDimensions,
boolean useExifOrientation
boolean useExifOrientation,
boolean useFrame,
Color frameColor
) {
this(
null,
Expand All @@ -572,7 +602,9 @@ public ThumbnailParameter(
filters,
new FixedResizerFactory(resizer),
fitWithinDimensions,
useExifOrientation
useExifOrientation,
useFrame,
frameColor
);

validateScalingFactor();
Expand Down Expand Up @@ -646,6 +678,9 @@ public ThumbnailParameter(
* If {@code true} is specified, then the
* Exif metadata will be used to determine
* the orientation of the thumbnail.
* @param useFrame Whether or not, the thumbnail should be scaled to
* fit a fixed size (frame) background.
* @param frameColor The background color of the frame.
*
* @throws IllegalArgumentException If size is {@code null} or if the
* dimensions are negative, or if the
Expand All @@ -663,7 +698,9 @@ public ThumbnailParameter(
List<ImageFilter> filters,
ResizerFactory resizerFactory,
boolean fitWithinDimensions,
boolean useExifOrientation
boolean useExifOrientation,
boolean useFrame,
Color frameColor
) {
this(
thumbnailSize,
Expand All @@ -678,7 +715,9 @@ public ThumbnailParameter(
filters,
resizerFactory,
fitWithinDimensions,
useExifOrientation
useExifOrientation,
useFrame,
frameColor
);

validateThumbnailSize();
Expand Down Expand Up @@ -757,6 +796,9 @@ public ThumbnailParameter(
* If {@code true} is specified, then the
* Exif metadata will be used to determine
* the orientation of the thumbnail.
* @param useFrame Whether or not, the thumbnail should be scaled to
* fit a fixed size (frame) background.
* @param frameColor The background color of the frame.
*
* @throws IllegalArgumentException If the scaling factor is not a
* rational number or is less than or
Expand All @@ -776,7 +818,9 @@ public ThumbnailParameter(
List<ImageFilter> filters,
ResizerFactory resizerFactory,
boolean fitWithinDimensions,
boolean useExifOrientation
boolean useExifOrientation,
boolean useFrame,
Color frameColor
) {
this(
null,
Expand All @@ -791,7 +835,9 @@ public ThumbnailParameter(
filters,
resizerFactory,
fitWithinDimensions,
useExifOrientation
useExifOrientation,
useFrame,
frameColor
);

validateScalingFactor();
Expand Down Expand Up @@ -993,4 +1039,25 @@ public boolean fitWithinDimenions() {
public boolean useExifOrientation() {
return useExifOrientation;
}


/**
* Whether or not, the thumbnail should be scaled to fit a fixed size (frame) background.
*
* @return {@code true} is returned when thumbnail should be generated in a frame.
* @since 0.4.21
*/
public boolean useFrame() {
return useFrame;
}

/**
* The background color of the frame
*
* @return The background color of the frame, if thumbnail should be generated in a frame
* @since 0.4.21
*/
public Color frameColor() {
return frameColor;
}
}
20 changes: 18 additions & 2 deletions src/main/java/net/coobird/thumbnailator/Thumbnailator.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import net.coobird.thumbnailator.filters.Pipeline;
import net.coobird.thumbnailator.filters.SwapDimensions;
import net.coobird.thumbnailator.makers.FixedSizeThumbnailMaker;
import net.coobird.thumbnailator.makers.FramedThumbnailMaker;
import net.coobird.thumbnailator.makers.ScaledThumbnailMaker;
import net.coobird.thumbnailator.name.Rename;
import net.coobird.thumbnailator.resizers.DefaultResizerFactory;
Expand Down Expand Up @@ -101,8 +102,23 @@ public static void createThumbnail(ThumbnailTask<?, ?> task) throws IOException
boolean isSwapDimensions = hasSwapDimensionsFilter(param.getImageFilters());

BufferedImage destinationImage;

if (param.getSize() != null) {

if (param.useFrame()) {
// Get the dimensions of the original and thumbnail images.
int destinationWidth = param.getSize().width;
int destinationHeight = param.getSize().height;

// Create the thumbnail.
destinationImage =
new FramedThumbnailMaker(destinationWidth, destinationHeight)
.keepAspectRatio(param.isKeepAspectRatio())
.fitWithinDimensions(param.fitWithinDimenions())
.frameColor(param.frameColor())
.imageType(imageType)
.resizerFactory(param.getResizerFactory())
.make(sourceImage);

} else if (param.getSize() != null) {
// Get the dimensions of the original and thumbnail images.
Dimension size = param.getSize();
int destinationWidth = !isSwapDimensions ? size.width : size.height;
Expand Down
36 changes: 34 additions & 2 deletions src/main/java/net/coobird/thumbnailator/Thumbnails.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

package net.coobird.thumbnailator;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.RenderingHints;
Expand Down Expand Up @@ -691,6 +692,8 @@ private static enum Properties implements Property {
ALLOW_OVERWRITE("allowOverwrite"),
CROP("crop"),
USE_EXIF_ORIENTATION("useExifOrientation"),
USE_FRAME("useFrame"),
FRAME_COLOR("frameColor")
;

private final String name;
Expand Down Expand Up @@ -733,6 +736,8 @@ public String getName() {
statusMap.put(Properties.ALLOW_OVERWRITE, Status.OPTIONAL);
statusMap.put(Properties.CROP, Status.OPTIONAL);
statusMap.put(Properties.USE_EXIF_ORIENTATION, Status.OPTIONAL);
statusMap.put(Properties.USE_FRAME, Status.OPTIONAL);
statusMap.put(Properties.FRAME_COLOR, Status.OPTIONAL);
}

/**
Expand Down Expand Up @@ -800,6 +805,10 @@ private void updateStatus(Properties property, Status newStatus) {
private boolean fitWithinDimenions = true;

private boolean useExifOrientation = true;

private boolean useFrame = false;

private Color frameColor = Color.WHITE;

/**
* This field should be set to the {@link Position} to be used for
Expand Down Expand Up @@ -1702,6 +1711,25 @@ public Builder<T> useExifOrientation(boolean useExifOrientation) {
this.useExifOrientation = useExifOrientation;
return this;
}

public Builder<T> frame(int width, int height) {
updateStatus(Properties.SIZE, Status.ALREADY_SET);
updateStatus(Properties.SCALE, Status.CANNOT_SET);
updateStatus(Properties.USE_FRAME, Status.ALREADY_SET);

validateDimensions(width, height);
this.width = width;
this.height = height;
this.useFrame = true;
return this;
}

public Builder<T> frameColor(Color color) {
updateStatus(Properties.FRAME_COLOR, Status.ALREADY_SET);

this.frameColor = color;
return this;
}

/**
* Indicates that the output format should be determined from the
Expand Down Expand Up @@ -2134,7 +2162,9 @@ private ThumbnailParameter makeParam() {
filterPipeline.getFilters(),
resizerFactory,
fitWithinDimenions,
useExifOrientation
useExifOrientation,
useFrame,
frameColor
);

} else {
Expand All @@ -2151,7 +2181,9 @@ private ThumbnailParameter makeParam() {
filterPipeline.getFilters(),
resizerFactory,
fitWithinDimenions,
useExifOrientation
useExifOrientation,
useFrame,
frameColor
);
}
}
Expand Down
Loading