From: "Aurelie REYMUND" <aurely001@xxxxxxxxx>
Sent: Monday, February 01, 2010 3:37 AM
To: <php-general@xxxxxxxxxxxxx>
Subject: session variables and SVG documents
Hello,
I have the following problem with the Adobe SVG viewer:
I try to generate a SVG document using PHP. the following code is working
well under Firefox, as well as IE with ASV:
<?php
header("Content-type: image/svg+xml");
$graph_title = 'title';
print('<?xml version="1.0" encoding="iso-8859-1" standalone="no"?>');
$svgwidth=500;
$svgheight=400;
?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "
http://www.w3.org/TR/SVG/DTD/svg10.dtd">
<svg width="<?php echo $svgwidth; ?>px" height="<?php echo $svgheight;
?>px"
xmlns="http://www.w3.org/2000/svg">
<desc>This is a php-random rectangle test</desc>
<?php
srand((double) microtime() * 1000000); //initalizing random generator
for ($i = 0; $i < 20; $i+=1) {
$x = floor(rand(0,$svgwidth-1)); //avoid getting a range 0..0 for rand
function
$y = floor(rand(0,$svgheight-1));
$width = floor(rand(0,$svgwidth-$x)); //avoid getting rect outside of
viewbox
$height = floor(rand(0,$svgheight-$y));
$red = floor(rand(0,255));
$blue = floor(rand(0,255));
$green = floor(rand(0,255));
$color = "rgb(".$red.",".$green.",".$
blue.")";
print "\t<rect x=\"$x\" y=\"$y\" width=\"$width\" height=\"$height\"
style=\"fill:$color;\"/>\n";
}
?>
<text x="<?php echo $svgwidth/2;?>px" y="300" style="font-size:15;"
text-anchor="middle">The servers Date and Time is: <?php print
(strftime("%Y-%m-%d, %H:%M:%S")); ?></text>
<text x="<?php echo $svgwidth/2;?>px" y="340" style="font-size:15;"
text-anchor="middle">You are running:</text>
<text x="<?php echo $svgwidth/2;?>px" y="360" style="font-size:15;"
text-anchor="middle"><?php print $HTTP_USER_AGENT; ?></text>
</svg>
If now I want to include the session_start() at the beginning of the code,
in IE I got a pop-up dialog called "download file"
What am I doing wrong ?
Regards,
Aurelie
It appears IE does not support svg yet and you need a plugin for it.
However, you could also design your code differently by using Imagemagick to
convert the svg to png.
If that suits your needs, then use the modified code below:
<?php
header("Content-type: image/png");
$graph_title = 'title';
$svgwidth=500;
$svgheight=400;
$svg = '<?xml version="1.0" encoding="iso-8859-1" standalone="no"?>
<svg width="'.$svgwidth.'px" height="'.$svgheight.'px"
xmlns="http://www.w3.org/2000/svg">
<desc>This is a php-random rectangle test</desc>';
for ($i = 0; $i < 20; $i++) {
$x = floor(rand(0,$svgwidth-1));
$y = floor(rand(0,$svgheight-1));
$width = floor(rand(0,$svgwidth-$x));
$height = floor(rand(0,$svgheight-$y));
$red = floor(rand(0,255));
$blue = floor(rand(0,255));
$green = floor(rand(0,255));
$color = "rgb(".$red.",".$green.",".$blue.")";
$svg .= "\t<rect x=\"$x\" y=\"$y\" width=\"$width\" height=\"$height\"
style=\"fill:$color;\"/>\n";
}
$svg .= '<text x="'.($svgwidth/2).'px" y="300" style="font-size:15;"
text-anchor="middle">The servers Date and Time is: '.date("Y-m-d,
H:m:s").'</text>
<text x="'.($svgwidth/2).'px" y="340" style="font-size:15;"
text-anchor="middle">You are running:</text>
<text x="'.($svgwidth/2).'px" y="360" style="font-size:15;"
text-anchor="middle">'.$HTTP_USER_AGENT.'</text>
</svg>';
file_put_contents('/tmp/image.svg', $svg);
exec("/usr/bin/rsvg /tmp/image.svg /tmp/image.png");
echo file_get_contents('/tmp/image.png');
?>
-Ray Solomon
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php