Skip to content

Commit a19b7f4

Browse files
committed
🛀 examples cleanup
1 parent 17e8cca commit a19b7f4

19 files changed

+582
-550
lines changed

examples/authenticator.php

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* authenticator.php
3+
* authenticator excample
44
*
55
* @created 09.07.2023
66
* @author smiley <[email protected]>
@@ -16,48 +16,46 @@
1616

1717
require_once __DIR__.'/../vendor/autoload.php';
1818

19-
// the options array
20-
$optionsArray = [
21-
/*
22-
* AuthenticatorOptionsTrait
23-
*
24-
* @see https://github.com/chillerlan/php-authenticator
25-
*/
26-
'mode' => AuthenticatorInterface::TOTP,
27-
'digits' => 8,
28-
'algorithm' => AuthenticatorInterface::ALGO_SHA512,
19+
// create a new options container on the fly that hosts both, authenticator and qrcode
20+
$options = new class extends SettingsContainerAbstract{
21+
use AuthenticatorOptionsTrait, QROptionsTrait;
22+
};
23+
24+
/*
25+
* AuthenticatorOptionsTrait
26+
*
27+
* @see https://github.com/chillerlan/php-authenticator
28+
*/
29+
$options->mode = AuthenticatorInterface::TOTP;
30+
$options->digits = 8;
31+
$options->algorithm = AuthenticatorInterface::ALGO_SHA512;
2932

30-
/*
31-
* QROptionsTrait
32-
*/
33-
'version' => 7,
34-
'addQuietzone' => false,
35-
'imageBase64' => false,
36-
'svgAddXmlHeader' => false,
37-
'cssClass' => 'my-qrcode',
38-
'drawLightModules' => false,
39-
'markupDark' => '',
40-
'markupLight' => '',
41-
'drawCircularModules' => true,
42-
'circleRadius' => 0.4,
43-
'connectPaths' => true,
44-
'keepAsSquare' => [
45-
QRMatrix::M_FINDER_DARK,
46-
QRMatrix::M_FINDER_DOT,
47-
QRMatrix::M_ALIGNMENT_DARK,
48-
],
49-
'svgDefs' => '
33+
/*
34+
* QROptionsTrait
35+
*/
36+
$options->version = 7;
37+
$options->addQuietzone = false;
38+
$options->imageBase64 = false;
39+
$options->svgAddXmlHeader = false;
40+
$options->cssClass = 'my-qrcode';
41+
$options->drawLightModules = false;
42+
$options->markupDark = '';
43+
$options->markupLight = '';
44+
$options->drawCircularModules = true;
45+
$options->circleRadius = 0.4;
46+
$options->connectPaths = true;
47+
$options->keepAsSquare = [
48+
QRMatrix::M_FINDER_DARK,
49+
QRMatrix::M_FINDER_DOT,
50+
QRMatrix::M_ALIGNMENT_DARK,
51+
];
52+
$options->svgDefs = '
5053
<linearGradient id="gradient" x1="1" y2="1">
5154
<stop id="stop1" offset="0" />
5255
<stop id="stop2" offset="0.5"/>
5356
<stop id="stop3" offset="1"/>
54-
</linearGradient>',
55-
];
57+
</linearGradient>';
5658

57-
// create a new options container on the fly that hosts both, authenticator and qrcode
58-
$options = new class($optionsArray) extends SettingsContainerAbstract{
59-
use AuthenticatorOptionsTrait, QROptionsTrait;
60-
};
6159

6260
// invoke the worker instances
6361
$authenticator = new Authenticator($options);

examples/custom_output.php

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22
/**
3+
* custom output example
4+
*
35
* @created 24.12.2017
46
* @author Smiley <[email protected]>
57
* @copyright 2017 Smiley
@@ -24,7 +26,7 @@ class MyCustomOutput extends QROutputAbstract{
2426
* @inheritDoc
2527
*/
2628
public static function moduleValueIsValid($value):bool{
27-
// TODO: Implement moduleValueIsValid() method. (abstract)
29+
// TODO: Implement moduleValueIsValid() method. (interface)
2830
return false;
2931
}
3032

