Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UTCT-66: Add QR Code generator #67

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
.htaccess
includes/vendor/*
!includes/vendor/.gitkeep
user/plugins/*/vendor/*

# environment-specific config
user/config.php
Expand Down
81 changes: 81 additions & 0 deletions user/plugins/seans-qrcode/QRImageWithLogo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
/**
* Class QRImageWithLogo
*
* @filesource QRImageWithLogo.php
* @created 18.11.2020
* @package chillerlan\QRCodeExamples
* @author smiley <[email protected]>
* @copyright 2020 smiley
* @license MIT
*
* @noinspection PhpComposerExtensionStubsInspection
*/

// namespace chillerlan\QRCode;

use chillerlan\QRCode\Output\{QRCodeOutputException, QRImage};

use function imagecopyresampled, imagecreatefrompng, imagesx, imagesy, is_file, is_readable;

/**
* @property \chillerlan\QRCodeExamples\LogoOptions $options
*/
class QRImageWithLogo extends QRImage{

/**
* @param string|null $file
* @param string|null $logo
*
* @return string
* @throws \chillerlan\QRCode\Output\QRCodeOutputException
*/
public function dump(string $file = null, string $logo = null):string{
// set returnResource to true to skip further processing for now
$this->options->returnResource = true;

// of course you could accept other formats too (such as resource or Imagick)
// i'm not checking for the file type either for simplicity reasons (assuming PNG)
if(!is_file($logo) || !is_readable($logo)){
throw new QRCodeOutputException('invalid logo');
}

$this->matrix->setLogoSpace(
$this->options->logoSpaceWidth,
$this->options->logoSpaceHeight
// not utilizing the position here
);

// there's no need to save the result of dump() into $this->image here
parent::dump($file);

$im = imagecreatefrompng($logo);

// get logo image size
$w = imagesx($im);
$h = imagesy($im);

// set new logo size, leave a border of 1 module (no proportional resize/centering)
$lw = ($this->options->logoSpaceWidth - 2) * $this->options->scale;
$lh = ($this->options->logoSpaceHeight - 2) * $this->options->scale;

// get the qrcode size
$ql = $this->matrix->size() * $this->options->scale;

// scale the logo and copy it over. done!
imagecopyresampled($this->image, $im, ($ql - $lw) / 2, ($ql - $lh) / 2, 0, 0, $lw, $lh, $w, $h);

$imageData = $this->dumpImage();

if($file !== null){
$this->saveToFile($imageData, $file);
}

if($this->options->imageBase64){
$imageData = 'data:image/'.$this->options->outputType.';base64,'.base64_encode($imageData);
}

return $imageData;
}

}
8 changes: 8 additions & 0 deletions user/plugins/seans-qrcode/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"require": {
"php": "^8.0",
"chillerlan/php-qrcode": "^4.4.0",
"ext-mbstring": "*",
"ext-gd": "*"
}
}
169 changes: 169 additions & 0 deletions user/plugins/seans-qrcode/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added user/plugins/seans-qrcode/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading