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
Solved

Resizing an uploaded image on the server

Posted on 2004-08-16
13
170 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

856 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