Skip to content

Commit

Permalink
Merge pull request endroid#255 from sprain/force-xlink-href
Browse files Browse the repository at this point in the history
Add option to force xlink:href when adding logos in svg
  • Loading branch information
endroid authored Jun 19, 2020
2 parents a7e07d2 + 8696f51 commit 1616778
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
Empty file added pr.txt
Empty file.
23 changes: 19 additions & 4 deletions src/Writer/SvgWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class SvgWriter extends AbstractWriter
{
public function writeString(QrCodeInterface $qrCode): string
{
$options = $qrCode->getWriterOptions();

if ($qrCode->getValidateResult()) {
throw new ValidationException('Built-in validation reader can not check SVG images: please disable via setValidateResult(false)');
}
Expand Down Expand Up @@ -64,7 +66,13 @@ public function writeString(QrCodeInterface $qrCode): string

$logoPath = $qrCode->getLogoPath();
if (is_string($logoPath)) {
$this->addLogo($svg, $data['outer_width'], $data['outer_height'], $logoPath, $qrCode->getLogoWidth(), $qrCode->getLogoHeight());

$forceXlinkHref = false;
if (isset($options['force_xlink_href']) && $options['force_xlink_href']) {
$forceXlinkHref = true;
}

$this->addLogo($svg, $data['outer_width'], $data['outer_height'], $logoPath, $qrCode->getLogoWidth(), $qrCode->getLogoHeight(), $forceXlinkHref);
}

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

$options = $qrCode->getWriterOptions();

if (isset($options['exclude_xml_declaration']) && $options['exclude_xml_declaration']) {
$xml = str_replace("<?xml version=\"1.0\"?>\n", '', $xml);
}

return $xml;
}

private function addLogo(SimpleXMLElement $svg, int $imageWidth, int $imageHeight, string $logoPath, int $logoWidth = null, int $logoHeight = null): void
private function addLogo(SimpleXMLElement $svg, int $imageWidth, int $imageHeight, string $logoPath, int $logoWidth = null, int $logoHeight = null, bool $forceXlinkHref = false): void
{
$mimeType = $this->getMimeType($logoPath);
$imageData = file_get_contents($logoPath);
Expand Down Expand Up @@ -125,7 +133,14 @@ private function addLogo(SimpleXMLElement $svg, int $imageWidth, int $imageHeigh
$imageDefinition->addAttribute('width', strval($logoWidth));
$imageDefinition->addAttribute('height', strval($logoHeight));
$imageDefinition->addAttribute('preserveAspectRatio', 'none');
$imageDefinition->addAttribute('xlink:href', 'data:'.$mimeType.';base64,'.base64_encode($imageData));

// xlink:href is actually deprecated, but still required when placing the qr code in a pdf.
// SimpleXML strips out the xlink part by using addAttribute(), so it must be set directly.
if ($forceXlinkHref) {
$imageDefinition['xlink:href'] = 'data:'.$mimeType.';base64,'.base64_encode($imageData);
} else {
$imageDefinition->addAttribute('href', 'data:'.$mimeType.';base64,'.base64_encode($imageData));
}
}

private function getOpacity(int $alpha): float
Expand Down

0 comments on commit 1616778

Please sign in to comment.