Generate Thumbnail Images in Java with Thumbnailator library

Tags: Java Thumbnail Thumbnailator

As a daily job of a web developer you usually have the scenario that you need to generate the thumbnail image from a large original image. This take can easily be done by the Thumbnailator library, an easy to use thumbnail generation library for Java. In this Java tutorial we are going to learn how to use the Thumbnailator library.

Thumbnailator home page: github.com/coobird/thumbnailator

Project Dependencies:

Adding below dependencies to build.gradle file if you are using Gradle build tool.

compile group: 'net.coobird', name: 'thumbnailator', version: '0.4.11'

Adding below XML to pom.xml file if you are using Maven build tool.

<dependency>
    <groupId>net.coobird</groupId>
    <artifactId>thumbnailator</artifactId>
    <version>0.4.11</version>
</dependency>

Create thumbnail image from an original image file

package dev.simplesolution.thumbnail;

import java.io.File;
import java.io.IOException;

import net.coobird.thumbnailator.Thumbnails;

public class CreateThumbnail {
	
	public static void main(String[] args) throws IOException {
		String originalFileName = "D:\\SimpleSolution\\test\\logo.png";
		String outputFileName = "D:\\SimpleSolution\\test\\logo_120_120.png";
		
		Thumbnails.of(new File(originalFileName))
			.size(120, 120)
			.toFile(new File(outputFileName));
	}

}

Source and output images:

Create thumbnail image from an original image file

Create thumbnail and rotate thumbnail image

package dev.simplesolution.thumbnail;

import java.io.File;
import java.io.IOException;

import net.coobird.thumbnailator.Thumbnails;

public class CreateThumbnailAndRotate {
	
	public static void main(String[] args) throws IOException {
		String originalFileName = "D:\\SimpleSolution\\test\\logo.png";
		String outputFileName = "D:\\SimpleSolution\\test\\logo_120_120_rotate.png";
		
		Thumbnails.of(new File(originalFileName))
			.size(120, 120)
			.rotate(90)
			.toFile(new File(outputFileName));
	}

}

Source and output images:

Create thumbnail and rotate thumbnail image

Scale an image

package dev.simplesolution.thumbnail;

import java.io.File;
import java.io.IOException;

import net.coobird.thumbnailator.Thumbnails;

public class ScaleImage {

	public static void main(String[] args) throws IOException {
		String originalFileName = "D:\\SimpleSolution\\test\\logo.png";
		String outputFileName = "D:\\SimpleSolution\\test\\logo_scale.png";
		
		Thumbnails.of(new File(originalFileName))
			.scale(0.1)
			.toFile(new File(outputFileName));
	}
}

Source and output images:

Scale an image

Create thumbnail image with a watermark

package dev.simplesolution.thumbnail;

import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;

public class ThumbnailAndWatermark {
	
	public static void main(String[] args) throws IOException {
		String originalFileName = "D:\\SimpleSolution\\test\\sample.png";
		String watermarkFileName = "D:\\SimpleSolution\\test\\watermark.png";
		String outputFileName = "D:\\SimpleSolution\\test\\thumbnail_watermark.png";
		
		Thumbnails.of(new File(originalFileName))
			.size(200, 200)
			.watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File(watermarkFileName)), 0.5f)
			.toFile(new File(outputFileName));
	}
}

Source and output images: Create thumbnail image with a watermark

Create thumbnail for multiple files

package dev.simplesolution.thumbnail;

import java.io.File;
import java.io.IOException;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.name.Rename;

public class CreateThumbnailMultipleFiles {
	
	public static void main(String[] args) throws IOException {
		String[] inputFiles = new String[] {
				"D:\\SimpleSolution\\test\\logo.png",
				"D:\\SimpleSolution\\test\\sample.png"
		};
		
		File outputDirectory = new File("D:\\SimpleSolution\\output");
		
		Thumbnails.of(inputFiles)
			.size(120, 120)
			.toFiles(outputDirectory, Rename.NO_CHANGE);
	}

}

Happy Coding 😊