How to convert SVG to PNG while still preserving the layers?

Viewed 255

I have an SVG image that looks like this:

enter image description here

This is an excerpt of that SVG file:

<g id="surface1">
    <g clip-path="url(#clip1)" clip-rule="nonzero">
        <path style=" stroke:none;fill-rule:nonzero;fill:rgb(100%,100%,100%);fill-opacity:1;" d="M 0.140625 0 L 1683.609375 0 L 1683.609375 2390.601562 L 0.140625 2390.601562 Z M 0.140625 0 "/>
        <path style=" stroke:none;fill-rule:nonzero;fill:rgb(100%,100%,100%);fill-opacity:1;" d="M 0.140625 0 L 1683.609375 0 L 1683.609375 2383.855469 L 0.140625 2383.855469 Z M 0.140625 0 "/>
        <path style=" stroke:none;fill-rule:nonzero;fill:rgb(93.728638%,93.328857%,91.369629%);fill-opacity:1;" d="M 0.140625 0 L 1683.609375 0 L 1683.609375 2383.855469 L 0.140625 2383.855469 Z M 0.140625 0 "/>
    </g>
    <g clip-path="url(#clip2)" clip-rule="nonzero">
        <path style=" stroke:none;fill-rule:nonzero;fill:rgb(85.879517%,85.879517%,85.879517%);fill-opacity:1;" d="M 196.179688 546.039062 C 185.273438 546.039062 176.347656 554.964844 176.347656 565.871094 C 176.347656 565.871094 176.347656 566.121094 176.347656 566.121094 C 177.339844 649.417969 181.304688 977.402344 176.101562 1277.371094 C 169.902344 1624.445312 207.089844 1785.585938 207.089844 1785.585938 L 1341.292969 1785.585938 C 1341.292969 1785.585938 1390.875 1438.511719 1378.476562 1172.011719 C 1366.082031 905.507812 1378.476562 713.378906 1335.09375 632.808594 C 1291.707031 552.238281 764.894531 558.4375 609.949219 552.238281 C 480.042969 547.03125 263.117188 546.289062 196.179688 546.039062 Z M 196.179688 546.039062 "/>
    </g>
    <g clip-path="url(#clip3)" clip-rule="nonzero">
        <g clip-path="url(#clip4)" clip-rule="nonzero">
            <g clip-path="url(#clip5)" clip-rule="nonzero">
                <path style=" stroke:none;fill-rule:nonzero;fill:rgb(73.329163%,42.349243%,17.248535%);fill-opacity:1;" d="M 407.738281 1958.871094 C 400.707031 1854.550781 467.722656 1826.082031 498.4375 1760.117188 C 529.148438 1694.152344 714.296875 1454.164062 1021.128906 1471.179688 C 1327.957031 1488.195312 1477.898438 1544.210938 1492.515625 1710.101562 C 1507.136719 1875.992188 1435.222656 2096.339844 1287.71875 2121.804688 C 1140.21875 2147.269531 707.75 2176.964844 636.011719 2178.085938 C 484.5 2180.882812 412.949219 2036.085938 407.738281 1958.871094 Z M 407.738281 1958.871094 "/>
            </g>
        </g>
    </g>
    ...
</g>

I am wondering if there is a known solution how to convert this SVG into an SVG that solely consist of PNG Base64 strings where the objects in each layer and the layers themselves are preserved. Therefore, as opposed to the current SVG file that only contain <g> and <path> tags, I want to convert this SVG into an SVG file that only contain <image> tags where the image data is contained in the xlink:href property, like this:

<image id="Lager_1" data-name="Lager 1" width="185.9" height="244.95000000000002" xlink:href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAALkAAAD ...

I have searched for the solution to this problem for 2 straight weeks now, so any help would of course be appreciated. And to those who is going to ask "are you sure you want to do this?" the answer is yes.

My currently best guess is to load the layers one by one on a Javascript canvas and convert them one by one to a PNG and then replace the tag entirely with the generated image string.

1 Answers

Like many in the comments, I am also interested in the use-case for this, I can't see any benefit.

And your:

Just kind of difficult to deal with all the special cases
on how attributes in an SVG can be defined,
a lot of parsing.

makes me even more curious at why, how and what you are doing.

Only parsing should be done by the browser.
Program flow shouldn't be more than:


You still haven't answered all questions Why you want a binary.

If you want an SVG <image> tag (for some reason)

You could also dataURI the layered SVG with:

function svg2img(){
    let svg = document.querySelector('svg');
    let xml = new XMLSerializer().serializeToString(svg);
    let svg64 = btoa(xml); //for utf8: btoa(unescape(encodeURIComponent(xml)))
    return 'data:image/svg+xml;base64,' + svg64;
};

Disclaimer: I copied the code snippet from a blog; didn't test the code

Related