BCGcode39.barcode.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /**
  3. *--------------------------------------------------------------------
  4. *
  5. * Sub-Class - Code 39
  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 BCGcode39 extends BCGBarcode1D {
  14. protected $starting, $ending;
  15. protected $checksum;
  16. /**
  17. * Constructor.
  18. */
  19. public function __construct() {
  20. parent::__construct();
  21. $this->starting = $this->ending = 43;
  22. $this->keys = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '-', '.', ' ', '$', '/', '+', '%', '*');
  23. $this->code = array( // 0 added to add an extra space
  24. '0001101000', /* 0 */
  25. '1001000010', /* 1 */
  26. '0011000010', /* 2 */
  27. '1011000000', /* 3 */
  28. '0001100010', /* 4 */
  29. '1001100000', /* 5 */
  30. '0011100000', /* 6 */
  31. '0001001010', /* 7 */
  32. '1001001000', /* 8 */
  33. '0011001000', /* 9 */
  34. '1000010010', /* A */
  35. '0010010010', /* B */
  36. '1010010000', /* C */
  37. '0000110010', /* D */
  38. '1000110000', /* E */
  39. '0010110000', /* F */
  40. '0000011010', /* G */
  41. '1000011000', /* H */
  42. '0010011000', /* I */
  43. '0000111000', /* J */
  44. '1000000110', /* K */
  45. '0010000110', /* L */
  46. '1010000100', /* M */
  47. '0000100110', /* N */
  48. '1000100100', /* O */
  49. '0010100100', /* P */
  50. '0000001110', /* Q */
  51. '1000001100', /* R */
  52. '0010001100', /* S */
  53. '0000101100', /* T */
  54. '1100000010', /* U */
  55. '0110000010', /* V */
  56. '1110000000', /* W */
  57. '0100100010', /* X */
  58. '1100100000', /* Y */
  59. '0110100000', /* Z */
  60. '0100001010', /* - */
  61. '1100001000', /* . */
  62. '0110001000', /* */
  63. '0101010000', /* $ */
  64. '0101000100', /* / */
  65. '0100010100', /* + */
  66. '0001010100', /* % */
  67. '0100101000' /* * */
  68. );
  69. $this->setChecksum(false);
  70. }
  71. /**
  72. * Sets if we display the checksum.
  73. *
  74. * @param bool $checksum
  75. */
  76. public function setChecksum($checksum) {
  77. $this->checksum = (bool)$checksum;
  78. }
  79. /**
  80. * Parses the text before displaying it.
  81. *
  82. * @param mixed $text
  83. */
  84. public function parse($text) {
  85. parent::parse(strtoupper($text)); // Only Capital Letters are Allowed
  86. }
  87. /**
  88. * Draws the barcode.
  89. *
  90. * @param resource $im
  91. */
  92. public function draw($im) {
  93. // Starting *
  94. $this->drawChar($im, $this->code[$this->starting], true);
  95. // Chars
  96. $c = strlen($this->text);
  97. for ($i = 0; $i < $c; $i++) {
  98. $this->drawChar($im, $this->findCode($this->text[$i]), true);
  99. }
  100. // Checksum (rarely used)
  101. if ($this->checksum === true) {
  102. $this->calculateChecksum();
  103. $this->drawChar($im, $this->code[$this->checksumValue % 43], true);
  104. }
  105. // Ending *
  106. $this->drawChar($im, $this->code[$this->ending], true);
  107. $this->drawText($im, 0, 0, $this->positionX, $this->thickness);
  108. }
  109. /**
  110. * Returns the maximal size of a barcode.
  111. *
  112. * @param int $w
  113. * @param int $h
  114. * @return int[]
  115. */
  116. public function getDimension($w, $h) {
  117. $textlength = 13 * strlen($this->text);
  118. $startlength = 13;
  119. $checksumlength = 0;
  120. if ($this->checksum === true) {
  121. $checksumlength = 13;
  122. }
  123. $endlength = 13;
  124. $w += $startlength + $textlength + $checksumlength + $endlength;
  125. $h += $this->thickness;
  126. return parent::getDimension($w, $h);
  127. }
  128. /**
  129. * Validates the input.
  130. */
  131. protected function validate() {
  132. $c = strlen($this->text);
  133. if ($c === 0) {
  134. throw new BCGParseException('code39', 'No data has been entered.');
  135. }
  136. // Checking if all chars are allowed
  137. for ($i = 0; $i < $c; $i++) {
  138. if (array_search($this->text[$i], $this->keys) === false) {
  139. throw new BCGParseException('code39', 'The character \'' . $this->text[$i] . '\' is not allowed.');
  140. }
  141. }
  142. if (strpos($this->text, '*') !== false) {
  143. throw new BCGParseException('code39', 'The character \'*\' is not allowed.');
  144. }
  145. parent::validate();
  146. }
  147. /**
  148. * Overloaded method to calculate checksum.
  149. */
  150. protected function calculateChecksum() {
  151. $this->checksumValue = 0;
  152. $c = strlen($this->text);
  153. for ($i = 0; $i < $c; $i++) {
  154. $this->checksumValue += $this->findIndex($this->text[$i]);
  155. }
  156. $this->checksumValue = $this->checksumValue % 43;
  157. }
  158. /**
  159. * Overloaded method to display the checksum.
  160. */
  161. protected function processChecksum() {
  162. if ($this->checksumValue === false) { // Calculate the checksum only once
  163. $this->calculateChecksum();
  164. }
  165. if ($this->checksumValue !== false) {
  166. return $this->keys[$this->checksumValue];
  167. }
  168. return false;
  169. }
  170. }
  171. ?>