]> _ Git - fluidbook_tools.git/commitdiff
wip #6753 @1
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 27 Feb 2024 11:03:45 +0000 (12:03 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 27 Feb 2024 11:03:45 +0000 (12:03 +0100)
src/SVG/SVGTools.php

index ad3e299ed2370ee3f3e7784969a8db9f956715f5..c8cdaf82f34b7e2c25a079d3da5cd8271d56c373 100644 (file)
@@ -6,6 +6,7 @@ use Cubist\Util\CommandLine;
 use Cubist\Util\Files\Files;
 use Cubist\Util\PHP;
 use Fluidbook\Tools\FluidbookTools;
+use Illuminate\Support\Facades\Log;
 
 class SVGTools
 {
@@ -69,8 +70,31 @@ class SVGTools
         }
         copy($in, $beforeOpt);
 
+        $cmd = 'timeout -s 1 120 ' . FluidbookTools::tools_path('svgcleaner/svgcleaner', true) . ' ' . $opt . ' --allow-bigger-file --paths-coordinates-precision 3 --copy-on-error --stdout ' . $in;
+        $svg = shell_exec($cmd);
+
+        Log::debug($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);
+        return $out;
+    }
+
+    public static function addDimensionsAttributes($in)
+    {
+        if (!strstr($in, '<svg') && file_exists($in)) {
+            $in = file_get_contents($in);
+        }
         try {
-            $xmlsvg = simplexml_load_string(file_get_contents($in));
+            $xmlsvg = simplexml_load_string($in);
             $attr = $xmlsvg->attributes();
             if (!isset($attr['width']) || !isset($attr['height'])) {
                 $viewbox = trim($attr['viewBox']);
@@ -81,30 +105,12 @@ class SVGTools
                 if (!isset($attr['height'])) {
                     $xmlsvg->addAttribute('height', (float)$e[3]);
                 }
-                $tmp = Files::tempnam() . '.svg';
-                file_put_contents($tmp, $xmlsvg->asXML());
+                return preg_replace('\<\?xml.*\?\>', '', $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;
+        return self::_disablePreserveRatio($in);
     }
 
     protected static function _disablePreserveRatio($in)