BCGean8.barcode.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. /**
  3. *--------------------------------------------------------------------
  4. *
  5. * Sub-Class - EAN-8
  6. *
  7. * EAN-8 contains
  8. * - 4 digits
  9. * - 3 digits
  10. * - 1 checksum
  11. *
  12. * The checksum is always displayed.
  13. *
  14. *--------------------------------------------------------------------
  15. * Copyright (C) Jean-Sebastien Goupil
  16. * http://www.barcodephp.com
  17. */
  18. include_once('BCGParseException.php');
  19. include_once('BCGBarcode.php');
  20. include_once('BCGBarcode1D.php');
  21. include_once('BCGLabel.php');
  22. class BCGean8 extends BCGBarcode1D {
  23. protected $labelLeft = null;
  24. protected $labelRight = null;
  25. /**
  26. * Constructor.
  27. */
  28. public function __construct() {
  29. parent::__construct();
  30. $this->keys = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
  31. // Left-Hand Odd Parity starting with a space
  32. // Right-Hand is the same of Left-Hand starting with a bar
  33. $this->code = array(
  34. '2100', /* 0 */
  35. '1110', /* 1 */
  36. '1011', /* 2 */
  37. '0300', /* 3 */
  38. '0021', /* 4 */
  39. '0120', /* 5 */
  40. '0003', /* 6 */
  41. '0201', /* 7 */
  42. '0102', /* 8 */
  43. '2001' /* 9 */
  44. );
  45. }
  46. /**
  47. * Draws the barcode.
  48. *
  49. * @param resource $im
  50. */
  51. public function draw($im) {
  52. // Checksum
  53. $this->calculateChecksum();
  54. $temp_text = $this->text . $this->keys[$this->checksumValue];
  55. // Starting Code
  56. $this->drawChar($im, '000', true);
  57. // Draw First 4 Chars (Left-Hand)
  58. for ($i = 0; $i < 4; $i++) {
  59. $this->drawChar($im, $this->findCode($temp_text[$i]), false);
  60. }
  61. // Draw Center Guard Bar
  62. $this->drawChar($im, '00000', false);
  63. // Draw Last 4 Chars (Right-Hand)
  64. for ($i = 4; $i < 8; $i++) {
  65. $this->drawChar($im, $this->findCode($temp_text[$i]), true);
  66. }
  67. // Draw Right Guard Bar
  68. $this->drawChar($im, '000', true);
  69. $this->drawText($im, 0, 0, $this->positionX, $this->thickness);
  70. if ($this->isDefaultEanLabelEnabled()) {
  71. $dimension = $this->labelRight->getDimension();
  72. $this->drawExtendedBars($im, $dimension[1] - 2);
  73. }
  74. }
  75. /**
  76. * Returns the maximal size of a barcode.
  77. *
  78. * @param int $w
  79. * @param int $h
  80. * @return int[]
  81. */
  82. public function getDimension($w, $h) {
  83. $startlength = 3;
  84. $centerlength = 5;
  85. $textlength = 8 * 7;
  86. $endlength = 3;
  87. $w += $startlength + $centerlength + $textlength + $endlength;
  88. $h += $this->thickness;
  89. return parent::getDimension($w, $h);
  90. }
  91. /**
  92. * Adds the default label.
  93. */
  94. protected function addDefaultLabel() {
  95. if ($this->isDefaultEanLabelEnabled()) {
  96. $this->processChecksum();
  97. $label = $this->getLabel();
  98. $font = $this->font;
  99. $this->labelLeft = new BCGLabel(substr($label, 0, 4), $font, BCGLabel::POSITION_BOTTOM, BCGLabel::ALIGN_LEFT);
  100. $labelLeftDimension = $this->labelLeft->getDimension();
  101. $this->labelLeft->setOffset(($this->scale * 30 - $labelLeftDimension[0]) / 2 + $this->scale * 2);
  102. $this->labelRight = new BCGLabel(substr($label, 4, 3) . $this->keys[$this->checksumValue], $font, BCGLabel::POSITION_BOTTOM, BCGLabel::ALIGN_LEFT);
  103. $labelRightDimension = $this->labelRight->getDimension();
  104. $this->labelRight->setOffset(($this->scale * 30 - $labelRightDimension[0]) / 2 + $this->scale * 34);
  105. $this->addLabel($this->labelLeft);
  106. $this->addLabel($this->labelRight);
  107. }
  108. }
  109. /**
  110. * Checks if the default ean label is enabled.
  111. *
  112. * @return bool
  113. */
  114. protected function isDefaultEanLabelEnabled() {
  115. $label = $this->getLabel();
  116. $font = $this->font;
  117. return $label !== null && $label !== '' && $font !== null && $this->defaultLabel !== null;
  118. }
  119. /**
  120. * Validates the input.
  121. */
  122. protected function validate() {
  123. $c = strlen($this->text);
  124. if ($c === 0) {
  125. throw new BCGParseException('ean8', 'No data has been entered.');
  126. }
  127. // Checking if all chars are allowed
  128. for ($i = 0; $i < $c; $i++) {
  129. if (array_search($this->text[$i], $this->keys) === false) {
  130. throw new BCGParseException('ean8', 'The character \'' . $this->text[$i] . '\' is not allowed.');
  131. }
  132. }
  133. // If we have 8 chars just flush the last one
  134. if ($c === 8) {
  135. $this->text = substr($this->text, 0, 7);
  136. } elseif ($c !== 7) {
  137. throw new BCGParseException('ean8', 'Must contain 7 digits, the 8th digit is automatically added.');
  138. }
  139. parent::validate();
  140. }
  141. /**
  142. * Overloaded method to calculate checksum.
  143. */
  144. protected function calculateChecksum() {
  145. // Calculating Checksum
  146. // Consider the right-most digit of the message to be in an "odd" position,
  147. // and assign odd/even to each character moving from right to left
  148. // Odd Position = 3, Even Position = 1
  149. // Multiply it by the number
  150. // Add all of that and do 10-(?mod10)
  151. $odd = true;
  152. $this->checksumValue = 0;
  153. $c = strlen($this->text);
  154. for ($i = $c; $i > 0; $i--) {
  155. if ($odd === true) {
  156. $multiplier = 3;
  157. $odd = false;
  158. } else {
  159. $multiplier = 1;
  160. $odd = true;
  161. }
  162. if (!isset($this->keys[$this->text[$i - 1]])) {
  163. return;
  164. }
  165. $this->checksumValue += $this->keys[$this->text[$i - 1]] * $multiplier;
  166. }
  167. $this->checksumValue = (10 - $this->checksumValue % 10) % 10;
  168. }
  169. /**
  170. * Overloaded method to display the checksum.
  171. */
  172. protected function processChecksum() {
  173. if ($this->checksumValue === false) { // Calculate the checksum only once
  174. $this->calculateChecksum();
  175. }
  176. if ($this->checksumValue !== false) {
  177. return $this->keys[$this->checksumValue];
  178. }
  179. return false;
  180. }
  181. /**
  182. * Draws the extended bars on the image.
  183. *
  184. * @param resource $im
  185. * @param int $plus
  186. */
  187. private function drawExtendedBars($im, $plus) {
  188. $rememberX = $this->positionX;
  189. $rememberH = $this->thickness;
  190. // We increase the bars
  191. $this->thickness = $this->thickness + intval($plus / $this->scale);
  192. $this->positionX = 0;
  193. $this->drawSingleBar($im, BCGBarcode::COLOR_FG);
  194. $this->positionX += 2;
  195. $this->drawSingleBar($im, BCGBarcode::COLOR_FG);
  196. // Center Guard Bar
  197. $this->positionX += 30;
  198. $this->drawSingleBar($im, BCGBarcode::COLOR_FG);
  199. $this->positionX += 2;
  200. $this->drawSingleBar($im, BCGBarcode::COLOR_FG);
  201. // Last Bars
  202. $this->positionX += 30;
  203. $this->drawSingleBar($im, BCGBarcode::COLOR_FG);
  204. $this->positionX += 2;
  205. $this->drawSingleBar($im, BCGBarcode::COLOR_FG);
  206. $this->positionX = $rememberX;
  207. $this->thickness = $rememberH;
  208. }
  209. }
  210. ?>