@@ -68,15 +70,16 @@ public function dump(string $file = null):string{
6870
* Runtime
6971
*/
7072

73+
$options = new QROptions;
74+
75+
$options->version = 5;
76+
$options->eccLevel = EccLevel::L;
77+
7178
$data = 'https://www.youtube.com/watch?v=DLzxrzFCyOs&t=43s';
7279

7380
// invoke the QROutputInterface manually
7481
// please note that an QROutputInterface invoked this way might become unusable after calling dump().
7582
// the clean way would be to extend the QRCode class to ensure a new QROutputInterface instance on each call to render().
76-
$options = new QROptions([
77-
'version' => 5,
78-
'eccLevel' => EccLevel::L,
79-
]);
8083

8184
$qrcode = new QRCode($options);
8285
$qrcode->addByteSegment($data);
@@ -85,14 +88,9 @@ public function dump(string $file = null):string{
8588

8689
var_dump($qrOutputInterface->dump());
8790

88-
8991
// or just via the options
90-
$options = new QROptions([
91-
'version' => 5,
92-
'eccLevel' => EccLevel::L,
93-
'outputType' => QROutputInterface::CUSTOM,
94-
'outputInterface' => MyCustomOutput::class,
95-
]);
92+
$options->outputType = QROutputInterface::CUSTOM;
93+
$options->outputInterface = MyCustomOutput::class;
9694

9795
var_dump((new QRCode($options))->render($data));
9896

examples/eps.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22
/**
3+
* EPS output example
4+
*
35
* @created 10.05.2022
46
* @author Smiley <[email protected]>
57
* @copyright 2022 Smiley
@@ -18,6 +20,7 @@
1820
$options->outputType = QROutputInterface::EPS;
1921
$options->scale = 5;
2022
$options->drawLightModules = false;
23+
// colors can be specified either as [R, G, B] or [C, M, Y, K] (0-255)
2124
$options->bgColor = [222, 222, 222];
2225
$options->moduleValues = [
2326
// finder
@@ -45,17 +48,19 @@
4548
QRMatrix::M_SEPARATOR => [233, 233, 233],
4649
// quietzone
4750
QRMatrix::M_QUIETZONE => [233, 233, 233],
48-
// logo (requires a call to QRMatrix::setLogoSpace()), see QRImageWithLogo
51+
// logo space (requires a call to QRMatrix::setLogoSpace()), see imageWithLogo example
4952
QRMatrix::M_LOGO => [233, 233, 233],
5053
];
5154

5255

56+
$out = (new QRCode($options))->render('https://www.youtube.com/watch?v=dQw4w9WgXcQ', __DIR__.'/qrcode.eps');
57+
5358
if(php_sapi_name() !== 'cli'){
5459
// if viewed in the browser, we should push it as file download as EPS isn't usually supported
5560
header('Content-type: application/postscript');
5661
header('Content-Disposition: filename="qrcode.eps"');
5762
}
5863

59-
echo (new QRCode($options))->render('https://www.youtube.com/watch?v=dQw4w9WgXcQ', __DIR__.'/test.eps');
64+
echo $out;
6065

6166
exit;

examples/fpdf.php

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,62 @@
11
<?php
22
/**
3+
* FPDF output example
4+
*
35
* @created 03.06.2020
46
* @author Maximilian Kresse
57
* @license MIT
68
*/
79

810
use chillerlan\QRCode\{QRCode, QROptions};
9-
use chillerlan\QRCode\Common\EccLevel;
1011
use chillerlan\QRCode\Data\QRMatrix;
1112
use chillerlan\QRCode\Output\QROutputInterface;
1213

1314
require_once __DIR__ . '/../vendor/autoload.php';
1415

15-
$options = new QROptions([
16-
'version' => 7,
17-
'outputType' => QROutputInterface::FPDF,
18-
'eccLevel' => EccLevel::L,
19-
'scale' => 5,
20-
'bgColor' => [234, 234, 234],
21-
'drawLightModules' => false,
22-
'imageBase64' => false,
23-
'moduleValues' => [
24-
// finder
25-
QRMatrix::M_FINDER_DARK => [0, 63, 255], // dark (true)
26-
QRMatrix::M_FINDER_DOT => [0, 63, 255], // finder dot, dark (true)
27-
QRMatrix::M_FINDER => [255, 255, 255], // light (false), white is the transparency color and is enabled by default
28-
// alignment
29-
QRMatrix::M_ALIGNMENT_DARK => [255, 0, 255],
30-
QRMatrix::M_ALIGNMENT => [255, 255, 255],
31-
// timing
32-
QRMatrix::M_TIMING_DARK => [255, 0, 0],
33-
QRMatrix::M_TIMING => [255, 255, 255],
34-
// format
35-
QRMatrix::M_FORMAT_DARK => [67, 191, 84],
36-
QRMatrix::M_FORMAT => [255, 255, 255],
37-
// version
38-
QRMatrix::M_VERSION_DARK => [62, 174, 190],
39-
QRMatrix::M_VERSION => [255, 255, 255],
40-
// data
41-
QRMatrix::M_DATA_DARK => [0, 0, 0],
42-
QRMatrix::M_DATA => [255, 255, 255],
43-
// darkmodule
44-
QRMatrix::M_DARKMODULE => [0, 0, 0],
45-
// separator
46-
QRMatrix::M_SEPARATOR => [255, 255, 255],
47-
// quietzone
48-
QRMatrix::M_QUIETZONE => [255, 255, 255],
49-
],
50-
]);
16+
$options = new QROptions;
17+
18+
$options->version = 7;
19+
$options->outputType = QROutputInterface::FPDF;
20+
$options->scale = 5;
21+
$options->fpdfMeasureUnit = 'mm';
22+
$options->bgColor = [222, 222, 222];
23+
$options->drawLightModules = false;
24+
$options->imageBase64 = false;
25+
$options->moduleValues = [
26+
// finder
27+
QRMatrix::M_FINDER_DARK => [0, 63, 255], // dark (true)
28+
QRMatrix::M_FINDER_DOT => [0, 63, 255], // finder dot, dark (true)
29+
QRMatrix::M_FINDER => [255, 255, 255], // light (false)
30+
// alignment
31+
QRMatrix::M_ALIGNMENT_DARK => [255, 0, 255],
32+
QRMatrix::M_ALIGNMENT => [255, 255, 255],
33+
// timing
34+
QRMatrix::M_TIMING_DARK => [255, 0, 0],
35+
QRMatrix::M_TIMING => [255, 255, 255],
36+
// format
37+
QRMatrix::M_FORMAT_DARK => [67, 191, 84],
38+
QRMatrix::M_FORMAT => [255, 255, 255],
39+
// version
40+
QRMatrix::M_VERSION_DARK => [62, 174, 190],
41+
QRMatrix::M_VERSION => [255, 255, 255],
42+
// data
43+
QRMatrix::M_DATA_DARK => [0, 0, 0],
44+
QRMatrix::M_DATA => [255, 255, 255],
45+
// darkmodule
46+
QRMatrix::M_DARKMODULE => [0, 0, 0],
47+
// separator
48+
QRMatrix::M_SEPARATOR => [255, 255, 255],
49+
// quietzone
50+
QRMatrix::M_QUIETZONE => [255, 255, 255],
51+
];
52+
53+
54+
$out = (new QRCode($options))->render('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
5155

5256
if(php_sapi_name() !== 'cli'){
5357
header('Content-type: application/pdf');
5458
}
5559

56-
echo (new QRCode($options))->render('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
60+
echo $out;
5761

5862
exit;

examples/html.php

Lines changed: 24 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,48 @@
11
<?php
22
/**
3+
* HTML output example
4+
*
35
* @created 21.12.2017
46
* @author Smiley <[email protected]>
57
* @copyright 2017 Smiley
68
* @license MIT
79
*/
810

911
use chillerlan\QRCode\{QRCode, QROptions};
10-
use chillerlan\QRCode\Common\EccLevel;
1112
use chillerlan\QRCode\Data\QRMatrix;
1213
use chillerlan\QRCode\Output\QROutputInterface;
1314

1415
require_once '../vendor/autoload.php';
1516

16-
header('Content-Type: text/html; charset=utf-8');
17+
$options = new QROptions;
18+
19+
$options->version = 5;
20+
$options->outputType = QROutputInterface::MARKUP_HTML;
21+
$options->cssClass = 'qrcode';
22+
$options->markupDark = '#555';
23+
$options->markupLight = '#CCC';
24+
$options->moduleValues = [
25+
// finder
26+
QRMatrix::M_FINDER_DARK => '#A71111', // dark (true)
27+
QRMatrix::M_FINDER_DOT => '#A71111', // finder dot, dark (true)
28+
QRMatrix::M_FINDER => '#FFBFBF', // light (false)
29+
// alignment
30+
QRMatrix::M_ALIGNMENT_DARK => '#A70364',
31+
QRMatrix::M_ALIGNMENT => '#FFC9C9',
32+
];
33+
1734

18-
$options = new QROptions([
19-
'version' => 5,
20-
'outputType' => QROutputInterface::MARKUP_HTML,
21-
'eccLevel' => EccLevel::L,
22-
'cssClass' => 'qrcode',
23-
'moduleValues' => [
24-
// finder
25-
QRMatrix::M_FINDER_DARK => '#A71111', // dark (true)
26-
QRMatrix::M_FINDER_DOT => '#A71111', // finder dot, dark (true)
27-
QRMatrix::M_FINDER => '#FFBFBF', // light (false)
28-
// alignment
29-
QRMatrix::M_ALIGNMENT_DARK => '#A70364',
30-
QRMatrix::M_ALIGNMENT => '#FFC9C9',
31-
// timing
32-
QRMatrix::M_TIMING_DARK => '#98005D',
33-
QRMatrix::M_TIMING => '#FFB8E9',
34-
// format
35-
QRMatrix::M_FORMAT_DARK => '#003804',
36-
QRMatrix::M_FORMAT => '#00FB12',
37-
// version
38-
QRMatrix::M_VERSION_DARK => '#650098',
39-
QRMatrix::M_VERSION => '#E0B8FF',
40-
// data
41-
QRMatrix::M_DATA_DARK => '#4A6000',
42-
QRMatrix::M_DATA => '#ECF9BE',
43-
// darkmodule
44-
QRMatrix::M_DARKMODULE => '#080063',
45-
// separator
46-
QRMatrix::M_SEPARATOR => '#AFBFBF',
47-
// quietzone
48-
QRMatrix::M_QUIETZONE => '#DDDDDD',
49-
],
50-
]);
35+
$out = (new QRCode($options))->render('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
36+
37+
header('Content-Type: text/html; charset=utf-8');
5138

5239
?>
5340
<!DOCTYPE html>
5441
<html lang="en">
5542
<head>
5643
<meta charset="UTF-8"/>
5744
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
58-
<title>QRCode test</title>
45+
<title>QRCode HTML Example</title>
5946
<style>
6047
div.qrcode{
6148
margin: 1em;
@@ -75,10 +62,6 @@
7562
</style>
7663
</head>
7764
<body>
78-
<?php
79-
80-
echo (new QRCode($options))->render('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
81-
82-
?>
65+
<?php echo $out; ?>
8366
</body>
8467
</html>

0 commit comments

Comments
 (0)