Skip to content

Commit 1616778

Browse files
authored
Merge pull request #255 from sprain/force-xlink-href
Add option to force xlink:href when adding logos in svg
2 parents a7e07d2 + 8696f51 commit 1616778

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

pr.txt

Whitespace-only changes.

src/Writer/SvgWriter.php

+19-4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ class SvgWriter extends AbstractWriter
2121
{
2222
public function writeString(QrCodeInterface $qrCode): string
2323
{
24+
$options = $qrCode->getWriterOptions();
25+
2426
if ($qrCode->getValidateResult()) {
2527
throw new ValidationException('Built-in validation reader can not check SVG images: please disable via setValidateResult(false)');
2628
}
@@ -64,7 +66,13 @@ public function writeString(QrCodeInterface $qrCode): string
6466

6567
$logoPath = $qrCode->getLogoPath();
6668
if (is_string($logoPath)) {
67-
$this->addLogo($svg, $data['outer_width'], $data['outer_height'], $logoPath, $qrCode->getLogoWidth(), $qrCode->getLogoHeight());
69+
70+
$forceXlinkHref = false;
71+
if (isset($options['force_xlink_href']) && $options['force_xlink_href']) {
72+
$forceXlinkHref = true;
73+
}
74+
75+
$this->addLogo($svg, $data['outer_width'], $data['outer_height'], $logoPath, $qrCode->getLogoWidth(), $qrCode->getLogoHeight(), $forceXlinkHref);
6876
}
6977

7078
$xml = $svg->asXML();
@@ -73,15 +81,15 @@ public function writeString(QrCodeInterface $qrCode): string
7381
throw new GenerateImageException('Unable to save SVG XML');
7482
}
7583

76-
$options = $qrCode->getWriterOptions();
84+
7785
if (isset($options['exclude_xml_declaration']) && $options['exclude_xml_declaration']) {
7886
$xml = str_replace("<?xml version=\"1.0\"?>\n", '', $xml);
7987
}
8088

8189
return $xml;
8290
}
8391

84-
private function addLogo(SimpleXMLElement $svg, int $imageWidth, int $imageHeight, string $logoPath, int $logoWidth = null, int $logoHeight = null): void
92+
private function addLogo(SimpleXMLElement $svg, int $imageWidth, int $imageHeight, string $logoPath, int $logoWidth = null, int $logoHeight = null, bool $forceXlinkHref = false): void
8593
{
8694
$mimeType = $this->getMimeType($logoPath);
8795
$imageData = file_get_contents($logoPath);
@@ -125,7 +133,14 @@ private function addLogo(SimpleXMLElement $svg, int $imageWidth, int $imageHeigh
125133
$imageDefinition->addAttribute('width', strval($logoWidth));
126134
$imageDefinition->addAttribute('height', strval($logoHeight));
127135
$imageDefinition->addAttribute('preserveAspectRatio', 'none');
128-
$imageDefinition->addAttribute('xlink:href', 'data:'.$mimeType.';base64,'.base64_encode($imageData));
136+
137+
// xlink:href is actually deprecated, but still required when placing the qr code in a pdf.
138+
// SimpleXML strips out the xlink part by using addAttribute(), so it must be set directly.
139+
if ($forceXlinkHref) {
140+
$imageDefinition['xlink:href'] = 'data:'.$mimeType.';base64,'.base64_encode($imageData);
141+
} else {
142+
$imageDefinition->addAttribute('href', 'data:'.$mimeType.';base64,'.base64_encode($imageData));
143+
}
129144
}
130145

131146
private function getOpacity(int $alpha): float

0 commit comments

Comments
 (0)