Skip to content

Commit

Permalink
Merge pull request #59 from 2amigos/patch_fix_label_for_svg_add_featu…
Browse files Browse the repository at this point in the history
…res_to_actions

Patch fix label for svg add features to actions
  • Loading branch information
2amjsouza authored Oct 18, 2023
2 parents 66597b5 + af27fd8 commit 013aee5
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 21 deletions.
43 changes: 43 additions & 0 deletions src/Action/QrCodeAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Da\QrCode\Action;

use Da\QrCode\Component\QrCodeComponent;
use Da\QrCode\Label;
use Yii;
use yii\base\Action;
use yii\web\Response;
Expand All @@ -31,6 +32,28 @@ class QrCodeAction extends Action
* @var string whether the URL parameter is passed via GET or POST. Defaults to 'get'.
*/
public $method = 'get';

/**
* @var string|Label|null
*/
public $label = null;

/**
* @var array|null
* 'r' => 0 //RED
* 'g' => 0 //GREEN
* 'B' => 0 //BLUE
*/
public $background = null;

/**
* @var array|null
* 'r' => 0 //RED
* 'g' => 0 //GREEN
* 'B' => 0 //BLUE
* 'a => 100 //ALPHA
*/
public $foreground = null;
/**
* @var string the qr component name configured on the Yii2 app. The component should have configured all the
* possible options like adding a logo, styling, labelling, etc.
Expand All @@ -49,6 +72,26 @@ public function run()
Yii::$app->response->format = Response::FORMAT_RAW;
Yii::$app->response->headers->add('Content-Type', $qrCode->getContentType());

if ($this->label) {
$qrCode->setLabel($this->label);
}

if (is_array($this->background)) {
$qrCode->setBackgroundColor(
$this->background['r'],
$this->background['g'],
$this->background['b'],
);
}

if (is_array($this->foreground)) {
$qrCode->setForegroundColor(
$this->foreground['r'],
$this->foreground['g'],
$this->foreground['b'],
! empty($this->foreground['a']) ? $this->foreground['a'] : 100,
);
}
return $qrCode->setText((string)$text)->writeString();
}
}
Expand Down
11 changes: 6 additions & 5 deletions src/Component/QrCodeComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
* @author Antonio Ramirez <hola@2amigos.us>
* @package Da\QrCode\Component
*
* @method QrCode useForegroundColor(integer $red, integer $green, integer $blue)
* @method QrCode useBackgroundColor(integer $red, integer $green, integer $blue)
* @method QrCode useEncoding(string $encoding)
* @method QrCode useWriter(WriterInterface $writer)
* @method QrCode useLogo(string $logo)
* @method QrCode setForegroundColor(integer $red, integer $green, integer $blue, integer $alpha)
* @method QrCode setBackgroundColor(integer $red, integer $green, integer $blue)
* @method QrCode setEncoding(string $encoding)
* @method QrCode setWriter(WriterInterface $writer)
* @method QrCode setLogo(string $logo)
* @method QrCode setText(string $text)
* @method QrCode setSize(integer $size)
* @method QrCode setLogoWidth(integer $width)
Expand Down Expand Up @@ -75,6 +75,7 @@ class QrCodeComponent extends Component
* 'r' => 0, // RED
* 'g' => 0, // GREEN
* 'b' => 0 // BLUE
* 'a => 100 // ALPHA
* ]
* ```
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Dto/LogoDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,4 @@ public function targetHeight()
{
return $this->targetHeight;
}
}
}
7 changes: 6 additions & 1 deletion src/Traits/ImageTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,12 @@ public function writeString(QrCodeInterface $qrCode): string
);

if ($qrCode->getLogoPath()) {
$image = $this->addLogo($image, $qrCode->getLogoPath(), $qrCode->getLogoWidth(), $qrCode->getScaleLogoHeight());
$image = $this->addLogo(
$image,
$qrCode->getLogoPath(),
$qrCode->getLogoWidth(),
$qrCode->getScaleLogoHeight()
);
}

if ($qrCode->getLabel()) {
Expand Down
2 changes: 0 additions & 2 deletions src/Traits/UrlTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ trait UrlTrait
*/
public function setUrl(string $value)
{
$error = null;

if (!filter_var($value, FILTER_VALIDATE_URL)) {
throw new InvalidConfigException('Url seems invalid.');
}
Expand Down
18 changes: 13 additions & 5 deletions src/Writer/SvgWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,13 @@ public function writeString(QrCodeInterface $qrCode): string
$this->addMargin($svg, $qrCode);

if ($qrCode->getLogoPath()) {
$this->addLogo($svg, $qrCode, $qrCode->getLogoPath(), $qrCode->getLogoWidth(), $qrCode->getScaleLogoHeight());
$this->addLogo(
$svg,
$qrCode,
$qrCode->getLogoPath(),
$qrCode->getLogoWidth(),
$qrCode->getScaleLogoHeight()
);
}

if ($qrCode->getLabel()) {
Expand Down Expand Up @@ -130,9 +136,7 @@ protected function addLabel($svg, $qrCode)

$labelText = $label->getText();
$labelFontSize = $label->getFontSize();
$labelFontPath = str_contains($label->getFont(), '../')
? explode('../', $label->getFont())[1]
: $label->getFont();
$labelFontPath = $label->getFont();

$labelMargin = $label->getMargins();
$labelAlignment = $label->getAlignment();
Expand All @@ -146,6 +150,10 @@ protected function addLabel($svg, $qrCode)
$svg->attributes()->viewBox = sprintf("0 0 %s %s", $qrCodeOriginalWidth, $qrCodeOriginalHeight + $blockSize);
$svg->rect->attributes()->height = $qrCodeOriginalHeight + $blockSize;

$labelFontPath = 'data:application/x-font-otf;charset=utf-8;base64,'
. base64_encode(
file_get_contents($label->getFont())
);
$svg->addChild(
'style',
<<<CSS
Expand All @@ -156,7 +164,7 @@ protected function addLabel($svg, $qrCode)
CSS
);

$labelY = ($qrCodeOriginalHeight + $blockSize) - $labelMargin['b'];
$labelY = max(($qrCodeOriginalHeight + $blockSize) - $labelMargin['b'], $qrCodeOriginalHeight + 4);

switch ($labelAlignment) {
case LabelInterface::ALIGN_LEFT:
Expand Down
14 changes: 13 additions & 1 deletion tests/_app/controllers/SiteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,19 @@ public function actions()
return [
'qr' => [
'class' => QrCodeAction::className(),
'text' => (new MailToFormat(['email' => 'hola@2amigos.us']))
'text' => (new MailToFormat(['email' => 'hola@2amigos.us'])),
'label' => '2am. Technologies',
'background' => [
'r' => 255,
'g' => 255,
'b' => 255,
],
'foreground' => [
'r' => 0,
'g' => 0,
'b' => 0,
'a' => 100,
],
]
];
}
Expand Down
Binary file modified tests/_data/data-action.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions tests/_data/data-svg-with-label.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions tests/_data/data-svg-with-label2.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions tests/_data/data-svg-with-label3.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions tests/functional/QrCodeActionCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public function qrCodeWorks(FunctionalTester $I)
$I->amGoingTo('Call the configured action "qr" and should receive the "png" image.');
$I->amOnRoute('/site/qr');
$source = $I->grabPageSource();

$I->openFile(codecept_data_dir('data-action.png'));
$I->seeInThisFile($source);
}
Expand Down

0 comments on commit 013aee5

Please sign in to comment.