Skip to content

Commit bd505b0

Browse files
authored
fix: Use inkscape to convert svg to png (#298)
1 parent 31cf436 commit bd505b0

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed

Aptfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
inkscape

app.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,16 @@
1616
"quantity": 1,
1717
"size": "free"
1818
}
19-
}
19+
},
20+
"buildpacks": [
21+
{
22+
"url": "https://github.com/heroku/heroku-buildpack-apt"
23+
},
24+
{
25+
"url": "https://github.com/DenverCoder1/heroku-buildpack-fonts-segoe-ui"
26+
},
27+
{
28+
"url": "heroku/php"
29+
}
30+
]
2031
}

src/card.php

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,6 @@ function generateErrorCard(string $message, array $params = null): string
352352
* @param string $svg The SVG for the card as a string
353353
*
354354
* @return string The generated PNG data
355-
*
356-
* @throws ImagickException
357355
*/
358356
function convertSvgToPng(string $svg): string
359357
{
@@ -367,22 +365,20 @@ function convertSvgToPng(string $svg): string
367365
$svg = preg_replace("/(animation: currentstreak.*?;)/m", "font-size: 28px;", $svg);
368366
$svg = preg_replace("/<a \X*?>(\X*?)<\/a>/m", '\1', $svg);
369367

370-
// create canvas
371-
$imagick = new Imagick();
372-
$imagick->setBackgroundColor(new ImagickPixel("transparent"));
373-
374-
// add svg image
375-
$imagick->setFormat("svg");
376-
$imagick->readImageBlob('<?xml version="1.0" encoding="UTF-8" standalone="no"?>' . $svg);
377-
$imagick->setFormat("png");
368+
// save svg to random file
369+
$filename = uniqid();
370+
file_put_contents("$filename.svg", $svg);
378371

379-
// get PNG data
380-
$png = $imagick->getImageBlob();
381-
382-
// clean up memory
383-
$imagick->clear();
384-
$imagick->destroy();
372+
// convert svg to png
373+
$out = shell_exec("inkscape -w 495 -h 195 {$filename}.svg -o {$filename}.png"); // skipcq: PHP-A1009
374+
if ($out !== null) {
375+
throw new Exception("Error converting SVG to PNG: $out");
376+
}
385377

378+
// read png data and delete temporary files
379+
$png = file_get_contents("{$filename}.png");
380+
unlink("{$filename}.svg");
381+
unlink("{$filename}.png");
386382
return $png;
387383
}
388384

0 commit comments

Comments
 (0)