....
> You can resize the image after it has arrived.
>> > ....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 ]