Convert uploaded images from various formats to JPEG

2.6k views Asked by At

I am developing a website that allows the user to upload images. I would like to be able to accept BMP, GIF, JPEG and PNG (and maybe one or two others like TIFF) formats. I am using the Apache Commons FileUpload library to achieve this. In order that all the images end up in the same format, I would like to convert all image formats to JPEG format.

What is the easiest way to do this which will work will all of the above image formats?

I have tried:

  • ImageIO.read(), which gave javax.imageio.IIOException: Unexpected block type 0! for some GIF files and java.awt.color.CMMException: Invalid image format for JPEG files.
  • JAI, which gave a humongous error trace for GIF files.

Edit: Information about the JAI method:

Stack trace:

Error: One factory fails for the operation "gif"

Occurs in: javax.media.jai.ThreadSafeOperationRegistry

followed by literally hundreds of lines of trace.

Code:

public static void convertToJPG(String originalFile, String newFile) throws Exception {
    RenderedImage image = JAI.create("fileload", originalFile);
    JAI.create("filestore", image, newFile, "JPEG");
}

Image:

An image which doesn't work.

1

There are 1 answers

3
seandavi On BEST ANSWER

Take a look at ImageMagick, and the convert command, in particular.