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.
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
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.20</version>
</dependency>2. Practical Examples
2.1 Create a Thumbnail
Thumbnails.of(new File("d:\\images\\7.png"))
.size(160, 160)
.toFile(new File("d:\\images\\output\\7.png"));Result:
2.2 Add Watermark
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"));Result:
2.3 Rotate Image
Thumbnails.of(new File("d:\\images\\7.png"))
.scale(1)
.rotate(180)
.toFile(new File("d:\\images\\output\\7_rotate.png"));Result:
2.4 Force Exact Size
Thumbnails.of(new File("d:\\images\\7.png"))
.size(200, 200)
.keepAspectRatio(false)
.toFile(new File("d:\\images\\output\\7_keepratio.png"));Result:
2.5 Scale by Factor
Thumbnails.of(new File("d:\\images\\7.png"))
.scale(0.25)
.toFile(new File("d:\\images\\output\\7_scale.png"));Result:
2.6 Batch Generation
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);Result:
2.7 Integration with Spring Boot
@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());
}Result:
The article concludes with a reminder to like, share, and follow for more Spring Boot tutorials.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
