From 0142405dfffa5778a29cecb61d68aa3e5a7a66d4 Mon Sep 17 00:00:00 2001 From: Jonah Lawrence Date: Thu, 19 Jan 2023 15:25:11 -0700 Subject: [PATCH] feat: Support inkscape and imagick for PNG conversion --- src/card.php | 56 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 39 insertions(+), 17 deletions(-) diff --git a/src/card.php b/src/card.php index 52d52fd1..86691af9 100644 --- a/src/card.php +++ b/src/card.php @@ -494,23 +494,45 @@ function convertSvgToPng(string $svg): string $svg = preg_replace("/(animation: currstreak[^;'\"]+)/m", "font-size: 28px;", $svg); $svg = preg_replace("/(\X*?)<\/a>/m", '\1', $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 495 -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 495 -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 pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

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:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy