Skip to content

Commit 8d217ed

Browse files
author
Martin Brecht-Precht
committed
Made the approximate size of PNG and TIFF renderers configurable.
1 parent db948dd commit 8d217ed

File tree

6 files changed

+134
-32
lines changed

6 files changed

+134
-32
lines changed

README.md

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,34 @@ use QrCodeSuite\QrRender\Color\RgbColor;
5151
use QrCodeSuite\QrRender\Color\CmykColor;
5252
5353
// Render the encoded QR code block data as RGB PNG
54+
// Width and height should measure about 800 pixels
5455
$renderer = new QrRender\QrCodeRendererPng();
5556
$renderer
57+
->setApproximateSize(800)
5658
->setForegroundColor(new RgbColor(255, 0, 255))
5759
->setBackgroundColor(new RgbColor(51, 51, 51))
5860
->render($qrCodeData, 'path/to/qr-code.png');
5961
62+
// Get the effective width and height of the rendered image
63+
$imageWidth = $renderer->getWidth();
64+
$imageHeight = $renderer->getHeight();
65+
6066
// Render the encoded QR code block data as CMYK TIFF
67+
// Width and height should measure about 800 pixels
6168
$renderer = new QrRender\QrCodeRendererTiff();
6269
$renderer
70+
->setApproximateSize(800)
6371
->setForegroundColor(new CmykColor(0, 100, 0, 0))
6472
->setBackgroundColor(new CmykColor(0, 100, 100, 0))
6573
->render($qrCodeData, 'path/to/qr-code.tif');
6674
75+
// Get the effective width and height of the rendered image
76+
$imageWidth = $renderer->getWidth();
77+
$imageHeight = $renderer->getHeight();
78+
6779
// Render the encoded QR code block data as CMYK vectorized EPS
6880
// EPS has no background color. It is just the blocks on blank cnavas.
81+
// EPS also has no approximate size. Scale the vectorized image as you like.
6982
$renderer = new QrRender\QrCodeRendererEps();
7083
$renderer
7184
->setForegroundColor(new CmykColor(0, 100, 0, 0))
@@ -79,14 +92,6 @@ $renderer
7992
Contributing to our projects is always very appreciated.
8093
**But: please follow the contribution guidelines written down in the `CONTRIBUTING.md` document.**
8194

82-
## TODOs
83-
84-
- ~~Decorate the code base with some unit tests.~~
85-
- Allow configuration of the QR codes like:
86-
- ~~Foreground color~~
87-
- ~~Background color~~
88-
- Image size of PNG and TIFF
89-
9095
## License
9196

9297
QR Code Suite is under the MIT license.

src/QrRender/QrCodeRendererEps.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ class QrCodeRendererEps implements Base\QrCodeRendererInterface
2020
const POINTS_PER_BLOCK = 5;
2121

2222
/**
23-
* @var int
23+
* @var CmykColor
2424
*/
25-
private $epsHeight;
25+
private $foregroundColor;
2626

2727
/**
28-
* @var CmykColor
28+
* @var int
2929
*/
30-
private $foregroundColor;
30+
private $epsHeight;
3131

3232
/**
3333
* @return CmykColor

src/QrRender/QrCodeRendererPng.php

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,21 @@ class QrCodeRendererPng implements Base\QrCodeRendererInterface
2626
*/
2727
private $backgroundColor;
2828

29+
/**
30+
* @var int
31+
*/
32+
private $approximateSize;
33+
34+
/**
35+
* @var int
36+
*/
37+
private $width;
38+
39+
/**
40+
* @var int
41+
*/
42+
private $height;
43+
2944
/**
3045
* QrCodeRendererPng constructor.
3146
*/
@@ -71,6 +86,43 @@ public function setBackgroundColor(RgbColor $backgroundColor)
7186
return $this;
7287
}
7388

