Limit uploaded image file dimensions

J

javadev

I use DiskFileUpload to upload images on my web application and am able
to restrict the file size to a defined value. Is there a way I can
restrict the dimensions(width and height) of the image uploaded using
DiskFileUpload or through some other way?

Thanks
Swetha
 
A

Andrew Thompson

javadev said:
I use DiskFileUpload to upload images on my web application and am able
to restrict the file size to a defined value. Is there a way I can
restrict the dimensions(width and height) of the image uploaded using
DiskFileUpload

Not according to the JavaDocs I found on Google,
are you looking at different ones?
...or through some other way?

Load an applet that can itself load the image types
of interest (GIF, PNG, JPEG in J2SE, more flavors
in JAI) and report the dimensions.

Andrew T.
 
J

javadev

Andrew said:
Load an applet that can itself load the image types
of interest (GIF, PNG, JPEG in J2SE, more flavors
in JAI) and report the dimensions.

Ok, I'll try doing that. Thanks.

Swetha
 
S

Simon Brooke

javadev said:
I use DiskFileUpload to upload images on my web application and am able
to restrict the file size to a defined value. Is there a way I can
restrict the dimensions(width and height) of the image uploaded using
DiskFileUpload or through some other way?

You can resize the image after it has arrived. If you need code mail me -
it's too long to post here.
 
A

Andrew Thompson

Simon said:
.....
You can resize the image after it has arrived.

Good idea. Saves having any Java dependency on the
client side, which is especially useful if you need to
use JAI for a wider image format coverage.
...If you need code mail me -
it's too long to post here.

How long is 'too long'?
This (simple) example achieves it in a handful* of lines..
<http://schmidt.devlib.org/java/save-jpeg-thumbnail.html>

* OK - 58 lines.

Andrew T.
 
D

Daniel Pitts

javadev said:
I use DiskFileUpload to upload images on my web application and am able
to restrict the file size to a defined value. Is there a way I can
restrict the dimensions(width and height) of the image uploaded using
DiskFileUpload or through some other way?

Thanks
Swetha

You want to check the image size AFTER the upload. There isn't much you
can *easily* do on the client side. You should always check
constraints on the server side anyway, as users with neferious purposes
can often bypass client side constraints checking. There used to be a
bug in ICQ that if the client sent too long of a password, the server
wouldn't know what to do and would always let the client log in, even
if the password wasn't even close to what it should have been.
 
S

Simon Brooke

Andrew said:
Good idea. Saves having any Java dependency on the
client side, which is especially useful if you need to
use JAI for a wider image format coverage.


How long is 'too long'?

In this case 446 lines, although that's the whole
ImageFileMaybeUploadWidget; the bit that does on-the-fly thumbnailing is
much smaller:

//~ Inner Classes -------------------------------------------------

/**
* Separate out thumbnail generation into a separate class. The thumbnail
* generator should generate thumbnails if the appropriate code is
* present and should not break horribly if it is not. Note that
* thumbnail generation does not work yet
*/
class ThumbnailPainter
{
//~ Instance fields ---------------------------------------------

/** can we generate? */
private boolean canGenerate = true;

//~ Methods -----------------------------------------------------

/**
* @return Returns the canGenerate.
*/
public boolean getCanGenerate( )
{
return canGenerate;
}

/**
* maybe generate a thumbnail for this source. Why maybe? Well, of
* course, if the appropriate library isn't available we can't.
*
* @param context the service context in which this generation takes
* place
* @param the name of the value being processed
* @param source the source file from which to generate the thumbnail
* @param prefix the string to prepend to the name of the source file
* to construct the name of the thumbnail file
* @param maxWidth the maximum width of the thumbnail image
* @param maxHeight the maximum height of the thumbnail image
*
* @exception IOException if it busts.
*/
protected File maybeGenerate( Context context, String name,
File source, String prefix, int maxWidth, int maxHeight )
throws IOException
{
File result = null;

if ( canGenerate && ( source != null ) )
{
String thumbName =
source.getParent( ) + File.separator + prefix +
source.getName( );

result = new File( thumbName );

if ( !result.exists( ) )
{
BufferedImage image = ImageIO.read( source );

ImageObserver observer = new Canvas( );

int height = image.getHeight( observer );
int width = image.getWidth( observer );

context.put( name +
ImageFileMaybeUploadWidget.IMAGEHEIGHTSUFFIX, height );
context.put( name +
ImageFileMaybeUploadWidget.IMAGEWIDTHSUFFIX, width );

if ( height > width )
{
width =
(int) ( (float) maxWidth * (float) ( (float) width / (float)
height ) );
height = maxHeight;
}
else
{
height =
(int) ( (float) maxHeight * (float) ( (float) height /
(float) width ) );
width = maxWidth;
}

context.put( name +
ImageFileMaybeUploadWidget.THUMBHEIGHTSUFFIX, height );
context.put( name +
ImageFileMaybeUploadWidget.THUMBWIDTHSUFFIX, width );

Image thumb =
image.getScaledInstance( width, height,
Image.SCALE_SMOOTH );

BufferedImage buff =
new BufferedImage( width, height,
BufferedImage.TYPE_INT_BGR );
buff.createGraphics( ).drawImage( thumb, 0, 0, observer );

System.err.println( "Writing thumbnail to " + thumbName );

if ( thumbName.toLowerCase().endsWith( ".gif" ) )
{
ImageIO.write( buff, "GIF", result );
}
else if ( thumbName.toLowerCase().endsWith( ".jpg" ) )
{
ImageIO.write( buff, "JPEG", result );
}
else
{
throw new IOException(
"Can't create a thumbnail from " +
source.getName( ) );
}
}
}

return result;
}
}
 
A

Andrew Thompson

Simon said:
In this case 446 lines, although that's the whole
ImageFileMaybeUploadWidget;

Yes, that would be a bit much, but..
...the bit that does on-the-fly thumbnailing is
much smaller:

//~ Inner Classes -------------------------------------------------
(snip)

...That amount of code is quite manageable.

Thanks for sharing your code - in a way that
can be archived and found via the web. :)

Andrew T.
 
S

Simon Brooke

Andrew said:
Yes, that would be a bit much, but..

(snip)

..That amount of code is quite manageable.

Thanks for sharing your code - in a way that
can be archived and found via the web. :)

Most of my code is on sourceforge, anyway - although that isn't. But it is
open source and freely downloadable.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top