GithubHelp home page GithubHelp logo

SVG Image into SVG about php-svg HOT 2 CLOSED

meyfa avatar meyfa commented on July 26, 2024
SVG Image into SVG

from php-svg.

Comments (2)

meyfa avatar meyfa commented on July 26, 2024

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>

from php-svg.

meyfa avatar meyfa commented on July 26, 2024

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.

from php-svg.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.