Skip to content

Commit

Permalink
Fixed some minor code style issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Brecht-Precht committed Jul 15, 2016
1 parent 3e007cb commit 2be71c7
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 127 deletions.
2 changes: 1 addition & 1 deletion .codeclimate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ ratings:
- "**.rb"
exclude_paths:
- test/
- vendor/
- vendor/
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/vendor
composer.lock
vagrantfile
vagrantfile
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,4 @@ Contributing to our projects is always very appreciated.

## License

PHP QR Code Suite is under the MIT license.
PHP QR Code Suite is under the MIT license.
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
</exclude>
</whitelist>
</filter>
</phpunit>
</phpunit>
2 changes: 1 addition & 1 deletion src/QrEncode/QrEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public function encodeQrCode($contents)
throw new QrEncoderException('QR encoder internal error');
}

$image = @imagecreatefrompng($pngPath);
$image = imagecreatefrompng($pngPath);
if ($image === false) {
throw new QrEncoderException('GD lib internal error');
}
Expand Down
85 changes: 85 additions & 0 deletions src/QrRender/AbstractPixelRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace Markenwerk\QrCodeSuite\QrRender;

/**
* Class AbstractPixelRenderer
*
* @package Markenwerk\QrCodeSuite\QrRender
*/
abstract class AbstractPixelRenderer
{

/**
* @var int
*/
private $approximateSize = 1000;

/**
* @var int
*/
private $width;

/**
* @var int
*/
private $height;

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

/**
* @param int $approximateSize
* @return $this
*/
public function setApproximateSize($approximateSize)
{
if (!is_int($approximateSize) || $approximateSize < 0 || $approximateSize > 5000) {
throw new \InvalidArgumentException('Approximate size has to be a positive integer less than 5000');
}
$this->approximateSize = $approximateSize;
return $this;
}

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

/**
* @param int $width
* @return $this
*/
protected function setWidth($width)
{
$this->width = $width;
return $this;
}

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

/**
* @param int $height
* @return $this
*/
protected function setHeight($height)
{
$this->height = $height;
return $this;
}

}
2 changes: 2 additions & 0 deletions src/QrRender/Base/QrCodeRendererInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
interface QrCodeRendererInterface
{

const MARGIN = 2;

/**
* @param QrCode $qrCode
* @param $filename
Expand Down
8 changes: 5 additions & 3 deletions src/QrRender/PathFinder/QrCodePathFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ public function perform(QrCode $qrCode)

private function findPaths()
{
for ($y = 1; $y <= $this->qrCode->getHeight(); $y++) {
$qrCodeHeight = $this->qrCode->getHeight();
$qrCodeWidth = $this->qrCode->getWidth();
for ($y = 1; $y <= $qrCodeHeight; $y++) {
$qrCodePointRow = $this->qrCode->getRow($y);
for ($x = 1; $x <= $this->qrCode->getWidth(); $x++) {
for ($x = 1; $x <= $qrCodeWidth; $x++) {
if (!isset($this->visited[$y][$x])) {
$qrCodePoint = $qrCodePointRow->getPoint($x);
if ($this->isCorner($x, $y)) {
Expand Down Expand Up @@ -86,7 +88,7 @@ private function isCorner($xPosition, $yPosition)
* @param int $startXPosition
* @param int $startYPosition
* @param bool $active
* @return array
* @return Path
*/
private function traceComposite($startXPosition, $startYPosition, $active)
{
Expand Down
4 changes: 2 additions & 2 deletions src/QrRender/QrCodeRendererEps.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
class QrCodeRendererEps implements Base\QrCodeRendererInterface
{

const MARGIN = 2;
const POINTS_PER_BLOCK = 5;

/**
Expand Down Expand Up @@ -84,7 +83,8 @@ public function render(QrCode $qrCode, $filename)
$epsSource[] = $this->foregroundColor->getEpsNotation() . ' setcmykcolor';

foreach ($paths as $path) {
for ($i = 0; $i < $path->countPoints(); $i++) {
$pointCount = $path->countPoints();
for ($i = 0; $i < $pointCount; $i++) {
if ($i == 0) {
$epsSource[] = $this->convertPoint($path->getFirstPoint()) . ' m';
} else {
Expand Down
66 changes: 6 additions & 60 deletions src/QrRender/QrCodeRendererPng.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@
*
* @package Markenwerk\QrCodeSuite\QrRender
*/
class QrCodeRendererPng implements Base\QrCodeRendererInterface
class QrCodeRendererPng extends AbstractPixelRenderer implements Base\QrCodeRendererInterface
{

const MARGIN = 2;

/**
* @var RgbColor
*/
Expand All @@ -26,21 +24,6 @@ class QrCodeRendererPng implements Base\QrCodeRendererInterface
*/
private $backgroundColor;

/**
* @var int
*/
private $approximateSize = 1000;

/**
* @var int
*/
private $width;

/**
* @var int
*/
private $height;

/**
* QrCodeRendererPng constructor.
*/
Expand Down Expand Up @@ -86,43 +69,6 @@ public function setBackgroundColor(RgbColor $backgroundColor)
return $this;
}

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

/**
* @param int $approximateSize
* @return $this
*/
public function setApproximateSize($approximateSize)
{
if (!is_int($approximateSize) || $approximateSize < 0 || $approximateSize > 5000) {
throw new \InvalidArgumentException('Approximate size has to be a positive integer less than 5000');
}
$this->approximateSize = $approximateSize;
return $this;
}

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

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

/**
* @param QrCode $qrCode
* @param string $filename
Expand All @@ -139,17 +85,17 @@ public function render(QrCode $qrCode, $filename)
$height = $qrCode->getHeight();

// Calculate params
$blockSize = round($this->approximateSize / ($width + 2 * self::MARGIN));
$this->width = (int)($width + 2 * self::MARGIN) * $blockSize;
$this->height = (int)($height + 2 * self::MARGIN) * $blockSize;
$blockSize = round($this->getApproximateSize() / ($width + 2 * self::MARGIN));
$this->setWidth((int)($width + 2 * self::MARGIN) * $blockSize);
$this->setHeight((int)($height + 2 * self::MARGIN) * $blockSize);

// Define colors
$black = new \ImagickPixel($this->foregroundColor->getHex());
$white = new \ImagickPixel($this->backgroundColor->getHex());

// Prepare canvas
$canvas = new \Imagick();
$canvas->newImage($this->width, $this->height, $white, "png");
$canvas->newImage($this->getWidth(), $this->getHeight(), $white, "png");
$canvas->setImageColorspace(\Imagick::COLORSPACE_RGB);
$canvas->setImageDepth(8);

Expand All @@ -172,7 +118,7 @@ public function render(QrCode $qrCode, $filename)
$canvas->drawImage($draw);

// Write out the image
$writeSuccess = @$canvas->writeImage($filename);
$writeSuccess = $canvas->writeImage($filename);
$canvas->clear();
$canvas->destroy();

Expand Down
63 changes: 6 additions & 57 deletions src/QrRender/QrCodeRendererTiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@
*
* @package Markenwerk\QrCodeSuite\QrRender
*/
class QrCodeRendererTiff implements Base\QrCodeRendererInterface
class QrCodeRendererTiff extends AbstractPixelRenderer implements Base\QrCodeRendererInterface
{

const MARGIN = 2;

/**
* @var CmykColor
*/
Expand All @@ -26,21 +24,6 @@ class QrCodeRendererTiff implements Base\QrCodeRendererInterface
*/
private $backgroundColor;

/**
* @var int
*/
private $approximateSize = 1000;

/**
* @var int
*/
private $width;

/**
* @var int
*/
private $height;

/**
* QrCodeRendererTiff constructor.
*/
Expand Down Expand Up @@ -87,40 +70,6 @@ public function setBackgroundColor(CmykColor $backgroundColor)
return $this;
}

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

/**
* @param int $approximateSize
* @return $this
*/
public function setApproximateSize($approximateSize)
{
$this->approximateSize = $approximateSize;
return $this;
}

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

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

/**
* @param QrCode $qrCode
* @param string $filename
Expand All @@ -137,17 +86,17 @@ public function render(QrCode $qrCode, $filename)
$height = $qrCode->getHeight();

// Calculate params
$blockSize = round($this->approximateSize / ($width + 2 * self::MARGIN));
$this->width = (int)($width + 2 * self::MARGIN) * $blockSize;
$this->height = (int)($height + 2 * self::MARGIN) * $blockSize;
$blockSize = round($this->getApproximateSize() / ($width + 2 * self::MARGIN));
$this->setWidth((int)($width + 2 * self::MARGIN) * $blockSize);
$this->setHeight((int)($height + 2 * self::MARGIN) * $blockSize);

// Define colors
$black = new \ImagickPixel($this->foregroundColor->getImagickNotation());
$white = new \ImagickPixel($this->backgroundColor->getImagickNotation());

// Prepare canvas
$canvas = new \Imagick();
$canvas->newImage($this->width, $this->height, $white, "tiff");
$canvas->newImage($this->getWidth(), $this->getHeight(), $white, "tiff");
$canvas->setImageColorspace(\Imagick::COLORSPACE_CMYK);
$canvas->setImageDepth(8);

Expand All @@ -173,7 +122,7 @@ public function render(QrCode $qrCode, $filename)
}

// Write out the image
$writeSuccess = @$canvas->writeImage($filename);
$writeSuccess = $canvas->writeImage($filename);
$canvas->clear();
$canvas->destroy();
$block->clear();
Expand Down

0 comments on commit 2be71c7

Please sign in to comment.