]> _ Git - fluidbook_tools.git/commitdiff
wait #6750 @0.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Sat, 24 Feb 2024 13:10:34 +0000 (14:10 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Sat, 24 Feb 2024 13:10:34 +0000 (14:10 +0100)
src/SVG/SVGTools.php

index 562d40531e7155ea199aa92e7b8728eb76967dd1..ad3e299ed2370ee3f3e7784969a8db9f956715f5 100644 (file)
@@ -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('<svg ', '<svg xmlns="http://www.w3.org/2000/svg" ', $svg);
         }
         $svg = self::_disablePreserveRatio($svg);
+
         file_put_contents($out, $svg);
+        if (isset($tmp)) {
+            Files::unlink($tmp);
+        }
         return $out;
     }