Skip to content
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
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Unreleased

* Added `Format` constants for the media types `convert` accepts, including JPEG XL (`image/jxl`).

## 1.8.9
* Upgraded dependencies (okio 3.12.0, gson 2.13.1, commons-codec 1.21.0)
* Upgraded all Maven plugins to latest stable versions
Expand Down
45 changes: 45 additions & 0 deletions src/main/java/com/tinify/Format.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.tinify;

/**
* Media types that images can be converted to.
*
* <p>Use these constants with {@link Source#convert(Options)}:
*
* <pre>{@code
* Tinify.fromFile("input.png")
* .convert(new Options().with("type", Format.JXL))
* .toFile("output.jxl");
* }</pre>
*
* <p>Multiple types may be supplied, in which case the API returns the
* smallest result:
*
* <pre>{@code
* new Options().with("type", new String[]{Format.JXL, Format.WEBP});
* }</pre>
*/
public final class Format {
/** WebP, {@code image/webp}. */
public static final String WEBP = "image/webp";

/** PNG, {@code image/png}. */
public static final String PNG = "image/png";

/** JPEG, {@code image/jpeg}. */
public static final String JPEG = "image/jpeg";

/** JPEG, {@code image/jpg}. An alias of {@link #JPEG}. */
public static final String JPG = "image/jpg";

/** AVIF, {@code image/avif}. */
public static final String AVIF = "image/avif";

/** JPEG XL, {@code image/jxl}. */
public static final String JXL = "image/jxl";

/** Wildcard, {@code &#42;/&#42;}. Returns the smallest of the supported types. */
public static final String ANY = "*/*";

private Format() {
}
}
34 changes: 34 additions & 0 deletions src/test/java/com/tinify/FormatTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.tinify;

import org.junit.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.equalTo;

public class FormatTest {
@Test
public void formatShouldExposeSupportedMediaTypes() {
assertThat(Format.WEBP, is(equalTo("image/webp")));
assertThat(Format.PNG, is(equalTo("image/png")));
assertThat(Format.JPEG, is(equalTo("image/jpeg")));
assertThat(Format.JPG, is(equalTo("image/jpg")));
assertThat(Format.AVIF, is(equalTo("image/avif")));
assertThat(Format.JXL, is(equalTo("image/jxl")));
assertThat(Format.ANY, is(equalTo("*/*")));
}

@Test
public void convertWithFormatShouldSerializeMediaType() {
Options options = new Options().with("convert", new Options().with("type", Format.JXL));
assertThat(options.toJson(), is(equalTo("{\"convert\":{\"type\":\"image/jxl\"}}")));
}

@Test
public void convertWithMultipleFormatsShouldSerializeMediaTypes() {
Options options = new Options().with("convert",
new Options().with("type", new String[]{Format.JXL, Format.WEBP}));
assertThat(options.toJson(),
is(equalTo("{\"convert\":{\"type\":[\"image/jxl\",\"image/webp\"]}}")));
}
}
Loading