BCGcode11.barcode.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /**
  3. *--------------------------------------------------------------------
  4. *
  5. * Sub-Class - Code 11
  6. *
  7. *--------------------------------------------------------------------
  8. * Copyright (C) Jean-Sebastien Goupil
  9. * http://www.barcodephp.com
  10. */
  11. include_once('BCGParseException.php');
  12. include_once('BCGBarcode1D.php');
  13. class BCGcode11 extends BCGBarcode1D {
  14. /**
  15. * Constructor.
  16. */
  17. public function __construct() {
  18. parent::__construct();
  19. $this->keys = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-');
  20. $this->code = array( // 0 added to add an extra space
  21. '000010', /* 0 */
  22. '100010', /* 1 */
  23. '010010', /* 2 */
  24. '110000', /* 3 */
  25. '001010', /* 4 */
  26. '101000', /* 5 */
  27. '011000', /* 6 */
  28. '000110', /* 7 */
  29. '100100', /* 8 */
  30. '100000', /* 9 */
  31. '001000' /* - */
  32. );
  33. }
  34. /**
  35. * Draws the barcode.
  36. *
  37. * @param resource $im
  38. */
  39. public function draw($im) {
  40. // Starting Code
  41. $this->drawChar($im, '001100', true);
  42. // Chars
  43. $c = strlen($this->text);
  44. for ($i = 0; $i < $c; $i++) {
  45. $this->drawChar($im, $this->findCode($this->text[$i]), true);
  46. }
  47. // Checksum
  48. $this->calculateChecksum();
  49. $c = count($this->checksumValue);
  50. for ($i = 0; $i < $c; $i++) {
  51. $this->drawChar($im, $this->code[$this->checksumValue[$i]], true);
  52. }
  53. // Ending Code
  54. $this->drawChar($im, '00110', true);
  55. $this->drawText($im, 0, 0, $this->positionX, $this->thickness);
  56. }
  57. /**
  58. * Returns the maximal size of a barcode.
  59. *
  60. * @param int $w
  61. * @param int $h
  62. * @return int[]
  63. */
  64. public function getDimension($w, $h) {
  65. $startlength = 8;
  66. $textlength = 0;
  67. $c = strlen($this->text);
  68. for ($i = 0; $i < $c; $i++) {
  69. $textlength += $this->getIndexLength($this->findIndex($this->text[$i]));
  70. }
  71. $checksumlength = 0;
  72. $this->calculateChecksum();
  73. $c = count($this->checksumValue);
  74. for ($i = 0; $i < $c; $i++) {
  75. $checksumlength += $this->getIndexLength($this->checksumValue[$i]);
  76. }
  77. $endlength = 7;
  78. $w += $startlength + $textlength + $checksumlength + $endlength;
  79. $h += $this->thickness;
  80. return parent::getDimension($w, $h);
  81. }
  82. /**
  83. * Validates the input.
  84. */
  85. protected function validate() {
  86. $c = strlen($this->text);
  87. if ($c === 0) {
  88. throw new BCGParseException('code11', 'No data has been entered.');
  89. }
  90. // Checking if all chars are allowed
  91. for ($i = 0; $i < $c; $i++) {
  92. if (array_search($this->text[$i], $this->keys) === false) {
  93. throw new BCGParseException('code11', 'The character \'' . $this->text[$i] . '\' is not allowed.');
  94. }
  95. }
  96. parent::validate();
  97. }
  98. /**
  99. * Overloaded method to calculate checksum.
  100. */
  101. protected function calculateChecksum() {
  102. // Checksum
  103. // First CheckSUM "C"
  104. // The "C" checksum character is the modulo 11 remainder of the sum of the weighted
  105. // value of the data characters. The weighting value starts at "1" for the right-most
  106. // data character, 2 for the second to last, 3 for the third-to-last, and so on up to 20.
  107. // After 10, the sequence wraps around back to 1.
  108. // Second CheckSUM "K"
  109. // Same as CheckSUM "C" but we count the CheckSum "C" at the end
  110. // After 9, the sequence wraps around back to 1.
  111. $sequence_multiplier = array(10, 9);
  112. $temp_text = $this->text;
  113. $this->checksumValue = array();
  114. for ($z = 0; $z < 2; $z++) {
  115. $c = strlen($temp_text);
  116. // We don't display the K CheckSum if the original text had a length less than 10
  117. if ($c <= 10 && $z === 1) {
  118. break;
  119. }
  120. $checksum = 0;
  121. for ($i = $c, $j = 0; $i > 0; $i--, $j++) {
  122. $multiplier = $i % $sequence_multiplier[$z];
  123. if ($multiplier === 0) {
  124. $multiplier = $sequence_multiplier[$z];
  125. }
  126. $checksum += $this->findIndex($temp_text[$j]) * $multiplier;
  127. }
  128. $this->checksumValue[$z] = $checksum % 11;
  129. $temp_text .= $this->keys[$this->checksumValue[$z]];
  130. }
  131. }
  132. /**
  133. * Overloaded method to display the checksum.
  134. */
  135. protected function processChecksum() {
  136. if ($this->checksumValue === false) { // Calculate the checksum only once
  137. $this->calculateChecksum();
  138. }
  139. if ($this->checksumValue !== false) {
  140. $ret = '';
  141. $c = count($this->checksumValue);
  142. for ($i = 0; $i < $c; $i++) {
  143. $ret .= $this->keys[$this->checksumValue[$i]];
  144. }
  145. return $ret;
  146. }
  147. return false;
  148. }
  149. private function getIndexLength($index) {
  150. $length = 0;
  151. if ($index !== false) {
  152. $length += 6;
  153. $length += substr_count($this->code[$index], '1');
  154. }
  155. return $length;
  156. }
  157. }
  158. ?>