Solved

Resizing an uploaded image on the server

Posted on 2004-08-16
13
169 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
ID: 11815723
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
ID: 11815739
Then to encode the image use:

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

Assisted Solution

by:girionis
girionis earned 100 total points
ID: 11818047
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

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

Expert Comment

by:objects
ID: 11826997
BufferedImage image = ImageIO.read(file);
0
 

Author Comment

by:sonythom
ID: 11827202
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
 
LVL 92

Expert Comment

by:objects
ID: 11827323
ImageIO does not have a gif encoder, try jpg.
0
 

Author Comment

by:sonythom
ID: 11827613
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
ID: 11827682
> 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
ID: 11830061
:)
0
 
LVL 92

Expert Comment

by:objects
ID: 11836602
0
 

Author Comment

by:sonythom
ID: 11836626
That URL seems to be down...
0
 
LVL 92

Expert Comment

by:objects
ID: 11836647
Bugger, been having ISP problems :(
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

777 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