Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SVG Image into SVG #60

Closed
smuglet opened this issue Oct 8, 2018 · 2 comments
Closed

SVG Image into SVG #60

smuglet opened this issue Oct 8, 2018 · 2 comments
Labels
question Further information is requested

Comments

@smuglet
Copy link

smuglet commented Oct 8, 2018

Hi there,

Is there a way to load a SVG for placement into a loaded SVG. An image inside an image.

Ideally not using xlink:href. Would very much like the produced SVG self contained.

Thanks.

@meyfa
Copy link
Owner

meyfa commented Nov 10, 2018

Yes, that's most definitely possible.

<svg> nodes can contain other <svg> nodes. As such, you can simply load image A, load image B, and then add B as a child node of A.

For example:

<?php
use SVG\SVG;

$a = SVG::fromFile(__DIR__.'/a.svg');
$b = SVG::fromFile(__DIR__.'/b.svg');

$a->getDocument()->addChild($b->getDocument());

echo $a;

File 'a.svg':

<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
    <rect x="10" y="10" width="5" height="20" />
</svg>

File 'b.svg':

<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
    <circle cx="40" cy="20" r="8" />
    <circle cx="80" cy="20" r="8" />
</svg>

Script output (formatted):

<?xml version="1.0" encoding="utf-8"?>
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
    <rect x="10" y="10" width="5" height="20" />
    <svg viewBox="0 0 100 100">
        <circle cx="40" cy="20" r="8" />
        <circle cx="80" cy="20" r="8" />
    </svg>
</svg>

@meyfa
Copy link
Owner

meyfa commented Nov 10, 2018

Note that if you need the output without the <?xml version="1.0" encoding="utf-8"?> line (for embedding in an HTML document), you can pass false to SVG::toXMLString() like so:

<?php
use SVG\SVG;

$a = SVG::fromFile(__DIR__.'/a.svg');
$b = SVG::fromFile(__DIR__.'/b.svg');

$a->getDocument()->addChild($b->getDocument());

echo $a->toXMLString(false);

Moreover, if you need to place image B somewhere deeper inside image A (inside a <g> element, for example), you can do that, too. You would just need to navigate to the target node and call addChild() on that. For example:

<?php
use SVG\SVG;

$a = SVG::fromFile(__DIR__.'/a.svg');
$b = SVG::fromFile(__DIR__.'/b.svg');

$groups = $a->getDocument()->getElementsByTagName("g");
$g = $groups[0];
$g->addChild($b->getDocument());

echo $a->toXMLString(false);

I hope I could answer your question (albeit a bit late). If not, feel free to reopen this issue.

@meyfa meyfa added the question Further information is requested label Nov 10, 2018
@meyfa meyfa closed this as completed Nov 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants