how to get the real (unscaled) size of an embedded image in the svg document?!

前端 未结 2 922
小鲜肉
小鲜肉 2020-12-20 15:11

I have a SVG document with the image entry in it:

          


        
2条回答
  •  星月不相逢
    2020-12-20 15:31

    I figured out how to solve it entirely on the Javascript side level, without doing any XHR calls or whatever!

    However, SVG doesn't have any dom functions to figure out the unscaled size of a picture.

    Sollution!

    Grab the SVGImageElement Object and it's URL from the attribute:

    var myPicXE = document.getElementsByTagName('image')[0];
    var address = myPicXE.getAttribute('xlink:href');
    

    create an Image Object and assign it's src addres:

    var myPicXO = new Image();
    myPicXO.src = address;
    

    and ask gently for it's height and width:

    myPicXO.width;
    myPicXO.height;
    

    should work also work wit base65jpeg inline images ( http://en.wikipedia.org/wiki/Data_URI_scheme )

    That's it!

提交回复
热议问题