Get external coordinates of SVG path (getBBox)?

I need to get the coordinates (x, y of the upper left corner and width / height) of an SVG path in PHP.

I mean the d attribute in the node path of the SVG file, something like:

<path d="M 100 100 L 300 100 L 200 300 z" /> 

My paths are quite complicated for this myself (some have absolute and / or relative coordinates, possible curves, etc.)

Are there libraries for this kind of work in PHP?

+2
source share
4 answers

I was looking for a solution for this, and this is the best I have found so far: http://rrbits.com/epb/2011/02/06/get-the-bounding-box-of-an-svg-path/

It converts the SVG to a bitmap, and then finds the bbox using ImageMagic. I thought about writing myself using the d coordinates, but I'm afraid it won't work on curves, etc.

+1
source

I think this solution to the problem is so simple that it should be wrong ;) . But, although I have not tried it, it seems to me very good. Just install inkscape on your server and then run this:

 inkscape --without-gui --query-all /path/to/doc.svg 

According to the help, it will be List id,x,y,w,h for all objects - it sounds as I need, as well as what is asked here! Output Example:

 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 fast enough to trigger a web request, but if you have many ways to download, you can easily do it in the background.

+1
source

If your shape does not use arcs or beziers (many shapes actually use straight lines to approximate curves), you can use this PHP function to calculate the bounding box: http://pastebin.com/8eLQccbg

+1
source

Not pure PHP, but the option that worked for me was to use http://phantomjs.org to get the SVG bounding box.

0
source

Source: https://habr.com/ru/post/908110/


All Articles