Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

Limit uploaded image file dimensions

Thread view: 
javadev - 07 Nov 2006 11:50 GMT
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
Andrew Thompson - 07 Nov 2006 11:59 GMT
> 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.
javadev - 07 Nov 2006 12:05 GMT
> 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
Simon Brooke - 07 Nov 2006 13:45 GMT
> 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.

Signature

simon@jasmine.org.uk (Simon Brooke) http://www.jasmine.org.uk/~simon/

       Hobbit ringleader gives Sauron One in the Eye.

Andrew Thompson - 07 Nov 2006 15:11 GMT
> > ....Is there a way I can
> > restrict the dimensions(width and height) of the image uploaded...
....
> 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.
Simon Brooke - 08 Nov 2006 09:43 GMT
>> > ....Is there a way I can
>> > restrict the dimensions(width and height) of the image uploaded...
[quoted text clipped - 9 lines]
>
> 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;
   }
 }

Signature

simon@jasmine.org.uk (Simon Brooke) http://www.jasmine.org.uk/~simon/

       IMHO, there aren't enough committed Christians, but that's care
       in the community for you.                          -- Ben Evans

Andrew Thompson - 08 Nov 2006 13:13 GMT
> >> > ....Is there a way I can
> >> > restrict the dimensions(width and height) of the image uploaded...
> > ....
> >> You can resize the image after it has arrived.
...
> >>...If you need code mail me -
> >> it's too long to post here.
[quoted text clipped - 3 lines]
> 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.
Simon Brooke - 08 Nov 2006 18:16 GMT
>> >> > ....Is there a way I can
>> >> > restrict the dimensions(width and height) of the image uploaded...
[quoted text clipped - 21 lines]
> 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.

Signature

simon@jasmine.org.uk (Simon Brooke) http://www.jasmine.org.uk/~simon/

               [ This .sig intentionally left blank ]

Daniel Pitts - 07 Nov 2006 19:54 GMT
> 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
[quoted text clipped - 3 lines]
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.