I'm trying to modify SVG files via a PHP script and at one point I need to calculate the length of some path elements.
In JavaScript, there's the handy function .getTotalLength()
, but I wasn't able to find a PHP parser for SVG that offers something similiar, so I guess I'll have to manually parse the d
-attribute and calculate the paths.
Loading the XML, getting the attribute etc. is not the problem, but I'm not very good at mathematics and especially not in the field of vectors. Let's take this example:
<path d="m 268.87448,476.05362 c -25.0135,11.49888 -35.53725,21.39636 -29.14819,47.61221 -0.79153,89.53821 7.5531,195.5936 -11.71546,284.63588 -24.02796,67.23588 -51.61983,89.1027 -112.32477,124.52935 0,0 -38.977804,16.40902 -54.19931,20.52688" />
What I would do first is explode
the path by spaces and iterate through the result, then check whether it's a letter or a number and act accordingly. And that's where I'm stuck and don't even know how to begin.
I have read the SVG path specs at https://www.w3.org/TR/SVG/paths.html - but still can't get my mind around the number crunching.