barcode.php 65 KB

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