BCGean13.barcode.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <?php
  2. /**
  3. *--------------------------------------------------------------------
  4. *
  5. * Sub-Class - EAN-13
  6. *
  7. * EAN-13 contains
  8. * - 2 system digits (1 not displayed but coded with parity)
  9. * - 5 manufacturer code digits
  10. * - 5 product digits
  11. * - 1 checksum digit
  12. *
  13. * The checksum is always displayed.
  14. *
  15. *--------------------------------------------------------------------
  16. * Copyright (C) Jean-Sebastien Goupil
  17. * http://www.barcodephp.com
  18. */
  19. include_once('BCGParseException.php');
  20. include_once('BCGBarcode.php');
  21. include_once('BCGBarcode1D.php');
  22. include_once('BCGLabel.php');
  23. class BCGean13 extends BCGBarcode1D {
  24. protected $codeParity = array();
  25. protected $labelLeft = null;
  26. protected $labelCenter1 = null;
  27. protected $labelCenter2 = null;
  28. protected $alignLabel;
  29. /**
  30. * Constructor.
  31. */
  32. public function __construct() {
  33. parent::__construct();
  34. $this->keys = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
  35. // Left-Hand Odd Parity starting with a space
  36. // Left-Hand Even Parity is the inverse (0=0012) starting with a space
  37. // Right-Hand is the same of Left-Hand starting with a bar
  38. $this->code = array(
  39. '2100', /* 0 */
  40. '1110', /* 1 */
  41. '1011', /* 2 */
  42. '0300', /* 3 */
  43. '0021', /* 4 */
  44. '0120', /* 5 */
  45. '0003', /* 6 */
  46. '0201', /* 7 */
  47. '0102', /* 8 */
  48. '2001' /* 9 */
  49. );
  50. // Parity, 0=Odd, 1=Even for manufacturer code. Depending on 1st System Digit
  51. $this->codeParity = array(
  52. array(0, 0, 0, 0, 0), /* 0 */
  53. array(0, 1, 0, 1, 1), /* 1 */
  54. array(0, 1, 1, 0, 1), /* 2 */
  55. array(0, 1, 1, 1, 0), /* 3 */
  56. array(1, 0, 0, 1, 1), /* 4 */
  57. array(1, 1, 0, 0, 1), /* 5 */
  58. array(1, 1, 1, 0, 0), /* 6 */
  59. array(1, 0, 1, 0, 1), /* 7 */
  60. array(1, 0, 1, 1, 0), /* 8 */
  61. array(1, 1, 0, 1, 0) /* 9 */
  62. );
  63. $this->alignDefaultLabel(true);
  64. }
  65. public function alignDefaultLabel($align) {
  66. $this->alignLabel = (bool)$align;
  67. }
  68. /**
  69. * Draws the barcode.
  70. *
  71. * @param resource $im
  72. */
  73. public function draw($im) {
  74. $this->drawBars($im);
  75. $this->drawText($im, 0, 0, $this->positionX, $this->thickness);
  76. if ($this->isDefaultEanLabelEnabled()) {
  77. $dimension = $this->labelCenter1->getDimension();
  78. $this->drawExtendedBars($im, $dimension[1] - 2);
  79. }
  80. }
  81. /**
  82. * Returns the maximal size of a barcode.
  83. *
  84. * @param int $w
  85. * @param int $h
  86. * @return int[]
  87. */
  88. public function getDimension($w, $h) {
  89. $startlength = 3;
  90. $centerlength = 5;
  91. $textlength = 12 * 7;
  92. $endlength = 3;
  93. $w += $startlength + $centerlength + $textlength + $endlength;
  94. $h += $this->thickness;
  95. return parent::getDimension($w, $h);
  96. }
  97. /**
  98. * Adds the default label.
  99. */
  100. protected function addDefaultLabel() {
  101. if ($this->isDefaultEanLabelEnabled()) {
  102. $this->processChecksum();
  103. $label = $this->getLabel();
  104. $font = $this->font;
  105. $this->labelLeft = new BCGLabel(substr($label, 0, 1), $font, BCGLabel::POSITION_LEFT, BCGLabel::ALIGN_BOTTOM);
  106. $this->labelLeft->setSpacing(4 * $this->scale);
  107. $this->labelCenter1 = new BCGLabel(substr($label, 1, 6), $font, BCGLabel::POSITION_BOTTOM, BCGLabel::ALIGN_LEFT);
  108. $labelCenter1Dimension = $this->labelCenter1->getDimension();
  109. $this->labelCenter1->setOffset(($this->scale * 44 - $labelCenter1Dimension[0]) / 2 + $this->scale * 2);
  110. $this->labelCenter2 = new BCGLabel(substr($label, 7, 5) . $this->keys[$this->checksumValue], $font, BCGLabel::POSITION_BOTTOM, BCGLabel::ALIGN_LEFT);
  111. $this->labelCenter2->setOffset(($this->scale * 44 - $labelCenter1Dimension[0]) / 2 + $this->scale * 48);
  112. if ($this->alignLabel) {
  113. $labelDimension = $this->labelCenter1->getDimension();
  114. $this->labelLeft->setOffset($labelDimension[1]);
  115. } else {
  116. $labelDimension = $this->labelLeft->getDimension();
  117. $this->labelLeft->setOffset($labelDimension[1] / 2);
  118. }
  119. $this->addLabel($this->labelLeft);
  120. $this->addLabel($this->labelCenter1);
  121. $this->addLabel($this->labelCenter2);
  122. }
  123. }
  124. /**
  125. * Checks if the default ean label is enabled.
  126. *
  127. * @return bool
  128. */
  129. protected function isDefaultEanLabelEnabled() {
  130. $label = $this->getLabel();
  131. $font = $this->font;
  132. return $label !== null && $label !== '' && $font !== null && $this->defaultLabel !== null;
  133. }
  134. /**
  135. * Validates the input.
  136. */
  137. protected function validate() {
  138. $c = strlen($this->text);
  139. if ($c === 0) {
  140. throw new BCGParseException('ean13', 'No data has been entered.');
  141. }
  142. $this->checkCharsAllowed();
  143. $this->checkCorrectLength();
  144. parent::validate();
  145. }
  146. /**
  147. * Check chars allowed.
  148. */
  149. protected function checkCharsAllowed() {
  150. // Checking if all chars are allowed
  151. $c = strlen($this->text);
  152. for ($i = 0; $i < $c; $i++) {
  153. if (array_search($this->text[$i], $this->keys) === false) {
  154. throw new BCGParseException('ean13', 'The character \'' . $this->text[$i] . '\' is not allowed.');
  155. }
  156. }
  157. }
  158. /**
  159. * Check correct length.
  160. */
  161. protected function checkCorrectLength() {
  162. // If we have 13 chars, just flush the last one without throwing anything
  163. $c = strlen($this->text);
  164. if ($c === 13) {
  165. $this->text = substr($this->text, 0, 12);
  166. } elseif ($c !== 12) {
  167. throw new BCGParseException('ean13', 'Must contain 12 digits, the 13th digit is automatically added.');
  168. }
  169. }
  170. /**
  171. * Overloaded method to calculate checksum.
  172. */
  173. protected function calculateChecksum() {
  174. // Calculating Checksum
  175. // Consider the right-most digit of the message to be in an "odd" position,
  176. // and assign odd/even to each character moving from right to left
  177. // Odd Position = 3, Even Position = 1
  178. // Multiply it by the number
  179. // Add all of that and do 10-(?mod10)
  180. $odd = true;
  181. $this->checksumValue = 0;
  182. $c = strlen($this->text);
  183. for ($i = $c; $i > 0; $i--) {
  184. if ($odd === true) {
  185. $multiplier = 3;
  186. $odd = false;
  187. } else {
  188. $multiplier = 1;
  189. $odd = true;
  190. }
  191. if (!isset($this->keys[$this->text[$i - 1]])) {
  192. return;
  193. }
  194. $this->checksumValue += $this->keys[$this->text[$i - 1]] * $multiplier;
  195. }
  196. $this->checksumValue = (10 - $this->checksumValue % 10) % 10;
  197. }
  198. /**
  199. * Overloaded method to display the checksum.
  200. */
  201. protected function processChecksum() {
  202. if ($this->checksumValue === false) { // Calculate the checksum only once
  203. $this->calculateChecksum();
  204. }
  205. if ($this->checksumValue !== false) {
  206. return $this->keys[$this->checksumValue];
  207. }
  208. return false;
  209. }
  210. /**
  211. * Draws the bars
  212. *
  213. * @param resource $im
  214. */
  215. protected function drawBars($im) {
  216. // Checksum
  217. $this->calculateChecksum();
  218. $temp_text = $this->text . $this->keys[$this->checksumValue];
  219. // Starting Code
  220. $this->drawChar($im, '000', true);
  221. // Draw Second Code
  222. $this->drawChar($im, $this->findCode($temp_text[1]), false);
  223. // Draw Manufacturer Code
  224. for ($i = 0; $i < 5; $i++) {
  225. $this->drawChar($im, self::inverse($this->findCode($temp_text[$i + 2]), $this->codeParity[(int)$temp_text[0]][$i]), false);
  226. }
  227. // Draw Center Guard Bar
  228. $this->drawChar($im, '00000', false);
  229. // Draw Product Code
  230. for ($i = 7; $i < 13; $i++) {
  231. $this->drawChar($im, $this->findCode($temp_text[$i]), true);
  232. }
  233. // Draw Right Guard Bar
  234. $this->drawChar($im, '000', true);
  235. }
  236. /**
  237. * Draws the extended bars on the image.
  238. *
  239. * @param resource $im
  240. * @param int $plus
  241. */
  242. protected function drawExtendedBars($im, $plus) {
  243. $rememberX = $this->positionX;
  244. $rememberH = $this->thickness;
  245. // We increase the bars
  246. $this->thickness = $this->thickness + intval($plus / $this->scale);
  247. $this->positionX = 0;
  248. $this->drawSingleBar($im, BCGBarcode::COLOR_FG);
  249. $this->positionX += 2;
  250. $this->drawSingleBar($im, BCGBarcode::COLOR_FG);
  251. // Center Guard Bar
  252. $this->positionX += 44;
  253. $this->drawSingleBar($im, BCGBarcode::COLOR_FG);
  254. $this->positionX += 2;
  255. $this->drawSingleBar($im, BCGBarcode::COLOR_FG);
  256. // Last Bars
  257. $this->positionX += 44;
  258. $this->drawSingleBar($im, BCGBarcode::COLOR_FG);
  259. $this->positionX += 2;
  260. $this->drawSingleBar($im, BCGBarcode::COLOR_FG);
  261. $this->positionX = $rememberX;
  262. $this->thickness = $rememberH;
  263. }
  264. /**
  265. * Inverses the string when the $inverse parameter is equal to 1.
  266. *
  267. * @param string $text
  268. * @param int $inverse
  269. * @return string
  270. */
  271. private static function inverse($text, $inverse = 1) {
  272. if ($inverse === 1) {
  273. $text = strrev($text);
  274. }
  275. return $text;
  276. }
  277. }
  278. ?>