From: Vincent Vanwaelscappel Date: Wed, 15 Mar 2023 17:57:55 +0000 (+0100) Subject: wip #5811 @0.25 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=98083167b6beb91e5a54cb641fef8b7963bd3645;p=fluidbook_tools.git wip #5811 @0.25 --- diff --git a/src/SVG/SVGTools.php b/src/SVG/SVGTools.php index c7c5b5f..53f4512 100644 --- a/src/SVG/SVGTools.php +++ b/src/SVG/SVGTools.php @@ -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 = 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(); + } }