89+
/**
90+
* @return int
91+
*/
92+
public function getApproximateSize()
93+
{
94+
return $this->approximateSize;
95+
}
96+
97+
/**
98+
* @param int $approximateSize
99+
* @return $this
100+
*/
101+
public function setApproximateSize($approximateSize)
102+
{
103+
if (!is_int($approximateSize) || $approximateSize < 0 || $approximateSize > 5000) {
104+
throw new \InvalidArgumentException('Approximate size has to be a positive integer less than 5000');
105+
}
106+
$this->approximateSize = $approximateSize;
107+
return $this;
108+
}
109+
110+
/**
111+
* @return int
112+
*/
113+
public function getWidth()
114+
{
115+
return $this->width;
116+
}
117+
118+
/**
119+
* @return int
120+
*/
121+
public function getHeight()
122+
{
123+
return $this->height;
124+
}
125+
74126
/**
75127
* @param QrCode $qrCode
76128
* @param string $filename
@@ -87,17 +139,17 @@ public function render(QrCode $qrCode, $filename)
87139
$height = $qrCode->getHeight();
88140

89141
// Calculate params
90-
$blockSize = ceil(1000 / ($width + 2 * self::MARGIN));
91-
$symbolWidth = ($width + 2 * self::MARGIN) * $blockSize;
92-
$symbolHeight = ($height + 2 * self::MARGIN) * $blockSize;
142+
$blockSize = round($this->approximateSize / ($width + 2 * self::MARGIN));
143+
$this->width = ($width + 2 * self::MARGIN) * $blockSize;
144+
$this->height = ($height + 2 * self::MARGIN) * $blockSize;
93145

94146
// Define colors
95147
$black = new \ImagickPixel($this->foregroundColor->getHex());
96148
$white = new \ImagickPixel($this->backgroundColor->getHex());
97149

98150
// Prepare canvas
99151
$canvas = new \Imagick();
100-
$canvas->newImage($symbolWidth, $symbolHeight, $white, "png");
152+
$canvas->newImage($this->width, $this->height, $white, "png");
101153
$canvas->setImageColorspace(\Imagick::COLORSPACE_RGB);
102154
$canvas->setImageDepth(8);
103155

src/QrRender/QrCodeRendererTiff.php

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,21 @@ class QrCodeRendererTiff implements Base\QrCodeRendererInterface
2626
*/
2727
private $backgroundColor;
2828

29+
/**
30+
* @var int
31+
*/
32+
private $approximateSize;
33+
34+
/**
35+
* @var int
36+
*/
37+
private $width;
38+
39+
/**
40+
* @var int
41+
*/
42+
private $height;
43+
2944
/**
3045
* QrCodeRendererTiff constructor.
3146
*/
@@ -72,6 +87,40 @@ public function setBackgroundColor(CmykColor $backgroundColor)
7287
return $this;
7388
}
7489

