Skip to content

Commit

Permalink
Update endroid/qr-code to 4.6
Browse files Browse the repository at this point in the history
  • Loading branch information
justusdieckmann committed Oct 29, 2024
1 parent fd1deb8 commit 7afef22
Show file tree
Hide file tree
Showing 440 changed files with 12,698 additions and 29,894 deletions.
79 changes: 29 additions & 50 deletions classes/output_image.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@

namespace block_qrcode;

use Endroid\QrCode\Color\Color;
use Endroid\QrCode\ErrorCorrectionLevel;
use Endroid\QrCode\Encoding\Encoding;
use Endroid\QrCode\Logo\Logo;
use Endroid\QrCode\QrCode;
use Symfony\Component\HttpFoundation\Response;
use Endroid\QrCode\Writer\PngWriter;
use Endroid\QrCode\Writer\SvgWriter;
use DOMDocument;
use core\datalib;

defined('MOODLE_INTERNAL') || die();

Expand Down Expand Up @@ -161,35 +164,42 @@ public function create_image() {

// Set advanced options.
$qrcode->setMargin(10);
$qrcode->setEncoding('UTF-8');
$qrcode->setErrorCorrectionLevel(ErrorCorrectionLevel::HIGH);
$qrcode->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0]);
$qrcode->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255]);
$qrcode->setEncoding(new Encoding('UTF-8'));
$qrcode->setErrorCorrectionLevel(new ErrorCorrectionLevel\ErrorCorrectionLevelHigh());
$qrcode->setForegroundColor(new Color(0, 0, 0));
$qrcode->setBackgroundColor(new Color(255, 255, 255));

// Png format.
$logo = null;
if ($this->format == 2) {
$qrcode->setWriterByName('png');
$writer = new PngWriter();
if ($this->uselogo) {
$qrcode->setLogoWidth($this->size * 0.4);
if ($this->logofile) {
$qrcode->setLogoPath($this->logofile->copy_content_to_temp());
$logopath = $this->logofile->copy_content_to_temp();
} else {
$qrcode->setLogoPath($CFG->dirroot . '/blocks/qrcode/pix/moodlelogo.png');
$logopath = $CFG->dirroot . '/blocks/qrcode/pix/moodlelogo.png';
}
$logo = new Logo(
path: $logopath,
resizeToWidth: $this->size * 0.4,
punchoutBackground: false
);
}
$qrcode->writeFile($this->file);
} else {
$qrcode->setWriterByName('svg');
$writer = new SvgWriter();
if ($this->uselogo) {
$qrcodestring = $qrcode->writeString();
$logopath = $this->logofile ? $this->logofile->copy_content_to_temp()
: $CFG->dirroot . '/blocks/qrcode/pix/moodlelogo.svg';
$newqrcode = $this->modify_svg($qrcodestring, $logopath);
file_put_contents($this->file, $newqrcode);
} else {
$qrcode->writeFile($this->file);
$logo = new Logo(
path: $logopath,
resizeToWidth: $this->size * 0.4,
resizeToHeight: $this->size * 0.4 / $this->get_logo_aspect_ratio($logopath),
punchoutBackground: false
);
}
}
$result = $writer->write($qrcode, $logo);
$result->saveToFile($this->file);
}

/**
Expand Down Expand Up @@ -230,47 +240,16 @@ public function output_image($download) {
readfile($this->file);
}

/**
* Inserts logo in the QR code (used for svg QR code).
* @param string $svgqrcode QR code
* @param string $logopath Path of logo to add
* @return string XML representation of the svg image
*/
private function modify_svg($svgqrcode, $logopath) {
// Loads QR code.
$xmldoc = new DOMDocument();
$xmldoc->loadXML($svgqrcode);
$viewboxcode = $xmldoc->documentElement->getAttribute('viewBox');
$codewidth = explode(' ', $viewboxcode)[2];

protected function get_logo_aspect_ratio($logopath) {
// Loads logo.
$xmllogo = new DOMDocument();
$xmllogo->load($logopath);

$logotargetwidth = $codewidth * 0.4;

$viewbox = $xmllogo->documentElement->getAttribute('viewBox');
$viewboxbounds = explode(' ', $viewbox);
$logowidth = $viewboxbounds[2];
$logoheight = $viewboxbounds[3];

// Calculate logo height from width.
$logotargetheight = $logotargetwidth * ($logoheight / $logowidth);

// Calculate logo coordinates.
$logoy = ($codewidth - $logotargetheight) / 2;
$logox = ($codewidth - $logotargetwidth) / 2;

$xmllogo->documentElement->setAttribute('width', $logotargetwidth);
$xmllogo->documentElement->setAttribute('height', $logotargetheight);
$xmllogo->documentElement->setAttribute('x', $logox);
$xmllogo->documentElement->setAttribute('y', $logoy);

$node = $xmldoc->importNode($xmllogo->documentElement, true);

$xmldoc->documentElement->appendChild($node);

return $xmldoc->saveXML();
return $logowidth / $logoheight;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion thirdparty/composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"require": {
"endroid/qrcode": "^2.4"
"endroid/qr-code": "^4.6"
}
}
Loading

0 comments on commit 7afef22

Please sign in to comment.