Skip to content

Instantly share code, notes, and snippets.

@JouriR
Last active January 20, 2022 09:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JouriR/8d89de9fa515e5cc81395149abb633be to your computer and use it in GitHub Desktop.
Save JouriR/8d89de9fa515e5cc81395149abb633be to your computer and use it in GitHub Desktop.
A PHP script that converts an SVG to a PNG image using Inkscape.
<?php
$contentType = $_SERVER["CONTENT_TYPE"];
if ($contentType == "application/json") {
$rawData = file_get_contents("php://input");
$json = json_decode($rawData, true);
$data = $json["svg"];
} else {
$data = file_get_contents("input.svg");
}
$svgFilename = tempnam("/tmp", "svg");
file_put_contents($svgFilename, $data);
$pngFileName = tempnam("/tmp", "png");
exec("inkscape --without-gui --export-area-drawing --export-background='#ffffff' --export-png=$pngFileName $svgFilename");
$fp = fopen($pngFileName, "rb");
header("Content-Type: image/png");
header("Content-Length: " . filesize($pngFileName));
fpassthru($fp);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment