File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes Getting the size for an img tag dynamically with struts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Getting the size for an img tag dynamically with struts" Watch "Getting the size for an img tag dynamically with struts" New topic
Author

Getting the size for an img tag dynamically with struts

Paul Duer
Ranch Hand

Joined: Oct 10, 2002
Posts: 98
Hi guys,
I have a situtation where I am going to have a good number of different images all going into the same spot on a webpage. I know that the spot will be a certain size. The images however will be many different sizes.
I want to be able to get the size of the image programatically to insert into the img height and width attributes of the tag.
This way I can say, the img must be less than X by X, but if you're under that, I will figure out how to display the img.
Does anybody know if this is possible to read these parameters from an img and if so, how?
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
I don't know about out-of-the-box components for the task but Java 2D API supports reading GIF, JPG, and PNG images and that way allows you to figure out the size of the images. See the Java 2D FAQ and Java 2D API Overview for more info.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
David Hibbs
Ranch Hand

Joined: Dec 19, 2002
Posts: 374
I'm not sure this answers your question, but if you set the height and width of an image, most browsers will scale the image to match the h/w that was given.
Since you say you know the space it will fill, it seems (IMHO) that this is a good answer.


"Write beautiful code; then profile that beautiful code and make little bits of it uglier but faster." --The JavaPerformanceTuning.com team, Newsletter 039.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Yes, but if the image is 20x10 and the space reserved for it is 100x50, the result looks less than attractive...
Paul Duer
Ranch Hand

Joined: Oct 10, 2002
Posts: 98
Yes Lasse is correct, my end goal is to be able to place an unknown sized image in the same space each time. By getting the size of the img, I can set it to scale to exactly the size of the image and not resize.
The Java2d apis seem to be the way to go.
I know that I could just leave the sizing off, because if the image can't be bigger than a certain size, I have change the images, but I think I might do cropping later.
Ed Mahoney
Greenhorn

Joined: Sep 19, 2000
Posts: 14
Here's the code to get the height and width of an image and to scale it down to 10% of its original size. The image is then returned as a jpeg byte array. This code assumes you have the image stored in a byte array to begin with.
Good luck,
Ed
ByteArraySeekableStream stream = null;
byte[] theOutputImage;

try {
/* change the following to return your byte[] */
stream = new ByteArraySeekableStream(/* your byte[] */);

/* Create an operator to decode the image file. */
RenderedOp image1 = JAI.create("stream", stream);

log.info("image height=" + image1.getHeight());
log.info("image width=" + image1.getWidth());

/*
* Create a standard bilinear interpolation object
* to be used with the "scale" operator.
*/
Interpolation interp = Interpolation.getInstance(
Interpolation.INTERP_BILINEAR);
/**
* Stores the required input source and parameters
* in a ParameterBlock to be sent to the operation
* registry, and eventually to the "scale" operator.
*/
ParameterBlock params = new ParameterBlock();
params.addSource(image1);
params.add(0.10F); // x scale factor
params.add(0.10F); // y scale factor
params.add(0.0F); // x translate
params.add(0.0F); // y translate
params.add(interp); // interpolation method
/* Create an operator to scale image1. */
RenderedOp image2 = JAI.create("scale", params);
log.info("image2 height=" + image2.getHeight());
log.info("image2 width=" + image2.getWidth());
ByteArrayOutputStream os = new ByteArrayOutputStream();
JPEGEncodeParam param = new JPEGEncodeParam();
ImageEncoder enc = ImageCodec.createImageEncoder("JPEG", os,param);
enc.encode(image2);
/* return a byte array */
return os.toByteArray();
Paul Duer
Ranch Hand

Joined: Oct 10, 2002
Posts: 98
Ed,
This is great code, and I think usable for what I want. Is it possible for you to show us how you might get the image as a JPG from a URL and convert it into a Byte array to be accepted by this code.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Something like this perhaps (disclaimer: it does not compile as such due to uncatched exceptions etc.)
Ed Mahoney
Greenhorn

Joined: Sep 19, 2000
Posts: 14
Actually, that code was designed to take images from the struts file upload feature and then save the byte array to a database managed by cmp ejb's. I use the following to upload a file from a user's local disk to the server ( this is not complete code). The struts FormFile class has a a call to get the byte array containing the file data. You just the pass the byte array to the code I previously posted and it works just fine.
org.apache.struts.upload.FormFile myFile;
byte[] myArray = myFile.getFileData();
Hope this helps,
Ed
Paul Duer
Ranch Hand

Joined: Oct 10, 2002
Posts: 98
Ed,
That is helpful, could you elaborate on how get the FormFile thing to work? I kind of understand what you are saying that there is a class and a tag or two for uploading files.
Is it possible to somehow make the file URL a paratmeter or something so that Struts can provide it just like any other form variable?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting the size for an img tag dynamically with struts