I fell in love with PHP when I was a kid, about twenty years ago. I have built a few full stack projects since then, but I never really found the time to give anything back to the community. This is my first attempt at doing that.

The idea came from my day job. I run oowia print on demand, and SVG paths turn up in our work more often than you might expect. Whenever I needed to measure a path, find the point where two curves cross, or reshape something a designer had drawn, the library I reached for was svgpathtools. It is a brilliant Python package. The awkward part was that my application is written in PHP.

So I did the slightly unreasonable thing and ported the whole library to PHP.

The result is svgpathtools-PHP, a native PHP library for parsing, inspecting, transforming and generating SVG paths. No Python, no NumPy, no SciPy anywhere in the runtime.

Why not just call Python from PHP?

I tried to talk myself out of writing a port for a while. The usual options are to shell out to a Python script, keep a small service running, or reach for FFI. Each one works in a demo and becomes a nuisance in production. You inherit a second runtime to deploy and monitor, you pay for a process launch on every request, and error handling gets weird at the boundary. Shared hosting makes the whole idea moot.

I wanted an SVG path library for PHP that behaved like a normal PHP library. Install it with Composer, include the autoloader, call a method, get a result. That is the bar I set for myself.

What the port actually covers

I did not want to ship a toy that handles only straight lines and calls it a day. The port aims for full API parity with the Python original, so code you already know transfers over with mostly naming changes.

You get the complete SVG path grammar: M, L, H, V, C, S, Q, T, A and Z, in absolute and relative form, with implicit commands and degenerate arcs handled. All five segment types are there (Line, QuadraticBezier, CubicBezier, Arc and the Path collection) and they share one consistent API.

On top of that you get points, derivatives, tangents, normals, curvature, length and inverse length, bounding boxes, radial ranges, area, intersections, cropping and splitting. Transforms support rotation, translation, scaling, arbitrary 3 by 3 affine matrices and SVG transform strings. For input and output there is file and string parsing with group and transform handling, SVG writing, and document editing through both DOM and XMLReader. The polynomial, Bézier, root finding, quadrature, colour and smoothing helpers came along too.

If your search has been for something like "parse SVG path in PHP" or "generate SVG paths with PHP", this is the library I wish had existed when I first went looking.

A taste of the API

Install it the usual way:

git clone https://github.com/gnomedster/svgpathtools-PHP.git svgpathtools-php cd svgpathtools-php composer install 

Then parse a path and ask it questions:

<?php require 'vendor/autoload.php'; use SvgPathTools\Facade; $path = Facade::parsePath('M 100,100 L 300,100 L 200,300 Z'); echo count($path); // 3 segments echo $path->d(); // M 100,100 L 300,100 L 200,300 L 100,100 echo $path->length(); // 647.21359549996 [$xmin, $xmax, $ymin, $ymax] = $path->bbox(); $point = $path->point(0.5); $transform = Facade::parseTransform('translate(10,20) rotate(30)'); $rotated = $path->transformed($transform); 

A Path behaves like a Python list of segments. It implements ArrayAccess, IteratorAggregate and Countable, so indexing, foreach and count all work the way you would expect. Segment properties such as start, end, control1 and control2 are public and mutable.

The bits that were genuinely fun

Python uses complex as its point and vector type all over svgpathtools. PHP has no complex primitive and no operator overloading, so I wrote a small immutable Complex class and express arithmetic as method chains like $a->add($b)->mul($t). It reads differently, but the maths stays close to the original.

The numeric dependencies needed the most care. numpy.poly1d became a Polynomial class with complex coefficients, derivatives, integrals and arithmetic. numpy.roots became a deterministic Aberth-Ehrlich root finder written in pure PHP. scipy.integrate.quad became adaptive Simpson quadrature, backed by the same recursive length fallback the upstream code uses. Numpy arrays for transforms became a compact 3 by 3 homogeneous Matrix3 class, and svgwrite was replaced with direct DOMDocument output.

One small place where the port goes beyond the original: upstream leaves Arc::radialrange() unimplemented. Here it is implemented analytically and checked against brute force sampling.

How I know it is not quietly lying to me

Porting numerical code is scary because it can look fine and still be wrong by a fraction of a percent. I did not want to guess, so I built a differential harness that runs the same cases through both the Python original and the PHP port and compares the results.

Right now that means 135 PHPUnit tests with 1,381 assertions, 824 differential checks with zero failures, and 58 extra checks for the arc radial range work. The comparisons cover lengths, bounding boxes, sampled points, derivatives, tangents, curvature, intersections, inverse arc length, radial ranges, cropped lengths and enclosed areas.

The golden values are committed in the repository, so anyone can run the whole verification suite with PHP alone. You only need a Python checkout if you want to regenerate those fixtures as a maintainer.

Things worth knowing before you use it

The library needs PHP 8.1 or newer plus the dom, xmlreader and mbstring extensions. I wrote it with 8.1 level features only, deliberately avoiding anything introduced later, so it runs happily on the 8.5 series. All the checks above currently pass on PHP 8.5.13.

The only real adjustment for anyone coming from the Python version is the arithmetic syntax. Since PHP cannot overload operators, you call methods instead of writing a + b. Everything else maps over closely.

Full architecture notes, including why each dependency was replaced, live in PHP_PORT.md.

Give it a try

If you work with SVG in a PHP application, I would love for you to try svgpathtools-PHP and tell me what breaks. Stars, issues and pull requests all help a first open source project grow.

I built this because SVG path tools saved me real time at oowia, and I wanted the PHP side of my stack to have the same advantages. If it saves you some time too, the port has done its job.