taig / object-fit   0.1.1

MIT License GitHub

A Java library that mimics CSS object-fit image scaling behavior

Scala versions: 3.x

Cover

A Java library that mimics CSS object-fit image scaling behavior

Maven Central

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.

Installation

sbt

libraryDependencies += "io.taig" % "object-fit" % "x.y.z"

Maven

<dependency>
  <groupId>io.taig</groupId>
  <artifactId>object-fit</artifactId>
  <version>x.y.z</version>
</dependency>

Gradle

implementation("io.taig:object-fit:x.y.z")

Usage

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, ...);

Samples

Cover

ObjectFit.options()
  .mode(ObjectFit.Mode.COVER)
  .size(250, 150)
  .format("webp")
Input Output

ObjectFit.options()
  .mode(ObjectFit.Mode.COVER)
  .size(250, 150)
  .format("webp")
  .scaleUp()
Input Output

Fill

ObjectFit.options()
  .mode(ObjectFit.Mode.FILL)
  .size(250, 250)
  .format("png")
Input Output

ObjectFit.options()
  .mode(ObjectFit.Mode.FILL)
  .size(250, 250)
  .format("png")
  .scaleUp()
Input Output

Contain

ObjectFit.options()
  .mode(ObjectFit.Mode.CONTAIN)
  .size(250, 250)
  .format("jpg")
Input Output

ObjectFit.options()
  .mode(ObjectFit.Mode.CONTAIN)
  .size(400, 500)
  .format("jpg")
  .scaleUp()
Input Output