views:

157

answers:

3

I have a bunch of images on a server. When client requests url for image, the client can specify to either receive the full image or get a thumbnail of the image. So, I'm looking to implement a servlet type solution that processes request, and generates thumbnail on demand if needed. It needs to support jpg, gif, tif, png.

It looks like using BufferedImage, JAI, and/or ImageMagick are the best options for java (from this post). Any others I might have missed?

Also, has anyone implemented something similar in java? If so, any suggestions on a solution that gives (1) fairly descent quality thumbnails, (2) doesn't hog a huge amount of memory when it processes the images, (3) acceptable response time?

+1  A: 

The core line of code for such a solution would be

img.getScaledInstance(w, h, Image.SCALE_DEFAULT);

Where, if you don't like the quality, you can use Image.SCALE_SMOOTH.

I've been using this (surronded by some extras, like replacing a color with a pattern), on a not-so-active site (~300 users per day), but one which serves many images, and this hasn't caused any problems, so you can freely use it.

Use ImageIO.write(image, formatName, response.getOutputStream()) for sending the image in the response.

Bozho
+4  A: 

The Perils of Image.getScaledInstance

camickr
A: 

Another solution is to do some systems call to a command line utility like non-java imagemagick and link output the generated file.

Phil
which is very developer-unfriendly
Bozho