You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2103 lines
66KB

  1. <?php
  2. // Adapted for mPDF from TCPDF barcode. Original Details left below.
  3. //============================================================+
  4. // File name : barcodes.php
  5. // Begin : 2008-06-09
  6. // Last Update : 2009-04-15
  7. // Version : 1.0.008
  8. // License : GNU LGPL (http://www.gnu.org/copyleft/lesser.html)
  9. // ----------------------------------------------------------------------------
  10. // Copyright (C) 2008-2009 Nicola Asuni - Tecnick.com S.r.l.
  11. //
  12. // This program is free software: you can redistribute it and/or modify
  13. // it under the terms of the GNU Lesser General Public License as published by
  14. // the Free Software Foundation, either version 2.1 of the License, or
  15. // (at your option) any later version.
  16. //
  17. // This program is distributed in the hope that it will be useful,
  18. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. // GNU Lesser General Public License for more details.
  21. //
  22. // You should have received a copy of the GNU Lesser General Public License
  23. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. //
  25. // See LICENSE.TXT file for more information.
  26. // ----------------------------------------------------------------------------
  27. //
  28. // Description : PHP class to creates array representations for
  29. // common 1D barcodes to be used with TCPDF.
  30. //
  31. // Author: Nicola Asuni
  32. //
  33. // (c) Copyright:
  34. // Nicola Asuni
  35. // Tecnick.com S.r.l.
  36. // Via della Pace, 11
  37. // 09044 Quartucciu (CA)
  38. // ITALY
  39. // www.tecnick.com
  40. // info@tecnick.com
  41. //============================================================+
  42. require_once __DIR__ . '/../MpdfException.php';
  43. class PDFBarcode
  44. {
  45. protected $barcode_array;
  46. protected $gapwidth;
  47. protected $print_ratio;
  48. protected $daft;
  49. public function __construct()
  50. {
  51. }
  52. public function getBarcodeArray($code, $type, $pr = '')
  53. {
  54. $this->setBarcode($code, $type, $pr);
  55. return $this->barcode_array;
  56. }
  57. public function getChecksum($code, $type)
  58. {
  59. $this->setBarcode($code, $type);
  60. if (!$this->barcode_array) {
  61. return '';
  62. } else {
  63. return $this->barcode_array['checkdigit'];
  64. }
  65. }
  66. public function setBarcode($code, $type, $pr = '')
  67. {
  68. $this->print_ratio = 1;
  69. switch (strtoupper($type)) {
  70. case 'ISBN':
  71. case 'ISSN':
  72. case 'EAN13': { // EAN 13
  73. $arrcode = $this->barcode_eanupc($code, 13);
  74. $arrcode['lightmL'] = 11; // LEFT light margin = x X-dim (http://www.gs1uk.org)
  75. $arrcode['lightmR'] = 7; // RIGHT light margin = x X-dim (http://www.gs1uk.org)
  76. $arrcode['nom-X'] = 0.33; // Nominal value for X-dim in mm (http://www.gs1uk.org)
  77. $arrcode['nom-H'] = 25.93; // Nominal bar height in mm incl. numerals (http://www.gs1uk.org)
  78. break;
  79. }
  80. case 'UPCA': { // UPC-A
  81. $arrcode = $this->barcode_eanupc($code, 12);
  82. $arrcode['lightmL'] = 9; // LEFT light margin = x X-dim (http://www.gs1uk.org)
  83. $arrcode['lightmR'] = 9; // RIGHT light margin = x X-dim (http://www.gs1uk.org)
  84. $arrcode['nom-X'] = 0.33; // Nominal value for X-dim in mm (http://www.gs1uk.org)
  85. $arrcode['nom-H'] = 25.91; // Nominal bar height in mm incl. numerals (http://www.gs1uk.org)
  86. break;
  87. }
  88. case 'UPCE': { // UPC-E
  89. $arrcode = $this->barcode_eanupc($code, 6);
  90. $arrcode['lightmL'] = 9; // LEFT light margin = x X-dim (http://www.gs1uk.org)
  91. $arrcode['lightmR'] = 7; // RIGHT light margin = x X-dim (http://www.gs1uk.org)
  92. $arrcode['nom-X'] = 0.33; // Nominal value for X-dim in mm (http://www.gs1uk.org)
  93. $arrcode['nom-H'] = 25.93; // Nominal bar height in mm incl. numerals (http://www.gs1uk.org)
  94. break;
  95. }
  96. case 'EAN8': { // EAN 8
  97. $arrcode = $this->barcode_eanupc($code, 8);
  98. $arrcode['lightmL'] = 7; // LEFT light margin = x X-dim (http://www.gs1uk.org)
  99. $arrcode['lightmR'] = 7; // RIGHT light margin = x X-dim (http://www.gs1uk.org)
  100. $arrcode['nom-X'] = 0.33; // Nominal value for X-dim in mm (http://www.gs1uk.org)
  101. $arrcode['nom-H'] = 21.64; // Nominal bar height in mm incl. numerals (http://www.gs1uk.org)
  102. break;
  103. }
  104. case 'EAN2': { // 2-Digits UPC-Based Extention
  105. $arrcode = $this->barcode_eanext($code, 2);
  106. $arrcode['lightmL'] = 7; // LEFT light margin = x X-dim (estimated)
  107. $arrcode['lightmR'] = 7; // RIGHT light margin = x X-dim (estimated)
  108. $arrcode['sepM'] = 9; // SEPARATION margin = x X-dim (http://web.archive.org/web/19990501035133/http://www.uc-council.org/d36-d.htm)
  109. $arrcode['nom-X'] = 0.33; // Nominal value for X-dim in mm (http://www.gs1uk.org)
  110. $arrcode['nom-H'] = 20; // Nominal bar height in mm incl. numerals (estimated) not used when combined
  111. break;
  112. }
  113. case 'EAN5': { // 5-Digits UPC-Based Extention
  114. $arrcode = $this->barcode_eanext($code, 5);
  115. $arrcode['lightmL'] = 7; // LEFT light margin = x X-dim (estimated)
  116. $arrcode['lightmR'] = 7; // RIGHT light margin = x X-dim (estimated)
  117. $arrcode['sepM'] = 9; // SEPARATION margin = x X-dim (http://web.archive.org/web/19990501035133/http://www.uc-council.org/d36-d.htm)
  118. $arrcode['nom-X'] = 0.33; // Nominal value for X-dim in mm (http://www.gs1uk.org)
  119. $arrcode['nom-H'] = 20; // Nominal bar height in mm incl. numerals (estimated) not used when combined
  120. break;
  121. }
  122. case 'IMB': { // IMB - Intelligent Mail Barcode - Onecode - USPS-B-3200
  123. $xdim = 0.508; // Nominal value for X-dim (bar width) in mm (spec.)
  124. $bpi = 22; // Bars per inch
  125. // Ratio of Nominal value for width of spaces in mm / Nominal value for X-dim (bar width) in mm based on bars per inch
  126. $this->gapwidth = ((25.4 / $bpi) - $xdim) / $xdim;
  127. $this->daft = array('D' => 2, 'A' => 2, 'F' => 3, 'T' => 1); // Descender; Ascender; Full; Tracker bar heights
  128. $arrcode = $this->barcode_imb($code);
  129. $arrcode['nom-X'] = $xdim;
  130. $arrcode['nom-H'] = 3.68; // Nominal value for Height of Full bar in mm (spec.)
  131. // USPS-B-3200 Revision C = 4.623
  132. // USPS-B-3200 Revision E = 3.68
  133. $arrcode['quietL'] = 3.175; // LEFT Quiet margin = mm (spec.)
  134. $arrcode['quietR'] = 3.175; // RIGHT Quiet margin = mm (spec.)
  135. $arrcode['quietTB'] = 0.711; // TOP/BOTTOM Quiet margin = mm (spec.)
  136. break;
  137. }
  138. case 'RM4SCC': { // RM4SCC (Royal Mail 4-state Customer Code) - CBC (Customer Bar Code)
  139. $xdim = 0.508; // Nominal value for X-dim (bar width) in mm (spec.)
  140. $bpi = 22; // Bars per inch
  141. // Ratio of Nominal value for width of spaces in mm / Nominal value for X-dim (bar width) in mm based on bars per inch
  142. $this->gapwidth = ((25.4 / $bpi) - $xdim) / $xdim;
  143. $this->daft = array('D' => 5, 'A' => 5, 'F' => 8, 'T' => 2); // Descender; Ascender; Full; Tracker bar heights
  144. $arrcode = $this->barcode_rm4scc($code, false);
  145. $arrcode['nom-X'] = $xdim;
  146. $arrcode['nom-H'] = 5.0; // Nominal value for Height of Full bar in mm (spec.)
  147. $arrcode['quietL'] = 2; // LEFT Quiet margin = mm (spec.)
  148. $arrcode['quietR'] = 2; // RIGHT Quiet margin = mm (spec.)
  149. $arrcode['quietTB'] = 2; // TOP/BOTTOM Quiet margin = mm (spec?)
  150. break;
  151. }
  152. case 'KIX': { // KIX (Klant index - Customer index)
  153. $xdim = 0.508; // Nominal value for X-dim (bar width) in mm (spec.)
  154. $bpi = 22; // Bars per inch
  155. // Ratio of Nominal value for width of spaces in mm / Nominal value for X-dim (bar width) in mm based on bars per inch
  156. $this->gapwidth = ((25.4 / $bpi) - $xdim) / $xdim;
  157. $this->daft = array('D' => 5, 'A' => 5, 'F' => 8, 'T' => 2); // Descender; Ascender; Full; Tracker bar heights
  158. $arrcode = $this->barcode_rm4scc($code, true);
  159. $arrcode['nom-X'] = $xdim;
  160. $arrcode['nom-H'] = 5.0; // Nominal value for Height of Full bar in mm (? spec.)
  161. $arrcode['quietL'] = 2; // LEFT Quiet margin = mm (spec.)
  162. $arrcode['quietR'] = 2; // RIGHT Quiet margin = mm (spec.)
  163. $arrcode['quietTB'] = 2; // TOP/BOTTOM Quiet margin = mm (spec.)
  164. break;
  165. }
  166. case 'POSTNET': { // POSTNET
  167. $xdim = 0.508; // Nominal value for X-dim (bar width) in mm (spec.)
  168. $bpi = 22; // Bars per inch
  169. // Ratio of Nominal value for width of spaces in mm / Nominal value for X-dim (bar width) in mm based on bars per inch
  170. $this->gapwidth = ((25.4 / $bpi) - $xdim) / $xdim;
  171. $arrcode = $this->barcode_postnet($code, false);
  172. $arrcode['nom-X'] = $xdim;
  173. $arrcode['nom-H'] = 3.175; // Nominal value for Height of Full bar in mm (spec.)
  174. $arrcode['quietL'] = 3.175; // LEFT Quiet margin = mm (?spec.)
  175. $arrcode['quietR'] = 3.175; // RIGHT Quiet margin = mm (?spec.)
  176. $arrcode['quietTB'] = 1.016; // TOP/BOTTOM Quiet margin = mm (?spec.)
  177. break;
  178. }
  179. case 'PLANET': { // PLANET
  180. $xdim = 0.508; // Nominal value for X-dim (bar width) in mm (spec.)
  181. $bpi = 22; // Bars per inch
  182. // Ratio of Nominal value for width of spaces in mm / Nominal value for X-dim (bar width) in mm based on bars per inch
  183. $this->gapwidth = ((25.4 / $bpi) - $xdim) / $xdim;
  184. $arrcode = $this->barcode_postnet($code, true);
  185. $arrcode['nom-X'] = $xdim;
  186. $arrcode['nom-H'] = 3.175; // Nominal value for Height of Full bar in mm (spec.)
  187. $arrcode['quietL'] = 3.175; // LEFT Quiet margin = mm (?spec.)
  188. $arrcode['quietR'] = 3.175; // RIGHT Quiet margin = mm (?spec.)
  189. $arrcode['quietTB'] = 1.016; // TOP/BOTTOM Quiet margin = mm (?spec.)
  190. break;
  191. }
  192. case 'C93': { // CODE 93 - USS-93
  193. $arrcode = $this->barcode_code93($code);
  194. if ($arrcode == false) {
  195. break;
  196. }
  197. $arrcode['nom-X'] = 0.381; // Nominal value for X-dim (bar width) in mm (2 X min. spec.)
  198. $arrcode['nom-H'] = 10; // Nominal value for Height of Full bar in mm (non-spec.)
  199. $arrcode['lightmL'] = 10; // LEFT light margin = x X-dim (spec.)
  200. $arrcode['lightmR'] = 10; // RIGHT light margin = x X-dim (spec.)
  201. $arrcode['lightTB'] = 0; // TOP/BOTTOM light margin = x X-dim (non-spec.)
  202. break;
  203. }
  204. case 'CODE11': { // CODE 11
  205. if ($pr > 0) {
  206. $this->print_ratio = $pr;
  207. } else {
  208. $this->print_ratio = 3;
  209. } // spec: Pr= 1:2.24 - 1:3.5
  210. $arrcode = $this->barcode_code11($code);
  211. if ($arrcode == false) {
  212. break;
  213. }
  214. $arrcode['nom-X'] = 0.381; // Nominal value for X-dim (bar width) in mm (2 X min. spec.)
  215. $arrcode['nom-H'] = 10; // Nominal value for Height of Full bar in mm (non-spec.)
  216. $arrcode['lightmL'] = 10; // LEFT light margin = x X-dim (spec.)
  217. $arrcode['lightmR'] = 10; // RIGHT light margin = x X-dim (spec.)
  218. $arrcode['lightTB'] = 0; // TOP/BOTTOM light margin = x X-dim (non-spec.)
  219. break;
  220. }
  221. case 'MSI': // MSI (Variation of Plessey code)
  222. case 'MSI+': { // MSI + CHECKSUM (modulo 11)
  223. if (strtoupper($type) == 'MSI') {
  224. $arrcode = $this->barcode_msi($code, false);
  225. }
  226. if (strtoupper($type) == 'MSI+') {
  227. $arrcode = $this->barcode_msi($code, true);
  228. }
  229. if ($arrcode == false) {
  230. break;
  231. }
  232. $arrcode['nom-X'] = 0.381; // Nominal value for X-dim (bar width) in mm (2 X min. spec.)
  233. $arrcode['nom-H'] = 10; // Nominal value for Height of Full bar in mm (non-spec.)
  234. $arrcode['lightmL'] = 12; // LEFT light margin = x X-dim (spec.)
  235. $arrcode['lightmR'] = 12; // RIGHT light margin = x X-dim (spec.)
  236. $arrcode['lightTB'] = 0; // TOP/BOTTOM light margin = x X-dim (non-spec.)
  237. break;
  238. }
  239. case 'CODABAR': { // CODABAR
  240. if ($pr > 0) {
  241. $this->print_ratio = $pr;
  242. } else {
  243. $this->print_ratio = 2.5;
  244. } // spec: Pr= 1:2 - 1:3 (>2.2 if X<0.50)
  245. if (strtoupper($type) == 'CODABAR') {
  246. $arrcode = $this->barcode_codabar($code);
  247. }
  248. if ($arrcode == false) {
  249. break;
  250. }
  251. $arrcode['nom-X'] = 0.381; // Nominal value for X-dim (bar width) in mm (2 X min. spec.)
  252. $arrcode['nom-H'] = 10; // Nominal value for Height of Full bar in mm (non-spec.)
  253. $arrcode['lightmL'] = 10; // LEFT light margin = x X-dim (spec.)
  254. $arrcode['lightmR'] = 10; // RIGHT light margin = x X-dim (spec.)
  255. $arrcode['lightTB'] = 0; // TOP/BOTTOM light margin = x X-dim (non-spec.)
  256. break;
  257. }
  258. case 'C128A': // CODE 128 A
  259. case 'C128B': // CODE 128 B
  260. case 'C128C': // CODE 128 C
  261. case 'EAN128A': // EAN 128 A
  262. case 'EAN128B': // EAN 128 B
  263. case 'EAN128C': { // EAN 128 C
  264. if (strtoupper($type) == 'C128A') {
  265. $arrcode = $this->barcode_c128($code, 'A');
  266. }
  267. if (strtoupper($type) == 'C128B') {
  268. $arrcode = $this->barcode_c128($code, 'B');
  269. }
  270. if (strtoupper($type) == 'C128C') {
  271. $arrcode = $this->barcode_c128($code, 'C');
  272. }
  273. if (strtoupper($type) == 'EAN128A') {
  274. $arrcode = $this->barcode_c128($code, 'A', true);
  275. }
  276. if (strtoupper($type) == 'EAN128B') {
  277. $arrcode = $this->barcode_c128($code, 'B', true);
  278. }
  279. if (strtoupper($type) == 'EAN128C') {
  280. $arrcode = $this->barcode_c128($code, 'C', true);
  281. }
  282. if ($arrcode == false) {
  283. break;
  284. }
  285. $arrcode['nom-X'] = 0.381; // Nominal value for X-dim (bar width) in mm (2 X min. spec.)
  286. $arrcode['nom-H'] = 10; // Nominal value for Height of Full bar in mm (non-spec.)
  287. $arrcode['lightmL'] = 10; // LEFT light margin = x X-dim (spec.)
  288. $arrcode['lightmR'] = 10; // RIGHT light margin = x X-dim (spec.)
  289. $arrcode['lightTB'] = 0; // TOP/BOTTOM light margin = x X-dim (non-spec.)
  290. break;
  291. }
  292. case 'C39': // CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9.
  293. case 'C39+': // CODE 39 with checksum
  294. case 'C39E': // CODE 39 EXTENDED
  295. case 'C39E+': { // CODE 39 EXTENDED + CHECKSUM
  296. if ($pr > 0) {
  297. $this->print_ratio = $pr;
  298. } else {
  299. $this->print_ratio = 2.5;
  300. } // spec: Pr= 1:2 - 1:3 (>2.2 if X<0.50)
  301. $code = str_replace(chr(194) . chr(160), ' ', $code); // mPDF 5.3.95 (for utf-8 encoded)
  302. $code = str_replace(chr(160), ' ', $code); // mPDF 5.3.95 (for win-1252)
  303. if (strtoupper($type) == 'C39') {
  304. $arrcode = $this->barcode_code39($code, false, false);
  305. }
  306. if (strtoupper($type) == 'C39+') {
  307. $arrcode = $this->barcode_code39($code, false, true);
  308. }
  309. if (strtoupper($type) == 'C39E') {
  310. $arrcode = $this->barcode_code39($code, true, false);
  311. }
  312. if (strtoupper($type) == 'C39E+') {
  313. $arrcode = $this->barcode_code39($code, true, true);
  314. }
  315. if ($arrcode == false) {
  316. break;
  317. }
  318. $arrcode['nom-X'] = 0.381; // Nominal value for X-dim (bar width) in mm (2 X min. spec.)
  319. $arrcode['nom-H'] = 10; // Nominal value for Height of Full bar in mm (non-spec.)
  320. $arrcode['lightmL'] = 10; // LEFT light margin = x X-dim (spec.)
  321. $arrcode['lightmR'] = 10; // RIGHT light margin = x X-dim (spec.)
  322. $arrcode['lightTB'] = 0; // TOP/BOTTOM light margin = x X-dim (non-spec.)
  323. break;
  324. }
  325. case 'S25': // Standard 2 of 5
  326. case 'S25+': { // Standard 2 of 5 + CHECKSUM
  327. if ($pr > 0) {
  328. $this->print_ratio = $pr;
  329. } else {
  330. $this->print_ratio = 3;
  331. } // spec: Pr=1:3/1:4.5
  332. if (strtoupper($type) == 'S25') {
  333. $arrcode = $this->barcode_s25($code, false);
  334. }
  335. if (strtoupper($type) == 'S25+') {
  336. $arrcode = $this->barcode_s25($code, true);
  337. }
  338. if ($arrcode == false) {
  339. break;
  340. }
  341. $arrcode['nom-X'] = 0.381; // Nominal value for X-dim (bar width) in mm (2 X min. spec.)
  342. $arrcode['nom-H'] = 10; // Nominal value for Height of Full bar in mm (non-spec.)
  343. $arrcode['lightmL'] = 10; // LEFT light margin = x X-dim (spec.)
  344. $arrcode['lightmR'] = 10; // RIGHT light margin = x X-dim (spec.)
  345. $arrcode['lightTB'] = 0; // TOP/BOTTOM light margin = x X-dim (non-spec.)
  346. break;
  347. }
  348. case 'I25': // Interleaved 2 of 5
  349. case 'I25+': { // Interleaved 2 of 5 + CHECKSUM
  350. if ($pr > 0) {
  351. $this->print_ratio = $pr;
  352. } else {
  353. $this->print_ratio = 2.5;
  354. } // spec: Pr= 1:2 - 1:3 (>2.2 if X<0.50)
  355. if (strtoupper($type) == 'I25') {
  356. $arrcode = $this->barcode_i25($code, false);
  357. }
  358. if (strtoupper($type) == 'I25+') {
  359. $arrcode = $this->barcode_i25($code, true);
  360. }
  361. if ($arrcode == false) {
  362. break;
  363. }
  364. $arrcode['nom-X'] = 0.381; // Nominal value for X-dim (bar width) in mm (2 X min. spec.)
  365. $arrcode['nom-H'] = 10; // Nominal value for Height of Full bar in mm (non-spec.)
  366. $arrcode['lightmL'] = 10; // LEFT light margin = x X-dim (spec.)
  367. $arrcode['lightmR'] = 10; // RIGHT light margin = x X-dim (spec.)
  368. $arrcode['lightTB'] = 0; // TOP/BOTTOM light margin = x X-dim (non-spec.)
  369. break;
  370. }
  371. case 'I25B': // Interleaved 2 of 5 + Bearer bars
  372. case 'I25B+': { // Interleaved 2 of 5 + CHECKSUM + Bearer bars
  373. if ($pr > 0) {
  374. $this->print_ratio = $pr;
  375. } else {
  376. $this->print_ratio = 2.5;
  377. } // spec: Pr= 1:2 - 1:3 (>2.2 if X<0.50)
  378. if (strtoupper($type) == 'I25B') {
  379. $arrcode = $this->barcode_i25($code, false);
  380. }
  381. if (strtoupper($type) == 'I25B+') {
  382. $arrcode = $this->barcode_i25($code, true);
  383. }
  384. if ($arrcode == false) {
  385. break;
  386. }
  387. $arrcode['nom-X'] = 0.381; // Nominal value for X-dim (bar width) in mm (2 X min. spec.)
  388. $arrcode['nom-H'] = 10; // Nominal value for Height of Full bar in mm (non-spec.)
  389. $arrcode['lightmL'] = 10; // LEFT light margin = x X-dim (spec.)
  390. $arrcode['lightmR'] = 10; // RIGHT light margin = x X-dim (spec.)
  391. $arrcode['lightTB'] = 2; // TOP/BOTTOM light margin = x X-dim (non-spec.) - used for bearer bars
  392. break;
  393. }
  394. default: {
  395. $this->barcode_array = false;
  396. }
  397. }
  398. $this->barcode_array = $arrcode;
  399. }
  400. /**
  401. * CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9.
  402. */
  403. protected function barcode_code39($code, $extended = false, $checksum = false)
  404. {
  405. $chr['0'] = '111221211';
  406. $chr['1'] = '211211112';
  407. $chr['2'] = '112211112';
  408. $chr['3'] = '212211111';
  409. $chr['4'] = '111221112';
  410. $chr['5'] = '211221111';
  411. $chr['6'] = '112221111';
  412. $chr['7'] = '111211212';
  413. $chr['8'] = '211211211';
  414. $chr['9'] = '112211211';
  415. $chr['A'] = '211112112';
  416. $chr['B'] = '112112112';
  417. $chr['C'] = '212112111';
  418. $chr['D'] = '111122112';
  419. $chr['E'] = '211122111';
  420. $chr['F'] = '112122111';
  421. $chr['G'] = '111112212';
  422. $chr['H'] = '211112211';
  423. $chr['I'] = '112112211';
  424. $chr['J'] = '111122211';
  425. $chr['K'] = '211111122';
  426. $chr['L'] = '112111122';
  427. $chr['M'] = '212111121';
  428. $chr['N'] = '111121122';
  429. $chr['O'] = '211121121';
  430. $chr['P'] = '112121121';
  431. $chr['Q'] = '111111222';
  432. $chr['R'] = '211111221';
  433. $chr['S'] = '112111221';
  434. $chr['T'] = '111121221';
  435. $chr['U'] = '221111112';
  436. $chr['V'] = '122111112';
  437. $chr['W'] = '222111111';
  438. $chr['X'] = '121121112';
  439. $chr['Y'] = '221121111';
  440. $chr['Z'] = '122121111';
  441. $chr['-'] = '121111212';
  442. $chr['.'] = '221111211';
  443. $chr[' '] = '122111211';
  444. $chr['$'] = '121212111';
  445. $chr['/'] = '121211121';
  446. $chr['+'] = '121112121';
  447. $chr['%'] = '111212121';
  448. $chr['*'] = '121121211';
  449. $code = strtoupper($code);
  450. $checkdigit = '';
  451. if ($extended) {
  452. // extended mode
  453. $code = $this->encode_code39_ext($code);
  454. }
  455. if ($code === false) {
  456. return false;
  457. }
  458. if ($checksum) {
  459. // checksum
  460. $checkdigit = $this->checksum_code39($code);
  461. $code .= $checkdigit;
  462. }
  463. // add start and stop codes
  464. $code = '*' . $code . '*';
  465. $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
  466. $k = 0;
  467. $clen = strlen($code);
  468. for ($i = 0; $i < $clen; ++$i) {
  469. $char = $code[$i];
  470. if (!isset($chr[$char])) {
  471. // invalid character
  472. return false;
  473. }
  474. for ($j = 0; $j < 9; ++$j) {
  475. if (($j % 2) == 0) {
  476. $t = true; // bar
  477. } else {
  478. $t = false; // space
  479. }
  480. $x = $chr[$char][$j];
  481. if ($x == 2) {
  482. $w = $this->print_ratio;
  483. } else {
  484. $w = 1;
  485. }
  486. $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0);
  487. $bararray['maxw'] += $w;
  488. ++$k;
  489. }
  490. $bararray['bcode'][$k] = array('t' => false, 'w' => 1, 'h' => 1, 'p' => 0);
  491. $bararray['maxw'] += 1;
  492. ++$k;
  493. }
  494. $bararray['checkdigit'] = $checkdigit;
  495. return $bararray;
  496. }
  497. /**
  498. * Encode a string to be used for CODE 39 Extended mode.
  499. */
  500. protected function encode_code39_ext($code)
  501. {
  502. $encode = array(
  503. chr(0) => '%U', chr(1) => '$A', chr(2) => '$B', chr(3) => '$C',
  504. chr(4) => '$D', chr(5) => '$E', chr(6) => '$F', chr(7) => '$G',
  505. chr(8) => '$H', chr(9) => '$I', chr(10) => '$J', chr(11) => '£K',
  506. chr(12) => '$L', chr(13) => '$M', chr(14) => '$N', chr(15) => '$O',
  507. chr(16) => '$P', chr(17) => '$Q', chr(18) => '$R', chr(19) => '$S',
  508. chr(20) => '$T', chr(21) => '$U', chr(22) => '$V', chr(23) => '$W',
  509. chr(24) => '$X', chr(25) => '$Y', chr(26) => '$Z', chr(27) => '%A',
  510. chr(28) => '%B', chr(29) => '%C', chr(30) => '%D', chr(31) => '%E',
  511. chr(32) => ' ', chr(33) => '/A', chr(34) => '/B', chr(35) => '/C',
  512. chr(36) => '/D', chr(37) => '/E', chr(38) => '/F', chr(39) => '/G',
  513. chr(40) => '/H', chr(41) => '/I', chr(42) => '/J', chr(43) => '/K',
  514. chr(44) => '/L', chr(45) => '-', chr(46) => '.', chr(47) => '/O',
  515. chr(48) => '0', chr(49) => '1', chr(50) => '2', chr(51) => '3',
  516. chr(52) => '4', chr(53) => '5', chr(54) => '6', chr(55) => '7',
  517. chr(56) => '8', chr(57) => '9', chr(58) => '/Z', chr(59) => '%F',
  518. chr(60) => '%G', chr(61) => '%H', chr(62) => '%I', chr(63) => '%J',
  519. chr(64) => '%V', chr(65) => 'A', chr(66) => 'B', chr(67) => 'C',
  520. chr(68) => 'D', chr(69) => 'E', chr(70) => 'F', chr(71) => 'G',
  521. chr(72) => 'H', chr(73) => 'I', chr(74) => 'J', chr(75) => 'K',
  522. chr(76) => 'L', chr(77) => 'M', chr(78) => 'N', chr(79) => 'O',
  523. chr(80) => 'P', chr(81) => 'Q', chr(82) => 'R', chr(83) => 'S',
  524. chr(84) => 'T', chr(85) => 'U', chr(86) => 'V', chr(87) => 'W',
  525. chr(88) => 'X', chr(89) => 'Y', chr(90) => 'Z', chr(91) => '%K',
  526. chr(92) => '%L', chr(93) => '%M', chr(94) => '%N', chr(95) => '%O',
  527. chr(96) => '%W', chr(97) => '+A', chr(98) => '+B', chr(99) => '+C',
  528. chr(100) => '+D', chr(101) => '+E', chr(102) => '+F', chr(103) => '+G',
  529. chr(104) => '+H', chr(105) => '+I', chr(106) => '+J', chr(107) => '+K',
  530. chr(108) => '+L', chr(109) => '+M', chr(110) => '+N', chr(111) => '+O',
  531. chr(112) => '+P', chr(113) => '+Q', chr(114) => '+R', chr(115) => '+S',
  532. chr(116) => '+T', chr(117) => '+U', chr(118) => '+V', chr(119) => '+W',
  533. chr(120) => '+X', chr(121) => '+Y', chr(122) => '+Z', chr(123) => '%P',
  534. chr(124) => '%Q', chr(125) => '%R', chr(126) => '%S', chr(127) => '%T');
  535. $code_ext = '';
  536. $clen = strlen($code);
  537. for ($i = 0; $i < $clen; ++$i) {
  538. if (ord($code[$i]) > 127) {
  539. return false;
  540. }
  541. $code_ext .= $encode[$code[$i]];
  542. }
  543. return $code_ext;
  544. }
  545. /**
  546. * Calculate CODE 39 checksum (modulo 43).
  547. */
  548. protected function checksum_code39($code)
  549. {
  550. $chars = array(
  551. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  552. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
  553. 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
  554. 'W', 'X', 'Y', 'Z', '-', '.', ' ', '$', '/', '+', '%');
  555. $sum = 0;
  556. $clen = strlen($code);
  557. for ($i = 0; $i < $clen; ++$i) {
  558. $k = array_keys($chars, $code[$i]);
  559. $sum += $k[0];
  560. }
  561. $j = ($sum % 43);
  562. return $chars[$j];
  563. }
  564. /**
  565. * CODE 93 - USS-93
  566. * Compact code similar to Code 39
  567. */
  568. protected function barcode_code93($code)
  569. {
  570. $chr[48] = '131112'; // 0
  571. $chr[49] = '111213'; // 1
  572. $chr[50] = '111312'; // 2
  573. $chr[51] = '111411'; // 3
  574. $chr[52] = '121113'; // 4
  575. $chr[53] = '121212'; // 5
  576. $chr[54] = '121311'; // 6
  577. $chr[55] = '111114'; // 7
  578. $chr[56] = '131211'; // 8
  579. $chr[57] = '141111'; // 9
  580. $chr[65] = '211113'; // A
  581. $chr[66] = '211212'; // B
  582. $chr[67] = '211311'; // C
  583. $chr[68] = '221112'; // D
  584. $chr[69] = '221211'; // E
  585. $chr[70] = '231111'; // F
  586. $chr[71] = '112113'; // G
  587. $chr[72] = '112212'; // H
  588. $chr[73] = '112311'; // I
  589. $chr[74] = '122112'; // J
  590. $chr[75] = '132111'; // K
  591. $chr[76] = '111123'; // L
  592. $chr[77] = '111222'; // M
  593. $chr[78] = '111321'; // N
  594. $chr[79] = '121122'; // O
  595. $chr[80] = '131121'; // P
  596. $chr[81] = '212112'; // Q
  597. $chr[82] = '212211'; // R
  598. $chr[83] = '211122'; // S
  599. $chr[84] = '211221'; // T
  600. $chr[85] = '221121'; // U
  601. $chr[86] = '222111'; // V
  602. $chr[87] = '112122'; // W
  603. $chr[88] = '112221'; // X
  604. $chr[89] = '122121'; // Y
  605. $chr[90] = '123111'; // Z
  606. $chr[45] = '121131'; // -
  607. $chr[46] = '311112'; // .
  608. $chr[32] = '311211'; //
  609. $chr[36] = '321111'; // $
  610. $chr[47] = '112131'; // /
  611. $chr[43] = '113121'; // +
  612. $chr[37] = '211131'; // %
  613. $chr[128] = '121221'; // ($)
  614. $chr[129] = '311121'; // (/)
  615. $chr[130] = '122211'; // (+)
  616. $chr[131] = '312111'; // (%)
  617. $chr[42] = '111141'; // start-stop
  618. $code = strtoupper($code);
  619. $encode = array(
  620. chr(0) => chr(131) . 'U', chr(1) => chr(128) . 'A', chr(2) => chr(128) . 'B', chr(3) => chr(128) . 'C',
  621. chr(4) => chr(128) . 'D', chr(5) => chr(128) . 'E', chr(6) => chr(128) . 'F', chr(7) => chr(128) . 'G',
  622. chr(8) => chr(128) . 'H', chr(9) => chr(128) . 'I', chr(10) => chr(128) . 'J', chr(11) => '£K',
  623. chr(12) => chr(128) . 'L', chr(13) => chr(128) . 'M', chr(14) => chr(128) . 'N', chr(15) => chr(128) . 'O',
  624. chr(16) => chr(128) . 'P', chr(17) => chr(128) . 'Q', chr(18) => chr(128) . 'R', chr(19) => chr(128) . 'S',
  625. chr(20) => chr(128) . 'T', chr(21) => chr(128) . 'U', chr(22) => chr(128) . 'V', chr(23) => chr(128) . 'W',
  626. chr(24) => chr(128) . 'X', chr(25) => chr(128) . 'Y', chr(26) => chr(128) . 'Z', chr(27) => chr(131) . 'A',
  627. chr(28) => chr(131) . 'B', chr(29) => chr(131) . 'C', chr(30) => chr(131) . 'D', chr(31) => chr(131) . 'E',
  628. chr(32) => ' ', chr(33) => chr(129) . 'A', chr(34) => chr(129) . 'B', chr(35) => chr(129) . 'C',
  629. chr(36) => chr(129) . 'D', chr(37) => chr(129) . 'E', chr(38) => chr(129) . 'F', chr(39) => chr(129) . 'G',
  630. chr(40) => chr(129) . 'H', chr(41) => chr(129) . 'I', chr(42) => chr(129) . 'J', chr(43) => chr(129) . 'K',
  631. chr(44) => chr(129) . 'L', chr(45) => '-', chr(46) => '.', chr(47) => chr(129) . 'O',
  632. chr(48) => '0', chr(49) => '1', chr(50) => '2', chr(51) => '3',
  633. chr(52) => '4', chr(53) => '5', chr(54) => '6', chr(55) => '7',
  634. chr(56) => '8', chr(57) => '9', chr(58) => chr(129) . 'Z', chr(59) => chr(131) . 'F',
  635. chr(60) => chr(131) . 'G', chr(61) => chr(131) . 'H', chr(62) => chr(131) . 'I', chr(63) => chr(131) . 'J',
  636. chr(64) => chr(131) . 'V', chr(65) => 'A', chr(66) => 'B', chr(67) => 'C',
  637. chr(68) => 'D', chr(69) => 'E', chr(70) => 'F', chr(71) => 'G',
  638. chr(72) => 'H', chr(73) => 'I', chr(74) => 'J', chr(75) => 'K',
  639. chr(76) => 'L', chr(77) => 'M', chr(78) => 'N', chr(79) => 'O',
  640. chr(80) => 'P', chr(81) => 'Q', chr(82) => 'R', chr(83) => 'S',
  641. chr(84) => 'T', chr(85) => 'U', chr(86) => 'V', chr(87) => 'W',
  642. chr(88) => 'X', chr(89) => 'Y', chr(90) => 'Z', chr(91) => chr(131) . 'K',
  643. chr(92) => chr(131) . 'L', chr(93) => chr(131) . 'M', chr(94) => chr(131) . 'N', chr(95) => chr(131) . 'O',
  644. chr(96) => chr(131) . 'W', chr(97) => chr(130) . 'A', chr(98) => chr(130) . 'B', chr(99) => chr(130) . 'C',
  645. chr(100) => chr(130) . 'D', chr(101) => chr(130) . 'E', chr(102) => chr(130) . 'F', chr(103) => chr(130) . 'G',
  646. chr(104) => chr(130) . 'H', chr(105) => chr(130) . 'I', chr(106) => chr(130) . 'J', chr(107) => chr(130) . 'K',
  647. chr(108) => chr(130) . 'L', chr(109) => chr(130) . 'M', chr(110) => chr(130) . 'N', chr(111) => chr(130) . 'O',
  648. chr(112) => chr(130) . 'P', chr(113) => chr(130) . 'Q', chr(114) => chr(130) . 'R', chr(115) => chr(130) . 'S',
  649. chr(116) => chr(130) . 'T', chr(117) => chr(130) . 'U', chr(118) => chr(130) . 'V', chr(119) => chr(130) . 'W',
  650. chr(120) => chr(130) . 'X', chr(121) => chr(130) . 'Y', chr(122) => chr(130) . 'Z', chr(123) => chr(131) . 'P',
  651. chr(124) => chr(131) . 'Q', chr(125) => chr(131) . 'R', chr(126) => chr(131) . 'S', chr(127) => chr(131) . 'T');
  652. $code_ext = '';
  653. $clen = strlen($code);
  654. for ($i = 0; $i < $clen; ++$i) {
  655. if (ord($code{$i}) > 127) {
  656. return false;
  657. }
  658. $code_ext .= $encode[$code{$i}];
  659. }
  660. // checksum
  661. $code_ext .= $this->checksum_code93($code_ext);
  662. // add start and stop codes
  663. $code = '*' . $code_ext . '*';
  664. $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
  665. $k = 0;
  666. $clen = strlen($code);
  667. for ($i = 0; $i < $clen; ++$i) {
  668. $char = ord($code{$i});
  669. if (!isset($chr[$char])) {
  670. // invalid character
  671. return false;
  672. }
  673. for ($j = 0; $j < 6; ++$j) {
  674. if (($j % 2) == 0) {
  675. $t = true; // bar
  676. } else {
  677. $t = false; // space
  678. }
  679. $w = $chr[$char]{$j};
  680. $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0);
  681. $bararray['maxw'] += $w;
  682. ++$k;
  683. }
  684. }
  685. $bararray['bcode'][$k] = array('t' => true, 'w' => 1, 'h' => 1, 'p' => 0);
  686. $bararray['maxw'] += 1;
  687. ++$k;
  688. return $bararray;
  689. }
  690. /**
  691. * Calculate CODE 93 checksum (modulo 47).
  692. */
  693. protected function checksum_code93($code)
  694. {
  695. $chars = array(
  696. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  697. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
  698. 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
  699. 'W', 'X', 'Y', 'Z', '-', '.', ' ', '$', '/', '+', '%',
  700. '<', '=', '>', '?');
  701. // translate special characters
  702. $code = strtr($code, chr(128) . chr(131) . chr(129) . chr(130), '<=>?');
  703. $len = strlen($code);
  704. // calculate check digit C
  705. $p = 1;
  706. $check = 0;
  707. for ($i = ($len - 1); $i >= 0; --$i) {
  708. $k = array_keys($chars, $code{$i});
  709. $check += ($k[0] * $p);
  710. ++$p;
  711. if ($p > 20) {
  712. $p = 1;
  713. }
  714. }
  715. $check %= 47;
  716. $c = $chars[$check];
  717. $code .= $c;
  718. // calculate check digit K
  719. $p = 1;
  720. $check = 0;
  721. for ($i = $len; $i >= 0; --$i) {
  722. $k = array_keys($chars, $code{$i});
  723. $check += ($k[0] * $p);
  724. ++$p;
  725. if ($p > 15) {
  726. $p = 1;
  727. }
  728. }
  729. $check %= 47;
  730. $k = $chars[$check];
  731. $checksum = $c . $k;
  732. // resto respecial characters
  733. $checksum = strtr($checksum, '<=>?', chr(128) . chr(131) . chr(129) . chr(130));
  734. return $checksum;
  735. }
  736. /**
  737. * Checksum for standard 2 of 5 barcodes.
  738. */
  739. protected function checksum_s25($code)
  740. {
  741. $len = strlen($code);
  742. $sum = 0;
  743. for ($i = 0; $i < $len; $i+=2) {
  744. $sum += $code[$i];
  745. }
  746. $sum *= 3;
  747. for ($i = 1; $i < $len; $i+=2) {
  748. $sum += ($code[$i]);
  749. }
  750. $r = $sum % 10;
  751. if ($r > 0) {
  752. $r = (10 - $r);
  753. }
  754. return $r;
  755. }
  756. /**
  757. * MSI.
  758. * Variation of Plessey code, with similar applications
  759. * Contains digits (0 to 9) and encodes the data only in the width of bars.
  760. */
  761. protected function barcode_msi($code, $checksum = false)
  762. {
  763. $chr['0'] = '100100100100';
  764. $chr['1'] = '100100100110';
  765. $chr['2'] = '100100110100';
  766. $chr['3'] = '100100110110';
  767. $chr['4'] = '100110100100';
  768. $chr['5'] = '100110100110';
  769. $chr['6'] = '100110110100';
  770. $chr['7'] = '100110110110';
  771. $chr['8'] = '110100100100';
  772. $chr['9'] = '110100100110';
  773. $chr['A'] = '110100110100';
  774. $chr['B'] = '110100110110';
  775. $chr['C'] = '110110100100';
  776. $chr['D'] = '110110100110';
  777. $chr['E'] = '110110110100';
  778. $chr['F'] = '110110110110';
  779. $checkdigit = '';
  780. if ($checksum) {
  781. // add checksum
  782. $clen = strlen($code);
  783. $p = 2;
  784. $check = 0;
  785. for ($i = ($clen - 1); $i >= 0; --$i) {
  786. $check += (hexdec($code[$i]) * $p);
  787. ++$p;
  788. if ($p > 7) {
  789. $p = 2;
  790. }
  791. }
  792. $check %= 11;
  793. if ($check > 0) {
  794. $check = 11 - $check;
  795. }
  796. $code .= $check;
  797. $checkdigit = $check;
  798. }
  799. $seq = '110'; // left guard
  800. $clen = strlen($code);
  801. for ($i = 0; $i < $clen; ++$i) {
  802. $digit = $code[$i];
  803. if (!isset($chr[$digit])) {
  804. // invalid character
  805. return false;
  806. }
  807. $seq .= $chr[$digit];
  808. }
  809. $seq .= '1001'; // right guard
  810. $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
  811. $bararray['checkdigit'] = $checkdigit;
  812. return $this->binseq_to_array($seq, $bararray);
  813. }
  814. /**
  815. * Standard 2 of 5 barcodes.
  816. * Used in airline ticket marking, photofinishing
  817. * Contains digits (0 to 9) and encodes the data only in the width of bars.
  818. */
  819. protected function barcode_s25($code, $checksum = false)
  820. {
  821. $chr['0'] = '10101110111010';
  822. $chr['1'] = '11101010101110';
  823. $chr['2'] = '10111010101110';
  824. $chr['3'] = '11101110101010';
  825. $chr['4'] = '10101110101110';
  826. $chr['5'] = '11101011101010';
  827. $chr['6'] = '10111011101010';
  828. $chr['7'] = '10101011101110';
  829. $chr['8'] = '10101110111010';
  830. $chr['9'] = '10111010111010';
  831. $checkdigit = '';
  832. if ($checksum) {
  833. // add checksum
  834. $checkdigit = $this->checksum_s25($code);
  835. $code .= $checkdigit;
  836. }
  837. if ((strlen($code) % 2) != 0) {
  838. // add leading zero if code-length is odd
  839. $code = '0' . $code;
  840. }
  841. $seq = '11011010';
  842. $clen = strlen($code);
  843. for ($i = 0; $i < $clen; ++$i) {
  844. $digit = $code[$i];
  845. if (!isset($chr[$digit])) {
  846. // invalid character
  847. return false;
  848. }
  849. $seq .= $chr[$digit];
  850. }
  851. $seq .= '1101011';
  852. $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
  853. $bararray['checkdigit'] = $checkdigit;
  854. return $this->binseq_to_array($seq, $bararray);
  855. }
  856. /**
  857. * Convert binary barcode sequence to barcode array
  858. */
  859. protected function binseq_to_array($seq, $bararray)
  860. {
  861. $len = strlen($seq);
  862. $w = 0;
  863. $k = 0;
  864. for ($i = 0; $i < $len; ++$i) {
  865. $w += 1;
  866. if (($i == ($len - 1)) OR ( ($i < ($len - 1)) AND ( $seq[$i] != $seq[($i + 1)]))) {
  867. if ($seq[$i] == '1') {
  868. $t = true; // bar
  869. } else {
  870. $t = false; // space
  871. }
  872. $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0);
  873. $bararray['maxw'] += $w;
  874. ++$k;
  875. $w = 0;
  876. }
  877. }
  878. return $bararray;
  879. }
  880. /**
  881. * Interleaved 2 of 5 barcodes.
  882. * Compact numeric code, widely used in industry, air cargo
  883. * Contains digits (0 to 9) and encodes the data in the width of both bars and spaces.
  884. */
  885. protected function barcode_i25($code, $checksum = false)
  886. {
  887. $chr['0'] = '11221';
  888. $chr['1'] = '21112';
  889. $chr['2'] = '12112';
  890. $chr['3'] = '22111';
  891. $chr['4'] = '11212';
  892. $chr['5'] = '21211';
  893. $chr['6'] = '12211';
  894. $chr['7'] = '11122';
  895. $chr['8'] = '21121';
  896. $chr['9'] = '12121';
  897. $chr['A'] = '11';
  898. $chr['Z'] = '21';
  899. $checkdigit = '';
  900. if ($checksum) {
  901. // add checksum
  902. $checkdigit = $this->checksum_s25($code);
  903. $code .= $checkdigit;
  904. }
  905. if ((strlen($code) % 2) != 0) {
  906. // add leading zero if code-length is odd
  907. $code = '0' . $code;
  908. }
  909. // add start and stop codes
  910. $code = 'AA' . strtolower($code) . 'ZA';
  911. $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
  912. $k = 0;
  913. $clen = strlen($code);
  914. for ($i = 0; $i < $clen; $i = ($i + 2)) {
  915. $char_bar = $code[$i];
  916. $char_space = $code[$i + 1];
  917. if ((!isset($chr[$char_bar])) OR ( !isset($chr[$char_space]))) {
  918. // invalid character
  919. return false;
  920. }
  921. // create a bar-space sequence
  922. $seq = '';
  923. $chrlen = strlen($chr[$char_bar]);
  924. for ($s = 0; $s < $chrlen; $s++) {
  925. $seq .= $chr[$char_bar][$s] . $chr[$char_space][$s];
  926. }
  927. $seqlen = strlen($seq);
  928. for ($j = 0; $j < $seqlen; ++$j) {
  929. if (($j % 2) == 0) {
  930. $t = true; // bar
  931. } else {
  932. $t = false; // space
  933. }
  934. $x = $seq[$j];
  935. if ($x == 2) {
  936. $w = $this->print_ratio;
  937. } else {
  938. $w = 1;
  939. }
  940. $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0);
  941. $bararray['maxw'] += $w;
  942. ++$k;
  943. }
  944. }
  945. $bararray['checkdigit'] = $checkdigit;
  946. return $bararray;
  947. }
  948. /**
  949. * C128 barcodes.
  950. * Very capable code, excellent density, high reliability; in very wide use world-wide
  951. */
  952. protected function barcode_c128($code, $type = 'B', $ean = false)
  953. {
  954. $code = strcode2utf($code); // mPDF 5.7.1 Allows e.g. <barcode code="5432&#013;1068" type="C128A" />
  955. $chr = array(
  956. '212222', /* 00 */
  957. '222122', /* 01 */
  958. '222221', /* 02 */
  959. '121223', /* 03 */
  960. '121322', /* 04 */
  961. '131222', /* 05 */
  962. '122213', /* 06 */
  963. '122312', /* 07 */
  964. '132212', /* 08 */
  965. '221213', /* 09 */
  966. '221312', /* 10 */
  967. '231212', /* 11 */
  968. '112232', /* 12 */
  969. '122132', /* 13 */
  970. '122231', /* 14 */
  971. '113222', /* 15 */
  972. '123122', /* 16 */
  973. '123221', /* 17 */
  974. '223211', /* 18 */
  975. '221132', /* 19 */
  976. '221231', /* 20 */
  977. '213212', /* 21 */
  978. '223112', /* 22 */
  979. '312131', /* 23 */
  980. '311222', /* 24 */
  981. '321122', /* 25 */
  982. '321221', /* 26 */
  983. '312212', /* 27 */
  984. '322112', /* 28 */
  985. '322211', /* 29 */
  986. '212123', /* 30 */
  987. '212321', /* 31 */
  988. '232121', /* 32 */
  989. '111323', /* 33 */
  990. '131123', /* 34 */
  991. '131321', /* 35 */
  992. '112313', /* 36 */
  993. '132113', /* 37 */
  994. '132311', /* 38 */
  995. '211313', /* 39 */
  996. '231113', /* 40 */
  997. '231311', /* 41 */
  998. '112133', /* 42 */
  999. '112331', /* 43 */
  1000. '132131', /* 44 */
  1001. '113123', /* 45 */
  1002. '113321', /* 46 */
  1003. '133121', /* 47 */
  1004. '313121', /* 48 */
  1005. '211331', /* 49 */
  1006. '231131', /* 50 */
  1007. '213113', /* 51 */
  1008. '213311', /* 52 */
  1009. '213131', /* 53 */
  1010. '311123', /* 54 */
  1011. '311321', /* 55 */
  1012. '331121', /* 56 */
  1013. '312113', /* 57 */
  1014. '312311', /* 58 */
  1015. '332111', /* 59 */
  1016. '314111', /* 60 */
  1017. '221411', /* 61 */
  1018. '431111', /* 62 */
  1019. '111224', /* 63 */
  1020. '111422', /* 64 */
  1021. '121124', /* 65 */
  1022. '121421', /* 66 */
  1023. '141122', /* 67 */
  1024. '141221', /* 68 */
  1025. '112214', /* 69 */
  1026. '112412', /* 70 */
  1027. '122114', /* 71 */
  1028. '122411', /* 72 */
  1029. '142112', /* 73 */
  1030. '142211', /* 74 */
  1031. '241211', /* 75 */
  1032. '221114', /* 76 */
  1033. '413111', /* 77 */
  1034. '241112', /* 78 */
  1035. '134111', /* 79 */
  1036. '111242', /* 80 */
  1037. '121142', /* 81 */
  1038. '121241', /* 82 */
  1039. '114212', /* 83 */
  1040. '124112', /* 84 */
  1041. '124211', /* 85 */
  1042. '411212', /* 86 */
  1043. '421112', /* 87 */
  1044. '421211', /* 88 */
  1045. '212141', /* 89 */
  1046. '214121', /* 90 */
  1047. '412121', /* 91 */
  1048. '111143', /* 92 */
  1049. '111341', /* 93 */
  1050. '131141', /* 94 */
  1051. '114113', /* 95 */
  1052. '114311', /* 96 */
  1053. '411113', /* 97 */
  1054. '411311', /* 98 */
  1055. '113141', /* 99 */
  1056. '114131', /* 100 */
  1057. '311141', /* 101 */
  1058. '411131', /* 102 */
  1059. '211412', /* 103 START A */
  1060. '211214', /* 104 START B */
  1061. '211232', /* 105 START C */
  1062. '233111', /* STOP */
  1063. '200000' /* END */
  1064. );
  1065. $keys = '';
  1066. switch (strtoupper($type)) {
  1067. case 'A': {
  1068. $startid = 103;
  1069. $keys = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_';
  1070. for ($i = 0; $i < 32; ++$i) {
  1071. $keys .= chr($i);
  1072. }
  1073. break;
  1074. }
  1075. case 'B': {
  1076. $startid = 104;
  1077. $keys = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~' . chr(127);
  1078. break;
  1079. }
  1080. case 'C': {
  1081. $startid = 105;
  1082. $keys = '';
  1083. if ((strlen($code) % 2) != 0) {
  1084. // The length of barcode value must be even ($code). You must pad the number with zeros
  1085. return false;
  1086. }
  1087. for ($i = 0; $i <= 99; ++$i) {
  1088. $keys .= chr($i);
  1089. }
  1090. $new_code = '';
  1091. $hclen = (strlen($code) / 2);
  1092. for ($i = 0; $i < $hclen; ++$i) {
  1093. $new_code .= chr(intval($code{(2 * $i)} . $code{(2 * $i + 1)}));
  1094. }
  1095. $code = $new_code;
  1096. break;
  1097. }
  1098. default: {
  1099. return false;
  1100. }
  1101. }
  1102. // calculate check character
  1103. $sum = $startid;
  1104. if ($ean) {
  1105. $code = chr(102) . $code;
  1106. } // Add FNC 1 - which identifies it as EAN-128
  1107. $clen = strlen($code);
  1108. for ($i = 0; $i < $clen; ++$i) {
  1109. if ($ean && $i == 0) {
  1110. $sum += 102;
  1111. } else {
  1112. $sum += (strpos($keys, $code[$i]) * ($i + 1));
  1113. }
  1114. }
  1115. $check = ($sum % 103);
  1116. $checkdigit = $check;
  1117. // add start, check and stop codes
  1118. $code = chr($startid) . $code . chr($check) . chr(106) . chr(107);
  1119. $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
  1120. $k = 0;
  1121. $len = strlen($code);
  1122. for ($i = 0; $i < $len; ++$i) {
  1123. $ck = strpos($keys, $code[$i]);
  1124. if (($i == 0) || ($ean && $i == 1) | ($i > ($len - 4))) {
  1125. $char_num = ord($code[$i]);
  1126. $seq = $chr[$char_num];
  1127. } elseif (($ck >= 0) AND isset($chr[$ck])) {
  1128. $seq = $chr[$ck];
  1129. } else {
  1130. // invalid character
  1131. return false;
  1132. }
  1133. for ($j = 0; $j < 6; ++$j) {
  1134. if (($j % 2) == 0) {
  1135. $t = true; // bar
  1136. } else {
  1137. $t = false; // space
  1138. }
  1139. $w = $seq[$j];
  1140. $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0);
  1141. $bararray['maxw'] += $w;
  1142. ++$k;
  1143. }
  1144. }
  1145. $bararray['checkdigit'] = $checkdigit;
  1146. return $bararray;
  1147. }
  1148. /**
  1149. * EAN13 and UPC-A barcodes.
  1150. * EAN13: European Article Numbering international retail product code
  1151. * UPC-A: Universal product code seen on almost all retail products in the USA and Canada
  1152. * UPC-E: Short version of UPC symbol
  1153. */
  1154. protected function barcode_eanupc($code, $len = 13)
  1155. {
  1156. $upce = false;
  1157. $checkdigit = false;
  1158. if ($len == 6) {
  1159. $len = 12; // UPC-A
  1160. $upce = true; // UPC-E mode
  1161. }
  1162. $data_len = $len - 1;
  1163. //Padding
  1164. $code = str_pad($code, $data_len, '0', STR_PAD_LEFT);
  1165. $code_len = strlen($code);
  1166. // calculate check digit
  1167. $sum_a = 0;
  1168. for ($i = 1; $i < $data_len; $i+=2) {
  1169. $sum_a += $code[$i];
  1170. }
  1171. if ($len > 12) {
  1172. $sum_a *= 3;
  1173. }
  1174. $sum_b = 0;
  1175. for ($i = 0; $i < $data_len; $i+=2) {
  1176. $sum_b += ($code[$i]);
  1177. }
  1178. if ($len < 13) {
  1179. $sum_b *= 3;
  1180. }
  1181. $r = ($sum_a + $sum_b) % 10;
  1182. if ($r > 0) {
  1183. $r = (10 - $r);
  1184. }
  1185. if ($code_len == $data_len) {
  1186. // add check digit
  1187. $code .= $r;
  1188. $checkdigit = $r;
  1189. } elseif ($r !== intval($code[$data_len])) {
  1190. // wrong checkdigit
  1191. return false;
  1192. }
  1193. if ($len == 12) {
  1194. // UPC-A
  1195. $code = '0' . $code;
  1196. ++$len;
  1197. }
  1198. if ($upce) {
  1199. // convert UPC-A to UPC-E
  1200. $tmp = substr($code, 4, 3);
  1201. $prod_code = intval(substr($code, 7, 5)); // product code
  1202. $invalid_upce = false;
  1203. if (($tmp == '000') OR ( $tmp == '100') OR ( $tmp == '200')) {
  1204. // manufacturer code ends in 000, 100, or 200
  1205. $upce_code = substr($code, 2, 2) . substr($code, 9, 3) . substr($code, 4, 1);
  1206. if ($prod_code > 999) {
  1207. $invalid_upce = true;
  1208. }
  1209. } else {
  1210. $tmp = substr($code, 5, 2);
  1211. if ($tmp == '00') {
  1212. // manufacturer code ends in 00
  1213. $upce_code = substr($code, 2, 3) . substr($code, 10, 2) . '3';
  1214. if ($prod_code > 99) {
  1215. $invalid_upce = true;
  1216. }
  1217. } else {
  1218. $tmp = substr($code, 6, 1);
  1219. if ($tmp == '0') {
  1220. // manufacturer code ends in 0
  1221. $upce_code = substr($code, 2, 4) . substr($code, 11, 1) . '4';
  1222. if ($prod_code > 9) {
  1223. $invalid_upce = true;
  1224. }
  1225. } else {
  1226. // manufacturer code does not end in zero
  1227. $upce_code = substr($code, 2, 5) . substr($code, 11, 1);
  1228. if ($prod_code > 9) {
  1229. $invalid_upce = true;
  1230. }
  1231. }
  1232. }
  1233. }
  1234. if ($invalid_upce) {
  1235. throw new MpdfException('Error - UPC-A cannot produce a valid UPC-E barcode');
  1236. } // Error generating a UPCE code
  1237. }
  1238. // Convert digits to bars
  1239. $codes = array(
  1240. 'A' => array(// left odd parity
  1241. '0' => '0001101',
  1242. '1' => '0011001',
  1243. '2' => '0010011',
  1244. '3' => '0111101',
  1245. '4' => '0100011',
  1246. '5' => '0110001',
  1247. '6' => '0101111',
  1248. '7' => '0111011',
  1249. '8' => '0110111',
  1250. '9' => '0001011'),
  1251. 'B' => array(// left even parity
  1252. '0' => '0100111',
  1253. '1' => '0110011',
  1254. '2' => '0011011',
  1255. '3' => '0100001',
  1256. '4' => '0011101',
  1257. '5' => '0111001',
  1258. '6' => '0000101',
  1259. '7' => '0010001',
  1260. '8' => '0001001',
  1261. '9' => '0010111'),
  1262. 'C' => array(// right
  1263. '0' => '1110010',
  1264. '1' => '1100110',
  1265. '2' => '1101100',
  1266. '3' => '1000010',
  1267. '4' => '1011100',
  1268. '5' => '1001110',
  1269. '6' => '1010000',
  1270. '7' => '1000100',
  1271. '8' => '1001000',
  1272. '9' => '1110100')
  1273. );
  1274. $parities = array(
  1275. '0' => array('A', 'A', 'A', 'A', 'A', 'A'),
  1276. '1' => array('A', 'A', 'B', 'A', 'B', 'B'),
  1277. '2' => array('A', 'A', 'B', 'B', 'A', 'B'),
  1278. '3' => array('A', 'A', 'B', 'B', 'B', 'A'),
  1279. '4' => array('A', 'B', 'A', 'A', 'B', 'B'),
  1280. '5' => array('A', 'B', 'B', 'A', 'A', 'B'),
  1281. '6' => array('A', 'B', 'B', 'B', 'A', 'A'),
  1282. '7' => array('A', 'B', 'A', 'B', 'A', 'B'),
  1283. '8' => array('A', 'B', 'A', 'B', 'B', 'A'),
  1284. '9' => array('A', 'B', 'B', 'A', 'B', 'A')
  1285. );
  1286. $upce_parities = array();
  1287. $upce_parities[0] = array(
  1288. '0' => array('B', 'B', 'B', 'A', 'A', 'A'),
  1289. '1' => array('B', 'B', 'A', 'B', 'A', 'A'),
  1290. '2' => array('B', 'B', 'A', 'A', 'B', 'A'),
  1291. '3' => array('B', 'B', 'A', 'A', 'A', 'B'),
  1292. '4' => array('B', 'A', 'B', 'B', 'A', 'A'),
  1293. '5' => array('B', 'A', 'A', 'B', 'B', 'A'),
  1294. '6' => array('B', 'A', 'A', 'A', 'B', 'B'),
  1295. '7' => array('B', 'A', 'B', 'A', 'B', 'A'),
  1296. '8' => array('B', 'A', 'B', 'A', 'A', 'B'),
  1297. '9' => array('B', 'A', 'A', 'B', 'A', 'B')
  1298. );
  1299. $upce_parities[1] = array(
  1300. '0' => array('A', 'A', 'A', 'B', 'B', 'B'),
  1301. '1' => array('A', 'A', 'B', 'A', 'B', 'B'),
  1302. '2' => array('A', 'A', 'B', 'B', 'A', 'B'),
  1303. '3' => array('A', 'A', 'B', 'B', 'B', 'A'),
  1304. '4' => array('A', 'B', 'A', 'A', 'B', 'B'),
  1305. '5' => array('A', 'B', 'B', 'A', 'A', 'B'),
  1306. '6' => array('A', 'B', 'B', 'B', 'A', 'A'),
  1307. '7' => array('A', 'B', 'A', 'B', 'A', 'B'),
  1308. '8' => array('A', 'B', 'A', 'B', 'B', 'A'),
  1309. '9' => array('A', 'B', 'B', 'A', 'B', 'A')
  1310. );
  1311. $k = 0;
  1312. $seq = '101'; // left guard bar
  1313. if ($upce) {
  1314. $bararray = array('code' => $upce_code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
  1315. $p = $upce_parities[$code{1}][$r];
  1316. for ($i = 0; $i < 6; ++$i) {
  1317. $seq .= $codes[$p[$i]][$upce_code[$i]];
  1318. }
  1319. $seq .= '010101'; // right guard bar
  1320. } else {
  1321. $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
  1322. $half_len = ceil($len / 2);
  1323. if ($len == 8) {
  1324. for ($i = 0; $i < $half_len; ++$i) {
  1325. $seq .= $codes['A'][$code[(int) $i]];
  1326. }
  1327. } else {
  1328. $p = $parities[$code{0}];
  1329. for ($i = 1; $i < $half_len; ++$i) {
  1330. $seq .= $codes[$p[$i - 1]][$code[(int) $i]];
  1331. }
  1332. }
  1333. $seq .= '01010'; // center guard bar
  1334. for ($i = $half_len; $i < $len; ++$i) {
  1335. $seq .= $codes['C'][$code[(int) $i]];
  1336. }
  1337. $seq .= '101'; // right guard bar
  1338. }
  1339. $clen = strlen($seq);
  1340. $w = 0;
  1341. for ($i = 0; $i < $clen; ++$i) {
  1342. $w += 1;
  1343. if (($i == ($clen - 1)) OR ( ($i < ($clen - 1)) AND ( $seq[$i] != $seq[($i + 1)]))) {
  1344. if ($seq[$i] == '1') {
  1345. $t = true; // bar
  1346. } else {
  1347. $t = false; // space
  1348. }
  1349. $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0);
  1350. $bararray['maxw'] += $w;
  1351. ++$k;
  1352. $w = 0;
  1353. }
  1354. }
  1355. $bararray['checkdigit'] = $checkdigit;
  1356. return $bararray;
  1357. }
  1358. /**
  1359. * UPC-Based Extentions
  1360. * 2-Digit Ext.: Used to indicate magazines and newspaper issue numbers
  1361. * 5-Digit Ext.: Used to mark suggested retail price of books
  1362. */
  1363. protected function barcode_eanext($code, $len = 5)
  1364. {
  1365. //Padding
  1366. $code = str_pad($code, $len, '0', STR_PAD_LEFT);
  1367. // calculate check digit
  1368. if ($len == 2) {
  1369. $r = $code % 4;
  1370. } elseif ($len == 5) {
  1371. $r = (3 * ($code{0} + $code{2} + $code{4})) + (9 * ($code{1} + $code{3}));
  1372. $r %= 10;
  1373. } else {
  1374. return false;
  1375. }
  1376. //Convert digits to bars
  1377. $codes = array(
  1378. 'A' => array(// left odd parity
  1379. '0' => '0001101',
  1380. '1' => '0011001',
  1381. '2' => '0010011',
  1382. '3' => '0111101',
  1383. '4' => '0100011',
  1384. '5' => '0110001',
  1385. '6' => '0101111',
  1386. '7' => '0111011',
  1387. '8' => '0110111',
  1388. '9' => '0001011'),
  1389. 'B' => array(// left even parity
  1390. '0' => '0100111',
  1391. '1' => '0110011',
  1392. '2' => '0011011',
  1393. '3' => '0100001',
  1394. '4' => '0011101',
  1395. '5' => '0111001',
  1396. '6' => '0000101',
  1397. '7' => '0010001',
  1398. '8' => '0001001',
  1399. '9' => '0010111')
  1400. );
  1401. $parities = array();
  1402. $parities[2] = array(
  1403. '0' => array('A', 'A'),
  1404. '1' => array('A', 'B'),
  1405. '2' => array('B', 'A'),
  1406. '3' => array('B', 'B')
  1407. );
  1408. $parities[5] = array(
  1409. '0' => array('B', 'B', 'A', 'A', 'A'),
  1410. '1' => array('B', 'A', 'B', 'A', 'A'),
  1411. '2' => array('B', 'A', 'A', 'B', 'A'),
  1412. '3' => array('B', 'A', 'A', 'A', 'B'),
  1413. '4' => array('A', 'B', 'B', 'A', 'A'),
  1414. '5' => array('A', 'A', 'B', 'B', 'A'),
  1415. '6' => array('A', 'A', 'A', 'B', 'B'),
  1416. '7' => array('A', 'B', 'A', 'B', 'A'),
  1417. '8' => array('A', 'B', 'A', 'A', 'B'),
  1418. '9' => array('A', 'A', 'B', 'A', 'B')
  1419. );
  1420. $p = $parities[$len][$r];
  1421. $seq = '1011'; // left guard bar
  1422. $seq .= $codes[$p[0]][$code{0}];
  1423. for ($i = 1; $i < $len; ++$i) {
  1424. $seq .= '01'; // separator
  1425. $seq .= $codes[$p[$i]][$code[$i]];
  1426. }
  1427. $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
  1428. return $this->binseq_to_array($seq, $bararray);
  1429. }
  1430. /**
  1431. * POSTNET and PLANET barcodes.
  1432. * Used by U.S. Postal Service for automated mail sorting
  1433. */
  1434. protected function barcode_postnet($code, $planet = false)
  1435. {
  1436. // bar lenght
  1437. if ($planet) {
  1438. $barlen = Array(
  1439. 0 => Array(1, 1, 2, 2, 2),
  1440. 1 => Array(2, 2, 2, 1, 1),
  1441. 2 => Array(2, 2, 1, 2, 1),
  1442. 3 => Array(2, 2, 1, 1, 2),
  1443. 4 => Array(2, 1, 2, 2, 1),
  1444. 5 => Array(2, 1, 2, 1, 2),
  1445. 6 => Array(2, 1, 1, 2, 2),
  1446. 7 => Array(1, 2, 2, 2, 1),
  1447. 8 => Array(1, 2, 2, 1, 2),
  1448. 9 => Array(1, 2, 1, 2, 2)
  1449. );
  1450. } else {
  1451. $barlen = Array(
  1452. 0 => Array(2, 2, 1, 1, 1),
  1453. 1 => Array(1, 1, 1, 2, 2),
  1454. 2 => Array(1, 1, 2, 1, 2),
  1455. 3 => Array(1, 1, 2, 2, 1),
  1456. 4 => Array(1, 2, 1, 1, 2),
  1457. 5 => Array(1, 2, 1, 2, 1),
  1458. 6 => Array(1, 2, 2, 1, 1),
  1459. 7 => Array(2, 1, 1, 1, 2),
  1460. 8 => Array(2, 1, 1, 2, 1),
  1461. 9 => Array(2, 1, 2, 1, 1)
  1462. );
  1463. }
  1464. $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 5, 'bcode' => array());
  1465. $k = 0;
  1466. $code = str_replace('-', '', $code);
  1467. $code = str_replace(' ', '', $code);
  1468. $len = strlen($code);
  1469. // calculate checksum
  1470. $sum = 0;
  1471. for ($i = 0; $i < $len; ++$i) {
  1472. $sum += intval($code[$i]);
  1473. }
  1474. $chkd = ($sum % 10);
  1475. if ($chkd > 0) {
  1476. $chkd = (10 - $chkd);
  1477. }
  1478. $code .= $chkd;
  1479. $checkdigit = $chkd;
  1480. $len = strlen($code);
  1481. // start bar
  1482. $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => 5, 'p' => 0);
  1483. $bararray['bcode'][$k++] = array('t' => 0, 'w' => $this->gapwidth, 'h' => 5, 'p' => 0);
  1484. $bararray['maxw'] += (1 + $this->gapwidth );
  1485. for ($i = 0; $i < $len; ++$i) {
  1486. for ($j = 0; $j < 5; ++$j) {
  1487. $bh = $barlen[$code[$i]][$j];
  1488. if ($bh == 2) {
  1489. $h = 5;
  1490. $p = 0;
  1491. } else {
  1492. $h = 2;
  1493. $p = 3;
  1494. }
  1495. $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => $h, 'p' => $p);
  1496. $bararray['bcode'][$k++] = array('t' => 0, 'w' => $this->gapwidth, 'h' => 2, 'p' => 0);
  1497. $bararray['maxw'] += (1 + $this->gapwidth );
  1498. }
  1499. }
  1500. // end bar
  1501. $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => 5, 'p' => 0);
  1502. $bararray['maxw'] += 1;
  1503. $bararray['checkdigit'] = $checkdigit;
  1504. return $bararray;
  1505. }
  1506. /**
  1507. * RM4SCC - CBC - KIX
  1508. * RM4SCC (Royal Mail 4-state Customer Code) - CBC (Customer Bar Code) - KIX (Klant index - Customer index)
  1509. * RM4SCC is the name of the barcode symbology used by the Royal Mail for its Cleanmail service.
  1510. */
  1511. protected function barcode_rm4scc($code, $kix = false)
  1512. {
  1513. $notkix = !$kix;
  1514. // bar mode
  1515. // 1 = pos 1, length 2
  1516. // 2 = pos 1, length 3
  1517. // 3 = pos 2, length 1
  1518. // 4 = pos 2, length 2
  1519. $barmode = array(
  1520. '0' => array(3, 3, 2, 2),
  1521. '1' => array(3, 4, 1, 2),
  1522. '2' => array(3, 4, 2, 1),
  1523. '3' => array(4, 3, 1, 2),
  1524. '4' => array(4, 3, 2, 1),
  1525. '5' => array(4, 4, 1, 1),
  1526. '6' => array(3, 1, 4, 2),
  1527. '7' => array(3, 2, 3, 2),
  1528. '8' => array(3, 2, 4, 1),
  1529. '9' => array(4, 1, 3, 2),
  1530. 'A' => array(4, 1, 4, 1),
  1531. 'B' => array(4, 2, 3, 1),
  1532. 'C' => array(3, 1, 2, 4),
  1533. 'D' => array(3, 2, 1, 4),
  1534. 'E' => array(3, 2, 2, 3),
  1535. 'F' => array(4, 1, 1, 4),
  1536. 'G' => array(4, 1, 2, 3),
  1537. 'H' => array(4, 2, 1, 3),
  1538. 'I' => array(1, 3, 4, 2),
  1539. 'J' => array(1, 4, 3, 2),
  1540. 'K' => array(1, 4, 4, 1),
  1541. 'L' => array(2, 3, 3, 2),
  1542. 'M' => array(2, 3, 4, 1),
  1543. 'N' => array(2, 4, 3, 1),
  1544. 'O' => array(1, 3, 2, 4),
  1545. 'P' => array(1, 4, 1, 4),
  1546. 'Q' => array(1, 4, 2, 3),
  1547. 'R' => array(2, 3, 1, 4),
  1548. 'S' => array(2, 3, 2, 3),
  1549. 'T' => array(2, 4, 1, 3),
  1550. 'U' => array(1, 1, 4, 4),
  1551. 'V' => array(1, 2, 3, 4),
  1552. 'W' => array(1, 2, 4, 3),
  1553. 'X' => array(2, 1, 3, 4),
  1554. 'Y' => array(2, 1, 4, 3),
  1555. 'Z' => array(2, 2, 3, 3)
  1556. );
  1557. $code = strtoupper($code);
  1558. $len = strlen($code);
  1559. $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => $this->daft['F'], 'bcode' => array());
  1560. if ($notkix) {
  1561. // table for checksum calculation (row,col)
  1562. $checktable = array(
  1563. '0' => array(1, 1),
  1564. '1' => array(1, 2),
  1565. '2' => array(1, 3),
  1566. '3' => array(1, 4),
  1567. '4' => array(1, 5),
  1568. '5' => array(1, 0),
  1569. '6' => array(2, 1),
  1570. '7' => array(2, 2),
  1571. '8' => array(2, 3),
  1572. '9' => array(2, 4),
  1573. 'A' => array(2, 5),
  1574. 'B' => array(2, 0),
  1575. 'C' => array(3, 1),
  1576. 'D' => array(3, 2),
  1577. 'E' => array(3, 3),
  1578. 'F' => array(3, 4),
  1579. 'G' => array(3, 5),
  1580. 'H' => array(3, 0),
  1581. 'I' => array(4, 1),
  1582. 'J' => array(4, 2),
  1583. 'K' => array(4, 3),
  1584. 'L' => array(4, 4),
  1585. 'M' => array(4, 5),
  1586. 'N' => array(4, 0),
  1587. 'O' => array(5, 1),
  1588. 'P' => array(5, 2),
  1589. 'Q' => array(5, 3),
  1590. 'R' => array(5, 4),
  1591. 'S' => array(5, 5),
  1592. 'T' => array(5, 0),
  1593. 'U' => array(0, 1),
  1594. 'V' => array(0, 2),
  1595. 'W' => array(0, 3),
  1596. 'X' => array(0, 4),
  1597. 'Y' => array(0, 5),
  1598. 'Z' => array(0, 0)
  1599. );
  1600. $row = 0;
  1601. $col = 0;
  1602. for ($i = 0; $i < $len; ++$i) {
  1603. $row += $checktable[$code[$i]][0];
  1604. $col += $checktable[$code[$i]][1];
  1605. }
  1606. $row %= 6;
  1607. $col %= 6;
  1608. $chk = array_keys($checktable, array($row, $col));
  1609. $code .= $chk[0];
  1610. $bararray['checkdigit'] = $chk[0];
  1611. ++$len;
  1612. }
  1613. $k = 0;
  1614. if ($notkix) {
  1615. // start bar
  1616. $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => $this->daft['A'], 'p' => 0);
  1617. $bararray['bcode'][$k++] = array('t' => 0, 'w' => $this->gapwidth, 'h' => $this->daft['A'], 'p' => 0);
  1618. $bararray['maxw'] += (1 + $this->gapwidth);
  1619. }
  1620. for ($i = 0; $i < $len; ++$i) {
  1621. for ($j = 0; $j < 4; ++$j) {
  1622. switch ($barmode[$code[$i]][$j]) {
  1623. case 1: {
  1624. // ascender (A)
  1625. $p = 0;
  1626. $h = $this->daft['A'];
  1627. break;
  1628. }
  1629. case 2: {
  1630. // full bar (F)
  1631. $p = 0;
  1632. $h = $this->daft['F'];
  1633. break;
  1634. }
  1635. case 3: {
  1636. // tracker (T)
  1637. $p = ($this->daft['F'] - $this->daft['T']) / 2;
  1638. $h = $this->daft['T'];
  1639. break;
  1640. }
  1641. case 4: {
  1642. // descender (D)
  1643. $p = $this->daft['F'] - $this->daft['D'];
  1644. $h = $this->daft['D'];
  1645. break;
  1646. }
  1647. }
  1648. $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => $h, 'p' => $p);
  1649. $bararray['bcode'][$k++] = array('t' => 0, 'w' => $this->gapwidth, 'h' => 2, 'p' => 0);
  1650. $bararray['maxw'] += (1 + $this->gapwidth);
  1651. }
  1652. }
  1653. if ($notkix) {
  1654. // stop bar
  1655. $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => $this->daft['F'], 'p' => 0);
  1656. $bararray['maxw'] += 1;
  1657. }
  1658. return $bararray;
  1659. }
  1660. /**
  1661. * CODABAR barcodes.
  1662. * Older code often used in library systems, sometimes in blood banks
  1663. */
  1664. protected function barcode_codabar($code)
  1665. {
  1666. $chr = array(
  1667. '0' => '11111221',
  1668. '1' => '11112211',
  1669. '2' => '11121121',
  1670. '3' => '22111111',
  1671. '4' => '11211211',
  1672. '5' => '21111211',
  1673. '6' => '12111121',
  1674. '7' => '12112111',
  1675. '8' => '12211111',
  1676. '9' => '21121111',
  1677. '-' => '11122111',
  1678. '$' => '11221111',
  1679. ':' => '21112121',
  1680. '/' => '21211121',
  1681. '.' => '21212111',
  1682. '+' => '11222221',
  1683. 'A' => '11221211',
  1684. 'B' => '12121121',
  1685. 'C' => '11121221',
  1686. 'D' => '11122211'
  1687. );
  1688. $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
  1689. $k = 0;
  1690. $w = 0;
  1691. $seq = '';
  1692. $code = strtoupper($code);
  1693. $len = strlen($code);
  1694. for ($i = 0; $i < $len; ++$i) {
  1695. if (!isset($chr[$code[$i]])) {
  1696. return false;
  1697. }
  1698. $seq = $chr[$code[$i]];
  1699. for ($j = 0; $j < 8; ++$j) {
  1700. if (($j % 2) == 0) {
  1701. $t = true; // bar
  1702. } else {
  1703. $t = false; // space
  1704. }
  1705. $x = $seq[$j];
  1706. if ($x == 2) {
  1707. $w = $this->print_ratio;
  1708. } else {
  1709. $w = 1;
  1710. }
  1711. $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0);
  1712. $bararray['maxw'] += $w;
  1713. ++$k;
  1714. }
  1715. }
  1716. return $bararray;
  1717. }
  1718. /**
  1719. * CODE11 barcodes.
  1720. * Used primarily for labeling telecommunications equipment
  1721. */
  1722. protected function barcode_code11($code)
  1723. {
  1724. $chr = array(
  1725. '0' => '111121',
  1726. '1' => '211121',
  1727. '2' => '121121',
  1728. '3' => '221111',
  1729. '4' => '112121',
  1730. '5' => '212111',
  1731. '6' => '122111',
  1732. '7' => '111221',
  1733. '8' => '211211',
  1734. '9' => '211111',
  1735. '-' => '112111',
  1736. 'S' => '112211'
  1737. );
  1738. $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
  1739. $k = 0;
  1740. $w = 0;
  1741. $seq = '';
  1742. $len = strlen($code);
  1743. // calculate check digit C
  1744. $p = 1;
  1745. $check = 0;
  1746. for ($i = ($len - 1); $i >= 0; --$i) {
  1747. $digit = $code[$i];
  1748. if ($digit == '-') {
  1749. $dval = 10;
  1750. } else {
  1751. $dval = intval($digit);
  1752. }
  1753. $check += ($dval * $p);
  1754. ++$p;
  1755. if ($p > 10) {
  1756. $p = 1;
  1757. }
  1758. }
  1759. $check %= 11;
  1760. if ($check == 10) {
  1761. $check = '-';
  1762. }
  1763. $code .= $check;
  1764. $checkdigit = $check;
  1765. if ($len > 10) {
  1766. // calculate check digit K
  1767. $p = 1;
  1768. $check = 0;
  1769. for ($i = $len; $i >= 0; --$i) {
  1770. $digit = $code[$i];
  1771. if ($digit == '-') {
  1772. $dval = 10;
  1773. } else {
  1774. $dval = intval($digit);
  1775. }
  1776. $check += ($dval * $p);
  1777. ++$p;
  1778. if ($p > 9) {
  1779. $p = 1;
  1780. }
  1781. }
  1782. $check %= 11;
  1783. $code .= $check;
  1784. $checkdigit .= $check;
  1785. ++$len;
  1786. }
  1787. $code = 'S' . $code . 'S';
  1788. $len += 3;
  1789. for ($i = 0; $i < $len; ++$i) {
  1790. if (!isset($chr[$code[$i]])) {
  1791. return false;
  1792. }
  1793. $seq = $chr[$code[$i]];
  1794. for ($j = 0; $j < 6; ++$j) {
  1795. if (($j % 2) == 0) {
  1796. $t = true; // bar
  1797. } else {
  1798. $t = false; // space
  1799. }
  1800. $x = $seq[$j];
  1801. if ($x == 2) {
  1802. $w = $this->print_ratio;
  1803. } else {
  1804. $w = 1;
  1805. }
  1806. $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0);
  1807. $bararray['maxw'] += $w;
  1808. ++$k;
  1809. }
  1810. }
  1811. $bararray['checkdigit'] = $checkdigit;
  1812. return $bararray;
  1813. }
  1814. /**
  1815. * IMB - Intelligent Mail Barcode - Onecode - USPS-B-3200
  1816. * (requires PHP bcmath extension)
  1817. * Intelligent Mail barcode is a 65-bar code for use on mail in the United States.
  1818. * The fields are described as follows:<ul><li>The Barcode Identifier shall be assigned by USPS to encode the presort identification that is currently printed in human readable form on the optional endorsement line (OEL) as well as for future USPS use. This shall be two digits, with the second digit in the range of 0-4. The allowable encoding ranges shall be 00-04, 10-14, 20-24, 30-34, 40-44, 50-54, 60-64, 70-74, 80-84, and 90-94.</li><li>The Service Type Identifier shall be assigned by USPS for any combination of services requested on the mailpiece. The allowable encoding range shall be 000-999. Each 3-digit value shall correspond to a particular mail class with a particular combination of service(s). Each service program, such as OneCode Confirm and OneCode ACS, shall provide the list of Service Type Identifier values.</li><li>The Mailer or Customer Identifier shall be assigned by USPS as a unique, 6 or 9 digit number that identifies a business entity. The allowable encoding range for the 6 digit Mailer ID shall be 000000- 899999, while the allowable encoding range for the 9 digit Mailer ID shall be 900000000-999999999.</li><li>The Serial or Sequence Number shall be assigned by the mailer for uniquely identifying and tracking mailpieces. The allowable encoding range shall be 000000000-999999999 when used with a 6 digit Mailer ID and 000000-999999 when used with a 9 digit Mailer ID. e. The Delivery Point ZIP Code shall be assigned by the mailer for routing the mailpiece. This shall replace POSTNET for routing the mailpiece to its final delivery point. The length may be 0, 5, 9, or 11 digits. The allowable encoding ranges shall be no ZIP Code, 00000-99999, 000000000-999999999, and 00000000000-99999999999.</li></ul>
  1819. */
  1820. protected function barcode_imb($code)
  1821. {
  1822. $asc_chr = array(4, 0, 2, 6, 3, 5, 1, 9, 8, 7, 1, 2, 0, 6, 4, 8, 2, 9, 5, 3, 0, 1, 3, 7, 4, 6, 8, 9, 2, 0, 5, 1, 9, 4, 3, 8, 6, 7, 1, 2, 4, 3, 9, 5, 7, 8, 3, 0, 2, 1, 4, 0, 9, 1, 7, 0, 2, 4, 6, 3, 7, 1, 9, 5, 8);
  1823. $dsc_chr = array(7, 1, 9, 5, 8, 0, 2, 4, 6, 3, 5, 8, 9, 7, 3, 0, 6, 1, 7, 4, 6, 8, 9, 2, 5, 1, 7, 5, 4, 3, 8, 7, 6, 0, 2, 5, 4, 9, 3, 0, 1, 6, 8, 2, 0, 4, 5, 9, 6, 7, 5, 2, 6, 3, 8, 5, 1, 9, 8, 7, 4, 0, 2, 6, 3);
  1824. $asc_pos = array(3, 0, 8, 11, 1, 12, 8, 11, 10, 6, 4, 12, 2, 7, 9, 6, 7, 9, 2, 8, 4, 0, 12, 7, 10, 9, 0, 7, 10, 5, 7, 9, 6, 8, 2, 12, 1, 4, 2, 0, 1, 5, 4, 6, 12, 1, 0, 9, 4, 7, 5, 10, 2, 6, 9, 11, 2, 12, 6, 7, 5, 11, 0, 3, 2);
  1825. $dsc_pos = array(2, 10, 12, 5, 9, 1, 5, 4, 3, 9, 11, 5, 10, 1, 6, 3, 4, 1, 10, 0, 2, 11, 8, 6, 1, 12, 3, 8, 6, 4, 4, 11, 0, 6, 1, 9, 11, 5, 3, 7, 3, 10, 7, 11, 8, 2, 10, 3, 5, 8, 0, 3, 12, 11, 8, 4, 5, 1, 3, 0, 7, 12, 9, 8, 10);
  1826. $code_arr = explode('-', $code);
  1827. $tracking_number = $code_arr[0];
  1828. if (isset($code_arr[1])) {
  1829. $routing_code = $code_arr[1];
  1830. } else {
  1831. $routing_code = '';
  1832. }
  1833. // Conversion of Routing Code
  1834. switch (strlen($routing_code)) {
  1835. case 0: {
  1836. $binary_code = 0;
  1837. break;
  1838. }
  1839. case 5: {
  1840. $binary_code = bcadd($routing_code, '1');
  1841. break;
  1842. }
  1843. case 9: {
  1844. $binary_code = bcadd($routing_code, '100001');
  1845. break;
  1846. }
  1847. case 11: {
  1848. $binary_code = bcadd($routing_code, '1000100001');
  1849. break;
  1850. }
  1851. default: {
  1852. return false;
  1853. break;
  1854. }
  1855. }
  1856. $binary_code = bcmul($binary_code, 10);
  1857. $binary_code = bcadd($binary_code, $tracking_number{0});
  1858. $binary_code = bcmul($binary_code, 5);
  1859. $binary_code = bcadd($binary_code, $tracking_number{1});
  1860. $binary_code .= substr($tracking_number, 2, 18);
  1861. // convert to hexadecimal
  1862. $binary_code = $this->dec_to_hex($binary_code);
  1863. // pad to get 13 bytes
  1864. $binary_code = str_pad($binary_code, 26, '0', STR_PAD_LEFT);
  1865. // convert string to array of bytes
  1866. $binary_code_arr = chunk_split($binary_code, 2, "\r");
  1867. $binary_code_arr = substr($binary_code_arr, 0, -1);
  1868. $binary_code_arr = explode("\r", $binary_code_arr);
  1869. // calculate frame check sequence
  1870. $fcs = $this->imb_crc11fcs($binary_code_arr);
  1871. // exclude first 2 bits from first byte
  1872. $first_byte = sprintf('%2s', dechex((hexdec($binary_code_arr[0]) << 2) >> 2));
  1873. $binary_code_102bit = $first_byte . substr($binary_code, 2);
  1874. // convert binary data to codewords
  1875. $codewords = array();
  1876. $data = $this->hex_to_dec($binary_code_102bit);
  1877. $codewords[0] = bcmod($data, 636) * 2;
  1878. $data = bcdiv($data, 636);
  1879. for ($i = 1; $i < 9; ++$i) {
  1880. $codewords[$i] = bcmod($data, 1365);
  1881. $data = bcdiv($data, 1365);
  1882. }
  1883. $codewords[9] = $data;
  1884. if (($fcs >> 10) == 1) {
  1885. $codewords[9] += 659;
  1886. }
  1887. // generate lookup tables
  1888. $table2of13 = $this->imb_tables(2, 78);
  1889. $table5of13 = $this->imb_tables(5, 1287);
  1890. // convert codewords to characters
  1891. $characters = array();
  1892. $bitmask = 512;
  1893. foreach ($codewords as $k => $val) {
  1894. if ($val <= 1286) {
  1895. $chrcode = $table5of13[$val];
  1896. } else {
  1897. $chrcode = $table2of13[($val - 1287)];
  1898. }
  1899. if (($fcs & $bitmask) > 0) {
  1900. // bitwise invert
  1901. $chrcode = ((~$chrcode) & 8191);
  1902. }
  1903. $characters[] = $chrcode;
  1904. $bitmask /= 2;
  1905. }
  1906. $characters = array_reverse($characters);
  1907. // build bars
  1908. $k = 0;
  1909. $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => $this->daft['F'], 'bcode' => array());
  1910. for ($i = 0; $i < 65; ++$i) {
  1911. $asc = (($characters[$asc_chr[$i]] & pow(2, $asc_pos[$i])) > 0);
  1912. $dsc = (($characters[$dsc_chr[$i]] & pow(2, $dsc_pos[$i])) > 0);
  1913. if ($asc AND $dsc) {
  1914. // full bar (F)
  1915. $p = 0;
  1916. $h = $this->daft['F'];
  1917. } elseif ($asc) {
  1918. // ascender (A)
  1919. $p = 0;
  1920. $h = $this->daft['A'];
  1921. } elseif ($dsc) {
  1922. // descender (D)
  1923. $p = $this->daft['F'] - $this->daft['D'];
  1924. $h = $this->daft['D'];
  1925. } else {
  1926. // tracker (T)
  1927. $p = ($this->daft['F'] - $this->daft['T']) / 2;
  1928. $h = $this->daft['T'];
  1929. }
  1930. $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => $h, 'p' => $p);
  1931. // Gap
  1932. $bararray['bcode'][$k++] = array('t' => 0, 'w' => $this->gapwidth, 'h' => 1, 'p' => 0);
  1933. $bararray['maxw'] += (1 + $this->gapwidth );
  1934. }
  1935. unset($bararray['bcode'][($k - 1)]);
  1936. $bararray['maxw'] -= $this->gapwidth;
  1937. return $bararray;
  1938. }
  1939. /**
  1940. * Convert large integer number to hexadecimal representation.
  1941. * (requires PHP bcmath extension)
  1942. */
  1943. public function dec_to_hex($number)
  1944. {
  1945. $i = 0;
  1946. $hex = array();
  1947. if ($number == 0) {
  1948. return '00';
  1949. }
  1950. while ($number > 0) {
  1951. if ($number == 0) {
  1952. array_push($hex, '0');
  1953. } else {
  1954. array_push($hex, strtoupper(dechex(bcmod($number, '16'))));
  1955. $number = bcdiv($number, '16', 0);
  1956. }
  1957. }
  1958. $hex = array_reverse($hex);
  1959. return implode($hex);
  1960. }
  1961. /**
  1962. * Convert large hexadecimal number to decimal representation (string).
  1963. * (requires PHP bcmath extension)
  1964. */
  1965. public function hex_to_dec($hex)
  1966. {
  1967. $dec = 0;
  1968. $bitval = 1;
  1969. $len = strlen($hex);
  1970. for ($pos = ($len - 1); $pos >= 0; --$pos) {
  1971. $dec = bcadd($dec, bcmul(hexdec($hex[$pos]), $bitval));
  1972. $bitval = bcmul($bitval, 16);
  1973. }
  1974. return $dec;
  1975. }
  1976. /**
  1977. * Intelligent Mail Barcode calculation of Frame Check Sequence
  1978. */
  1979. protected function imb_crc11fcs($code_arr)
  1980. {
  1981. $genpoly = 0x0F35; // generator polynomial
  1982. $fcs = 0x07FF; // Frame Check Sequence
  1983. // do most significant byte skipping the 2 most significant bits
  1984. $data = hexdec($code_arr[0]) << 5;
  1985. for ($bit = 2; $bit < 8; ++$bit) {
  1986. if (($fcs ^ $data) & 0x400) {
  1987. $fcs = ($fcs << 1) ^ $genpoly;
  1988. } else {
  1989. $fcs = ($fcs << 1);
  1990. }
  1991. $fcs &= 0x7FF;
  1992. $data <<= 1;
  1993. }
  1994. // do rest of bytes
  1995. for ($byte = 1; $byte < 13; ++$byte) {
  1996. $data = hexdec($code_arr[$byte]) << 3;
  1997. for ($bit = 0; $bit < 8; ++$bit) {
  1998. if (($fcs ^ $data) & 0x400) {
  1999. $fcs = ($fcs << 1) ^ $genpoly;
  2000. } else {
  2001. $fcs = ($fcs << 1);
  2002. }
  2003. $fcs &= 0x7FF;
  2004. $data <<= 1;
  2005. }
  2006. }
  2007. return $fcs;
  2008. }
  2009. /**
  2010. * Reverse unsigned short value
  2011. */
  2012. protected function imb_reverse_us($num)
  2013. {
  2014. $rev = 0;
  2015. for ($i = 0; $i < 16; ++$i) {
  2016. $rev <<= 1;
  2017. $rev |= ($num & 1);
  2018. $num >>= 1;
  2019. }
  2020. return $rev;
  2021. }
  2022. /**
  2023. * generate Nof13 tables used for Intelligent Mail Barcode
  2024. */
  2025. protected function imb_tables($n, $size)
  2026. {
  2027. $table = array();
  2028. $lli = 0; // LUT lower index
  2029. $lui = $size - 1; // LUT upper index
  2030. for ($count = 0; $count < 8192; ++$count) {
  2031. $bit_count = 0;
  2032. for ($bit_index = 0; $bit_index < 13; ++$bit_index) {
  2033. $bit_count += intval(($count & (1 << $bit_index)) != 0);
  2034. }
  2035. // if we don't have the right number of bits on, go on to the next value
  2036. if ($bit_count == $n) {
  2037. $reverse = ($this->imb_reverse_us($count) >> 3);
  2038. // if the reverse is less than count, we have already visited this pair before
  2039. if ($reverse >= $count) {
  2040. // If count is symmetric, place it at the first free slot from the end of the list.
  2041. // Otherwise, place it at the first free slot from the beginning of the list AND place $reverse ath the next free slot from the beginning of the list
  2042. if ($reverse == $count) {
  2043. $table[$lui] = $count;
  2044. --$lui;
  2045. } else {
  2046. $table[$lli] = $count;
  2047. ++$lli;
  2048. $table[$lli] = $reverse;
  2049. ++$lli;
  2050. }
  2051. }
  2052. }
  2053. }
  2054. return $table;
  2055. }
  2056. }