Skip to content

Commit

Permalink
Add SVG writer option for excluding width and height attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
endroid committed Aug 21, 2022
1 parent 1aaaa51 commit 3668147
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/Writer/SvgWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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('<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"/>');
$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');

Expand Down

0 comments on commit 3668147

Please sign in to comment.