如何获取 svg 文档中嵌入图像的真实(未缩放)大小?!
我有一个包含图像条目的 SVG 文档:
<image id="image12975" x="30" y="40" width="30" height="45"
xlink:href="img/Akira1.jpg">
</image>
我通过执行以下操作在 Javascript 中获取了 SVG 图像:
var imgE = document.getElementsByTagName('rect');
document.getElementById('test').innerHTML = imgE;
为我带来 Javascript 中的 [object SVGImageElement] 。
通过迭代该对象,我拥有函数和属性的完整子集。函数“getBBox()”的 height 和 width 只给我带来定义为宽度 (30) 和高度 (45) 的属性值。
我正在寻找真实的参数,就像我单独打开图片一样,它们实际上是宽度=“300”和高度=“430”像素。
如果有任何支持,我会非常感谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想出了如何完全在 Javascript 层面解决这个问题,而不需要进行任何 XHR 调用或其他任何操作!
然而,SVG 没有任何 dom 函数来计算图片的未缩放大小。
解决方案!
从属性中获取 SVGImageElement 对象及其 URL:
创建一个图像对象并分配它的 src 地址:
并轻轻询问它的高度和宽度:
应该也可以与 base65jpeg 内联图像一起使用( http://en.wikipedia.org/wiki/Data_URI_scheme )
就是这样!
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:
create an Image Object and assign it's src addres:
and ask gently for it's height and width:
should work also work wit base65jpeg inline images ( http://en.wikipedia.org/wiki/Data_URI_scheme )
That's it!
我做了一些研究,但无法发现可以枚举以获得图像实际宽度和高度的属性。然而,还有另一种方法似乎很有趣,其中包括一个 xmlHttpRequest 来获取图像并枚举它的尺寸。
这让我想到了一个稍微快一点的方法。解决方案是利用隐藏的
作为工作容器,并下拉必要的图像(使用
),您可以在其中枚举宽度和高度。这个伪代码例如:
有帮助的话),这里是 xmlHttpRequest 的一些代码草稿:
只是为了笑(如果 这并不能解决你的问题,我希望它至少能创造一些新的想法来解决它。
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:Just for grins (if it helps) here was a draft of some code for the xmlHttpRequest:
If this doesn't solve your problem, I hope it will at least create some new ideas in solving it.