svg 2 gif/png

svg 2 gif/png

am 03.03.2010 11:37:30 von Michael Peters

I'm moving all of my dynamic image generation to svg.
Not only does it look better, but it is less resource intensive on my
server allowing me to generate the svg on demand instead of
pre-generating (via cron) twice a month like I had to do with gd dynamic
generation.

However, some browsers *cough*IE*cough* do not support SVG, so I still
need png or gif fallback (I'll decide which after investigating size
difference).

Example SVG to convert -

http://www.shastaherps.org/map/map22.svg

Using convert from ImageMagick in the CLI is fast enough that I may just
use the ImageMagick php module and do the fallback dynamic for the IE
users, but I've run into a bit of a snag - it seems that ImageMagick
doesn't understand xlink.

IE running

convert --size=800x574 map22.svg map22.png

on above file results in a nice pretty map with the county and text, but
the colored hexagons are not displayed.

Is there an easy way around this? IE a php class/function that
understands SVG w/ xlink and can replace the use tags with the polygons
they refer to? If not, I'll have to try to write one, but I'd rather not ...

Thanks for suggestions. It is too bad ImageMagick doesn't understand the
use tag and xlink, that is one of the more useful features of SVG that I
have personally found, makes dynamic generation so much easier.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: svg 2 gif/png

am 03.03.2010 12:25:51 von Michael Peters

Michael A. Peters wrote:
*snip*
>
> Is there an easy way around this? IE a php class/function that
> understands SVG w/ xlink and can replace the use tags with the polygons
> they refer to? If not, I'll have to try to write one, but I'd rather not
> ...

I just did, haven't tested yet, but this may work -

function use2polygon($use,$polygon) {
// get the xy coords
$x = 0; $y = 0;
if ($use->hasAttribute('x') {
$x = 0 + $use->getAttribute('x');
}
if ($use->hasAttribute('y') {
$y = 0 + $use->getAttribute('y');
}
// clone the polygon
if ($polygon->hasAttribute('points') {
$points = preg_replace('/\s+/',' ',$polygon->getAttribute('points'));
$pointArray = explode(' ',$points);
$sizeof = sizeof($pointArray);
for ($i=0;$i<$sizeof;$i++) {
$foo = explode(',',$pointArray[$i]);
$foo[0] = $x + $foo[0];
$foo[1] = $y + $foo[1];
$pointArray[$i] = $foo[0] . ',' . $foo[1];
}
$points = implode(' ',$points);
$newPolygon = $polygon->cloneNode(true);
$newPolygon->setAttribute('points',$points);
$use->parentNode->replaceChild($newPolygon,$use);
}
}

of course that requires domdocument and probably requires looping
through the dom node list backwards, but hopefully that will do it.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: svg 2 gif/png

am 03.03.2010 12:48:06 von Michael Peters

Michael A. Peters wrote:
> Michael A. Peters wrote:
> *snip*
>>
>> Is there an easy way around this? IE a php class/function that
>> understands SVG w/ xlink and can replace the use tags with the
>> polygons they refer to? If not, I'll have to try to write one, but I'd
>> rather not ...
>
> I just did, haven't tested yet, but this may work -

This does work (fixed version of function):

function use2polygon($use,$polygon) {
// get the xy coords
$x = 0; $y = 0;
if ($use->hasAttribute('x')) {
$x = 0 + $use->getAttribute('x');
}
if ($use->hasAttribute('y')) {
$y = 0 + $use->getAttribute('y');
}
// clone the polygon
if ($polygon->hasAttribute('points')) {
$points = preg_replace('/\s+/',' ',$polygon->getAttribute('points'));
$pointArray = explode(' ',$points);
$sizeof = sizeof($pointArray);
for ($i=0;$i<$sizeof;$i++) {
$foo = explode(',',$pointArray[$i]);
$foo[0] = $x + $foo[0];
$foo[1] = $y + $foo[1];
$pointArray[$i] = $foo[0] . ',' . $foo[1];
}
$points = implode(' ',$pointArray);
$newPolygon = $polygon->cloneNode(true);
$newPolygon->setAttribute('points',$points);
$use->parentNode->replaceChild($newPolygon,$use);
}
}

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: svg 2 gif/png

am 03.03.2010 13:18:22 von Michael Peters

Michael A. Peters wrote:
> Michael A. Peters wrote:
>> Michael A. Peters wrote:
>> *snip*
>>>
>>> Is there an easy way around this? IE a php class/function that
>>> understands SVG w/ xlink and can replace the use tags with the
>>> polygons they refer to? If not, I'll have to try to write one, but
>>> I'd rather not ...
>>
>> I just did, haven't tested yet, but this may work -
>
> This does work (fixed version of function):
>
> function use2polygon($use,$polygon) {
> // get the xy coords
> $x = 0; $y = 0;
> if ($use->hasAttribute('x')) {
> $x = 0 + $use->getAttribute('x');
> }
> if ($use->hasAttribute('y')) {
> $y = 0 + $use->getAttribute('y');
> }
> // clone the polygon
> if ($polygon->hasAttribute('points')) {
> $points = preg_replace('/\s+/',' ',$polygon->getAttribute('points'));
> $pointArray = explode(' ',$points);
> $sizeof = sizeof($pointArray);
> for ($i=0;$i<$sizeof;$i++) {
> $foo = explode(',',$pointArray[$i]);
> $foo[0] = $x + $foo[0];
> $foo[1] = $y + $foo[1];
> $pointArray[$i] = $foo[0] . ',' . $foo[1];
> }
> $points = implode(' ',$pointArray);
> $newPolygon = $polygon->cloneNode(true);
> $newPolygon->setAttribute('points',$points);
> $use->parentNode->replaceChild($newPolygon,$use);
> }
> }
>

doh - for xml sanity, add this before the replaceChild

if ($newPolygon->hasAttribute('id')) {
$newPolygon->removeAttribute('id');
}

Now it works, and makes identical svg image that works with convert.

http://www.shastaherps.org/map/map22.svg vs
http://www.shastaherps.org/map/map22.svg?convert=true

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php