Last active
January 20, 2022 09:32
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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