Get outer coordinates of a SVG path (getBBox)?
I need to get the coordinates (x,y of top-left corner and width/height) of a SVG path in PHP.
I mean the 开发者_JAVA百科d-attribute in a path node of an SVG-file, somehting like this:
<path d="M 100 100 L 300 100 L 200 300 z" />
My paths are quite complex to do this myself (some have absolute and/or relative coordinates, possible curves, etc.)
Are there any libraries for this kind of work in PHP?
If your shape doesn't use arcs or beziers (many shapes actually use straight lines to approximate curves) then you can use this PHP function to calculate the bounding box: http://pastebin.com/8eLQccbg
I've been looking for a solution for this as well and this is the best I found so far: http://rrbits.com/epb/2011/02/06/get-the-bounding-box-of-an-svg-path/
It converts the SVG into a bitmap and then finds the bbox using ImageMagic. Been thinking about writing one myself using the d coordinates but I'm afraid it will fail on curves etc.
I'm thinking this solution to the problem is so simple that it must be wrong ;)
. But, although I've not tried it, it looks pretty good to me. Just install inkscape
on your server, and then run this:
inkscape --without-gui --query-all /path/to/doc.svg
According to the help, this will List id,x,y,w,h for all objects
- sounds like what I need, and also what is being asked for here! Sample output:
Layer_1,1.8032746e-06,-0.002,780.37099,455.91999
g4,0.090994976,-72.489183,72.445768,72.454697
polygon6,2.125,-70.896,69,69
polygon8,2.125,-70.896,69,69
g10,0.090994976,-72.489183,64.778768,3.5796973
path12,61.423995,-72.489183,3.4457675,3.5796973
path14,53.756995,-72.489183,3.4457675,3.5796973
path16,46.090995,-72.489183,3.4457675,3.5796973
path18,38.423995,-72.489183,3.4457675,3.5796973
....
This is quick enough to call in a web request, but if you've much in the way of load, you could push it to a background process very easily.
Not pure PHP, but an option that worked for me was to use http://phantomjs.org to get the SVG bounding box.
精彩评论