PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/carempleadolist/barcode/BCGean8.barcode.php

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