Skip to content

Commit 4968063

Browse files
committed
:octocat: logo image example (#52)
1 parent 05e4fc4 commit 4968063

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed

examples/QRImageWithLogo.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
/**
3+
* Class QRImageWithLogo
4+
*
5+
* @filesource QRImageWithLogo.php
6+
* @created 18.11.2020
7+
* @package chillerlan\QRCodeExamples
8+
* @author smiley <[email protected]>
9+
* @copyright 2020 smiley
10+
* @license MIT
11+
*
12+
* @noinspection PhpComposerExtensionStubsInspection
13+
*/
14+
15+
namespace chillerlan\QRCodeExamples;
16+
17+
use chillerlan\QRCode\Output\{QRCodeOutputException, QRImage};
18+
19+
use function imagecopyresampled, imagecreatefrompng, imagesx, imagesy, is_file, is_readable;
20+
21+
/**
22+
* @property \chillerlan\QRCodeExamples\LogoOptions $options
23+
*/
24+
class QRImageWithLogo extends QRImage{
25+
26+
/**
27+
* @param string|null $file
28+
* @param string|null $logo
29+
*
30+
* @return string
31+
* @throws \chillerlan\QRCode\Output\QRCodeOutputException
32+
*/
33+
public function dump(string $file = null, string $logo = null):string{
34+
// set returnResource to true to skip further processing for now
35+
$this->options->returnResource = true;
36+
37+
// of course you could accept other formats too (such as resource or Imagick)
38+
// i'm not checking for the file type either for simplicity reasons (assuming PNG)
39+
if(!is_file($logo) || !is_readable($logo)){
40+
throw new QRCodeOutputException('invalid logo');
41+
}
42+
43+
$this->matrix->setLogoSpace(
44+
$this->options->logoWidth,
45+
$this->options->logoHeight
46+
// not utilizing the position here
47+
);
48+
49+
// there's no need to save the result of dump() into $this->image here
50+
parent::dump($file);
51+
52+
$im = imagecreatefrompng($logo);
53+
54+
// get logo image size
55+
$w = imagesx($im);
56+
$h = imagesy($im);
57+
58+
// set new logo size, leave a border of 1 module
59+
$lw = ($this->options->logoWidth - 2) * $this->options->scale;
60+
$lh = ($this->options->logoHeight - 2) * $this->options->scale;
61+
62+
// get the qrcode size
63+
$ql = $this->matrix->size() * $this->options->scale;
64+
65+
// scale the logo and copy it over. done!
66+
imagecopyresampled($this->image, $im, ($ql - $lw) / 2, ($ql - $lh) / 2, 0, 0, $lw, $lh, $w, $h);
67+
68+
$imageData = $this->dumpImage();
69+
70+
if($file !== null){
71+
$this->saveToFile($imageData, $file);
72+
}
73+
74+
if($this->options->imageBase64){
75+
$imageData = 'data:image/'.$this->options->outputType.';base64,'.base64_encode($imageData);
76+
}
77+
78+
return $imageData;
79+
}
80+
81+
}

examples/imageWithLogo.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
/**
3+
*
4+
* @filesource imageWithLogo.php
5+
* @created 18.11.2020
6+
* @author smiley <[email protected]>
7+
* @copyright 2020 smiley
8+
* @license MIT
9+
*/
10+
11+
namespace chillerlan\QRCodeExamples;
12+
13+
use chillerlan\QRCode\{QRCode, QROptions};
14+
15+
require_once __DIR__.'/../vendor/autoload.php';
16+
17+
$data = 'https://www.youtube.com/watch?v=DLzxrzFCyOs&t=43s';
18+
/**
19+
* @property int $logoWidth
20+
* @property int $logoHeight
21+
*
22+
* @noinspection PhpIllegalPsrClassPathInspection
23+
*/
24+
class LogoOptions extends QROptions{
25+
protected int $logoWidth;
26+
protected int $logoHeight;
27+
}
28+
29+
$options = new LogoOptions;
30+
31+
$options->version = 7;
32+
$options->eccLevel = QRCode::ECC_H;
33+
$options->imageBase64 = false;
34+
$options->logoWidth = 13;
35+
$options->logoHeight = 13;
36+
$options->scale = 5;
37+
$options->imageTransparent = false;
38+
39+
header('Content-type: image/png');
40+
41+
$qrOutputInterface = new QRImageWithLogo($options, (new QRCode($options))->getMatrix($data));
42+
43+
// dump the output, with an additional logo
44+
echo $qrOutputInterface->dump(null, __DIR__.'/octocat.png');

examples/octocat.png

2.41 KB
Loading

0 commit comments

Comments
 (0)