diff --git a/src/card.php b/src/card.php index b38ec9b7..99314dcb 100644 --- a/src/card.php +++ b/src/card.php @@ -697,23 +697,45 @@ function convertSvgToPng(string $svg, int $cardWidth): string // remove style and animations $svg = removeAnimations($svg); - // escape svg for shell - $svg = escapeshellarg($svg); - - // `--pipe`: read input from pipe (stdin) - // `--export-filename -`: write output to stdout - // `-w 495 -h 195`: set width and height of the output image - // `--export-type png`: set the output format to PNG - $cmd = "echo {$svg} | inkscape --pipe --export-filename - -w {$cardWidth} -h 195 --export-type png"; - - // convert svg to png - $png = shell_exec($cmd); // skipcq: PHP-A1009 - - // check if the conversion was successful - if (empty($png)) { - // `2>&1`: redirect stderr to stdout - $error = shell_exec("$cmd 2>&1"); // skipcq: PHP-A1009 - throw new InvalidArgumentException("Failed to convert SVG to PNG: {$error}", 500); + // check if inkscape is installed + if (shell_exec("which inkscape")) { + // skipcq: PHP-A1009 + // escape svg for shell + $svg = escapeshellarg($svg); + + // `--pipe`: read input from pipe (stdin) + // `--export-filename -`: write output to stdout + // `-w 495 -h 195`: set width and height of the output image + // `--export-type png`: set the output format to PNG + $cmd = "echo {$svg} | inkscape --pipe --export-filename - -w {$cardWidth} -h 195 --export-type png"; + + // convert svg to png + $png = shell_exec($cmd); // skipcq: PHP-A1009 + + // check if the conversion was successful + if (empty($png)) { + // `2>&1`: redirect stderr to stdout + $error = shell_exec("$cmd 2>&1"); // skipcq: PHP-A1009 + throw new Exception("Failed to convert SVG to PNG: {$error}", 500); + } + } elseif (class_exists("Imagick")) { + // create canvas + $imagick = new Imagick(); + $imagick->setBackgroundColor(new ImagickPixel("transparent")); + + // add svg image + $imagick->setFormat("svg"); + $imagick->readImageBlob('' . $svg); + $imagick->setFormat("png"); + + // get PNG data + $png = $imagick->getImageBlob(); + + // clean up memory + $imagick->clear(); + $imagick->destroy(); + } else { + throw new Exception("Inkscape or Imagick is required to convert SVG to PNG", 500); } // return the generated png
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: