Skip to content

Commit

Permalink
🚿
Browse files Browse the repository at this point in the history
  • Loading branch information
codemasher committed Nov 18, 2020
1 parent f012a15 commit d8bf297
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
45 changes: 25 additions & 20 deletions examples/QRImageWithText.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,24 @@
* Class QRImageWithText
*
* example for additional text
* @link https://github.com/chillerlan/php-qrcode/issues/35
*
* @link https://github.com/chillerlan/php-qrcode/issues/35
*
* @filesource QRImageWithText.php
* @created 22.06.2019
* @package chillerlan\QRCodeExamples
* @author smiley <smiley@chillerlan.net>
* @copyright 2019 smiley
* @license MIT
*
* @noinspection PhpComposerExtensionStubsInspection
*/

namespace chillerlan\QRCodeExamples;

use chillerlan\QRCode\Output\QRImage;
use function base64_encode, imagechar, imagecolorallocate, imagecolortransparent, imagecopymerge, imagecreatetruecolor,
imagedestroy, imagefilledrectangle, imagefontwidth, in_array, round, str_split, strlen;

class QRImageWithText extends QRImage{

Expand All @@ -26,14 +31,14 @@ class QRImageWithText extends QRImage{
* @return string
*/
public function dump(string $file = null, string $text = null):string{
$this->image = \imagecreatetruecolor($this->length, $this->length);
$background = \imagecolorallocate($this->image, ...$this->options->imageTransparencyBG);
$this->image = imagecreatetruecolor($this->length, $this->length);
$background = imagecolorallocate($this->image, ...$this->options->imageTransparencyBG);

if((bool)$this->options->imageTransparent && \in_array($this->options->outputType, $this::TRANSPARENCY_TYPES, true)){
\imagecolortransparent($this->image, $background);
if((bool)$this->options->imageTransparent && in_array($this->options->outputType, $this::TRANSPARENCY_TYPES, true)){
imagecolortransparent($this->image, $background);
}

\imagefilledrectangle($this->image, 0, 0, $this->length, $this->length, $background);
imagefilledrectangle($this->image, 0, 0, $this->length, $this->length, $background);

foreach($this->matrix->matrix() as $y => $row){
foreach($row as $x => $M_TYPE){
Expand All @@ -49,7 +54,7 @@ public function dump(string $file = null, string $text = null):string{
$imageData = $this->dumpImage($file);

if((bool)$this->options->imageBase64){
$imageData = 'data:image/'.$this->options->outputType.';base64,'.\base64_encode($imageData);
$imageData = 'data:image/'.$this->options->outputType.';base64,'.base64_encode($imageData);
}

return $imageData;
Expand All @@ -63,36 +68,36 @@ protected function addText(string $text):void{
$qrcode = $this->image;

// options things
$textSize = 3; // see imagefontheight() and imagefontwidth()
$textSize = 3; // see imagefontheight() and imagefontwidth()
$textBG = [200, 200, 200];
$textColor = [50, 50, 50];

$bgWidth = $this->length;
$bgHeight = $bgWidth + 20; // 20px extra space

// create a new image with additional space
$this->image = \imagecreatetruecolor($bgWidth, $bgHeight);
$background = \imagecolorallocate($this->image, ...$textBG);
$this->image = imagecreatetruecolor($bgWidth, $bgHeight);
$background = imagecolorallocate($this->image, ...$textBG);

// allow transparency
if((bool)$this->options->imageTransparent && \in_array($this->options->outputType, $this::TRANSPARENCY_TYPES, true)){
\imagecolortransparent($this->image, $background);
if((bool)$this->options->imageTransparent && in_array($this->options->outputType, $this::TRANSPARENCY_TYPES, true)){
imagecolortransparent($this->image, $background);
}

// fill the background
\imagefilledrectangle($this->image, 0, 0, $bgWidth, $bgHeight, $background);
imagefilledrectangle($this->image, 0, 0, $bgWidth, $bgHeight, $background);

// copy over the qrcode
\imagecopymerge($this->image, $qrcode, 0, 0, 0, 0, $this->length, $this->length, 100);
\imagedestroy($qrcode);
imagecopymerge($this->image, $qrcode, 0, 0, 0, 0, $this->length, $this->length, 100);
imagedestroy($qrcode);

$fontColor = \imagecolorallocate($this->image, ...$textColor);
$w = \imagefontwidth($textSize);
$x = \round(($bgWidth - \strlen($text) * $w) / 2);
$fontColor = imagecolorallocate($this->image, ...$textColor);
$w = imagefontwidth($textSize);
$x = round(($bgWidth - strlen($text) * $w) / 2);

// loop through the string and draw the letters
foreach(\str_split($text) as $i => $chr){
\imagechar($this->image, $textSize, $i * $w + $x, $this->length, $chr, $fontColor);
foreach(str_split($text) as $i => $chr){
imagechar($this->image, $textSize, $i * $w + $x, $this->length, $chr, $fontColor);
}
}

Expand Down
4 changes: 2 additions & 2 deletions examples/imageWithLogo.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
* @noinspection PhpIllegalPsrClassPathInspection
*/
class LogoOptions extends QROptions{
protected int $logoWidth;
protected int $logoHeight;
protected $logoWidth;
protected $logoHeight;
}

$options = new LogoOptions;
Expand Down

0 comments on commit d8bf297

Please sign in to comment.