|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @created 05.03.2022 |
| 4 | + * @author smiley <[email protected]> |
| 5 | + * @copyright 2022 smiley |
| 6 | + * @license MIT |
| 7 | + * |
| 8 | + * @noinspection PhpComposerExtensionStubsInspection |
| 9 | + */ |
| 10 | + |
| 11 | +namespace chillerlan\QRCodeExamples; |
| 12 | + |
| 13 | +use chillerlan\QRCode\{QRCode, QRCodeException, QROptions}; |
| 14 | +use chillerlan\QRCode\Data\QRMatrix; |
| 15 | +use chillerlan\QRCode\Common\EccLevel; |
| 16 | +use function file_exists, gzencode, header, is_readable, max, min; |
| 17 | + |
| 18 | +require_once __DIR__.'/../vendor/autoload.php'; |
| 19 | + |
| 20 | +$data = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ'; |
| 21 | +$gzip = true; |
| 22 | + |
| 23 | +$options_arr = [ |
| 24 | + // SVG logo options (see extended class below) |
| 25 | + 'svgLogo' => __DIR__.'/github.svg', // logo from: https://github.com/simple-icons/simple-icons |
| 26 | + 'svgLogoScale' => 0.25, |
| 27 | + 'svgLogoCssClass' => 'dark', |
| 28 | + // QROptions |
| 29 | + 'version' => 5, |
| 30 | + 'outputType' => QRCode::OUTPUT_CUSTOM, |
| 31 | + 'outputInterface' => QRSvgWithLogo::class, |
| 32 | + 'imageBase64' => false, |
| 33 | + // ECC level H is necessary when using logos |
| 34 | + 'eccLevel' => EccLevel::H, |
| 35 | + 'addQuietzone' => true, |
| 36 | + // if set to true, the light modules won't be rendered |
| 37 | + 'imageTransparent' => false, |
| 38 | + // empty the default value to remove the fill* attributes from the <path> elements |
| 39 | + 'markupDark' => '', |
| 40 | + 'markupLight' => '', |
| 41 | + // draw the modules as circles isntead of squares |
| 42 | + 'drawCircularModules' => true, |
| 43 | + 'circleRadius' => 0.45, |
| 44 | + // connect paths |
| 45 | + 'svgConnectPaths' => true, |
| 46 | + // keep modules of thhese types as square |
| 47 | + 'keepAsSquare' => [ |
| 48 | + QRMatrix::M_FINDER|QRMatrix::IS_DARK, |
| 49 | + QRMatrix::M_FINDER_DOT, |
| 50 | + QRMatrix::M_ALIGNMENT|QRMatrix::IS_DARK, |
| 51 | + ], |
| 52 | + // https://developer.mozilla.org/en-US/docs/Web/SVG/Element/linearGradient |
| 53 | + 'svgDefs' => ' |
| 54 | + <linearGradient id="gradient" x1="100%" y2="100%"> |
| 55 | + <stop stop-color="#D70071" offset="0"/> |
| 56 | + <stop stop-color="#9C4E97" offset="0.5"/> |
| 57 | + <stop stop-color="#0035A9" offset="1"/> |
| 58 | + </linearGradient> |
| 59 | + <style><![CDATA[ |
| 60 | + .dark{fill: url(#gradient);} |
| 61 | + .light{fill: #eaeaea;} |
| 62 | + ]]></style>', |
| 63 | +]; |
| 64 | + |
| 65 | +// augment the QROptions class |
| 66 | +$options = new class ($options_arr) extends QROptions{ |
| 67 | + // path to svg logo |
| 68 | + protected string $svgLogo; |
| 69 | + // logo scale in % of QR Code size, clamped to 10%-30% |
| 70 | + protected float $svgLogoScale = 0.20; |
| 71 | + // css class for the logo (defined in $svgDefs) |
| 72 | + protected string $svgLogoCssClass = ''; |
| 73 | + |
| 74 | + // check logo |
| 75 | + protected function set_svgLogo(string $svgLogo):void{ |
| 76 | + |
| 77 | + if(!file_exists($svgLogo) || !is_readable($svgLogo)){ |
| 78 | + throw new QRCodeException('invalid svg logo'); |
| 79 | + } |
| 80 | + |
| 81 | + // @todo: validate svg |
| 82 | + |
| 83 | + $this->svgLogo = $svgLogo; |
| 84 | + } |
| 85 | + |
| 86 | + // clamp logo scale |
| 87 | + protected function set_svgLogoScale(float $svgLogoScale):void{ |
| 88 | + $this->svgLogoScale = max(0.05, min(0.3, $svgLogoScale)); |
| 89 | + } |
| 90 | + |
| 91 | +}; |
| 92 | + |
| 93 | +$qrcode = (new QRCode($options))->render($data); |
| 94 | + |
| 95 | +header('Content-type: image/svg+xml'); |
| 96 | + |
| 97 | +if($gzip){ |
| 98 | + header('Vary: Accept-Encoding'); |
| 99 | + header('Content-Encoding: gzip'); |
| 100 | + $qrcode = gzencode($qrcode, 9); |
| 101 | +} |
| 102 | + |
| 103 | +echo $qrcode; |
0 commit comments