BCGupcext5.barcode.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /**
  3. *--------------------------------------------------------------------
  4. *
  5. * Sub-Class - UPC Supplemental Barcode 2 digits
  6. *
  7. * Working with UPC-A, UPC-E, EAN-13, EAN-8
  8. * This includes 5 digits (normaly for suggested retail price)
  9. * Must be placed next to UPC or EAN Code
  10. * If 90000 -> No suggested Retail Price
  11. * If 99991 -> Book Complimentary (normally free)
  12. * If 90001 to 98999 -> Internal Purpose of Publisher
  13. * If 99990 -> Used by the National Association of College Stores to mark used books
  14. * If 0xxxx -> Price Expressed in British Pounds (xx.xx)
  15. * If 5xxxx -> Price Expressed in U.S. dollars (US$xx.xx)
  16. *
  17. *--------------------------------------------------------------------
  18. * Copyright (C) Jean-Sebastien Goupil
  19. * http://www.barcodephp.com
  20. */
  21. include_once('BCGParseException.php');
  22. include_once('BCGBarcode1D.php');
  23. include_once('BCGLabel.php');
  24. class BCGupcext5 extends BCGBarcode1D {
  25. protected $codeParity = array();
  26. /**
  27. * Constructor.
  28. */
  29. public function __construct() {
  30. parent::__construct();
  31. $this->keys = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
  32. $this->code = array(
  33. '2100', /* 0 */
  34. '1110', /* 1 */
  35. '1011', /* 2 */
  36. '0300', /* 3 */
  37. '0021', /* 4 */
  38. '0120', /* 5 */
  39. '0003', /* 6 */
  40. '0201', /* 7 */
  41. '0102', /* 8 */
  42. '2001' /* 9 */
  43. );
  44. // Parity, 0=Odd, 1=Even. Depending Checksum
  45. $this->codeParity = array(
  46. array(1, 1, 0, 0, 0), /* 0 */
  47. array(1, 0, 1, 0, 0), /* 1 */
  48. array(1, 0, 0, 1, 0), /* 2 */
  49. array(1, 0, 0, 0, 1), /* 3 */
  50. array(0, 1, 1, 0, 0), /* 4 */
  51. array(0, 0, 1, 1, 0), /* 5 */
  52. array(0, 0, 0, 1, 1), /* 6 */
  53. array(0, 1, 0, 1, 0), /* 7 */
  54. array(0, 1, 0, 0, 1), /* 8 */
  55. array(0, 0, 1, 0, 1) /* 9 */
  56. );
  57. }
  58. /**
  59. * Draws the barcode.
  60. *
  61. * @param resource $im
  62. */
  63. public function draw($im) {
  64. // Checksum
  65. $this->calculateChecksum();
  66. // Starting Code
  67. $this->drawChar($im, '001', true);
  68. // Code
  69. for ($i = 0; $i < 5; $i++) {
  70. $this->drawChar($im, self::inverse($this->findCode($this->text[$i]), $this->codeParity[$this->checksumValue][$i]), false);
  71. if ($i < 4) {
  72. $this->drawChar($im, '00', false); // Inter-char
  73. }
  74. }
  75. $this->drawText($im, 0, 0, $this->positionX, $this->thickness);
  76. }
  77. /**
  78. * Returns the maximal size of a barcode.
  79. *
  80. * @param int $w
  81. * @param int $h
  82. * @return int[]
  83. */
  84. public function getDimension($w, $h) {
  85. $startlength = 4;
  86. $textlength = 5 * 7;
  87. $intercharlength = 2 * 4;
  88. $w += $startlength + $textlength + $intercharlength;
  89. $h += $this->thickness;
  90. return parent::getDimension($w, $h);
  91. }
  92. /**
  93. * Adds the default label.
  94. */
  95. protected function addDefaultLabel() {
  96. parent::addDefaultLabel();
  97. if ($this->defaultLabel !== null) {
  98. $this->defaultLabel->setPosition(BCGLabel::POSITION_TOP);
  99. }
  100. }
  101. /**
  102. * Validates the input.
  103. */
  104. protected function validate() {
  105. $c = strlen($this->text);
  106. if ($c === 0) {
  107. throw new BCGParseException('upcext5', '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('upcext5', 'The character \'' . $this->text[$i] . '\' is not allowed.');
  113. }
  114. }
  115. // Must contain 5 digits
  116. if ($c !== 5) {
  117. throw new BCGParseException('upcext5', 'Must contain 5 digits.');
  118. }
  119. parent::validate();
  120. }
  121. /**
  122. * Overloaded method to calculate checksum.
  123. */
  124. protected function calculateChecksum() {
  125. // Calculating Checksum
  126. // Consider the right-most digit of the message to be in an "odd" position,
  127. // and assign odd/even to each character moving from right to left
  128. // Odd Position = 3, Even Position = 9
  129. // Multiply it by the number
  130. // Add all of that and do ?mod10
  131. $odd = true;
  132. $this->checksumValue = 0;
  133. $c = strlen($this->text);
  134. for ($i = $c; $i > 0; $i--) {
  135. if ($odd === true) {
  136. $multiplier = 3;
  137. $odd = false;
  138. } else {
  139. $multiplier = 9;
  140. $odd = true;
  141. }
  142. if (!isset($this->keys[$this->text[$i - 1]])) {
  143. return;
  144. }
  145. $this->checksumValue += $this->keys[$this->text[$i - 1]] * $multiplier;
  146. }
  147. $this->checksumValue = $this->checksumValue % 10;
  148. }
  149. /**
  150. * Overloaded method to display the checksum.
  151. */
  152. protected function processChecksum() {
  153. if ($this->checksumValue === false) { // Calculate the checksum only once
  154. $this->calculateChecksum();
  155. }
  156. if ($this->checksumValue !== false) {
  157. return $this->keys[$this->checksumValue];
  158. }
  159. return false;
  160. }
  161. /**
  162. * Inverses the string when the $inverse parameter is equal to 1.
  163. *
  164. * @param string $text
  165. * @param int $inverse
  166. * @return string
  167. */
  168. private static function inverse($text, $inverse = 1) {
  169. if ($inverse === 1) {
  170. $text = strrev($text);
  171. }
  172. return $text;
  173. }
  174. }
  175. ?>