Jump to content

PHP SVG Generator


shamuraq

Recommended Posts

Hi all i've got this thing that got me stumped...

 

<?
ob_start();
$output = "header(\"Content-type: image/svg+xml\")";
$output .=  "<?xml version=\"1.0\" standalone=\"no\"?>";
$output .=  "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"
\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";

$output .=   "<svg width=\"750\" height=\"710\" xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">";

$cx = 110; //Coordinate of x-axis @ centre of circle
$cy = 110; //Coordinate of y-axis @ centre of circle
//$r = rand(100,150); // radius
$r = 100;

//Melukis segi empat dari data dinamis yg didapati di atas
$output .= "<circle class=\"Connect\" cx=\"$cx\" cy=\"$cy\" r=\"$r\">\n";
$output .= "</circle>\n";

$output .=   "</svg>\n";

$date = date('dmy');
$kali = 0;

$content = ob_get_clean($output);
file_put_contents("1".".svg", $content);
?>

 

I get the file 1.svg (meaning file placement is successful) but when i execute the svg file, it is empty... Error in the browser says

 

XML Parsing Error: no element found
Location: file:///D:/Apache%20Group/Apache2/htdocs/math/testing/1.svg
Line Number 1, Column 1:
^

 

Any ideas?

Link to comment
Share on other sites

Well, the things that jump out at me are...

<?
$output = "header(\"Content-type: image/svg+xml\")";
$output .=  "<?xml version=\"1.0\" standalone=\"no\"?>";
$output .=  "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"
\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";
?>

 

Try just echoing out $content instead of writing to file. You might be able to find out something that way.

Link to comment
Share on other sites

Well, the things that jump out at me are...

<?
$output = "header(\"Content-type: image/svg+xml\")";
$output .=  "<?xml version=\"1.0\" standalone=\"no\"?>";
$output .=  "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"
\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";
?>

 

Try just echoing out $content instead of writing to file. You might be able to find out something that way.

 

I am successful in using echo

but i need to write this to file for future development

 

Any ideas?

Link to comment
Share on other sites

So when you echo $contents, you see the right contents?

 

Did you try opening the SVG file with gedit/notepad/$plain_text_editor?

 

Yes... its blank

 

i added error notifier and it says:

Warning: Wrong parameter count for ob_get_clean() in D:\Apache Group\Apache2\htdocs\....01.php on line 41

 

I'm suspecting there's a better syntax to "ob_get_clean($output);"

 

Link to comment
Share on other sites

Bingo... tt's what i was looking fer...

Now i'm very close to it

 

just that the svg is not well formed... the error on firefox says:

XML Parsing Error: not well-formed
Location: file:///D:/Apache%20Group/Apache2/htdocs/...050610144301.svg
Line Number 1, Column 7:header("Content-type: image/svg+xml")
------^

 

Notepad successfully display:

header("Content-type: image/svg+xml")
<?xml version="1.0" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<svg width="750" height="710" xmlns="http://www.w3.org/2000/svg" version="1.1">

<circle class="Connect" cx="110" cy="110" r="100">
</circle>
</svg>

Link to comment
Share on other sites

Cool.

 

You definitely need to drop the header(...) line. This is the structure you need in the file:

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" 
  "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="5cm" height="4cm" version="1.1"
     xmlns="http://www.w3.org/2000/svg">
  <desc>Four separate rectangles
  </desc>
    <rect x="0.5cm" y="0.5cm" width="2cm" height="1cm"/>
    <rect x="0.5cm" y="2cm" width="1cm" height="1.5cm"/>
    <rect x="3cm" y="0.5cm" width="1.5cm" height="2cm"/>
    <rect x="3.5cm" y="3cm" width="1cm" height="0.5cm"/>
  <!-- Show outline of canvas using 'rect' element -->
  <rect x=".01cm" y=".01cm" width="4.98cm" height="3.98cm"
        fill="none" stroke="blue" stroke-width=".02cm" />
</svg>

 

See more here: http://www.w3.org/TR/SVG11/struct.html

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.