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\Eps; |
15
|
|
|
use BaconQrCode\Writer; |
16
|
|
|
use Da\QrCode\Contracts\QrCodeInterface; |
17
|
|
|
|
18
|
|
|
class EpsWriter extends AbstractWriter |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* EpsWriter constructor. |
22
|
|
|
*/ |
23
|
|
|
public function __construct() |
24
|
|
|
{ |
25
|
|
|
parent::__construct(new Eps()); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @inheritdoc |
30
|
|
|
*/ |
31
|
|
|
public function writeString(QrCodeInterface $qrCode): string |
32
|
|
|
{ |
33
|
|
|
/** @var Eps $renderer */ |
34
|
|
|
$renderer = $this->renderer; |
35
|
|
|
$renderer->setWidth($qrCode->getSize()); |
36
|
|
|
$renderer->setHeight($qrCode->getSize()); |
37
|
|
|
$renderer->setMargin(0); |
38
|
|
|
$renderer->setForegroundColor($this->convertColor($qrCode->getForegroundColor())); |
39
|
|
|
$renderer->setBackgroundColor($this->convertColor($qrCode->getBackgroundColor())); |
40
|
|
|
$writer = new Writer($renderer); |
41
|
|
|
$string = $writer->writeString( |
42
|
|
|
$qrCode->getText(), |
43
|
|
|
$qrCode->getEncoding(), |
44
|
|
|
$this->convertErrorCorrectionLevel($qrCode->getErrorCorrectionLevel()) |
45
|
|
|
); |
46
|
|
|
$string = $this->addMargin($string, $qrCode); |
47
|
|
|
|
48
|
|
|
return $string; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @inheritdoc |
53
|
|
|
*/ |
54
|
|
|
public function getContentType(): string |
55
|
|
|
{ |
56
|
|
|
return 'image/eps'; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $string |
61
|
|
|
* @param QrCodeInterface $qrCode |
62
|
|
|
* |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
|
|
protected function addMargin(string $string, QrCodeInterface $qrCode): string |
66
|
|
|
{ |
67
|
|
|
$targetSize = $qrCode->getSize() + $qrCode->getMargin() * 2; |
68
|
|
|
$lines = explode("\n", $string); |
69
|
|
|
$sourceBlockSize = 0; |
70
|
|
|
$additionalWhitespace = $qrCode->getSize(); |
71
|
|
|
foreach ($lines as $line) { |
72
|
|
|
if (preg_match('#\d+ \d+ \d+ \d+ F#i', $line) && strpos( |
73
|
|
|
$line, |
74
|
|
|
$qrCode->getSize() . ' ' . $qrCode->getSize() . ' F' |
75
|
|
|
) === false) { |
76
|
|
|
$parts = explode(' ', $line); |
77
|
|
|
$sourceBlockSize = $parts[2]; |
78
|
|
|
$additionalWhitespace = min($additionalWhitespace, $parts[0]); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
$blockCount = ($qrCode->getSize() - 2 * $additionalWhitespace) / $sourceBlockSize; |
82
|
|
|
$targetBlockSize = $qrCode->getSize() / $blockCount; |
83
|
|
|
$newLines =[]; |
84
|
|
|
foreach ($lines as $line) { |
85
|
|
|
if (strpos($line, 'BoundingBox') !== false) { |
86
|
|
|
$newLines[] = '%%BoundingBox: 0 0 ' . $targetSize . ' ' . $targetSize; |
87
|
|
|
} elseif (strpos($line, $qrCode->getSize() . ' ' . $qrCode->getSize() . ' F') !== false) { |
88
|
|
|
$newLines[] = '0 0 ' . $targetSize . ' ' . $targetSize . ' F'; |
89
|
|
|
} elseif (preg_match('#\d+ \d+ \d+ \d+ + F#i', $line)) { |
90
|
|
|
$parts = explode(' ', $line); |
91
|
|
|
$parts[0] = $qrCode->getMargin( |
92
|
|
|
) + $targetBlockSize * ($parts[0] - $additionalWhitespace) / $sourceBlockSize; |
93
|
|
|
$parts[1] = $qrCode->getMargin( |
94
|
|
|
) + $targetBlockSize * ($parts[1] - $sourceBlockSize - $additionalWhitespace) / $sourceBlockSize; |
95
|
|
|
$parts[2] = $targetBlockSize; |
96
|
|
|
$parts[3] = $targetBlockSize; |
97
|
|
|
$newLines[] = implode(' ', $parts); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
$string = implode("\n", $newLines); |
101
|
|
|
|
102
|
|
|
return $string; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|