Embedded image size

How can we get the size of any embedded image. I don’t want to first set the height and width of the embedded image. I actually want that whatever the image is coming in embedded object, i can retrieve its size. I need this so that i can re-size embedded image, if it is bigger than my panel’s size…

If you are using ThemeResource or ExternalResource, then what you are asking isn’t possible through Vaadin itself. Those two classes add direct references to the image URL to the HTML, and as such have no knowledge of if the image even exists. In this case I recommend using some custom client-side code for the processing.

If you are serving the image yourself, from a FileResource for instance, the you can use any existing image library (I imagine e.g. Apache has a few) to check the size directly from the byte content.

If you are using an InputStream() to get the image, you can use the Buffered Image to get its width and height.

Here’s the code I use in my project

BufferedImage bi = ImageIO.read(streamSource.getStream());
				if(bi.getHeight() > 768)
				{
					imgAux.setHeight("768px");
				}
				if(bi.getWidth() > 768)
				{
					imgAux.setWidth("768px");
				}

You can also create a BufferedImage from a File, URL, or ImageInputStream (
http://docs.oracle.com/javase/7/docs/api/javax/imageio/ImageIO.html
)

For example, from a file:

File file = new File("/some/path/to/some/img.png");
BufferedImage bi = ImageIO.read(file);