Backend Development 5 min read

Java PNG Image Compression Using OpenViewerFX and Thumbnailator: Pitfalls and Solutions

This article documents the challenges of compressing PNG images in Java, demonstrates why Thumbnailator can increase file size, and presents a practical solution using OpenViewerFX together with Thumbnailator, including code examples, Maven dependencies, and important implementation notes.

Rare Earth Juejin Tech Community
Rare Earth Juejin Tech Community
Rare Earth Juejin Tech Community
Java PNG Image Compression Using OpenViewerFX and Thumbnailator: Pitfalls and Solutions

The company needed to reorganize file‑upload handling and compress user‑uploaded images to save bandwidth, but most existing solutions only compress JPG files, leaving PNG compression largely unsupported in Java.

Using Google’s Thumbnails library initially seemed promising because it claims to handle both JPG and PNG, yet practical tests showed that PNG files became larger after compression (e.g., 47 KB grew to 80 KB), as reported in GitHub issue #77 .

Two alternative approaches were investigated: (1) the open‑source PDF rendering tool OpenViewerFX , which includes a TinyPNG‑like algorithm, and (2) java-png-compress-util that wraps the libimagequant library via JNI. The decision was to adopt OpenViewerFX for PNG compression.

Testing OpenViewerFX with the original expert22.png showed no size change, but a different image was successfully reduced to just over 100 KB, confirming the library’s effectiveness.

The implementation adds the following Maven dependencies:

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

<!-- https://mvnrepository.com/artifact/org.jpedal/OpenViewerFX -->
<dependency>
    <groupId>org.jpedal</groupId>
    <artifactId>OpenViewerFX</artifactId>
    <version>6.6.14</version>
</dependency>

Key compression logic is encapsulated in the compressFile method, which validates the file extension, reads the image, optionally resizes it, sets TYPE_INT_ARGB for PNG, applies Thumbnails processing, and finally invokes PngCompressor.compress from OpenViewerFX for PNG files.

Important notes include: (1) when using Thumbnails on PNG, BufferedImage.TYPE_INT_ARGB must be set to avoid black backgrounds; (2) animated PNGs (or GIFs) lose their animation after compression.

In conclusion, the article shares a concise, reproducible solution for Java‑based PNG compression, helping developers avoid common pitfalls and save time when selecting third‑party libraries.

BackendJavaimage compressionPNGopenviewerfxThumbnailator
Rare Earth Juejin Tech Community
Written by

Rare Earth Juejin Tech Community

Juejin, a tech community that helps developers grow.

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.