-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(SLB-458): branded preview qr (#1565)
- Loading branch information
1 parent
9dc44d0
commit 2330e1f
Showing
8 changed files
with
193 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
packages/composer/amazeelabs/silverback_preview_link/src/QRCodeWithLogo.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
namespace Drupal\silverback_preview_link; | ||
|
||
use Drupal\Tests\Component\Annotation\Doctrine\Fixtures\Annotation\Version; | ||
use chillerlan\QRCode\{QRCode, QRCodeException, QROptions}; | ||
use chillerlan\QRCode\Data\QRMatrix; | ||
use chillerlan\QRCode\Common\EccLevel; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use function file_exists, gzencode, header, is_readable, max, min; | ||
|
||
/** | ||
* Creates and renders a QR Code with embedded SVG logo. | ||
*/ | ||
class QRCodeWithLogo { | ||
|
||
private $config = [ | ||
'svgLogo' => __DIR__ . '/images/amazee-labs_logo-square-green.svg', | ||
'svgLogoScale' => 1, | ||
'svgLogoCssClass' => 'dark', | ||
'version' => QRCode::VERSION_AUTO, | ||
'outputType' => QRCode::OUTPUT_CUSTOM, | ||
'outputInterface' => QRMarkupSVGWithLogo::class, | ||
'imageBase64' => FALSE, | ||
// ECC level H is necessary when using logos. | ||
'eccLevel' => EccLevel::H, | ||
'addQuietzone' => TRUE, | ||
// If set to TRUE, the light modules won't be rendered. | ||
'imageTransparent' => FALSE, | ||
// Empty the default value to remove the fill* attributes from the <path> elements | ||
'markupDark' => '', | ||
'markupLight' => '', | ||
'drawCircularModules' => TRUE, | ||
'circleRadius' => 0.45, | ||
'svgConnectPaths' => TRUE, | ||
'keepAsSquare' => [ | ||
QRMatrix::M_FINDER | QRMatrix::IS_DARK, | ||
QRMatrix::M_FINDER_DOT, | ||
QRMatrix::M_ALIGNMENT | QRMatrix::IS_DARK, | ||
], | ||
// https://developer.mozilla.org/en-US/docs/Web/SVG/Element/linearGradient | ||
'svgDefs' => ' | ||
<linearGradient id="gradient" x1="100%" y2="100%"> | ||
<stop stop-color="#951b81" offset="0"/> | ||
<stop stop-color="#00a29a" offset="0.8"/> | ||
</linearGradient> | ||
<style><![CDATA[ | ||
.dark{fill: url(#gradient);} | ||
.light{fill: #fff;} | ||
]]></style>', | ||
]; | ||
|
||
private function getOptions(): QROptions { | ||
// Augment the QROptions class. | ||
return new class ($this->config) extends QROptions { | ||
|
||
protected string $svgLogo; | ||
|
||
// Logo scale in % of QR Code size, clamped to 10%-30%. | ||
protected float $svgLogoScale = 0.20; | ||
|
||
// CSS class for the logo (defined in $svgDefs). | ||
protected string $svgLogoCssClass = ''; | ||
|
||
protected function set_svgLogo(string $svgLogo): void { | ||
if (!file_exists($svgLogo) || !is_readable($svgLogo)) { | ||
throw new QRCodeException('invalid svg logo'); | ||
} | ||
$this->svgLogo = $svgLogo; | ||
} | ||
|
||
// Clamp logo scale. | ||
protected function set_svgLogoScale(float $svgLogoScale): void { | ||
$this->svgLogoScale = max(0.05, min(0.3, $svgLogoScale)); | ||
} | ||
|
||
}; | ||
} | ||
|
||
public function getQRCode(string $data) { | ||
return (new QRCode($this->getOptions()))->render($data); | ||
} | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
packages/composer/amazeelabs/silverback_preview_link/src/QRMarkupSVGWithLogo.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace Drupal\silverback_preview_link; | ||
|
||
use chillerlan\QRCode\Output\QRMarkupSVG; | ||
|
||
/** | ||
* Output interface for QRCode::OUTPUT_CUSTOM. | ||
*/ | ||
class QRMarkupSVGWithLogo extends QRMarkupSVG { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function paths(): string { | ||
$size = (int) ceil($this->moduleCount * $this->options->svgLogoScale); | ||
// Calling QRMatrix::setLogoSpace() manually, | ||
// so QROptions::$addLogoSpace has no effect. | ||
$this->matrix->setLogoSpace($size, $size); | ||
$svg = parent::paths(); | ||
$svg .= $this->getLogo(); | ||
return $svg; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function path(string $path, int $M_TYPE): string { | ||
// Omit the "fill" and "opacity" attributes on the path element. | ||
return sprintf('<path class="%s" d="%s"/>', $this->getCssClass($M_TYPE), $path); | ||
} | ||
|
||
/** | ||
* Returns a <g> element that contains the SVG logo and positions | ||
* it properly within the QR Code. | ||
* | ||
* @see https://developer.mozilla.org/en-US/docs/Web/SVG/Element/g | ||
* @see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/transform | ||
*/ | ||
protected function getLogo(): string { | ||
return sprintf( | ||
'%5$s<g transform="translate(%1$s %1$s) scale(%2$s)" class="%3$s">%5$s%4$s%5$s</g>', | ||
(($this->moduleCount - ($this->moduleCount * $this->options->svgLogoScale)) / 2), | ||
$this->options->svgLogoScale, | ||
$this->options->svgLogoCssClass, | ||
file_get_contents($this->options->svgLogo), | ||
$this->options->eol | ||
); | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
...amazeelabs/silverback_preview_link/src/images/amazee-labs_logo-square-green.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters