Solved

Resizing an uploaded image on the server

Posted on 2004-08-16
13
171 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
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:

730 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