coobird / thumbnailator

Thumbnailator - a thumbnail generation library for Java
MIT License
5.08k stars 780 forks source link

webp format image campress fail with exception "No suitable ImageReader found for source data" #220

Closed rickyzhuang closed 2 months ago

rickyzhuang commented 2 months ago

Expected behavior

support webp format image campress

Actual behavior

transfer webp to bytes call the method Thumbnails.of(new ByteArrayInputStream(data)).scale(scale).outputQuality(getScale(kbSize)).toOutputStream(baos); fail and throw exception "No suitable ImageReader found for source data"

Steps to reproduce the behavior

1、webp image to bytes
2、 call the following method private static byte[] compress(byte[] data, double scale, double kbSize) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); Thumbnails.of(new ByteArrayInputStream(data)).scale(scale).outputQuality(getScale(kbSize)).toOutputStream(baos); baos.close(); return baos.toByteArray(); } 3、throw the exception "No suitable ImageReader found for source data."

Environment

Please provide vendor and version information for the Operating System, JDK, and Thumbnailator. Please feel free to add any other information which may be pertinent.

coobird commented 2 months ago

@rickyzhuang, Thumbnailator relies on Image I/O plugins available at runtime for reading and writing images. The default Java environment doesn't have support for WebP, so Thumbnailator cannot handle them.

If you want to handle other image formats, you'll need to add the appropriate Image I/O plugins to support them. For example, if you need WebP support, TwelveMonkeys ImageIO has read support for them. You'll basically just need to add a Maven dependency to the WebP plugin ("imageio-webp") and that should be all you'll need to do.

For more information, please refer to the FAQ on this topic.