BCGupcext2.barcode.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. *--------------------------------------------------------------------
  4. *
  5. * Sub-Class - UPC Supplemental Barcode 2 digits
  6. *
  7. * Working with UPC-A, UPC-E, EAN-13, EAN-8
  8. * This includes 2 digits (normaly for publications)
  9. * Must be placed next to UPC or EAN Code
  10. *
  11. *--------------------------------------------------------------------
  12. * Copyright (C) Jean-Sebastien Goupil
  13. * http://www.barcodephp.com
  14. */
  15. include_once('BCGParseException.php');
  16. include_once('BCGBarcode1D.php');
  17. include_once('BCGLabel.php');
  18. class BCGupcext2 extends BCGBarcode1D {
  19. protected $codeParity = array();
  20. /**
  21. * Constructor.
  22. */
  23. public function __construct() {
  24. parent::__construct();
  25. $this->keys = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
  26. $this->code = array(
  27. '2100', /* 0 */
  28. '1110', /* 1 */
  29. '1011', /* 2 */
  30. '0300', /* 3 */
  31. '0021', /* 4 */
  32. '0120', /* 5 */
  33. '0003', /* 6 */
  34. '0201', /* 7 */
  35. '0102', /* 8 */
  36. '2001' /* 9 */
  37. );
  38. // Parity, 0=Odd, 1=Even. Depending on ?%4
  39. $this->codeParity = array(
  40. array(0, 0), /* 0 */
  41. array(0, 1), /* 1 */
  42. array(1, 0), /* 2 */
  43. array(1, 1) /* 3 */
  44. );
  45. }
  46. /**
  47. * Draws the barcode.
  48. *
  49. * @param resource $im
  50. */
  51. public function draw($im) {
  52. // Starting Code
  53. $this->drawChar($im, '001', true);
  54. // Code
  55. for ($i = 0; $i < 2; $i++) {
  56. $this->drawChar($im, self::inverse($this->findCode($this->text[$i]), $this->codeParity[intval($this->text) % 4][$i]), false);
  57. if ($i === 0) {
  58. $this->drawChar($im, '00', false); // Inter-char
  59. }
  60. }
  61. $this->drawText($im, 0, 0, $this->positionX, $this->thickness);
  62. }
  63. /**
  64. * Returns the maximal size of a barcode.
  65. *
  66. * @param int $w
  67. * @param int $h
  68. * @return int[]
  69. */
  70. public function getDimension($w, $h) {
  71. $startlength = 4;
  72. $textlength = 2 * 7;
  73. $intercharlength = 2;
  74. $w += $startlength + $textlength + $intercharlength;
  75. $h += $this->thickness;
  76. return parent::getDimension($w, $h);
  77. }
  78. /**
  79. * Adds the default label.
  80. */
  81. protected function addDefaultLabel() {
  82. parent::addDefaultLabel();
  83. if ($this->defaultLabel !== null) {
  84. $this->defaultLabel->setPosition(BCGLabel::POSITION_TOP);
  85. }
  86. }
  87. /**
  88. * Validates the input.
  89. */
  90. protected function validate() {
  91. $c = strlen($this->text);
  92. if ($c === 0) {
  93. throw new BCGParseException('upcext2', 'No data has been entered.');
  94. }
  95. // Checking if all chars are allowed
  96. for ($i = 0; $i < $c; $i++) {
  97. if (array_search($this->text[$i], $this->keys) === false) {
  98. throw new BCGParseException('upcext2', 'The character \'' . $this->text[$i] . '\' is not allowed.');
  99. }
  100. }
  101. // Must contain 2 digits
  102. if ($c !== 2) {
  103. throw new BCGParseException('upcext2', 'Must contain 2 digits.');
  104. }
  105. parent::validate();
  106. }
  107. /**
  108. * Inverses the string when the $inverse parameter is equal to 1.
  109. *
  110. * @param string $text
  111. * @param int $inverse
  112. * @return string
  113. */
  114. private static function inverse($text, $inverse = 1) {
  115. if ($inverse === 1) {
  116. $text = strrev($text);
  117. }
  118. return $text;
  119. }
  120. }
  121. ?>