Solved

Resizing an uploaded image on the server

Posted on 2004-08-16
13
167 Views
Last Modified: 2010-03-31
Hi,

I'm developing a Java server app using Struts, JSP etc that has a requirement whereby users can upload images on to the server. I've implemented this part successfully. However there is a constraint that is currently put in the logic to prevent large images (> 50K) from being uploaded - for obvious performance/storage reasons. I'd like to remove this constraint by being able to resize the image immediately after it gets uploaded, so that the users are free to upload any sized images. I'm not familiar with Java imaging API's, so could you suggest the best way to go about this?

Thanks!
0
Comment
Question by:sonythom
  • 7
  • 4
  • 2
13 Comments
 
LVL 92

Expert Comment

by:objects
Comment Utility
int w = i.getWidth(null);
int h = i.getHeight(null);
double ratio = (double) W / (double) w;
AffineTransform scale = AffineTransform.getScaleInstance(ratio, ratio);
BufferedImage scaled = new BufferedImage(W, (int)(h*ratio), BufferedImage.TYPE_INT_RGB);
Graphics2D g = scaled.createGraphics();
g.setTransform(scale);
g.drawImage(i, 0, 0, null);

0
 
LVL 92

Expert Comment

by:objects
Comment Utility
Then to encode the image use:

ImageIO.write(scaled, format, outstream);
0
 
LVL 35

Assisted Solution

by:girionis
girionis earned 100 total points
Comment Utility
If you have the image instance you might as well do (if "oldImage" is the instance of your image that is uploaded):

Image newImage = oldImage.getScaledInstance(100 ,100 , Image.SCALE_DEFAULT);

where 100 and 100 is the new width and height of the image and Image.SCALE_DEFAULT is the default algorithm that will be used for scaling the image:

http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Image.html#getScaledInstance(int, int, int)
0
 

Author Comment

by:sonythom
Comment Utility
How do I create an Image object from a file on disk?
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
BufferedImage image = ImageIO.read(file);
0
 

Author Comment

by:sonythom
Comment Utility
I'm getting a 0KB file with the following code:

                                                     is = formFile.getInputStream();
                       os = new FileOutputStream(filename);

                                                      BufferedImage i = ImageIO.read(is);
                                                      if(i.getWidth() > 500) {
                        int w = i.getWidth(null);
                        int h = i.getHeight(null);
                        double ratio = (double) 500 / (double) w;
                        AffineTransform scale = AffineTransform.getScaleInstance(ratio, ratio);
                        BufferedImage scaled = new BufferedImage(500, (int)(h*ratio), BufferedImage.TYPE_INT_RGB);
                        Graphics2D g = scaled.createGraphics();
                        g.setTransform(scale);
                        g.drawImage(i, 0, 0, null);
                        
                        ImageIO.write(scaled, "GIF", os);
                                                        }

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 92

Expert Comment

by:objects
Comment Utility
ImageIO does not have a gif encoder, try jpg.
0
 

Author Comment

by:sonythom
Comment Utility
Ok, got it to work.

So, even if the original user uploaded image is in gif format, is it safe to encode it in jpg?

Moreover, is it ok to save the resulting image with a .gif extension?

This is how I tested it, and everything looks fine. But just wanted to see if there could be any hidden issues that I may not be aware of with htis approach.

Thanks

0
 
LVL 92

Accepted Solution

by:
objects earned 400 total points
Comment Utility
> So, even if the original user uploaded image is in gif format, is it safe to encode it in jpg?

Really up to your requirements. png is another option if you don't want to use a lossy format like jpeg

> Moreover, is it ok to save the resulting image with a .gif extension?

Not really, the extension should really match the file type.
0
 
LVL 35

Expert Comment

by:girionis
Comment Utility
:)
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
0
 

Author Comment

by:sonythom
Comment Utility
That URL seems to be down...
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
Bugger, been having ISP problems :(
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now