Skip to content

Commit

Permalink
feat(screenshots): add support to width, height and clip image options
Browse files Browse the repository at this point in the history
  • Loading branch information
cherfia committed Jul 3, 2024
1 parent d3b3c6d commit 1469e6f
Showing 1 changed file with 61 additions and 1 deletion.
62 changes: 61 additions & 1 deletion src/main/java/dev/inaka/screenshots/ImageProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@ public final class ImageProperties {
private final String format;
private final String quality;
private final String omitBackground;
private final String width;
private final String height;
private final String clip;

private ImageProperties(Builder builder) {
format = builder.format;
quality = builder.quality;
omitBackground = builder.omitBackground;
width = builder.width;
height = builder.height;
clip = builder.clip;
}

/**
Expand All @@ -22,13 +28,28 @@ public static class Builder {
private String format = ImageFormat.PNG.format();
private String quality = null;
private String omitBackground = "false";
private String width = "800";
private String height = "600";
private String clip = "false";


/**
* Sets the image compression format to the ImageProperties being constructed.
*
* @param format The image compression format, either "png", "jpeg" or "webp".
* @return The Builder instance for method chaining.
*/
public Builder addFormat(String format) {
this.format = format;
return this;
}

/**
* Sets the image compression quality to the ImageProperties being constructed.
*
* @param quality The compression quality from range 0 to 100 (jpeg only).
* @return The Builder instance for method chaining.
* @throws IllegalArgumentException if the quality is not within the valid range (0 to 100).
*/
public Builder addQuality(Integer quality) {
if (quality < 0 || quality > 100) {
throw new IllegalArgumentException("Quality must be between 0 and 100");
Expand All @@ -37,11 +58,50 @@ public Builder addQuality(Integer quality) {
return this;
}

/**
* Sets whether to omit the default white background from the ImageProperties being constructed.
*
* @param omitBackground Hide the default white background and allow generating screenshots with transparency.
* @return The Builder instance for method chaining.
*/
public Builder addOmitBackground(boolean omitBackground) {
this.omitBackground = String.valueOf(omitBackground);
return this;
}

/**
* Sets the device screen width in pixels.
*
* @param width The width of the device screen.
* @return The Builder instance for method chaining.
*/
public Builder addWidth(Integer width) {
this.width = String.valueOf(width);
return this;
}

/**
* Sets the device screen height in pixels.
*
* @param height The height of the device screen.
* @return The Builder instance for method chaining.
*/
public Builder addHeight(Integer height) {
this.height = String.valueOf(height);
return this;
}

/**
* Sets whether to clip the screenshot according to the device dimensions.
*
* @param clip Whether to clip the image to the viewport.
* @return The Builder instance for method chaining.
*/
public Builder addClip(boolean clip) {
this.clip = String.valueOf(clip);
return this;
}

/**
* Builds an instance of ImageProperties with the specified configuration options.
*
Expand Down

0 comments on commit 1469e6f

Please sign in to comment.