{"id":218,"date":"2006-01-28T23:07:09","date_gmt":"2006-01-29T05:07:09","guid":{"rendered":"http:\/\/blog.codedread.com\/?p=218"},"modified":"2006-01-28T23:07:09","modified_gmt":"2006-01-29T05:07:09","slug":"make-svg-from-php","status":"publish","type":"post","link":"https:\/\/www.codedread.com\/blog\/archives\/2006\/01\/28\/make-svg-from-php\/","title":{"rendered":"Make SVG From PHP"},"content":{"rendered":"<p>I've been writing a bit of PHP, SVG and JavaScript.  Creating nested XML nodes with a procedural language can be a pain, PHP is no exception here.  I wrote a quick PHP function to help with my SVG node creation.  <!--more--><\/p>\n<p><script language=\"JavaScript\" src=\"http:\/\/www.codedread.com\/yahooads.js\"><\/script><br \/>\n<script language=\"JavaScript\" src=\"http:\/\/ypn-js.overture.com\/partner\/js\/ypn.js\"><\/script><\/p>\n<p>It's nothing fancy, I just thought I'd save somebody the time of writing it themselves:<\/p>\n<div class=\"code\">\nfunction make($domxml, $element, $attrs = null, $children = null) {<br \/>\n&#160;&#160;$node = $domxml->create_element_ns(\"http:\/\/www.w3.org\/2000\/svg\", $element, \"svg\");<br \/>\n&#160;&#160;if($attrs != null) {<br \/>\n&#160;&#160;&#160;&#160;foreach($attrs as $key => $value) {<br \/>\n&#160;&#160;&#160;&#160;&#160;&#160;$node->set_attribute($key, $value);<br \/>\n&#160;&#160;&#160;&#160;}<br \/>\n&#160;&#160;}<br \/>\n&#160;&#160;if($children != null) {<br \/>\n&#160;&#160;&#160;&#160;foreach($children as $child) { $node->append_child($child); }<br \/>\n&#160;&#160;}<br \/>\n&#160;&#160;return $node;<br \/>\n}\n<\/div>\n<p>Simple usage to create a <em>&#60;g><\/em> element and append it to the <em>&#60;svg><\/em> element $svgnode.  ($doc in this context is the DOM XML object i.e. the \"document\" in JavaScript):<\/p>\n<div class=\"code\">\n$svgnode->append_child(make($doc, \"g\"));\n<\/div>\n<p>To set several attributes, you send an array of attribute name\/value pairs to the function:<\/p>\n<div class=\"code\">\n$svgnode->append_child(make($doc, \"circle\", array(\"cx\" => 35.5, \"cy\" => -12.5, \"fill\" => \"blue\")));\n<\/div>\n<p>To append children to the node you're creating, fill an array with the nodes:<\/p>\n<div class=\"code\">\n$svgnode->append_child( make($doc, \"linearGradient\",<br \/>\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;array(\"x1\" => 0.0, \"y1\" => 0.0, \"x2\" => 1.0, \"y2\" => 1.0),<br \/>\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;array(make($out, \"stop\", array(\"offset\" => 0.0, \"stop-color\" => \"#ffffff\")),<br \/>\n&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;make($out, \"stop\", array(\"offset\" => 1.0, \"stop-color\" => \"#cccccc\")))));\n<\/div>\n<p>You can use this technique to create a tree of nodes to an arbitrary depth, though the biggest pain with using this technique is getting the correct number of right parentheses, make sure you have a good editor to match them up.<\/p>\n<p><script language=\"JavaScript\" src=\"http:\/\/www.codedread.com\/yahooads.js\"><\/script><br \/>\n<script language=\"JavaScript\" src=\"http:\/\/ypn-js.overture.com\/partner\/js\/ypn.js\"><\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been writing a bit of PHP, SVG and JavaScript. Creating nested XML nodes with a procedural language can be a pain, PHP is no exception here. I wrote a quick PHP function to help with my SVG node creation.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42,25,46,11,14,28,30],"tags":[],"class_list":["post-218","post","type-post","status-publish","format-standard","hentry","category-php","category-software","category-svg","category-technology","category-tips","category-web","category-xml"],"_links":{"self":[{"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/posts\/218","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/comments?post=218"}],"version-history":[{"count":0,"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/posts\/218\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/media?parent=218"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/categories?post=218"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codedread.com\/blog\/wp-json\/wp\/v2\/tags?post=218"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}