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

Added an example with png rounded modules using gd #215

Merged
merged 3 commits into from
Sep 18, 2023
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 examples/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
- [Custom output](./custom_output.php): a simple example that demonstrates the usage of custom output classes
- [GD Image with logo](./imageWithLogo.php): a logo on top of the QR Code
- [GD image with text](./imageWithText.php): description text under the QR Code ([#35](https://github.com/chillerlan/php-qrcode/issues/35))
- [GD Image with rounded modules](./pngWithRoundedShapes.php): Similar to "melted" modules, but more rounded ([#127](https://github.com/chillerlan/php-qrcode/issues/127))
- [ImageMagick with logo](./imagickWithLogo.php): a logo on top of the QR Code
- [SVG with logo](./svgWithLogo.php): an SVG QR Code with embedded logo (that is also SVG)
- [SVG with "melted" modules](./svgMeltedModules.php): an effect where the matrix appears to be like melted wax ([#127](https://github.com/chillerlan/php-qrcode/issues/127))
Expand Down
222 changes: 222 additions & 0 deletions examples/pngWithRoundedShapes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
<?php

livingroot marked this conversation as resolved.
Show resolved Hide resolved
/**
* php-gd realization of QR code with rounded modules
*
* @see https://github.com/chillerlan/php-qrcode/pull/215
* @see https://github.com/chillerlan/php-qrcode/issues/127
*
* @created 17.09.2023
* @author livingroot
* @copyright 2023 livingroot
* @license MIT
*/

use chillerlan\QRCode\Common\EccLevel;
use chillerlan\QRCode\Data\QRMatrix;
use chillerlan\QRCode\Output\QRGdImage;
use chillerlan\QRCode\Output\QROutputInterface;
use chillerlan\QRCode\QRCode;
use chillerlan\QRCode\QROptions;

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

// --------------------
// Class definition
// --------------------

class QRGdRounded extends QRGdImage {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also intercept the constructor to enable the internal upscaling for "rounder" corners at lower scales (which would be counter-intuitive for the user to set):

	public function __construct(SettingsContainerInterface $options, QRMatrix $matrix){
		$options->drawCircularModules = true;

		parent::__construct($options, $matrix);
	}

protected function module(int $x, int $y, int $M_TYPE): void {

$x1 = ($x * $this->scale);
$y1 = ($y * $this->scale);
$x2 = (($x + 1) * $this->scale);
$y2 = (($y + 1) * $this->scale);

$rectsize = ($this->scale / 2);

/**
* @var int $neighbours
* The right bit order (starting from 0):
* 0 1 2
* 7 # 3
* 6 5 4
*/
$neighbours = $this->matrix->checkNeighbours($x, $y);

// ------------------
// Outer rounding
// ------------------

if ($neighbours & (1 << 7)) { // neighbour left
// top left
imagefilledrectangle(
$this->image,
$x1,
$y1,
($x1 + $rectsize),
($y1 + $rectsize),
$this->moduleValues[$M_TYPE]
);
// bottom left
imagefilledrectangle(
$this->image,
$x1,
($y2 - $rectsize),
($x1 + $rectsize),
$y2,
$this->moduleValues[$M_TYPE]
);
}

if ($neighbours & (1 << 3)) { // neighbour right
// top right
imagefilledrectangle(
$this->image,
($x2 - $rectsize),
$y1,
$x2,
($y1 + $rectsize),
$this->moduleValues[$M_TYPE]
);
// bottom right
imagefilledrectangle(
$this->image,
($x2 - $rectsize),
($y2 - $rectsize),
$x2,
$y2,
$this->moduleValues[$M_TYPE]
);
}

if ($neighbours & (1 << 1)) { // neighbour top
// top left
imagefilledrectangle(
$this->image,
$x1,
$y1,
($x1 + $rectsize),
($y1 + $rectsize),
$this->moduleValues[$M_TYPE]
);
// top right
imagefilledrectangle(
$this->image,
($x2 - $rectsize),
$y1,
$x2,
($y1 + $rectsize),
$this->moduleValues[$M_TYPE]
);
}

if ($neighbours & (1 << 5)) { // neighbour bottom
// bottom left
imagefilledrectangle(
$this->image,
$x1,
($y2 - $rectsize),
($x1 + $rectsize),
$y2,
$this->moduleValues[$M_TYPE]
);
// bottom right
imagefilledrectangle(
$this->image,
($x2 - $rectsize),
($y2 - $rectsize),
$x2,
$y2,
$this->moduleValues[$M_TYPE]
);
}

// ---------------------
// inner rounding
// ---------------------

if (!$this->matrix->check($x, $y)) {

if (($neighbours & 1) && ($neighbours & (1 << 7)) && ($neighbours & (1 << 1))) {
// top left
imagefilledrectangle(
$this->image,
$x1,
$y1,
($x1 + $rectsize),
($y1 + $rectsize),
$this->moduleValues[($M_TYPE | QRMatrix::IS_DARK)]
);
}

if (($neighbours & (1 << 1)) && ($neighbours & (1 << 2)) && ($neighbours & (1 << 3))) {
// top right
imagefilledrectangle(
$this->image,
($x2 - $rectsize),
$y1,
$x2,
($y1 + $rectsize),
$this->moduleValues[($M_TYPE | QRMatrix::IS_DARK)]
);
}

if (($neighbours & (1 << 7)) && ($neighbours & (1 << 6)) && ($neighbours & (1 << 5))) {
// bottom left
imagefilledrectangle(
$this->image,
$x1,
($y2 - $rectsize),
($x1 + $rectsize),
$y2,
$this->moduleValues[($M_TYPE | QRMatrix::IS_DARK)]
);
}

if (($neighbours & (1 << 3)) && ($neighbours & (1 << 4)) && ($neighbours & (1 << 5))) {
// bottom right
imagefilledrectangle(
$this->image,
($x2 - $rectsize),
($y2 - $rectsize),
$x2,
$y2,
$this->moduleValues[($M_TYPE | QRMatrix::IS_DARK)]
);
}
}

imagefilledellipse(
$this->image,
(int)(($x * $this->scale) + ($this->scale / 2)),
(int)(($y * $this->scale) + ($this->scale / 2)),
(int)($this->scale - 1),
(int)($this->scale - 1),
$this->moduleValues[$M_TYPE]
);
}
}


// --------------------
// Example
// --------------------

$options = new QROptions([
'outputType' => QROutputInterface::CUSTOM,
'outputInterface' => QRGdRounded::class,
'eccLevel' => EccLevel::M,
'imageTransparent' => false,
'imageBase64' => false,
'scale' => 30
]);

$qrcode = new QRCode($options);

$img = $qrcode->render('https://www.youtube.com/watch?v=dQw4w9WgXcQ');

header('Content-type: image/png');

echo $img;
livingroot marked this conversation as resolved.
Show resolved Hide resolved