@@ -352,8 +352,6 @@ function generateErrorCard(string $message, array $params = null): string
352
352
* @param string $svg The SVG for the card as a string
353
353
*
354
354
* @return string The generated PNG data
355
- *
356
- * @throws ImagickException
357
355
*/
358
356
function convertSvgToPng (string $ svg ): string
359
357
{
@@ -367,22 +365,20 @@ function convertSvgToPng(string $svg): string
367
365
$ svg = preg_replace ("/(animation: currentstreak.*?;)/m " , "font-size: 28px; " , $ svg );
368
366
$ svg = preg_replace ("/<a \X*?>(\X*?)<\/a>/m " , '\1 ' , $ svg );
369
367
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 );
378
371
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
+ }
385
377
378
+ // read png data and delete temporary files
379
+ $ png = file_get_contents ("{$ filename }.png " );
380
+ unlink ("{$ filename }.svg " );
381
+ unlink ("{$ filename }.png " );
386
382
return $ png ;
387
383
}
388
384
0 commit comments