Backend Development 7 min read

Master Image Processing in Spring Boot 3 with Thumbnailator: From Basics to Advanced

This article explains how to handle image resizing, watermarking, rotation, and batch processing in Java using the Thumbnailator library, provides Maven setup, detailed code examples, and demonstrates seamless integration with Spring Boot for uploading and serving processed images.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Master Image Processing in Spring Boot 3 with Thumbnailator: From Basics to Advanced

In web development, processing user‑uploaded images is essential; common requirements include resizing, adding watermarks, rotating, and batch generation. This article introduces Thumbnailator , a fluent Java library that simplifies high‑quality thumbnail creation.

1. Maven Dependency

<code><dependency>
  <groupId>net.coobird</groupId>
  <artifactId>thumbnailator</artifactId>
  <version>0.4.20</version>
</dependency></code>

2. Practical Examples

2.1 Create a Thumbnail

<code>Thumbnails.of(new File("d:\\images\\7.png"))
  .size(160, 160)
  .toFile(new File("d:\\images\\output\\7.png"));</code>

Result:

2.2 Add Watermark

<code>Thumbnails.of(new File("d:\\images\\7.png"))
  .size(200, 200)
  .watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File("d:\\images\\water.png")), 0.5f)
  .outputQuality(0.8)
  .toFile(new File("d:\\images\\output\\7_watermark.png"));</code>

Result:

2.3 Rotate Image

<code>Thumbnails.of(new File("d:\\images\\7.png"))
  .scale(1)
  .rotate(180)
  .toFile(new File("d:\\images\\output\\7_rotate.png"));</code>

Result:

2.4 Force Exact Size

<code>Thumbnails.of(new File("d:\\images\\7.png"))
  .size(200, 200)
  .keepAspectRatio(false)
  .toFile(new File("d:\\images\\output\\7_keepratio.png"));</code>

Result:

2.5 Scale by Factor

<code>Thumbnails.of(new File("d:\\images\\7.png"))
  .scale(0.25)
  .toFile(new File("d:\\images\\output\\7_scale.png"));</code>

Result:

2.6 Batch Generation

<code>File destinationDir = new File("d:\\images\\output");
Thumbnails.of("d:\\images\\1.png", "d:\\images\\2.png", "d:\\images\\3.png")
  .size(200, 200)
  .toFiles(destinationDir, Rename.PREFIX_DOT_THUMBNAIL);</code>

Result:

2.7 Integration with Spring Boot

<code>@PostMapping("/upload")
public void upload(MultipartFile file, HttpServletResponse response) throws Exception {
  InputStream is = file.getInputStream();
  BufferedImage bi = Thumbnails.of(is)
    .size(200, 200)
    .asBufferedImage();
  response.setContentType("image/png");
  response.setHeader("Content-Disposition", "inline; filename=image.png");
  ImageIO.write(bi, "png", response.getOutputStream());
}</code>

Result:

The article concludes with a reminder to like, share, and follow for more Spring Boot tutorials.

backendJavaimage processingSpring BootThumbnailator
Spring Full-Stack Practical Cases
Written by

Spring Full-Stack Practical Cases

Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.