BCGmsi.barcode.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**
  3. *--------------------------------------------------------------------
  4. *
  5. * Sub-Class - MSI Plessey
  6. *
  7. *--------------------------------------------------------------------
  8. * Copyright (C) Jean-Sebastien Goupil
  9. * http://www.barcodephp.com
  10. */
  11. include_once('BCGParseException.php');
  12. include_once('BCGArgumentException.php');
  13. include_once('BCGBarcode1D.php');
  14. class BCGmsi extends BCGBarcode1D {
  15. private $checksum;
  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. '01010101', /* 0 */
  24. '01010110', /* 1 */
  25. '01011001', /* 2 */
  26. '01011010', /* 3 */
  27. '01100101', /* 4 */
  28. '01100110', /* 5 */
  29. '01101001', /* 6 */
  30. '01101010', /* 7 */
  31. '10010101', /* 8 */
  32. '10010110' /* 9 */
  33. );
  34. $this->setChecksum(0);
  35. }
  36. /**
  37. * Sets how many checksums we display. 0 to 2.
  38. *
  39. * @param int $checksum
  40. */
  41. public function setChecksum($checksum) {
  42. $checksum = intval($checksum);
  43. if ($checksum < 0 && $checksum > 2) {
  44. throw new BCGArgumentException('The checksum must be between 0 and 2 included.', 'checksum');
  45. }
  46. $this->checksum = $checksum;
  47. }
  48. /**
  49. * Draws the barcode.
  50. *
  51. * @param resource $im
  52. */
  53. public function draw($im) {
  54. // Checksum
  55. $this->calculateChecksum();
  56. // Starting Code
  57. $this->drawChar($im, '10', true);
  58. // Chars
  59. $c = strlen($this->text);
  60. for ($i = 0; $i < $c; $i++) {
  61. $this->drawChar($im, $this->findCode($this->text[$i]), true);
  62. }
  63. $c = count($this->checksumValue);
  64. for ($i = 0; $i < $c; $i++) {
  65. $this->drawChar($im, $this->findCode($this->checksumValue[$i]), true);
  66. }
  67. // Ending Code
  68. $this->drawChar($im, '010', true);
  69. $this->drawText($im, 0, 0, $this->positionX, $this->thickness);
  70. }
  71. /**
  72. * Returns the maximal size of a barcode.
  73. *
  74. * @param int $w
  75. * @param int $h
  76. * @return int[]
  77. */
  78. public function getDimension($w, $h) {
  79. $textlength = 12 * strlen($this->text);
  80. $startlength = 3;
  81. $checksumlength = $this->checksum * 12;
  82. $endlength = 4;
  83. $w += $startlength + $textlength + $checksumlength + $endlength;
  84. $h += $this->thickness;
  85. return parent::getDimension($w, $h);
  86. }
  87. /**
  88. * Validates the input.
  89. */
  90. protected function validate() {
  91. $c = strlen($this->text);
  92. if ($c === 0) {
  93. throw new BCGParseException('msi', '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('msi', 'The character \'' . $this->text[$i] . '\' is not allowed.');
  99. }
  100. }
  101. }
  102. /**
  103. * Overloaded method to calculate checksum.
  104. */
  105. protected function calculateChecksum() {
  106. // Forming a new number
  107. // If the original number is even, we take all even position
  108. // If the original number is odd, we take all odd position
  109. // 123456 = 246
  110. // 12345 = 135
  111. // Multiply by 2
  112. // Add up all the digit in the result (270 : 2+7+0)
  113. // Add up other digit not used.
  114. // 10 - (? Modulo 10). If result = 10, change to 0
  115. $last_text = $this->text;
  116. $this->checksumValue = array();
  117. for ($i = 0; $i < $this->checksum; $i++) {
  118. $new_text = '';
  119. $new_number = 0;
  120. $c = strlen($last_text);
  121. if ($c % 2 === 0) { // Even
  122. $starting = 1;
  123. } else {
  124. $starting = 0;
  125. }
  126. for ($j = $starting; $j < $c; $j += 2) {
  127. $new_text .= $last_text[$j];
  128. }
  129. $new_text = strval(intval($new_text) * 2);
  130. $c2 = strlen($new_text);
  131. for ($j = 0; $j < $c2; $j++) {
  132. $new_number += intval($new_text[$j]);
  133. }
  134. for ($j = ($starting === 0) ? 1 : 0; $j < $c; $j += 2) {
  135. $new_number += intval($last_text[$j]);
  136. }
  137. $new_number = (10 - $new_number % 10) % 10;
  138. $this->checksumValue[] = $new_number;
  139. $last_text .= $new_number;
  140. }
  141. }
  142. /**
  143. * Overloaded method to display the checksum.
  144. */
  145. protected function processChecksum() {
  146. if ($this->checksumValue === false) { // Calculate the checksum only once
  147. $this->calculateChecksum();
  148. }
  149. if ($this->checksumValue !== false) {
  150. $ret = '';
  151. $c = count($this->checksumValue);
  152. for ($i = 0; $i < $c; $i++) {
  153. $ret .= $this->keys[$this->checksumValue[$i]];
  154. }
  155. return $ret;
  156. }
  157. return false;
  158. }
  159. }
  160. ?>