BCGi25.barcode.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. *--------------------------------------------------------------------
  4. *
  5. * Sub-Class - Interleaved 2 of 5
  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 BCGi25 extends BCGBarcode1D {
  14. private $checksum;
  15. private $ratio;
  16. /**
  17. * Constructor.
  18. */
  19. public function __construct() {
  20. parent::__construct();
  21. $this->keys = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
  22. $this->code = array(
  23. '00110', /* 0 */
  24. '10001', /* 1 */
  25. '01001', /* 2 */
  26. '11000', /* 3 */
  27. '00101', /* 4 */
  28. '10100', /* 5 */
  29. '01100', /* 6 */
  30. '00011', /* 7 */
  31. '10010', /* 8 */
  32. '01010' /* 9 */
  33. );
  34. $this->setChecksum(false);
  35. $this->setRatio(2);
  36. }
  37. /**
  38. * Sets the checksum.
  39. *
  40. * @param bool $checksum
  41. */
  42. public function setChecksum($checksum) {
  43. $this->checksum = (bool)$checksum;
  44. }
  45. /**
  46. * Sets the ratio of the black bar compared to the white bars.
  47. *
  48. * @param int $ratio
  49. */
  50. public function setRatio($ratio) {
  51. $this->ratio = $ratio;
  52. }
  53. /**
  54. * Draws the barcode.
  55. *
  56. * @param resource $im
  57. */
  58. public function draw($im) {
  59. $temp_text = $this->text;
  60. // Checksum
  61. if ($this->checksum === true) {
  62. $this->calculateChecksum();
  63. $temp_text .= $this->keys[$this->checksumValue];
  64. }
  65. // Starting Code
  66. $this->drawChar($im, '0000', true);
  67. // Chars
  68. $c = strlen($temp_text);
  69. for ($i = 0; $i < $c; $i += 2) {
  70. $temp_bar = '';
  71. $c2 = strlen($this->findCode($temp_text[$i]));
  72. for ($j = 0; $j < $c2; $j++) {
  73. $temp_bar .= substr($this->findCode($temp_text[$i]), $j, 1);
  74. $temp_bar .= substr($this->findCode($temp_text[$i + 1]), $j, 1);
  75. }
  76. $this->drawChar($im, $this->changeBars($temp_bar), true);
  77. }
  78. // Ending Code
  79. $this->drawChar($im, $this->changeBars('100'), true);
  80. $this->drawText($im, 0, 0, $this->positionX, $this->thickness);
  81. }
  82. /**
  83. * Returns the maximal size of a barcode.
  84. *
  85. * @param int $w
  86. * @param int $h
  87. * @return int[]
  88. */
  89. public function getDimension($w, $h) {
  90. $textlength = (3 + ($this->ratio + 1) * 2) * strlen($this->text);
  91. $startlength = 4;
  92. $checksumlength = 0;
  93. if ($this->checksum === true) {
  94. $checksumlength = (3 + ($this->ratio + 1) * 2);
  95. }
  96. $endlength = 2 + ($this->ratio + 1);
  97. $w += $startlength + $textlength + $checksumlength + $endlength;
  98. $h += $this->thickness;
  99. return parent::getDimension($w, $h);
  100. }
  101. /**
  102. * Validates the input.
  103. */
  104. protected function validate() {
  105. $c = strlen($this->text);
  106. if ($c === 0) {
  107. throw new BCGParseException('i25', 'No data has been entered.');
  108. }
  109. // Checking if all chars are allowed
  110. for ($i = 0; $i < $c; $i++) {
  111. if (array_search($this->text[$i], $this->keys) === false) {
  112. throw new BCGParseException('i25', 'The character \'' . $this->text[$i] . '\' is not allowed.');
  113. }
  114. }
  115. // Must be even
  116. if ($c % 2 !== 0 && $this->checksum === false) {
  117. throw new BCGParseException('i25', 'i25 must contain an even amount of digits if checksum is false.');
  118. } elseif ($c % 2 === 0 && $this->checksum === true) {
  119. throw new BCGParseException('i25', 'i25 must contain an odd amount of digits if checksum is true.');
  120. }
  121. parent::validate();
  122. }
  123. /**
  124. * Overloaded method to calculate checksum.
  125. */
  126. protected function calculateChecksum() {
  127. // Calculating Checksum
  128. // Consider the right-most digit of the message to be in an "even" position,
  129. // and assign odd/even to each character moving from right to left
  130. // Even Position = 3, Odd Position = 1
  131. // Multiply it by the number
  132. // Add all of that and do 10-(?mod10)
  133. $even = true;
  134. $this->checksumValue = 0;
  135. $c = strlen($this->text);
  136. for ($i = $c; $i > 0; $i--) {
  137. if ($even === true) {
  138. $multiplier = 3;
  139. $even = false;
  140. } else {
  141. $multiplier = 1;
  142. $even = true;
  143. }
  144. $this->checksumValue += $this->keys[$this->text[$i - 1]] * $multiplier;
  145. }
  146. $this->checksumValue = (10 - $this->checksumValue % 10) % 10;
  147. }
  148. /**
  149. * Overloaded method to display the checksum.
  150. */
  151. protected function processChecksum() {
  152. if ($this->checksumValue === false) { // Calculate the checksum only once
  153. $this->calculateChecksum();
  154. }
  155. if ($this->checksumValue !== false) {
  156. return $this->keys[$this->checksumValue];
  157. }
  158. return false;
  159. }
  160. /**
  161. * Changes the size of the bars based on the ratio
  162. *
  163. * @param string $in
  164. * @return string
  165. */
  166. private function changeBars($in) {
  167. if ($this->ratio > 1) {
  168. $c = strlen($in);
  169. for ($i = 0; $i < $c; $i++) {
  170. $in[$i] = $in[$i] === '1' ? $this->ratio : $in[$i];
  171. }
  172. }
  173. return $in;
  174. }
  175. }
  176. ?>