# Providing dimensions for images in SVG output

**URL:** <https://forum.graphviz.org/t/providing-dimensions-for-images-in-svg-output/1773>\
**Category:** Help\
**Created:** [September 7, 2023, 9:30pm UTC](https://forum.graphviz.org/t/providing-dimensions-for-images-in-svg-output/1773 "2023-09-07T21:30:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mdaines](https://avatars.discourse-cdn.com/v4/letter/m/a698b9/32.png) [@mdaines](https://forum.graphviz.org/u/mdaines)\
**Post date:** [September 7, 2023, 9:30pm UTC](https://forum.graphviz.org/t/providing-dimensions-for-images-in-svg-output/1773/1 "2023-09-07T21:30:51Z")

</div>

I’m using Graphviz as a library, and I’d like to render a graph like this as SVG:

```plaintext
digraph {
  a [image="http://example.com/myimage.jpg"]
}

```

That is, the resulting SVG file should have an `<image>` element referring to `"http://example.com/myimage.jpg"`. Of course, because Graphviz needs the image dimensions to render the graph, and only local images are supported, the result is this warning: `"http://example.com/myimage.jpg" was not found as a file or as a shape library member`.

However, I don’t want Graphviz (or a script like dot\_url\_resolve.py) to load the image – I already know its dimensions. And from looking at `gvloadimage_core.c`, it seems like Graphviz only really needs to know the dimensions of the image when rendering SVG – it doesn’t need to do anything else with the image data.

Is there currently any way to provide dimensions for an image ahead of time so that Graphviz won’t try to read them from an image file that doesn’t exist?

Specifically, this is to support images in [Viz.js](https://github.com/mdaines/viz-js). I did figure out [an implementation](https://github.com/mdaines/viz-js/commit/5e0a451eb0955e417a6155522e8e1a149ae101e6) of this previously, but it involves writing a dummy SVG image to the Emscripten virtual filesystem at a path like `/http:/example.com/myimage.jpg`, which Graphviz will read to find its dimensions. I’m wondering if there’s a way to do it that’s less of a hack.

---

<div class="post-metadata">

**Author:** ![smattr](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/smattr/32/85_2.png) [@smattr](https://forum.graphviz.org/u/smattr)\
**Post date:** [September 8, 2023, 8:48am UTC](https://forum.graphviz.org/t/providing-dimensions-for-images-in-svg-output/1773/2 "2023-09-08T08:48:32Z")

</div>

Not that I’m aware of, but this seems like a reasonable feature request. Happy to accept merge requests over on Gitlab.

---

<div class="post-metadata">

**Author:** ![mark](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/mark/32/84_2.png) [@mark](https://forum.graphviz.org/u/mark)\
**Post date:** [September 8, 2023, 9:32am UTC](https://forum.graphviz.org/t/providing-dimensions-for-images-in-svg-output/1773/3 "2023-09-08T09:32:19Z")

</div>

Yeah it seems reasonable to add width and height attributes or something similar. Html img tags have width and height specifyable for this reason, so you can reserve space before downloading the image.

---

<div class="post-metadata">

**Author:** ![mdaines](https://avatars.discourse-cdn.com/v4/letter/m/a698b9/32.png) [@mdaines](https://forum.graphviz.org/u/mdaines)\
**Post date:** [September 8, 2023, 2:17pm UTC](https://forum.graphviz.org/t/providing-dimensions-for-images-in-svg-output/1773/4 "2023-09-08T14:17:14Z")

</div>

OK, I’ll give it a shot!

I was thinking of this as an addition for library usage, but it seems like attributes would be easier to use.

What about a single point attribute that specifies the size of the image?

```plaintext
digraph {
  a [image="http://example.com/myimage.jpg", imagesize="300,200"]
}

```

Are user shapes measured in pixels or points? (That is, the `w` and `h` fields of `usershape_s`.)

---

<div class="post-metadata">

**Author:** ![smattr](https://sea2.discourse-cdn.com/graphviz/user_avatar/forum.graphviz.org/smattr/32/85_2.png) [@smattr](https://forum.graphviz.org/u/smattr)\
**Post date:** [September 9, 2023, 8:13am UTC](https://forum.graphviz.org/t/providing-dimensions-for-images-in-svg-output/1773/5 "2023-09-09T08:13:42Z")

</div>

`imagesize` sounds like a reasonable choice to me. Not sure offhand what units `w` and `h` are in. You would have to check the source.

For user-facing stuff like attributes there’s also been discussion of allowing explicit units, [Feature request: allow specifying units in size attributes (#2100) · Issues · graphviz / graphviz · GitLab](https://gitlab.com/graphviz/graphviz/-/issues/2100). This would be a nice thing to add uniformly.
