Fix barcode tests and implementation

This commit is contained in:
Dennis Eichhorn 2017-11-15 16:45:16 +01:00
parent 3993033465
commit 220ea81f3e
5 changed files with 43 additions and 50 deletions

View File

@ -78,6 +78,16 @@ abstract class C128Abstract
*/ */
protected $dimension = ['width' => 0, 'height' => 0]; protected $dimension = ['width' => 0, 'height' => 0];
/**
* Barcode dimension.
*
* @todo : Implement!
*
* @var int
* @since 1.0.0
*/
protected $margin = 10;
/** /**
* Content to encrypt. * Content to encrypt.
* *
@ -94,14 +104,6 @@ abstract class C128Abstract
*/ */
protected $showText = true; protected $showText = true;
/**
* Margin for barcode (padding).
*
* @var int[]
* @since 1.0.0
*/
protected $margin = ['top' => 0, 'right' => 4, 'bottom' => 0, 'left' => 4];
/** /**
* Background color. * Background color.
* *
@ -130,7 +132,7 @@ abstract class C128Abstract
* *
* @since 1.0.0 * @since 1.0.0
*/ */
public function __construct(string $content = '', int $width = 20, int $height = 20, int $orientation = OrientationType::HORIZONTAL) public function __construct(string $content = '', int $width = 100, int $height = 20, int $orientation = OrientationType::HORIZONTAL)
{ {
$this->content = $content; $this->content = $content;
$this->setDimension($width, $height); $this->setDimension($width, $height);
@ -159,6 +161,18 @@ abstract class C128Abstract
$this->dimension['height'] = $height; $this->dimension['height'] = $height;
} }
/**
* Set barcode margins
*
* @param int $margin Barcode margin
*
* @since 1.0.0
*/
public function setMargin(int $margin) /* : void */
{
$this->margin = $margin;
}
/** /**
* Set barcode orientation * Set barcode orientation
* *
@ -235,7 +249,6 @@ abstract class C128Abstract
} }
$codeString .= static::$CODEARRAY[$keys[($checksum - (intval($checksum / 103) * 103))]]; $codeString .= static::$CODEARRAY[$keys[($checksum - (intval($checksum / 103) * 103))]];
$codeString = static::$CODE_START . $codeString . static::$CODE_END;
return $codeString; return $codeString;
} }
@ -275,14 +288,24 @@ abstract class C128Abstract
$cur_size = $location + (int) (substr($codeString, ($position - 1), 1)); $cur_size = $location + (int) (substr($codeString, ($position - 1), 1));
if ($this->orientation === OrientationType::HORIZONTAL) { if ($this->orientation === OrientationType::HORIZONTAL) {
imagefilledrectangle($image, $location, 0, $cur_size, $imgHeight, ($position % 2 == 0 ? $white : $black)); imagefilledrectangle($image, $location + $this->margin, 0 + $this->margin, $cur_size + $this->margin, $imgHeight - $this->margin, ($position % 2 == 0 ? $white : $black));
} else { } else {
imagefilledrectangle($image, 0, $location, $imgWidth, $cur_size, ($position % 2 == 0 ? $white : $black)); imagefilledrectangle($image, 0 + $this->margin, $location + $this->margin, $imgWidth - $this->margin, $cur_size + $this->margin, ($position % 2 == 0 ? $white : $black));
} }
$location = $cur_size; $location = $cur_size;
} }
if($location + $this->margin < $this->dimension['width']) {
if ($this->orientation === OrientationType::HORIZONTAL) {
$image = imagecrop($image, [
'x' => 0, 'y' => 0,
'width' => $location + $this->margin * 2,
'height' => $this->dimension['height']]
);
}
}
return $image; return $image;
} }
} }

View File

@ -79,22 +79,6 @@ class C128a extends C128Abstract
*/ */
protected static $CODE_END = '2331112'; protected static $CODE_END = '2331112';
/**
* Constructor
*
* @param string $content Content to encrypt
* @param int $size Barcode height
* @param int $orientation Orientation of the barcode
*
* @todo : add mirror parameter
*
* @since 1.0.0
*/
public function __construct(string $content = '', int $size = 20, int $orientation = OrientationType::HORIZONTAL)
{
parent::__construct(strtoupper($content), $size, $orientation);
}
/** /**
* Set content to encrypt * Set content to encrypt
* *

View File

@ -65,20 +65,21 @@ class C25 extends C128Abstract
* Constructor * Constructor
* *
* @param string $content Content to encrypt * @param string $content Content to encrypt
* @param int $size Barcode height * @param int $width Barcode width
* @param int $height Barcode height
* @param int $orientation Orientation of the barcode * @param int $orientation Orientation of the barcode
* *
* @todo : add mirror parameter * @todo : add mirror parameter
* *
* @since 1.0.0 * @since 1.0.0
*/ */
public function __construct(string $content = '', int $size = 20, int $orientation = OrientationType::HORIZONTAL) public function __construct(string $content = '', int $width = 100, int $height = 20, int $orientation = OrientationType::HORIZONTAL)
{ {
if (!ctype_digit($content)) { if (!ctype_digit($content)) {
throw new \InvalidArgumentException($content); throw new \InvalidArgumentException($content);
} }
parent::__construct($content, $size, $orientation); parent::__construct($content, $width, $height, $orientation);
} }
/** /**

View File

@ -64,16 +64,17 @@ class C39 extends C128Abstract
* Constructor * Constructor
* *
* @param string $content Content to encrypt * @param string $content Content to encrypt
* @param int $size Barcode height * @param int $width Barcode width
* @param int $height Barcode height
* @param int $orientation Orientation of the barcode * @param int $orientation Orientation of the barcode
* *
* @todo : add mirror parameter * @todo : add mirror parameter
* *
* @since 1.0.0 * @since 1.0.0
*/ */
public function __construct(string $content = '', int $size = 20, int $orientation = OrientationType::HORIZONTAL) public function __construct(string $content = '', int $width = 100, int $height = 20, int $orientation = OrientationType::HORIZONTAL)
{ {
parent::__construct(strtoupper($content), $size, $orientation); parent::__construct($content, $width, $height, $orientation);
} }
/** /**

View File

@ -61,22 +61,6 @@ class Codebar extends C128Abstract
*/ */
protected static $CODE_END = '1122121'; protected static $CODE_END = '1122121';
/**
* Constructor
*
* @param string $content Content to encrypt
* @param int $size Barcode height
* @param int $orientation Orientation of the barcode
*
* @todo : add mirror parameter
*
* @since 1.0.0
*/
public function __construct(string $content = '', int $size = 20, int $orientation = OrientationType::HORIZONTAL)
{
parent::__construct(strtoupper($content), $size, $orientation);
}
/** /**
* Set content to encrypt * Set content to encrypt
* *