diff --git a/Utils/Barcode/C128Abstract.php b/Utils/Barcode/C128Abstract.php index c82e620de..cb68f0851 100644 --- a/Utils/Barcode/C128Abstract.php +++ b/Utils/Barcode/C128Abstract.php @@ -78,6 +78,16 @@ abstract class C128Abstract */ protected $dimension = ['width' => 0, 'height' => 0]; + /** + * Barcode dimension. + * + * @todo : Implement! + * + * @var int + * @since 1.0.0 + */ + protected $margin = 10; + /** * Content to encrypt. * @@ -94,14 +104,6 @@ abstract class C128Abstract */ 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. * @@ -130,7 +132,7 @@ abstract class C128Abstract * * @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->setDimension($width, $height); @@ -159,6 +161,18 @@ abstract class C128Abstract $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 * @@ -235,7 +249,6 @@ abstract class C128Abstract } $codeString .= static::$CODEARRAY[$keys[($checksum - (intval($checksum / 103) * 103))]]; - $codeString = static::$CODE_START . $codeString . static::$CODE_END; return $codeString; } @@ -275,14 +288,24 @@ abstract class C128Abstract $cur_size = $location + (int) (substr($codeString, ($position - 1), 1)); 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 { - 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; } + 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; } } diff --git a/Utils/Barcode/C128a.php b/Utils/Barcode/C128a.php index 44f10ac8b..2a5e485a0 100644 --- a/Utils/Barcode/C128a.php +++ b/Utils/Barcode/C128a.php @@ -79,22 +79,6 @@ class C128a extends C128Abstract */ 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 * diff --git a/Utils/Barcode/C25.php b/Utils/Barcode/C25.php index 6e9c1041e..10941aacf 100644 --- a/Utils/Barcode/C25.php +++ b/Utils/Barcode/C25.php @@ -65,20 +65,21 @@ class C25 extends C128Abstract * Constructor * * @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 * * @todo : add mirror parameter * * @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)) { throw new \InvalidArgumentException($content); } - parent::__construct($content, $size, $orientation); + parent::__construct($content, $width, $height, $orientation); } /** diff --git a/Utils/Barcode/C39.php b/Utils/Barcode/C39.php index fddf4fde2..6b8dd747d 100644 --- a/Utils/Barcode/C39.php +++ b/Utils/Barcode/C39.php @@ -64,16 +64,17 @@ class C39 extends C128Abstract * Constructor * * @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 * * @todo : add mirror parameter * * @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); } /** diff --git a/Utils/Barcode/Codebar.php b/Utils/Barcode/Codebar.php index 33201c956..08ae8ddae 100644 --- a/Utils/Barcode/Codebar.php +++ b/Utils/Barcode/Codebar.php @@ -61,22 +61,6 @@ class Codebar extends C128Abstract */ 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 *