Skip to content

Commit

Permalink
Add test for image size
Browse files Browse the repository at this point in the history
  • Loading branch information
endroid committed Apr 16, 2016
1 parent 3567bf4 commit d156111
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 29 deletions.
18 changes: 10 additions & 8 deletions src/QrCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class QrCode
const LABEL_VALIGN_BOTTOM = 4;

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

protected $logo_size = 48;

Expand Down Expand Up @@ -379,16 +379,19 @@ public function getPath()
* Set logo in QR Code.
*
* @param string $logo Logo Path
*
* @throws Exceptions\DataDoesntExistsException
*
* @return QrCode
*/
public function setLogo($logo)
{
if(!file_exists($logo)){
if (!file_exists($logo)) {
throw new DataDoesntExistsException("$logo file does not exist");
}

$this->logo = $logo;

return $this;
}

Expand Down Expand Up @@ -1532,11 +1535,10 @@ public function create()
}
}

if(!empty($this->logo))
{
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' );
if (!$logo_image) {
throw new ImageFunctionFailedException('imagecreatefrom'.$this->image_type.' '.$this->logo.'failed');
}
$src_w = imagesx($logo_image);
$src_h = imagesy($logo_image);
Expand All @@ -1545,7 +1547,7 @@ public function create()
$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){
if (!$successful) {
throw new ImageFunctionFailedException('add logo [image'.$this->format.'] failed.');
}
imagedestroy($logo_image);
Expand Down
52 changes: 31 additions & 21 deletions tests/QrCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class QrCodeTest extends PHPUnit_Framework_TestCase
*/
public function testGetDataUri()
{
$qrCode = $this->getQrCode();
$qrCode = $this->createQrCode();
$dataUri = $qrCode->getDataUri();

$this->assertTrue(is_string($dataUri));
Expand All @@ -40,12 +40,27 @@ public function testGetDataUri()
*/
public function testGetImageString()
{
$qrCode = $this->getQrCode();
$qrCode = $this->createQrCode();
$imageString = $qrCode->get('png');

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

/**
* Tests if the resulting image size is correct.
*/
public function testImageSize()
{
$qrCode = $this->createQrCode();

$size = $qrCode->getSize();
$padding = $qrCode->getPadding();
$image = $qrCode->getImage();

$this->assertTrue($padding > 0);
$this->assertTrue(imagesx($image) == $size + 2 * $padding);
}

/**
* Tests if a valid image string is returned.
*
Expand All @@ -60,18 +75,6 @@ public function testGetQrCodeWithLogoString()
$this->assertTrue(is_string($imageString));
}

/**
* Returns a QR code.
*/
protected function getQrCode()
{
if (!$this->qrCode) {
$this->qrCode = $this->createQrCode();
}

return $this->qrCode;
}

/**
* Creates a QR code.
*
Expand All @@ -80,19 +83,26 @@ protected function getQrCode()
protected function createQrCode()
{
$qrCode = new QrCode();
$qrCode->setText('Life is too short to be generating QR codes');
$qrCode->setSize(300);
$qrCode
->setText('Life is too short to be generating QR codes')
->setSize(300)
->setPadding(20);

return $qrCode;
}

/**
* Creates a QR code with a logo.
*
* @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);
$qrCode = $this->createQrCode();
$qrCode
->setLogo(dirname(__DIR__).'/assets/image/logo.png')
->setLogoSize(60)
;

return $qrCode;
}
Expand Down

0 comments on commit d156111

Please sign in to comment.