File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes servlets and thumbnails Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlets and thumbnails" Watch "servlets and thumbnails" New topic

servlets and thumbnails

fahim chowdhury

Joined: Jul 14, 2002
Posts: 9
Let's say that I have large image files on my server. Does Java servlet technology have the capacity to send back, on the response object, just a thumbnail of a particular image? I thought about writing an HTML image tag that would determime smaller dimensions for the image, but that would actually send the entire image first before cropping it on the client side (so I am told). Any suggestions?
P.S. I just discovered your site and I think you guys are fabulous. Keep up the great work!
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63854

It is true that if you use the HTML IMG tag's height and width attibutes to create smaller versions of your image as a thumbnail that the entire image is downloaded to the client and then resized (not cropped) by the browser to the specified size for display.
Servlet and JSP technology would indeed give you the abililty to define your own custom "THUMBNAIL" tag and to write code that would resize the image on the server-side for shipping to the client.
Search this site and you will find quite a few threads on sending images as the response of a servlet request. As far as image manipulation on the server-side, perhaps the Java Advanced Imaging API could help you.

[Asking smart questions] [About Bear] [Books by Bear]
chanoch wiggers
Ranch Hand

Joined: May 24, 2001
Posts: 245
i've got some code at home to do this for jpeg and gif - I will post it tonite (GMT)

chanoch<p><a href="" target="_blank" rel="nofollow">Author of Professional Apache Tomcat</a></p>
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Welcome to the Java Ranch, we hope you�ll enjoy visiting as a regular however,
your name is not in keeping with our naming policy here at the ranch. Please change your display name to an appropriate name as shown in the policy.
Thanks again and we hope to see you around the ranch!!

chanoch wiggers
Ranch Hand

Joined: May 24, 2001
Posts: 245
I hope this gives you an idea of where to start. This is what I based my code on...(thanks richard huss)
import com.sun.image.codec.jpeg.*;
public class JPEGSizer extends AbstractSizer {
private JPEGDecodeParam params;
public JPEGSizer(String filename) {
try {
JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder
(new FileInputStream(filename));
params = decoder.getJPEGDecodeParam();
width = params.getWidth();
height = params.getHeight();
} catch (IOException e) {
width = 0;
height = 0;
public static void main(String args[]) throws Exception {
JPEGSizer sizer = new JPEGSizer(args[0]);
System.out.println("Width " + sizer.getWidth());
System.out.println("Height " + sizer.getHeight());
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import com.sun.image.codec.jpeg.*;
public class PhotoResizer {
private String oldFile, newFile;

public PhotoResizer(String oldFile, String newFile) {
this.oldFile = oldFile;
this.newFile = newFile;

public void writeScaledImage() {
try {
JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder
(new FileInputStream(oldFile));
BufferedImage originalImage = decoder.decodeAsBufferedImage();
JPEGDecodeParam params = decoder.getJPEGDecodeParam();
int width = params.getWidth();
int height = params.getHeight();
double scale = 100.0 / width;
ColorModel cm = originalImage.getColorModel();
AffineTransform transform = AffineTransform.getScaleInstance(scale,scale);
AffineTransformOp scaler =
new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);
BufferedImage scaledImage =
scaler.createCompatibleDestImage(originalImage, cm);
scaler.filter(originalImage, scaledImage);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder
(new FileOutputStream(newFile));
} catch (IOException e) {

public static void main(String args[]) {
new PhotoResizer("\\Documents and Settings\\richard\\My Documents\\TR Web Site\\images\\jpeg\\" + args[0] + ".jpg",
args[0] + "_sml.jpg").writeScaledImage();
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: servlets and thumbnails
It's not a secret anymore!