3

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?

luketorjussen
  • 3,156
  • 1
  • 21
  • 38
  • Remeber that as long as the image has not been loaded, the width and height is not known. You should probably wait for the "Loaded" event to be fired and then do your stuff. – Guillaume Polet Mar 09 '12 at 23:16
  • @GuillaumePolet I tried doing that but the event took well over a minute to get fired. But if I ask the application twice within 10 seconds it loads instantly on the second attempt. – luketorjussen Mar 09 '12 at 23:18
  • There must be something wrong in your code because I have already used those methods succesfully and it did not take a minute to load. Could you try reproducing the error in an SSCCE (http://sscce.org)? It may help you find your error. If not, we can take a look at it. – Guillaume Polet Mar 09 '12 at 23:24
  • In fact I have just tried again and I never see the load handler getting called, even when the image width comes back as a non-zero. – luketorjussen Mar 09 '12 at 23:28

1 Answers1

4

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);
    }
Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
  • thanks for this sample code, I have set up a project with this code but it doesn't seem to be working. The load handler is not getting called again. – luketorjussen Mar 09 '12 at 23:52
  • I just viewed the image in the web browser in a new tab, then re-ran the sample code and it worked... But the question remains, why doesn't it find the image first time? – luketorjussen Mar 09 '12 at 23:53
  • Ok, then maybe there are some issues with your browser? Have you tried with a different one? If you are using FF, maybe try to disable some extensions? I know I had very severe issues with the Web Developer Tools. – Guillaume Polet Mar 09 '12 at 23:56
  • I have the same problem in every browser on every machine I have tried. Could it be a GWT bug? (I can't see where the bug could be in my code given that I had the same result with the small sample you gave) – luketorjussen Mar 10 '12 at 00:04
  • Is the image ever added to the document? If not then the browser won't load it and the LoadHandler is then not called. I also changed my sample image url in my code because it did not work very well – Guillaume Polet Mar 10 '12 at 00:22
  • I don't add that image to the page itself I only use the Image class to find out the height and width of the original image. Perhaps there is a better way to do this... – luketorjussen Mar 10 '12 at 10:13
  • Then there is your issue. You must add the image to the document (and maybe event make it visible) so that the browser loads your image and you can retrieve the size of the image. Alternatively, if this cannot be done, then you could ask the server to load the image and compute the size of it. – Guillaume Polet Mar 10 '12 at 12:39
  • I have found a "work-around" which will work for now, I'll see if I can move the size checking to the server. Thanks for your help – luketorjussen Mar 10 '12 at 13:04