PDA

View Full Version : Integrating PHP and SVG


slushy77
02-24-2006, 09:08 PM
hey everyone,
due to the way SVG is currently being rendered, sending it through PHP wont work as expected - only text is rendered, even with output buffering :mad:.

The solution is to send html in the normal way and, use <object> to call the svg.

if (colorScheme=="blue"){
echo "<object data=\"blue.svg\">this should be a blue pattern</object>";
}
else {
echo "<object data=\"yellow.svg\">this should be a yellow pattern</object>";
}

this method allows you to compensate for browsers that dont support SVG or have it switched off

eak
02-24-2006, 11:44 PM
what does php have to do with svg?

are you thinking of native support for svg in the browser VS embeding as an object?

slushy77
02-25-2006, 12:34 AM
Thread was orignally posted as a workaround to the following which is both valid php and svg, but wont render properly

<? php
...
echo "<?xml version=\"1.0\" standalone=\"no\"?>\n";
echo "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n;
echo "<svg ....>"
....
if (colorScheme=="blue"){
echo "<circle cx=\"100\" cy=\"50\" r=\"40\" stroke=\"black\" stroke-width=\"2\" fill=\"blue\"/>"
}
...
echo "</svg>";
?>

fci
02-25-2006, 12:44 AM
you do know there is a syntax error in that post, right? and, I bet anyone maintaining would prefer you do this:
?><circle cx="100" cy="50" r="40" stroke="black" stroke-width="2" fill="blue" /><?php

or this:
print '<circle cx="100" cy="50" r="40" stroke="black" stroke-width="2" fill="blue" />';

having to escape all the double quotes is insanely annoying and unnecessary.

slushy77
02-25-2006, 08:09 AM
oops forgot the semi colons.:rolleyes:
this thread is about manipulating svg with php, not the quote styles i use

Even with syntacticly correct php, and valid svg, svg will not render correctly if embedded in php. The idea of this thread, was to provide a not-very-elegant workaround to this problem - you wont be able to plot charts from a database using it.But its post 1 if you need it.

steps to prove me wrong:
create some valid svg - use colored shapes, and some text, call it svgtest.svg
check using FF, or IE and the SVG plugin
embed in php - dont forget those semi colons
note: you will need to print/echo the xml declaration otherwise there will be an error
save the php file as svgtest.php
use IE with the SVG plugin, or FF to view from your webserver

missing-score
02-25-2006, 08:41 AM
You are most likley failing to send a valid SVG content type, just add:

<?php
header('Content-Type: image/svg+xml');
?>

to the top of your PHP file.... i tried your steps and it worked fine :)

slushy77
02-25-2006, 08:46 AM
lol that would be an even better workaround