BCGcodabar.barcode.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. *--------------------------------------------------------------------
  4. *
  5. * Sub-Class - Codabar
  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 BCGcodabar 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', '-', '$', ':', '/', '.', '+', 'A', 'B', 'C', 'D');
  20. $this->code = array( // 0 added to add an extra space
  21. '00000110', /* 0 */
  22. '00001100', /* 1 */
  23. '00010010', /* 2 */
  24. '11000000', /* 3 */
  25. '00100100', /* 4 */
  26. '10000100', /* 5 */
  27. '01000010', /* 6 */
  28. '01001000', /* 7 */
  29. '01100000', /* 8 */
  30. '10010000', /* 9 */
  31. '00011000', /* - */
  32. '00110000', /* $ */
  33. '10001010', /* : */
  34. '10100010', /* / */
  35. '10101000', /* . */
  36. '00111110', /* + */
  37. '00110100', /* A */
  38. '01010010', /* B */
  39. '00010110', /* C */
  40. '00011100' /* D */
  41. );
  42. }
  43. /**
  44. * Parses the text before displaying it.
  45. *
  46. * @param mixed $text
  47. */
  48. public function parse($text) {
  49. parent::parse(strtoupper($text)); // Only Capital Letters are Allowed
  50. }
  51. /**
  52. * Draws the barcode.
  53. *
  54. * @param resource $im
  55. */
  56. public function draw($im) {
  57. $c = strlen($this->text);
  58. for ($i = 0; $i < $c; $i++) {
  59. $this->drawChar($im, $this->findCode($this->text[$i]), true);
  60. }
  61. $this->drawText($im, 0, 0, $this->positionX, $this->thickness);
  62. }
  63. /**
  64. * Returns the maximal size of a barcode.
  65. *
  66. * @param int $w
  67. * @param int $h
  68. * @return int[]
  69. */
  70. public function getDimension($w, $h) {
  71. $textLength = 0;
  72. $c = strlen($this->text);
  73. for ($i = 0; $i < $c; $i++) {
  74. $index = $this->findIndex($this->text[$i]);
  75. if ($index !== false) {
  76. $textLength += 8;
  77. $textLength += substr_count($this->code[$index], '1');
  78. }
  79. }
  80. $w += $textLength;
  81. $h += $this->thickness;
  82. return parent::getDimension($w, $h);
  83. }
  84. /**
  85. * Validates the input.
  86. */
  87. protected function validate() {
  88. $c = strlen($this->text);
  89. if ($c === 0) {
  90. throw new BCGParseException('codabar', 'No data has been entered.');
  91. }
  92. // Checking if all chars are allowed
  93. for ($i = 0; $i < $c; $i++) {
  94. if (array_search($this->text[$i], $this->keys) === false) {
  95. throw new BCGParseException('codabar', 'The character \'' . $this->text[$i] . '\' is not allowed.');
  96. }
  97. }
  98. // Must start by A, B, C or D
  99. if ($c == 0 || ($this->text[0] !== 'A' && $this->text[0] !== 'B' && $this->text[0] !== 'C' && $this->text[0] !== 'D')) {
  100. throw new BCGParseException('codabar', 'The text must start by the character A, B, C, or D.');
  101. }
  102. // Must end by A, B, C or D
  103. $c2 = $c - 1;
  104. if ($c2 === 0 || ($this->text[$c2] !== 'A' && $this->text[$c2] !== 'B' && $this->text[$c2] !== 'C' && $this->text[$c2] !== 'D')) {
  105. throw new BCGParseException('codabar', 'The text must end by the character A, B, C, or D.');
  106. }
  107. parent::validate();
  108. }
  109. }
  110. ?>