BCGs25.barcode.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. *--------------------------------------------------------------------
  4. *
  5. * Sub-Class - Standard 2 of 5
  6. *
  7. * TODO I25 and S25 -> 1/3 or 1/2 for the big bar
  8. *
  9. *--------------------------------------------------------------------
  10. * Copyright (C) Jean-Sebastien Goupil
  11. * http://www.barcodephp.com
  12. */
  13. include_once('BCGParseException.php');
  14. include_once('BCGBarcode1D.php');
  15. class BCGs25 extends BCGBarcode1D {
  16. private $checksum;
  17. /**
  18. * Constructor.
  19. */
  20. public function __construct() {
  21. parent::__construct();
  22. $this->keys = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
  23. $this->code = array(
  24. '0000202000', /* 0 */
  25. '2000000020', /* 1 */
  26. '0020000020', /* 2 */
  27. '2020000000', /* 3 */
  28. '0000200020', /* 4 */
  29. '2000200000', /* 5 */
  30. '0020200000', /* 6 */
  31. '0000002020', /* 7 */
  32. '2000002000', /* 8 */
  33. '0020002000' /* 9 */
  34. );
  35. $this->setChecksum(false);
  36. }
  37. /**
  38. * Sets if we display the checksum.
  39. *
  40. * @param bool $checksum
  41. */
  42. public function setChecksum($checksum) {
  43. $this->checksum = (bool)$checksum;
  44. }
  45. /**
  46. * Draws the barcode.
  47. *
  48. * @param resource $im
  49. */
  50. public function draw($im) {
  51. $temp_text = $this->text;
  52. // Checksum
  53. if ($this->checksum === true) {
  54. $this->calculateChecksum();
  55. $temp_text .= $this->keys[$this->checksumValue];
  56. }
  57. // Starting Code
  58. $this->drawChar($im, '101000', true);
  59. // Chars
  60. $c = strlen($temp_text);
  61. for ($i = 0; $i < $c; $i++) {
  62. $this->drawChar($im, $this->findCode($temp_text[$i]), true);
  63. }
  64. // Ending Code
  65. $this->drawChar($im, '10001', true);
  66. $this->drawText($im, 0, 0, $this->positionX, $this->thickness);
  67. }
  68. /**
  69. * Returns the maximal size of a barcode.
  70. *
  71. * @param int $w
  72. * @param int $h
  73. * @return int[]
  74. */
  75. public function getDimension($w, $h) {
  76. $c = strlen($this->text);
  77. $startlength = 8;
  78. $textlength = $c * 14;
  79. $checksumlength = 0;
  80. if ($c % 2 !== 0) {
  81. $checksumlength = 14;
  82. }
  83. $endlength = 7;
  84. $w += $startlength + $textlength + $checksumlength + $endlength;
  85. $h += $this->thickness;
  86. return parent::getDimension($w, $h);
  87. }
  88. /**
  89. * Validates the input.
  90. */
  91. protected function validate() {
  92. $c = strlen($this->text);
  93. if ($c === 0) {
  94. throw new BCGParseException('s25', 'No data has been entered.');
  95. }
  96. // Checking if all chars are allowed
  97. for ($i = 0; $i < $c; $i++) {
  98. if (array_search($this->text[$i], $this->keys) === false) {
  99. throw new BCGParseException('s25', 'The character \'' . $this->text[$i] . '\' is not allowed.');
  100. }
  101. }
  102. // Must be even
  103. if ($c % 2 !== 0 && $this->checksum === false) {
  104. throw new BCGParseException('s25', 's25 must contain an even amount of digits if checksum is false.');
  105. } elseif ($c % 2 === 0 && $this->checksum === true) {
  106. throw new BCGParseException('s25', 's25 must contain an odd amount of digits if checksum is true.');
  107. }
  108. parent::validate();
  109. }
  110. /**
  111. * Overloaded method to calculate checksum.
  112. */
  113. protected function calculateChecksum() {
  114. // Calculating Checksum
  115. // Consider the right-most digit of the message to be in an "even" position,
  116. // and assign odd/even to each character moving from right to left
  117. // Even Position = 3, Odd Position = 1
  118. // Multiply it by the number
  119. // Add all of that and do 10-(?mod10)
  120. $even = true;
  121. $this->checksumValue = 0;
  122. $c = strlen($this->text);
  123. for ($i = $c; $i > 0; $i--) {
  124. if ($even === true) {
  125. $multiplier = 3;
  126. $even = false;
  127. } else {
  128. $multiplier = 1;
  129. $even = true;
  130. }
  131. $this->checksumValue += $this->keys[$this->text[$i - 1]] * $multiplier;
  132. }
  133. $this->checksumValue = (10 - $this->checksumValue % 10) % 10;
  134. }
  135. /**
  136. * Overloaded method to display the checksum.
  137. */
  138. protected function processChecksum() {
  139. if ($this->checksumValue === false) { // Calculate the checksum only once
  140. $this->calculateChecksum();
  141. }
  142. if ($this->checksumValue !== false) {
  143. return $this->keys[$this->checksumValue];
  144. }
  145. return false;
  146. }
  147. }
  148. ?>