最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin3浏览0评论

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

<image id="image12975" x="30" y="40" width="30" height="45"
xlink:href="img/Akira1.jpg">    
</image>

I have fetched the SVG Image in Javascript by doing:

var imgE = document.getElementsByTagName('rect');
document.getElementById('test').innerHTML = imgE;

brings me the [object SVGImageElement] in Javascript.

By iterating the object I have a full subset of functions and attributes. height and width of the function "getBBox()" bring me only the attribut values defined as width (30), and height (45).

I am looking for the real parameters, like I would open the picture alone, which are in reality width="300" and height="430" pixels.

for any support I would kindly thank you

Tamer

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

<image id="image12975" x="30" y="40" width="30" height="45"
xlink:href="img/Akira1.jpg">    
</image>

I have fetched the SVG Image in Javascript by doing:

var imgE = document.getElementsByTagName('rect');
document.getElementById('test').innerHTML = imgE;

brings me the [object SVGImageElement] in Javascript.

By iterating the object I have a full subset of functions and attributes. height and width of the function "getBBox()" bring me only the attribut values defined as width (30), and height (45).

I am looking for the real parameters, like I would open the picture alone, which are in reality width="300" and height="430" pixels.

for any support I would kindly thank you

Tamer

Share Improve this question asked Jul 30, 2011 at 16:39 SmileMZSmileMZ 5475 silver badges13 bronze badges 2
  • Can you explain what you are trying to do in your code snippet? It looks like you're assigning a NodeList to the innerHTML property of the "test" element. My understanding is that innerHTML should be a string; I'm not sure what happens when you assign to it an object (except I imagine that it might convert the object to its string representation, and attempt to parse that as HTML). In any case, hopefully you can clarify what this code is supposed to express. – jbeard4 Commented Jul 30, 2011 at 21:00
  • I try to get the usual size of the ebedded image in the SVG document. The usual size, is the size of the picture unscaled. However, it's solved. In a short time (after the time period expired) I will self answer this question. I figured out, that the SVG Image methods don't get me the desired informations to figure out the measure data, which is the picture itself in an unscaled status. – SmileMZ Commented Jul 30, 2011 at 23:52
Add a ment  | 

2 Answers 2

Reset to default 10

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/wiki/Data_URI_scheme )

That's it!

I did a little research and was unable to discover a property that you could enumerate to get the images actual width and height. However there was another approach which seemed interesting which included an xmlHttpRequest to get the image and enumerate it's dimensions.

That made me think of a slightly faster way. The solution is to utilize a hidden <div> as a working container and pulling down the necessary images (using <img>) where you can enumerate width and height there. This pseudoCode For example:

  • Iterate through collection of images
  • Create an image node with the appropriate src path
  • Append the new node to div#ImageEnumeration
  • Enumerate image retrieved

Just for grins (if it helps) here was a draft of some code for the xmlHttpRequest:

var imgRequest = 
  function(sImgUrl, oRequestCallback){
    var xhr = new XMLHttpRequest();  
    xhr.open('GET', sImgUrl, true);  
    xhr.onreadystatechange = oRequestCallback;
    xhr.send(null);  
    };

var xhrImageRequest =
  function(result){
    var oImageDimensions;
    //Create and append <img> to <div #ImageEnumeration>
    //Get image width and height
    //return image width and height
    };

var myImage = imgRequest("http://example./myImage.png", xhrImageRequest);

If this doesn't solve your problem, I hope it will at least create some new ideas in solving it.

发布评论

评论列表(0)

  1. 暂无评论