Home / Blog / Calculating SVG bounds in PHP

Calculating SVG bounds in PHP

17 Apr 2013

Scalable Vector Graphics (SVG) is an image file format increasingly used on the internet. For a recent project, we needed to calculate the bounding box of the shape of a country in an SVG file and we created this simple PHP class to help. Maybe you’ll find it useful too.

Note that it’s a very simple implementation of the SVG path specification. It only understands straight line segments, not arcs and Bezier curves. For our purposes, this was enough, since the curves in country outlines are usually modelled as small facets rather than true curves.

You can get the code from GitHub.

Example usage:

$bounds = SvgBounds::fromPath('M 100 100L300 100 200 300z');
echo $bounds->getWidth(); //200
$bounds->extend(350, 100);
echo $bounds->getWidth(); //250