]> _ Git - fluidbook_tools.git/commitdiff
wip #6167 @0.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 23 Aug 2023 15:31:00 +0000 (17:31 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 23 Aug 2023 15:31:00 +0000 (17:31 +0200)
src/SVG/SVGTools.php

index a948022cd5b5b4a3a63e9cf4617dffd7a4c86a91..43c18b6eefdfdca4a9a1bd2814b2703b98f02f03 100644 (file)
@@ -7,13 +7,15 @@ use Cubist\Util\Files\Files;
 use Cubist\Util\PHP;
 use Fluidbook\Tools\FluidbookTools;
 
-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);
@@ -22,10 +24,15 @@ class SVGTools {
             $out = implode('.', $e);
         }
 
+        if (file_exists($out) && filemtime($out) >= filemtime($in) && filesize($out) === 0) {
+            return $out;
+        }
+
         return self::_optimizeSVG($in, $out);
     }
 
-    public static function optimizeSVGStr($in, $fix = false) {
+    public static function optimizeSVGStr($in, $fix = false)
+    {
 
         $dir = Files::mkdir(sys_get_temp_dir() . '/svgopt');
         `rm $dir/*.svg`;
@@ -49,7 +56,8 @@ class SVGTools {
         return $res;
     }
 
-    public static function _optimizeSVG($in, $out) {
+    public static function _optimizeSVG($in, $out)
+    {
 
         $beforeOpt = str_replace('.svg', '.orig.svg', $in);
         if (file_exists($beforeOpt) && is_link($beforeOpt)) {
@@ -61,12 +69,14 @@ class SVGTools {
         if (!$svg) {
             $svg = file_get_contents($in);
         }
+        $svg = str_replace('<svg ', '<svg xmlns="http://www.w3.org/2000/svg" ', $svg);
         $svg = self::_disablePreserveRatio($svg);
         file_put_contents($out, $svg);
         return $out;
     }
 
-    protected static function _disablePreserveRatio($in) {
+    protected static function _disablePreserveRatio($in)
+    {
         $str = 'preserveAspectRatio="none"';
         if (str_contains($in, $str)) {
             return $in;
@@ -74,7 +84,8 @@ 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);
@@ -86,7 +97,8 @@ 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;
@@ -110,7 +122,8 @@ class SVGTools {
         return $c;
     }
 
-    public static function optimizeRaster($matches, $resolution) {
+    public static function optimizeRaster($matches, $resolution)
+    {
         PHP::neverStop();
         preg_match_all('/([a-z\:\-]*)="([^"]*)"/', $matches[1], $m);
 
@@ -152,7 +165,8 @@ class SVGTools {
         return $res;
     }
 
-    public static function fix($source, $dest) {
+    public static function fix($source, $dest)
+    {
         $cli = new CommandLine('oslllo-svg-fixer');
         $cli->setArg('s', $source);
         $cli->setArg('d', $dest);
@@ -163,7 +177,8 @@ class SVGTools {
         }
     }
 
-    public static function append($first, $second, $dest, $direction = 'h') {
+    public static function append($first, $second, $dest, $direction = 'h')
+    {
         $cli = new CommandLine('python3');
         $cli->setArg(null, '/usr/local/svg_stack/svg_stack.py');
         $cli->setArg('direction', $direction);