BCGpostnet.barcode.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. *--------------------------------------------------------------------
  4. *
  5. * Sub-Class - PostNet
  6. *
  7. * A postnet is composed of either 5, 9 or 11 digits used by US postal service.
  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 BCGpostnet extends BCGBarcode1D {
  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. '11000', /* 0 */
  24. '00011', /* 1 */
  25. '00101', /* 2 */
  26. '00110', /* 3 */
  27. '01001', /* 4 */
  28. '01010', /* 5 */
  29. '01100', /* 6 */
  30. '10001', /* 7 */
  31. '10010', /* 8 */
  32. '10100' /* 9 */
  33. );
  34. $this->setThickness(9);
  35. }
  36. /**
  37. * Draws the barcode.
  38. *
  39. * @param resource $im
  40. */
  41. public function draw($im) {
  42. // Checksum
  43. $checksum = 0;
  44. $c = strlen($this->text);
  45. for ($i = 0; $i < $c; $i++) {
  46. $checksum += intval($this->text[$i]);
  47. }
  48. $checksum = 10 - ($checksum % 10);
  49. // Starting Code
  50. $this->drawChar($im, '1');
  51. // Code
  52. for ($i = 0; $i < $c; $i++) {
  53. $this->drawChar($im, $this->findCode($this->text[$i]));
  54. }
  55. // Checksum
  56. $this->drawChar($im, $this->findCode($checksum));
  57. // Ending Code
  58. $this->drawChar($im, '1');
  59. $this->drawText($im, 0, 0, $this->positionX, $this->thickness);
  60. }
  61. /**
  62. * Returns the maximal size of a barcode.
  63. *
  64. * @param int $w
  65. * @param int $h
  66. * @return int[]
  67. */
  68. public function getDimension($w, $h) {
  69. $c = strlen($this->text);
  70. $startlength = 3;
  71. $textlength = $c * 5 * 3;
  72. $checksumlength = 5 * 3;
  73. $endlength = 3;
  74. // We remove the white on the right
  75. $removelength = -1.56;
  76. $w += $startlength + $textlength + $checksumlength + $endlength + $removelength;
  77. $h += $this->thickness;
  78. return parent::getDimension($w, $h);
  79. }
  80. /**
  81. * Validates the input.
  82. */
  83. protected function validate() {
  84. $c = strlen($this->text);
  85. if ($c === 0) {
  86. throw new BCGParseException('postnet', 'No data has been entered.');
  87. }
  88. // Checking if all chars are allowed
  89. for ($i = 0; $i < $c; $i++) {
  90. if (array_search($this->text[$i], $this->keys) === false) {
  91. throw new BCGParseException('postnet', 'The character \'' . $this->text[$i] . '\' is not allowed.');
  92. }
  93. }
  94. // Must contain 5, 9 or 11 chars
  95. if ($c !== 5 && $c !== 9 && $c !== 11) {
  96. throw new BCGParseException('postnet', 'Must contain 5, 9, or 11 characters.');
  97. }
  98. parent::validate();
  99. }
  100. /**
  101. * Overloaded method for drawing special barcode.
  102. *
  103. * @param resource $im
  104. * @param string $code
  105. * @param boolean $startBar
  106. */
  107. protected function drawChar($im, $code, $startBar = true) {
  108. $c = strlen($code);
  109. for ($i = 0; $i < $c; $i++) {
  110. if ($code[$i] === '0') {
  111. $posY = $this->thickness - ($this->thickness / 2.5);
  112. } else {
  113. $posY = 0;
  114. }
  115. $this->drawFilledRectangle($im, $this->positionX, $posY, $this->positionX + 0.44, $this->thickness - 1, BCGBarcode::COLOR_FG);
  116. $this->positionX += 3;
  117. }
  118. }
  119. }
  120. ?>