A Java library that mimics CSS object-fit
image scaling behavior
A Java AWT based image scaling library that is inspired by CSS object-fit
scaling modes. This library allows to scale images in cover
, contain
and fill
modes, where cover
mode is the noteworthy feature.
Additionally, this library automatically handles image rotation from JPEG Exif data if available.
object-fit is well suited for thumbnail generation but works just as well with larger images.
libraryDependencies += "io.taig" % "object-fit" % "x.y.z"
<dependency>
<groupId>io.taig</groupId>
<artifactId>object-fit</artifactId>
<version>x.y.z</version>
</dependency>
implementation("io.taig:object-fit:x.y.z")
import io.taig.object.fit.ObjectFit;
import java.io.File;
InputStream input = ...
final ObjectFit.Options = ObjectFit.options()
.mode(ObjectFit.Mode.COVER)
.size(500)
.format("jpg");
ObjectFit.toBufferedImage(input, options);
ObjectFit.write(input, options, ...);
ObjectFit.options()
.mode(ObjectFit.Mode.COVER)
.size(250, 150)
.format("webp")
ObjectFit.options()
.mode(ObjectFit.Mode.COVER)
.size(250, 150)
.format("webp")
.scaleUp()
Input |
Output |
|
|
ObjectFit.options()
.mode(ObjectFit.Mode.FILL)
.size(250, 250)
.format("png")
ObjectFit.options()
.mode(ObjectFit.Mode.FILL)
.size(250, 250)
.format("png")
.scaleUp()
Input |
Output |
|
|
ObjectFit.options()
.mode(ObjectFit.Mode.CONTAIN)
.size(250, 250)
.format("jpg")
ObjectFit.options()
.mode(ObjectFit.Mode.CONTAIN)
.size(400, 500)
.format("jpg")
.scaleUp()