From a1c8a47249a87f1950886f655673ee614fd9650f Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Mon, 22 May 2023 15:27:03 +0200 Subject: [PATCH] wip #5936 @0.25 --- src/Graphics/Image.php | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/Graphics/Image.php b/src/Graphics/Image.php index 6ac535c..fb9381a 100644 --- a/src/Graphics/Image.php +++ b/src/Graphics/Image.php @@ -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 -- 2.39.5