?
Solved

Resizing an uploaded image on the server

Posted on 2004-08-16
13
Medium Priority
?
175 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 300 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

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 1200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month13 days, 15 hours left to enroll

800 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