]> _ Git - fluidbook_tools.git/commitdiff
wip #5811 @0.25
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 15 Mar 2023 17:57:55 +0000 (18:57 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 15 Mar 2023 17:57:55 +0000 (18:57 +0100)
src/SVG/SVGTools.php

index c7c5b5fd4049ecc294aa8288f65aa177835a8a2a..53f4512b3fcd840afd25ed0fd3538ed5987d6e52 100644 (file)
@@ -2,18 +2,18 @@
 
 namespace Fluidbook\Tools\SVG;
 
+use Cubist\Util\CommandLine\Inkscape;
 use Cubist\Util\Files\Files;
 use Fluidbook\Tools\FluidbookTools;
+use Illuminate\Validation\Rules\In;
 
-class SVGTools
-{
+class SVGTools {
     protected static $_r;
     protected static $_i;
     protected static $_e;
     protected static $_u;
 
-    public static function optimizeSVG($in, $out = null)
-    {
+    public static function optimizeSVG($in, $out = null) {
         if (null === $out) {
             $e = explode('.', $in);
             $ext = array_pop($e);
@@ -25,20 +25,21 @@ class SVGTools
         return self::_optimizeSVG($in, $out);
     }
 
-    public static function optimizeSVGStr($in)
-    {
+    public static function optimizeSVGStr($in, $strokeToPaths = false) {
         $infile = Files::tempnam() . '.svg';
         file_put_contents($infile, $in);
         $outfile = Files::tempnam() . '.svg';
         self::optimizeSVG($infile, $outfile);
+        if ($strokeToPaths) {
+            self::strokeToPaths($outfile);
+        }
         $res = file_get_contents($outfile);
         unlink($infile);
         unlink($outfile);
         return $res;
     }
 
-    public static function _optimizeSVG($in, $out)
-    {
+    public static function _optimizeSVG($in, $out) {
 
         $svg = shell_exec('timeout -s 1 120 ' . FluidbookTools::tools_path('svgcleaner/svgcleaner', true) . ' --allow-bigger-file --paths-coordinates-precision 3 --copy-on-error --stdout ' . $in);
         if (!$svg) {
@@ -49,8 +50,7 @@ class SVGTools
         return $out;
     }
 
-    protected static function _disablePreserveRatio($in)
-    {
+    protected static function _disablePreserveRatio($in) {
         $str = 'preserveAspectRatio="none"';
         if (str_contains($in, $str)) {
             return $in;
@@ -58,8 +58,7 @@ class SVGTools
         return str_replace("<svg ", '<svg ' . $str . ' ', $in);
     }
 
-    public static function optimizeSVGImages($in, $out, $resolution)
-    {
+    public static function optimizeSVGImages($in, $out, $resolution) {
         $svg = file_get_contents($in);
         $svg = preg_replace('/\<\?xml([^\?]*)\?\>/', '', $svg);
         $svg = self::_disablePreserveRatio($svg);
@@ -71,8 +70,7 @@ class SVGTools
         file_put_contents($out, $osvg);
     }
 
-    protected static function _svg($c, $p)
-    {
+    protected static function _svg($c, $p) {
         self::$_i = 0;
         self::$_e = 0;
         self::$_u = 0;
@@ -96,8 +94,7 @@ class SVGTools
         return $c;
     }
 
-    public static function optimizeRaster($matches, $resolution)
-    {
+    public static function optimizeRaster($matches, $resolution) {
         preg_match_all('/([a-z\:\-]*)="([^"]*)"/', $matches[1], $m);
 
         foreach ($m[1] as $i => $key) {
@@ -137,4 +134,10 @@ class SVGTools
         $res .= '/>';
         return $res;
     }
+
+    public static function strokeToPaths($svgfile) {
+        $inkscape = new Inkscape();
+        $inkscape->strokeToPath($svgfile);
+        $inkscape->execute();
+    }
 }