From 36681470bd10352b53bcb9731bdf2270e0d79b22 Mon Sep 17 00:00:00 2001 From: Jeroen van den Enden Date: Sun, 21 Aug 2022 09:22:43 +0000 Subject: [PATCH] Add SVG writer option for excluding width and height attributes --- src/Writer/SvgWriter.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Writer/SvgWriter.php b/src/Writer/SvgWriter.php index 233c903..29c4108 100644 --- a/src/Writer/SvgWriter.php +++ b/src/Writer/SvgWriter.php @@ -17,6 +17,7 @@ final class SvgWriter implements WriterInterface public const DECIMAL_PRECISION = 10; public const WRITER_OPTION_BLOCK_ID = 'block_id'; public const WRITER_OPTION_EXCLUDE_XML_DECLARATION = 'exclude_xml_declaration'; + public const WRITER_OPTION_EXCLUDE_SVG_WIDTH_AND_HEIGHT = 'exclude_svg_width_and_height'; public const WRITER_OPTION_FORCE_XLINK_HREF = 'force_xlink_href'; public function write(QrCodeInterface $qrCode, LogoInterface $logo = null, LabelInterface $label = null, array $options = []): ResultInterface @@ -29,13 +30,19 @@ public function write(QrCodeInterface $qrCode, LogoInterface $logo = null, Label $options[self::WRITER_OPTION_EXCLUDE_XML_DECLARATION] = false; } + if (!isset($options[self::WRITER_OPTION_EXCLUDE_SVG_WIDTH_AND_HEIGHT])) { + $options[self::WRITER_OPTION_EXCLUDE_SVG_WIDTH_AND_HEIGHT] = false; + } + $matrixFactory = new MatrixFactory(); $matrix = $matrixFactory->create($qrCode); $xml = new \SimpleXMLElement(''); $xml->addAttribute('version', '1.1'); - $xml->addAttribute('width', $matrix->getOuterSize().'px'); - $xml->addAttribute('height', $matrix->getOuterSize().'px'); + if (!$options[self::WRITER_OPTION_EXCLUDE_SVG_WIDTH_AND_HEIGHT]) { + $xml->addAttribute('width', $matrix->getOuterSize().'px'); + $xml->addAttribute('height', $matrix->getOuterSize().'px'); + } $xml->addAttribute('viewBox', '0 0 '.$matrix->getOuterSize().' '.$matrix->getOuterSize()); $xml->addChild('defs');