1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the 2amigos/qrcode-library project. |
5
|
|
|
* |
6
|
|
|
* (c) 2amigOS! <http://2amigos.us/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view |
9
|
|
|
* the LICENSE file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Da\QrCode\Writer; |
13
|
|
|
|
14
|
|
|
use BaconQrCode\Renderer\Image\Svg; |
15
|
|
|
use BaconQrCode\Writer; |
16
|
|
|
use Da\QrCode\Contracts\QrCodeInterface; |
17
|
|
|
use SimpleXMLElement; |
18
|
|
|
|
19
|
|
|
class SvgWriter extends AbstractWriter |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* SvgWriter constructor. |
23
|
|
|
*/ |
24
|
|
|
public function __construct() |
25
|
|
|
{ |
26
|
|
|
parent::__construct(new Svg()); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @inheritdoc |
31
|
|
|
*/ |
32
|
|
|
public function writeString(QrCodeInterface $qrCode): string |
33
|
|
|
{ |
34
|
|
|
/** @var Svg $renderer */ |
35
|
|
|
$renderer = $this->renderer; |
36
|
|
|
$renderer->setWidth($qrCode->getSize()); |
37
|
|
|
$renderer->setHeight($qrCode->getSize()); |
38
|
|
|
$renderer->setMargin(0); |
39
|
|
|
$renderer->setForegroundColor($this->convertColor($qrCode->getForegroundColor())); |
40
|
|
|
$renderer->setBackgroundColor($this->convertColor($qrCode->getBackgroundColor())); |
41
|
|
|
$writer = new Writer($renderer); |
42
|
|
|
|
43
|
|
|
$string = $writer->writeString( |
44
|
|
|
$qrCode->getText(), |
45
|
|
|
$qrCode->getEncoding(), |
46
|
|
|
$this->convertErrorCorrectionLevel($qrCode->getErrorCorrectionLevel()) |
47
|
|
|
); |
48
|
|
|
|
49
|
|
|
$string = $this->addMargin($string, $qrCode->getMargin(), $qrCode->getSize()); |
50
|
|
|
|
51
|
|
|
return $string; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
|
|
public function getContentType(): string |
58
|
|
|
{ |
59
|
|
|
return 'image/svg+xml'; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $string |
64
|
|
|
* @param int $margin |
65
|
|
|
* @param int $size |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
protected function addMargin(string $string, int $margin, int $size): string |
69
|
|
|
{ |
70
|
|
|
$targetSize = $size + $margin * 2; |
71
|
|
|
$xml = new SimpleXMLElement($string); |
72
|
|
|
|
73
|
|
|
$xml['width'] = $targetSize; |
74
|
|
|
$xml['height'] = $targetSize; |
75
|
|
|
$xml['viewBox'] = '0 0 '.$targetSize.' '.$targetSize; |
76
|
|
|
$xml->rect['width'] = $targetSize; |
77
|
|
|
$xml->rect['height'] = $targetSize; |
78
|
|
|
$additionalWhitespace = $targetSize; |
79
|
|
|
|
80
|
|
|
foreach ($xml->use as $block) { |
81
|
|
|
$additionalWhitespace = min($additionalWhitespace, (int) $block['x']); |
82
|
|
|
} |
83
|
|
|
$sourceBlockSize = (int) $xml->defs->rect['width']; |
84
|
|
|
$blockCount = ($size - 2 * $additionalWhitespace) / $sourceBlockSize; |
85
|
|
|
$targetBlockSize = $size / $blockCount; |
86
|
|
|
$xml->defs->rect['width'] = $targetBlockSize; |
87
|
|
|
$xml->defs->rect['height'] = $targetBlockSize; |
88
|
|
|
|
89
|
|
|
foreach ($xml->use as $block) { |
90
|
|
|
$block['x'] = $margin + $targetBlockSize * ($block['x'] - $additionalWhitespace) / $sourceBlockSize; |
91
|
|
|
$block['y'] = $margin + $targetBlockSize * ($block['y'] - $additionalWhitespace) / $sourceBlockSize; |
92
|
|
|
} |
93
|
|
|
return $xml->asXML(); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|