Skip to content

Commit

Permalink
Merge pull request #60 from 2amigos/develop
Browse files Browse the repository at this point in the history
Develop/master
  • Loading branch information
2amjsouza committed Oct 18, 2023
2 parents 7684d55 + 013aee5 commit 0e8516c
Show file tree
Hide file tree
Showing 23 changed files with 542 additions and 38 deletions.
11 changes: 11 additions & 0 deletions docs/helpful-guides/advance-usage.md
Expand Up @@ -35,5 +35,16 @@ $qrCode->writeFile(__DIR__ . '/codes/my-code.png');

```

By default, the logo image height will be transformed to fit the image width,
keeping a square shaped pattern. You can choose to scale the height
instead:

```PHP
$qrCode = (new QrCode('https://2am.tech'))
->setLogo(__DIR__ . '/data/logo.png')
->setLogoWidth(48)
->setScaleLogoHeight(false);
```


© [2amigos](https://2am.tech/) 2013-2023
15 changes: 13 additions & 2 deletions docs/index.md
Expand Up @@ -66,14 +66,25 @@ echo '<img src="' . $qrCode->writeDataUri() . '">';
?>
```

You can set the foreground color, defining RGBA values, where the alpha is optional.

```PHP
$qrCode = (new QrCode('This is my text'))
->setForeground(0, 0, 0);

// or, setting alpha too
$qrCode = (new QrCode('This is my text'))
->setForeground(0, 0, 0, 50);
```

In order to ease the task to write different formats into a QrCode, the library comes with a set of classes. These are:

- [BookmarkFormat](formats/bookmark.md)
- [BtcFormat](formats/bitcoin.md)
- [GeoFormat](formats/geo.md)
- [ICalFormat](formats/ical.md)
- [MailMessageFormat](formats/geo.md)
- [MailToFormat](formats/bookmark.md)
- [MailMessageFormat](formats/mail-message.md)
- [MailToFormat](formats/mail-to.md)
- [MeCardFormat](formats/me-card.md)
- [MmsFormat](formats/mms.md)
- [PhoneFormat](formats/phone.md)
Expand Down
43 changes: 43 additions & 0 deletions src/Action/QrCodeAction.php
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
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
5 changes: 5 additions & 0 deletions src/Contracts/QrCodeInterface.php
Expand Up @@ -58,6 +58,11 @@ public function getLogoPath(): ?string;
*/
public function getLogoWidth(): ?int;

/**
* @var
*/
public function getScaleLogoHeight(): bool;

/**
* @return LabelInterface
*/
Expand Down
89 changes: 89 additions & 0 deletions src/Dto/LogoDto.php
@@ -0,0 +1,89 @@
<?php

namespace Da\QrCode\Dto;

