Skip to content

Commit

Permalink
Add Center Logo Feature To QrCode Library, Hope It Is Useful :)
Browse files Browse the repository at this point in the history
  • Loading branch information
ice6 committed Apr 9, 2016
1 parent 6687025 commit 1021481
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
Binary file added assets/image/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions composer.json
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
"php": ">=5.3.0",
"ext-gd": "*"
},
"require-dev" : {
"phpunit/phpunit" : "~5.3"
},
"autoload": {
"psr-4": {
"Endroid\\QrCode\\": "src/"
Expand Down
54 changes: 54 additions & 0 deletions src/QrCode.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ class QrCode
/** @const int Vertical label alignment to the bottom */
const LABEL_VALIGN_BOTTOM = 4;

/** @var string */
protected $logo = NULL;

protected $logo_size = 48;

/** @var string */
protected $text = '';

Expand Down Expand Up @@ -370,6 +375,37 @@ public function getPath()
return $this->path;
}

/**
* Set logo in QR Code.
*
* @param string $logo Logo Path
* @throws Exceptions\DataDoesntExistsException
* @return QrCode
*/
public function setLogo($logo)
{
if(!file_exists($logo)){
throw new DataDoesntExistsException("$logo file does not exist");
}

$this->logo = $logo;
return $this;
}

/**
* Set logo size in QR Code(default 48).
*
* @param int $logo_size Logo Size
*
* @return QrCode
*/
public function setLogoSize($logo_size)
{
$this->logo_size = $logo_size;

return $this;
}

/**
* Set text to hide in QR Code.
*
Expand Down Expand Up @@ -1496,6 +1532,24 @@ public function create()
}
}

if(!empty($this->logo))
{
$logo_image = call_user_func('imagecreatefrom'.$this->image_type, $this->logo);
if(!$logo_image){
throw new ImageFunctionFailedException('imagecreatefrom'.$this->image_type . ' ' . $this->logo . 'failed' );
}
$src_w = imagesx($logo_image);
$src_h = imagesy($logo_image);

$dst_x = ($image_width - $this->logo_size) / 2;
$dst_y = ($this->size + $this->padding * 2 - $this->logo_size) / 2;

$successful = imagecopyresampled($output_image, $logo_image, $dst_x, $dst_y, 0, 0, $this->logo_size, $this->logo_size, $src_w, $src_h);
if(!$successful){
throw new ImageFunctionFailedException('add logo [image'.$this->format.'] failed.');
}
imagedestroy($logo_image);
}
$this->image = $output_image;
}
}
25 changes: 25 additions & 0 deletions tests/QrCodeTest.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ public function testGetImageString()
$this->assertTrue(is_string($imageString));
}

/**
* Tests if a valid image string is returned.
*
* @throws ImageFunctionFailedException
* @throws ImageFunctionUnknownException
*/
public function testGetQrCodeWithLogoString()
{
$qrCode = $this->createQrCodeWithLogo();
$imageString = $qrCode->get('png');

$this->assertTrue(is_string($imageString));
}

/**
* Returns a QR code.
*/
Expand All @@ -71,4 +85,15 @@ protected function createQrCode()

return $qrCode;
}

protected function createQrCodeWithLogo()
{
$qrCode = new QrCode();
$qrCode->setText('Life is too short to be generating QR codes')
->setSize(300)
->setLogo(dirname(__DIR__) . '/assets/image/logo.png')
->setLogoSize(60);

return $qrCode;
}
}

0 comments on commit 1021481

Please sign in to comment.