Resizing an image with java

Posted: 13 March 2010 in Java, Teamsite
Tags: , , , , , , , , ,

We’re running website and we let users upload images to our CMS. Users being users, find their big images and upload them to display as tiny thumbnails. We, as web savy people, find that inacceptable as the image is not optimised for the web and resources are wasted both in bandwidth and client processing time. After all, we want the total page download size to be as small as possible. don’t blame the users, they don’t know any better. We do and that why we should attempt to resize the images when the users upload them to the CMS. so how is it done? With a little java class that will have a function like this one in it:

 
public BufferedImage imageResize(BufferedImage image, int imageWidth, int imageHeight, boolean fillRectangle, boolean keepAspectRatio, boolean upscale) {
float x = 0; float y = 0;
float rectangleX = image.getWidth();
float rectangleY = image.getHeight(); 
// our image is smaller than the image we're trying to achieve 
if (fillRectangle && keepAspectRatio && !upscale && (image.getWidth() < imageWidth || image.getHeight() < imageHeight)) {
    x = (imageWidth / 2f) - (image.getWidth() / 2f);
    y = (imageHeight / 2f) - (image.getHeight() / 2f);
    // this side is smaller than the image we're trying to go into
    if (image.getWidth() < imageWidth) {
        rectangleX = image.getWidth();
    }
    if (image.getHeight() < imageHeight) {
        rectangleY = image.getHeight();
    }

} else {
    float imageScale = (float) imageWidth / (float) image.getWidth(); 
    float scaledImageWidth=imageWidth; 
    float scaledImageHeight=imageHeight; 
    if (keepAspectRatio && !fillRectangle) { 
        // find which one of the two sides is the largest side and scale the other to match that 
        if (image.getWidth() > image.getHeight()) { 
            scaledImageHeight = (((float)image.getHeight()) * imageScale); 
        } else { 
            scaledImageWidth = (((float)image.getWidth()) * imageScale); 
        }
    } else if (keepAspectRatio && fillRectangle) {
        // we need to scale on the smaller side 
        if (image.getWidth() < image.getHeight()) { 
            scaledImageHeight = (((float)image.getHeight()) * imageScale);  
        } else { 
            scaledImageWidth = (((float)image.getWidth()) * imageScale); 
        } 
        // we need to crop a bit too 
        x = (imageWidth / 2f) - (scaledImageWidth / 2f); 
        y = (imageHeight / 2f) - (scaledImageHeight / 2f); 
    } 
    rectangleX = scaledImageWidth;
    rectangleY = scaledImageHeight; 
}

int type = (image.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB; // check if the image supports transparency  
BufferedImage resizedImage = new BufferedImage(imageWidth, imageHeight, type);
Graphics2D graphics2D = resizedImage.createGraphics();
graphics2D.setComposite(AlphaComposite.Src);  

if (image.getTransparency() == Transparency.OPAQUE){
    // paint a big white background by default, not a black one
    graphics2D.setColor(Color.white);
    graphics2D.fillRect(0, 0, imageWidth, imageHeight);
} else {
    // leave it as transparent by default
}

graphics2D.drawImage(image, (int) x, (int) y, (int) rectangleX, (int) rectangleY, null); // draw the original image on the new image, rescaling it at the same time
graphics2D.dispose(); // we're done drawing 
return (resizedImage);
}
Advertisements
Comments
  1. Cris B says:

    Great post, but I am curious about process used between user uploading images and managing those images in the CMS (TeamSite). I doubt if the CMS is exposed to the users, are you using OpenDeploy(reverse deployment?) to send the images to TS?

  2. I did a custom import functionality, which I suppose I could share too. I will write it up and post it afterwards. Like everything else, I just need MORE TIME!!!!!

  3. Chris,

    In was revisiting this post and found your comment. Sorry I missed it when you first posted it…

    I created a servlet that I compiled into teamsite as an extension. I am using a callout button in a DCT to invoke the servlet which gets passed a vpath for where the image gets uploaded.

    The servlet is nothing but an file uploader and you can get a few examples of these around the web. After the image is uploaded, the servlet closes the window and updates the DCT’s item’s browser value to the area relative path of the image.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s