From a1789d2c245b0002b331ccff36fc0c8c4182eb2d Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Sat, 24 Feb 2024 14:10:34 +0100 Subject: [PATCH] wait #6750 @0.5 --- src/SVG/SVGTools.php | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/SVG/SVGTools.php b/src/SVG/SVGTools.php index 562d405..ad3e299 100644 --- a/src/SVG/SVGTools.php +++ b/src/SVG/SVGTools.php @@ -32,7 +32,7 @@ class SVGTools return $out; } - return self::_optimizeSVG($in, $out); + return self::_optimizeSVG($in, $out, $opt); } public static function optimizeSVGStr($in, $fix = false, $opt = '') @@ -69,15 +69,41 @@ class SVGTools } copy($in, $beforeOpt); - $svg = shell_exec('timeout -s 1 120 ' . FluidbookTools::tools_path('svgcleaner/svgcleaner', true) . ' ' . $opt . ' --allow-bigger-file --paths-coordinates-precision 3 --copy-on-error --stdout ' . $in); + try { + $xmlsvg = simplexml_load_string(file_get_contents($in)); + $attr = $xmlsvg->attributes(); + if (!isset($attr['width']) || !isset($attr['height'])) { + $viewbox = trim($attr['viewBox']); + $e = explode(' ', $viewbox); + if (!isset($attr['width'])) { + $xmlsvg->addAttribute('width', (float)$e[2]); + } + if (!isset($attr['height'])) { + $xmlsvg->addAttribute('height', (float)$e[3]); + } + $tmp = Files::tempnam() . '.svg'; + file_put_contents($tmp, $xmlsvg->asXML()); + } + } catch (\Exception $e) { + + } + + $cmd = 'timeout -s 1 120 ' . FluidbookTools::tools_path('svgcleaner/svgcleaner', true) . ' ' . $opt . ' --allow-bigger-file --paths-coordinates-precision 3 --copy-on-error --stdout ' . ($tmp ?? $in); + $svg = shell_exec($cmd); + if (!$svg) { $svg = file_get_contents($in); } + if (!stristr($svg, 'xmlns="http://www.w3.org/2000/svg"')) { $svg = str_replace('