]> _ Git - cubist_util.git/commitdiff
wip #5936 @0.25
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 22 May 2023 13:27:03 +0000 (15:27 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 22 May 2023 13:27:03 +0000 (15:27 +0200)
src/Graphics/Image.php

index 6ac535c33ea99d0ec31fab45c804fcd3e12389d5..fb9381a5874bb7de4b47d1b181acf4c1b0933e61 100644 (file)
@@ -102,4 +102,32 @@ class Image
         }
         return $res;
     }
+
+    /**
+     * @throws \Exception
+     */
+    public static function fixSVGDimensions($source, $preserveAspectRatio = "none")
+    {
+        $fixed = str_replace('.svg', '.f.svg', $source);
+        if (file_exists($fixed) && filemtime($fixed) >= filemtime($source)) {
+            return $fixed;
+        }
+        $svg = simplexml_load_string(file_get_contents($source));
+        if (!$svg) {
+            copy($source, $fixed);
+            return $fixed;
+        }
+        $attr = $svg->attributes();
+        $dim = self::getimagesize($source);
+        if ($preserveAspectRatio) {
+            $svg->addAttribute('preserveAspectRatio', $preserveAspectRatio);
+        }
+        if (!isset($attr['width'], $attr['height'])) {
+            $svg->addAttribute('width', $dim[0]);
+            $svg->addAttribute('height', $dim[1]);
+        }
+        file_put_contents($fixed, $svg->asXML());
+
+        return $fixed;
+    }
 }
\ No newline at end of file