Search code examples
javajavascriptgwtsvggwt2

Checking the size of an Image with GWT


I have a GWT application that generates SVG files.

I want to embed an image from some URL into my SVG, but I want it to be resized keeping the correct aspect ratio.

I do this by loading the image using the Image class in GWT, and check the height to width, then do some sums to find the height and width I need it to be in my SVG.

I embed the image into the SVG as follows:

<image href="http://some.image/URL.png" height="100" x="50" width="150" y="50"></image>

The issue that I have is when I do the following:

Image image = new Image(sourceURL);
int width = image.getWidth();
int height = image.getHeight();
....

The first time I do this for a particular URL the value of width or height comes back as 0. Unfortunately retrying in a loop doesn't seem to fix the problem, but if I ask my application to generate the SVG again, it works.

Any ideas?


Solution

  • Ok, sorry this is not an answer, but comments are too small to put some code.

    I tried the following and it worked perfectly. So you should probably try to isolate your issue in a smaller piece of code:

        @Override
        public void onModuleLoad() {
    
        final Image image = new Image("https://www.google.com/images/srpr/logo3w.png");
        image.addLoadHandler(new LoadHandler() {
    
            @Override
            public void onLoad(LoadEvent event) {
                Window.alert(image.getWidth() + " " + image.getHeight());
            }
        });
        RootLayoutPanel.get().add(image);
        }