90+
/**
91+
* @return int
92+
*/
93+
public function getApproximateSize()
94+
{
95+
return $this->approximateSize;
96+
}
97+
98+
/**
99+
* @param int $approximateSize
100+
* @return $this
101+
*/
102+
public function setApproximateSize($approximateSize)
103+
{
104+
$this->approximateSize = $approximateSize;
105+
return $this;
106+
}
107+
108+
/**
109+
* @return int
110+
*/
111+
public function getWidth()
112+
{
113+
return $this->width;
114+
}
115+
116+
/**
117+
* @return int
118+
*/
119+
public function getHeight()
120+
{
121+
return $this->height;
122+
}
123+
75124
/**
76125
* @param QrCode $qrCode
77126
* @param string $filename
@@ -88,17 +137,17 @@ public function render(QrCode $qrCode, $filename)
88137
$height = $qrCode->getHeight();
89138

90139
// Calculate params
91-
$blockSize = ceil(1000 / ($width + 2 * self::MARGIN));
92-
$symbolWidth = ($width + 2 * self::MARGIN) * $blockSize;
93-
$symbolHeight = ($height + 2 * self::MARGIN) * $blockSize;
140+
$blockSize = ceil($this->approximateSize / ($width + 2 * self::MARGIN));
141+
$this->width = ($width + 2 * self::MARGIN) * $blockSize;
142+
$this->height = ($height + 2 * self::MARGIN) * $blockSize;
94143

95144
// Define colors
96145
$black = new \ImagickPixel($this->foregroundColor->getImagickNotation());
97146
$white = new \ImagickPixel($this->backgroundColor->getImagickNotation());
98147

99148
// Prepare canvas
100149
$canvas = new \Imagick();
101-
$canvas->newImage($symbolWidth, $symbolHeight, $white, "tiff");
150+
$canvas->newImage($this->width, $this->height, $white, "tiff");
102151
$canvas->setImageColorspace(\Imagick::COLORSPACE_CMYK);
103152
$canvas->setImageDepth(8);
104153

test/QrRender/QrCodeRendererPngTest.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public function testRender()
2828
$qrCodeOutputPath = __DIR__ . '/tmp/qrcode-test.png';
2929
$qrCodeRendererPng = new QrCodeRendererPng();
3030
$qrCodeRendererPng
31+
->setApproximateSize(800)
3132
->setForegroundColor(new RgbColor(255, 0, 255))
3233
->setBackgroundColor(new RgbColor(0, 0, 0))
3334
->render($qrCode, $qrCodeOutputPath);
@@ -36,12 +37,9 @@ public function testRender()
3637
$this->assertFileExists($qrCodeOutputPath);
3738

3839
// Test QR code output file mesaurement
39-
$blockSize = ceil(1000 / ($qrCode->getWidth() + 2 * QrCodeRendererPng::MARGIN));
40-
$symbolWidth = ($qrCode->getWidth() + 2 * QrCodeRendererPng::MARGIN) * $blockSize;
41-
$symbolHeight = ($qrCode->getHeight() + 2 * QrCodeRendererPng::MARGIN) * $blockSize;
4240
$imageSize = getimagesize($qrCodeOutputPath);
43-
$this->assertEquals($symbolWidth, $imageSize[0]);
44-
$this->assertEquals($symbolHeight, $imageSize[1]);
41+
$this->assertEquals($qrCodeRendererPng->getWidth(), $imageSize[0]);
42+
$this->assertEquals($qrCodeRendererPng->getHeight(), $imageSize[1]);
4543

4644
// Remove test QR code output file
4745
unlink($qrCodeOutputPath);

test/QrRender/QrCodeRendererTiffTest.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ public function testRender()
2626

2727
// Render image
2828
$qrCodeOutputPath = __DIR__ . '/tmp/qrcode-test.tif';
29-
$qrCodeRendererPng = new QrCodeRendererTiff();
30-
$qrCodeRendererPng
29+
$qrCodeRendererTiff = new QrCodeRendererTiff();
30+
$qrCodeRendererTiff
31+
->setApproximateSize(800)
3132
->setForegroundColor(new CmykColor(60, 80, 0, 0))
3233
->setBackgroundColor(new CmykColor(40, 100, 100, 70))
3334
->render($qrCode, $qrCodeOutputPath);
@@ -36,12 +37,9 @@ public function testRender()
3637
$this->assertFileExists($qrCodeOutputPath);
3738

3839
// Test QR code output file mesaurement
39-
$blockSize = ceil(1000 / ($qrCode->getWidth() + 2 * QrCodeRendererPng::MARGIN));
40-
$symbolWidth = ($qrCode->getWidth() + 2 * QrCodeRendererPng::MARGIN) * $blockSize;
41-
$symbolHeight = ($qrCode->getHeight() + 2 * QrCodeRendererPng::MARGIN) * $blockSize;
4240
$imageSize = getimagesize($qrCodeOutputPath);
43-
$this->assertEquals($symbolWidth, $imageSize[0]);
44-
$this->assertEquals($symbolHeight, $imageSize[1]);
41+
$this->assertEquals($qrCodeRendererTiff->getWidth(), $imageSize[0]);
42+
$this->assertEquals($qrCodeRendererTiff->getHeight(), $imageSize[1]);
4543

4644
// Remove test QR code output file
4745
unlink($qrCodeOutputPath);

0 commit comments

Comments
 (0)