diff --git a/assets/image/logo.png b/assets/image/logo.png new file mode 100755 index 0000000..96cb280 Binary files /dev/null and b/assets/image/logo.png differ diff --git a/composer.json b/composer.json old mode 100644 new mode 100755 index 2a746b8..81df703 --- a/composer.json +++ b/composer.json @@ -16,6 +16,9 @@ "php": ">=5.3.0", "ext-gd": "*" }, + "require-dev" : { + "phpunit/phpunit" : "~5.3" + }, "autoload": { "psr-4": { "Endroid\\QrCode\\": "src/" diff --git a/src/QrCode.php b/src/QrCode.php old mode 100644 new mode 100755 index 4b05ce0..ac2fadf --- a/src/QrCode.php +++ b/src/QrCode.php @@ -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 = ''; @@ -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. * @@ -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; } } diff --git a/tests/QrCodeTest.php b/tests/QrCodeTest.php old mode 100644 new mode 100755 index 86bf03f..58f58a1 --- a/tests/QrCodeTest.php +++ b/tests/QrCodeTest.php @@ -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. */ @@ -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; + } }