class LogoDto
{
/**
* @var \GdImage
*/
private $image;
/**
* @var int
*/
private $width;
/**
* @var int
*/
private $height;
/**
* @var int
*/
private $targetWidth;
/**
* @var int
*/
private $targetHeight;

private function __construct($image, $width, $height, $targetWidth, $targetHeight)
{
$this->image = $image;
$this->width = $width;
$this->height = $height;
$this->targetWidth = $targetWidth;
$this->targetHeight = $targetHeight;
}

/**
* @param \GdImage $image
* @param int $width
* @param int $height
* @param int $targetWidth
* @param int $targetHeight
* @return LogoDto
*/
public static function create($image, $width, $height, $targetWidth, $targetHeight): LogoDto
{
return new self($image, $width, $height, $targetWidth, $targetHeight);
}

/**
* @return \GdImage
*/
public function image()
{
return $this->image;
}

/**
* @return int
*/
public function width()
{
return $this->width;
}

/**
* @return int
*/
public function height()
{
return $this->height;
}

/**
* @return int
*/
public function targetWidth()
{
return $this->targetWidth;
}

/**
* @return int
*/
public function targetHeight()
{
return $this->targetHeight;
}
}
41 changes: 38 additions & 3 deletions src/QrCode.php
Expand Up @@ -38,15 +38,17 @@ class QrCode implements QrCodeInterface
protected $foregroundColor = [
'r' => 0,
'g' => 0,
'b' => 0
'b' => 0,
'a' => 100,
];
/**
* @var array
*/
protected $backgroundColor = [
'r' => 255,
'g' => 255,
'b' => 255
'b' => 255,
'a' => 100,
];
/**
* @var string
Expand All @@ -64,6 +66,10 @@ class QrCode implements QrCodeInterface
* @var int
*/
protected $logoWidth;
/**
* @var bool
*/
protected $scaleLogoHeight = false;
/**
* @var LabelInterface
*/
Expand Down Expand Up @@ -94,12 +100,13 @@ public function __construct(string $text, string $errorCorrectionLevel = null, W
*
* @return $this
*/
public function setForegroundColor(int $red, int $green, int $blue): self
public function setForegroundColor(int $red, int $green, int $blue, int $alpha = 100): self
{
$this->foregroundColor = [
'r' => $red,
'g' => $green,
'b' => $blue,
'a' => $alpha,
];

return $this;
Expand All @@ -118,6 +125,7 @@ public function setBackgroundColor(int $red, int $green, int $blue): self
'r' => $red,
'g' => $green,
'b' => $blue,
'a' => 100,
];

return $this;
Expand Down Expand Up @@ -164,6 +172,14 @@ public function setWriter(WriterInterface $writer): self
return $this;
}

/**
* @return WriterInterface
*/
public function getWriter(): WriterInterface
{
return $this->writer;
}

/**
* @param string $errorCorrectionLevel
*
Expand Down Expand Up @@ -224,6 +240,17 @@ public function setLogoWidth(int $width): self
return $this;
}

/**
* @param bool $scale
* @return $this
*/
public function setScaleLogoHeight(bool $scale): self
{
$this->scaleLogoHeight = $scale;

return $this;
}

/**
* @param LabelInterface|string $label
*
Expand Down Expand Up @@ -351,4 +378,12 @@ public function writeFile(string $path)
{
return $this->writer->writeFile($this, $path);
}

/**
* @return bool
*/
public function getScaleLogoHeight(): bool
{
return $this->scaleLogoHeight;
}
}
26 changes: 13 additions & 13 deletions src/Traits/ImageTrait.php
Expand Up @@ -71,7 +71,12 @@ public function writeString(QrCodeInterface $qrCode): string
);

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

if ($qrCode->getLabel()) {
Expand Down Expand Up @@ -191,19 +196,14 @@ protected function calculateAdditionalWhiteSpace($image, array $foregroundColor)
*
* @return resource
*/
protected function addLogo($sourceImage, $logoPath, $logoWidth = null)
protected function addLogo($sourceImage, $logoPath, $logoWidth = null, $scale = false)
{
$logoImage = imagecreatefromstring(file_get_contents($logoPath));
$logoSourceWidth = imagesx($logoImage);
$logoSourceHeight = imagesy($logoImage);

$logoTargetWidth = $logoWidth ?: $logoSourceWidth;
$logoTargetHeight = $logoSourceHeight;

if ($logoTargetWidth !== null) {
$scale = $logoTargetWidth / $logoSourceWidth;
$logoTargetHeight = intval($scale * imagesy($logoImage));
}
$logoContents = $this->transformLogo($logoPath, $logoWidth, $scale);
$logoImage = $logoContents->image();
$logoSourceWidth = $logoContents->width();
$logoSourceHeight = $logoContents->height();
$logoTargetWidth = $logoContents->targetWidth();
$logoTargetHeight = $logoContents->targetHeight();

$logoX = imagesx($sourceImage) / 2 - $logoTargetWidth / 2;
$logoY = imagesy($sourceImage) / 2 - $logoTargetHeight / 2;
Expand Down
2 changes: 0 additions & 2 deletions src/Traits/UrlTrait.php
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

0 comments on commit 0e8516c

Please sign in to comment.