• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Getting the size for an img tag dynamically with struts

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Something like this perhaps (disclaimer: it does not compile as such due to uncatched exceptions etc.)
 
Ed Mahoney
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ever since I found this suit I've felt strange new needs. And a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic