Generating train illustrations in SVG with PHP
In Ramedex, train cards aren't drawn one by one: one sheet per family (silhouette, livery, composition) and a PHP function that assembles the SVG. A regional livery becomes a line of data, not a file to redo.
Every capture in Ramedex ends with a card, showing the illustration of the train you just caught. Behind those illustrations, one sheet per family and a single PHP function.
The game covers train families that have very little in common. Each has its own silhouette, some come in several compositions, and the regional trains (TER) wear the livery of the region that operates them. Each illustration also has to exist in several contexts, from the thumbnail in the collection to the large format on the shared card. Drawing all of that by hand is a cartesian product of files.
The first idea that comes along is mass export: one silhouette per variant in a graphics tool, then files stored all over the place. It doesn't hold up. Every tweak to a silhouette has to be replayed on each variant. A livery that changes becomes a series of exports to redo, not counting the ones you forget. And nothing keeps the thumbnail and the large format consistent.
One sheet per family
I described each family with a sheet. Three blocks: silhouette , livery , cars . The silhouette points to a drawing template, the shape of the car bodies and the nose. The livery assigns colors to zones of the train, the body, the stripe, the roof. The cars say how many elements follow one another. In simplified form:
$fiche = [
'silhouette' => 'autorail_deux_caisses',
'livery' => [
'body' => '#ffffff',
'stripe' => '#b5252b',
'roof' => '#51504f',
],
'cars' => 2,
];The generator builds the SVG from that. The principle fits in a few lines, much simpler than the real thing:
function trainSvg(array $fiche): string
{
$c = $fiche['livery'];
$svg = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 310 90">';
for ($i = 0; $i < $fiche['cars']; $i++) {
$x = 10 + $i * 150;
$svg .= "<rect x='{$x}' y='24' width='130' height='42' rx='10' fill='{$c['body']}'/>";
$svg .= "<rect x='{$x}' y='24' width='130' height='8' rx='4' fill='{$c['roof']}'/>";
$svg .= "<rect x='{$x}' y='44' width='130' height='9' fill='{$c['stripe']}'/>";
$svg .= "<circle cx='" . ($x + 25) . "' cy='70' r='7' fill='#232323'/>";
$svg .= "<circle cx='" . ($x + 105) . "' cy='70' r='7' fill='#232323'/>";
}
return $svg . '</svg>';
}The real version picks a shape template based on the silhouette and draws more detail, but the idea is there: shapes parameterized by colors, repeated according to the composition. Since the output is SVG, the same string serves every size. A collection thumbnail and a big card display the same markup at different widths, and the vectors keep up. No per-density variants, no resize script. SVG is only text, and PHP knows how to produce text.
Because every illustration comes out of the same code, they look like each other. Same proportions, same rendering of the colors. That's what you want in a collection you flip through.
The region as a parameter
The TER case shows what this buys you. With files, you'd need one illustration per regional livery, then one per livery and per size. With sheets, there's one silhouette and arrays of colors. The same body rendered with a green stripe or a red stripe switches regions without switching files.
If a region modernizes its livery, the fix is one line of data. If I got a shade wrong, I correct it once and it propagates everywhere that livery shows up. The region has become a parameter, on the same footing as the composition.
Two lessons from Blade
Two misadventures with Blade, both in Ramedex, point the same way.
The first comes from JSON-LD. The metadata block on the pages contains a @context key. Blade, Laravel's template engine, saw a directive in it and tried to execute it. The block is now built in PHP, assembled as an array and then encoded.
The second comes from the sitemap. A sitemap starts with an <?xml prologue. In production, where short_open_tag is enabled, PHP takes that <? for an opening tag, and the Blade view crashed. The sitemap is now written in PHP, which returns XML just as calmly as JSON.
Those two formats, and SVG along with them, have something in common. They're machine formats, repetitive, with no layout. A template engine brings its own syntax, its escaping, and its surprise interpretations to them. A function that builds a string brings none of that. I keep Blade for the HTML pages, where it earns its place.
What it costs
Two limits, both accepted. The first is style. Drawing trains as parameterized shapes gives a deliberately simplified look. For a game you read as thumbnails on a phone, readability counts for more than detail, and a spare rendering ages better than a drawing chasing realism. I won't pretend I come out ahead on that front. An illustrator would do better, given the means.
The second limit is upkeep. When a train gets a new livery in the real world, the sheet has to follow. And a mistake in a sheet isn't isolated. It surfaces everywhere the data gets used, long after I introduced it.
There's also a boundary I haven't reached yet. As long as a silhouette fits into simple shapes, the sheet stays data. The day I want train noses with proper linework, the silhouette becomes a drawing, and a drawing isn't maintained like an array of colors. For now, the sheets are enough. Adding a train to an existing family means giving it colors and a composition, not drawing it.