barcode.lib.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. <?php
  2. /* Copyright (C) 2004-2016 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2004-2010 Folke Ashberg: Some lines of code were inspired from work
  4. * of Folke Ashberg into PHP-Barcode 0.3pl2, available as GPL
  5. * source code at http://www.ashberg.de/bar.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/core/lib/barcode.lib.php
  22. * \brief Set of functions used for barcode generation (internal lib, also code 'phpbarcode')
  23. * \ingroup core
  24. */
  25. /* ******************************************************************** */
  26. /* COLORS */
  27. /* ******************************************************************** */
  28. $bar_color = array(0, 0, 0);
  29. $bg_color = array(255, 255, 255);
  30. $text_color = array(0, 0, 0);
  31. /* ******************************************************************** */
  32. /* FONT FILE */
  33. /* ******************************************************************** */
  34. if (defined('DOL_DEFAULT_TTF_BOLD')) {
  35. $font_loc = constant('DOL_DEFAULT_TTF_BOLD');
  36. }
  37. // Automatic-Detection of Font if running Windows
  38. // @CHANGE LDR
  39. if (isset($_SERVER['WINDIR']) && @file_exists($_SERVER['WINDIR'])) {
  40. $font_loc = $_SERVER['WINDIR'].'\Fonts\arialbd.ttf';
  41. }
  42. if (empty($font_loc)) {
  43. die('DOL_DEFAULT_TTF_BOLD must de defined with full path to a TTF font.');
  44. }
  45. /* ******************************************************************** */
  46. /* GENBARCODE */
  47. /* ******************************************************************** */
  48. /* location of 'genbarcode'
  49. * leave blank if you don't have them :(
  50. * genbarcode is needed to render encodings other than EAN-12/EAN-13/ISBN
  51. */
  52. if (defined('PHP-BARCODE_PATH_COMMAND')) {
  53. $genbarcode_loc = constant('PHP-BARCODE_PATH_COMMAND');
  54. } else {
  55. $genbarcode_loc = '';
  56. if (getDolGlobalString('GENBARCODE_LOCATION')) {
  57. $genbarcode_loc = $conf->global->GENBARCODE_LOCATION;
  58. }
  59. }
  60. /**
  61. * Print barcode
  62. *
  63. * @param string $code Code
  64. * @param string $encoding Encoding ('EAN13', 'ISBN', 'C128', 'UPC', 'CBR', 'QRCODE', 'DATAMATRIX', 'ANY'...)
  65. * @param integer $scale Scale
  66. * @param string $mode 'png' or 'jpg' ...
  67. * @return array|string $bars array('encoding': the encoding which has been used, 'bars': the bars, 'text': text-positioning info) or string with error message
  68. */
  69. function barcode_print($code, $encoding = "ANY", $scale = 2, $mode = "png")
  70. {
  71. dol_syslog("barcode.lib.php::barcode_print $code $encoding $scale $mode");
  72. $bars = barcode_encode($code, $encoding);
  73. if (!$bars || !empty($bars['error'])) {
  74. // Return error message instead of array
  75. if (empty($bars['error'])) {
  76. $error = 'Bad Value '.$code.' for encoding '.$encoding;
  77. } else {
  78. $error = $bars['error'];
  79. }
  80. dol_syslog('barcode.lib.php::barcode_print '.$error, LOG_ERR);
  81. return $error;
  82. }
  83. if (!$mode) {
  84. $mode = "png";
  85. }
  86. //if (preg_match("/^(text|txt|plain)$/i",$mode)) print barcode_outtext($bars['text'],$bars['bars']);
  87. //elseif (preg_match("/^(html|htm)$/i",$mode)) print barcode_outhtml($bars['text'],$bars['bars'], $scale,0, 0);
  88. //else
  89. barcode_outimage($bars['text'], $bars['bars'], $scale, $mode);
  90. return $bars;
  91. }
  92. /**
  93. * Encodes $code with $encoding using genbarcode OR built-in encoder if you don't have genbarcode only EAN-13/ISBN or UPC is possible
  94. *
  95. * You can use the following encodings (when you have genbarcode):
  96. * ANY choose best-fit (default)
  97. * EAN 8 or 13 EAN-Code
  98. * UPC 12-digit EAN
  99. * ISBN isbn numbers (still EAN-13)
  100. * 39 code 39
  101. * 128 code 128 (a,b,c: autoselection)
  102. * 128C code 128 (compact form for digits)
  103. * 128B code 128, full printable ascii
  104. * I25 interleaved 2 of 5 (only digits)
  105. * 128RAW Raw code 128 (by Leonid A. Broukhis)
  106. * CBR Codabar (by Leonid A. Broukhis)
  107. * MSI MSI (by Leonid A. Broukhis)
  108. * PLS Plessey (by Leonid A. Broukhis)
  109. *
  110. * @param string $code Code
  111. * @param string $encoding Encoding
  112. * @return array|false array('encoding': the encoding which has been used, 'bars': the bars, 'text': text-positioning info)
  113. */
  114. function barcode_encode($code, $encoding)
  115. {
  116. global $genbarcode_loc;
  117. if ((preg_match("/^upc$/i", $encoding))
  118. && (preg_match("/^[0-9]{11,12}$/", $code))
  119. ) {
  120. /* use built-in UPC-Encoder */
  121. dol_syslog("barcode.lib.php::barcode_encode Use barcode_encode_upc");
  122. $bars = barcode_encode_upc($code, $encoding);
  123. } elseif ((preg_match("/^ean$/i", $encoding))
  124. || (($encoding) && (preg_match("/^isbn$/i", $encoding))
  125. && ((strlen($code) == 9 || strlen($code) == 10) ||
  126. (((preg_match("/^978/", $code) && strlen($code) == 12) ||
  127. (strlen($code) == 13)))))
  128. || ((!isset($encoding) || !$encoding || (preg_match("/^ANY$/i", $encoding)))
  129. && (preg_match("/^[0-9]{12,13}$/", $code)))
  130. ) {
  131. /* use built-in EAN-Encoder */
  132. dol_syslog("barcode.lib.php::barcode_encode Use barcode_encode_ean");
  133. $bars = barcode_encode_ean($code, $encoding);
  134. } elseif (file_exists($genbarcode_loc)) { // For example C39
  135. /* use genbarcode */
  136. dol_syslog("barcode.lib.php::barcode_encode Use genbarcode ".$genbarcode_loc." code=".$code." encoding=".$encoding);
  137. $bars = barcode_encode_genbarcode($code, $encoding);
  138. } else {
  139. print "barcode_encode needs an external program for encodings other then EAN/ISBN (code=".dol_escape_htmltag($code).", encoding=".dol_escape_htmltag($encoding).")<BR>\n";
  140. print "<UL>\n";
  141. print "<LI>download gnu-barcode from <A href=\"https://www.gnu.org/software/barcode/\">www.gnu.org/software/barcode/</A>\n";
  142. print "<LI>compile and install them\n";
  143. print "<LI>specify path the genbarcode in barcode module setup\n";
  144. print "</UL>\n";
  145. print "<BR>\n";
  146. return false;
  147. }
  148. return $bars;
  149. }
  150. /**
  151. * Calculate EAN sum
  152. *
  153. * @param string $ean EAN to encode
  154. * @return integer Sum
  155. */
  156. function barcode_gen_ean_sum($ean)
  157. {
  158. $even = true;
  159. $esum = 0;
  160. $osum = 0;
  161. $ln = strlen($ean) - 1;
  162. for ($i = $ln; $i >= 0; $i--) {
  163. if ($even) {
  164. $esum += $ean[$i];
  165. } else {
  166. $osum += $ean[$i];
  167. }
  168. $even = !$even;
  169. }
  170. return (10 - ((3 * $esum + $osum) % 10)) % 10;
  171. }
  172. /**
  173. * Generate EAN bars
  174. *
  175. * @param string $ean EAN to encode
  176. * @return string Encoded EAN
  177. */
  178. function barcode_gen_ean_bars($ean)
  179. {
  180. $digits = array(3211, 2221, 2122, 1411, 1132, 1231, 1114, 1312, 1213, 3112);
  181. $mirror = array("000000", "001011", "001101", "001110", "010011", "011001", "011100", "010101", "010110", "011010");
  182. $guards = array("9a1a", "1a1a1", "a1a7");
  183. $line = $guards[0];
  184. for ($i = 1; $i < 13; $i++) {
  185. $str = $digits[$ean[$i]];
  186. if ($i < 7 && $mirror[$ean[0]][$i - 1] == 1) {
  187. $line .= strrev($str);
  188. } else {
  189. $line .= $str;
  190. }
  191. if ($i == 6) {
  192. $line .= $guards[1];
  193. }
  194. }
  195. $line .= $guards[2];
  196. return $line;
  197. }
  198. /**
  199. * Encode EAN
  200. *
  201. * @param string $ean Code
  202. * @param string $encoding Encoding
  203. * @return array array('encoding': the encoding which has been used, 'bars': the bars, 'text': text-positioning info, 'error': error message if error)
  204. */
  205. function barcode_encode_ean($ean, $encoding = "EAN-13")
  206. {
  207. $ean = trim($ean);
  208. if (preg_match("/[^0-9]/i", $ean)) {
  209. return array("error"=>"Invalid encoding/code. encoding=".$encoding." code=".$ean." (not a numeric)", "text"=>"Invalid encoding/code. encoding=".$encoding." code=".$ean." (not a numeric)");
  210. }
  211. $encoding = strtoupper($encoding);
  212. if ($encoding == "ISBN") {
  213. if (!preg_match("/^978/", $ean)) {
  214. $ean = "978".$ean;
  215. }
  216. }
  217. if (preg_match("/^97[89]/", $ean)) {
  218. $encoding = "ISBN";
  219. }
  220. if (strlen($ean) < 12 || strlen($ean) > 13) {
  221. return array("error"=>"Invalid encoding/code. encoding=".$encoding." code=".$ean." (must have 12/13 numbers)", "text"=>"Invalid encoding/code. encoding=".$encoding." code=".$ean." (must have 12/13 numbers)");
  222. }
  223. $ean = substr($ean, 0, 12);
  224. $eansum = barcode_gen_ean_sum($ean);
  225. $ean .= $eansum;
  226. $bars = barcode_gen_ean_bars($ean);
  227. /* create text */
  228. $pos = 0;
  229. $text = "";
  230. for ($a = 0; $a < 13; $a++) {
  231. if ($a > 0) {
  232. $text .= " ";
  233. }
  234. $text .= $pos.":12:".$ean[$a];
  235. if ($a == 0) {
  236. $pos += 12;
  237. } elseif ($a == 6) {
  238. $pos += 12;
  239. } else {
  240. $pos += 7;
  241. }
  242. }
  243. return array(
  244. "error" => '',
  245. "encoding" => $encoding,
  246. "bars" => $bars,
  247. "text" => $text
  248. );
  249. }
  250. /**
  251. * Encode UPC
  252. *
  253. * @param string $upc Code
  254. * @param string $encoding Encoding
  255. * @return array array('encoding': the encoding which has been used, 'bars': the bars, 'text': text-positioning info, 'error': error message if error)
  256. */
  257. function barcode_encode_upc($upc, $encoding = "UPC")
  258. {
  259. $upc = trim($upc);
  260. if (preg_match("/[^0-9]/i", $upc)) {
  261. return array("error"=>"Invalid encoding/code. encoding=".$encoding." code=".$upc." (not a numeric)", "text"=>"Invalid encoding/code. encoding=".$encoding." code=".$upc." (not a numeric)");
  262. }
  263. $encoding = strtoupper($encoding);
  264. if (strlen($upc) < 11 || strlen($upc) > 12) {
  265. return array("error"=>"Invalid encoding/code. encoding=".$encoding." code=".$upc." (must have 11/12 numbers)", "text"=>"Invalid encoding/code. encoding=".$encoding." code=".$upc." (must have 11/12 numbers)");
  266. }
  267. $upc = substr("0".$upc, 0, 12);
  268. $eansum = barcode_gen_ean_sum($upc);
  269. $upc .= $eansum;
  270. $bars = barcode_gen_ean_bars($upc);
  271. /* create text */
  272. $pos = 0;
  273. $text = "";
  274. for ($a = 1; $a < 13; $a++) {
  275. if ($a > 1) {
  276. $text .= " ";
  277. }
  278. $text .= $pos.":12:".$upc[$a];
  279. if ($a == 1) {
  280. $pos += 15;
  281. } elseif ($a == 6) {
  282. $pos += 17;
  283. } elseif ($a == 11) {
  284. $pos += 15;
  285. } else {
  286. $pos += 7;
  287. }
  288. }
  289. return array(
  290. "error" => '',
  291. "encoding" => $encoding,
  292. "bars" => $bars,
  293. "text" => $text
  294. );
  295. }
  296. /**
  297. * Encode result of genbarcode command
  298. *
  299. * @param string $code Code
  300. * @param string $encoding Encoding
  301. * @return array|false array('encoding': the encoding which has been used, 'bars': the bars, 'text': text-positioning info)
  302. */
  303. function barcode_encode_genbarcode($code, $encoding)
  304. {
  305. global $conf, $db, $genbarcode_loc;
  306. // Clean parameters
  307. if (preg_match("/^ean$/i", $encoding) && strlen($code) == 13) {
  308. $code = substr($code, 0, 12);
  309. }
  310. if (!$encoding) {
  311. $encoding = "ANY";
  312. }
  313. $encoding = dol_string_nospecial($encoding, '_');
  314. $code = dol_string_nospecial($code, "_");
  315. $command = escapeshellarg($genbarcode_loc);
  316. $paramclear = " ".escapeshellarg($code)." ".escapeshellarg(strtoupper($encoding));
  317. $fullcommandclear = $command." ".$paramclear." 2>&1";
  318. //print $fullcommandclear."<br>\n";exit;
  319. dol_syslog("Run command ".$fullcommandclear);
  320. $outputfile = $conf->user->dir_temp.'/genbarcode.tmp'; // File used with popen method
  321. // Execute a CLI
  322. include_once DOL_DOCUMENT_ROOT.'/core/class/utils.class.php';
  323. $utils = new Utils($db);
  324. $result = $utils->executeCLI($fullcommandclear, $outputfile);
  325. if (!empty($result['output'])) {
  326. $tmparr = explode("\n", $result['output']);
  327. $bars = $tmparr[0];
  328. $text = $tmparr[1];
  329. $encoding = $tmparr[2];
  330. } else {
  331. dol_syslog("barcode.lib.php::barcode_encode_genbarcode failed to run ".$fullcommandclear, LOG_ERR);
  332. return false;
  333. }
  334. //var_dump($bars);
  335. $ret = array(
  336. "bars" => trim($bars),
  337. "text" => trim($text),
  338. "encoding" => trim($encoding),
  339. "error" => ""
  340. );
  341. //var_dump($ret);
  342. if (preg_match('/permission denied/i', $ret['bars'])) {
  343. $ret['error'] = $ret['bars'];
  344. $ret['bars'] = '';
  345. return $ret;
  346. }
  347. if (!$ret['bars']) {
  348. return false;
  349. }
  350. if (!$ret['text']) {
  351. return false;
  352. }
  353. if (!$ret['encoding']) {
  354. return false;
  355. }
  356. return $ret;
  357. }
  358. /**
  359. * Output image onto standard output, or onto disk if global filebarcode is defined
  360. *
  361. * @param string $text the text-line (<position>:<font-size>:<character> ...)
  362. * @param string $bars where to place the bars (<space-width><bar-width><space-width><bar-width>...)
  363. * @param int $scale scale factor ( 1 < scale < unlimited (scale 50 will produce 5400x300 pixels when using EAN-13!!!))
  364. * @param string $mode png,gif,jpg (default='png')
  365. * @param int $total_y the total height of the image ( default: scale * 60 )
  366. * @param array $space default: $space[top] = 2 * $scale; $space[bottom]= 2 * $scale; $space[left] = 2 * $scale; $space[right] = 2 * $scale;
  367. * @return string|null
  368. */
  369. function barcode_outimage($text, $bars, $scale = 1, $mode = "png", $total_y = 0, $space = '')
  370. {
  371. global $bar_color, $bg_color, $text_color;
  372. global $font_loc, $filebarcode;
  373. //print "$text, $bars, $scale, $mode, $total_y, $space, $font_loc, $filebarcode<br>";
  374. //var_dump($text);
  375. //var_dump($bars);
  376. //var_dump($font_loc);
  377. /* set defaults */
  378. if ($scale < 1) {
  379. $scale = 2;
  380. }
  381. $total_y = (int) $total_y;
  382. if ($total_y < 1) {
  383. $total_y = (int) $scale * 60;
  384. }
  385. if (!$space) {
  386. $space = array('top'=>2 * $scale, 'bottom'=>2 * $scale, 'left'=>2 * $scale, 'right'=>2 * $scale);
  387. }
  388. /* count total width */
  389. $xpos = 0;
  390. $width = true;
  391. $ln = strlen($bars);
  392. for ($i = 0; $i < $ln; $i++) {
  393. $val = strtolower($bars[$i]);
  394. if ($width) {
  395. $xpos += $val * $scale;
  396. $width = false;
  397. continue;
  398. }
  399. if (preg_match("/[a-z]/", $val)) {
  400. /* tall bar */
  401. $val = ord($val) - ord('a') + 1;
  402. }
  403. $xpos += $val * $scale;
  404. $width = true;
  405. }
  406. /* allocate the image */
  407. $total_x = ($xpos) + $space['right'] + $space['right'];
  408. $xpos = $space['left'];
  409. if (!function_exists("imagecreate")) {
  410. print "You don't have the gd2 extension enabled<br>\n";
  411. return "";
  412. }
  413. $im = imagecreate($total_x, $total_y);
  414. /* create two images */
  415. $col_bg = ImageColorAllocate($im, $bg_color[0], $bg_color[1], $bg_color[2]);
  416. $col_bar = ImageColorAllocate($im, $bar_color[0], $bar_color[1], $bar_color[2]);
  417. $col_text = ImageColorAllocate($im, $text_color[0], $text_color[1], $text_color[2]);
  418. $height = round($total_y - ($scale * 10));
  419. $height2 = round($total_y - $space['bottom']);
  420. /* paint the bars */
  421. $width = true;
  422. $ln = strlen($bars);
  423. for ($i = 0; $i < $ln; $i++) {
  424. $val = strtolower($bars[$i]);
  425. if ($width) {
  426. $xpos += $val * $scale;
  427. $width = false;
  428. continue;
  429. }
  430. if (preg_match("/[a-z]/", $val)) {
  431. /* tall bar */
  432. $val = ord($val) - ord('a') + 1;
  433. $h = $height2;
  434. } else {
  435. $h = $height;
  436. }
  437. imagefilledrectangle($im, $xpos, $space['top'], $xpos + ($val * $scale) - 1, $h, $col_bar);
  438. $xpos += $val * $scale;
  439. $width = true;
  440. }
  441. $chars = explode(" ", $text);
  442. foreach ($chars as $v) {
  443. if (trim($v)) {
  444. $inf = explode(":", $v);
  445. $fontsize = $scale * ($inf[1] / 1.8);
  446. $fontheight = $total_y - ($fontsize / 2.7) + 2;
  447. imagettftext($im, $fontsize, 0, $space['left'] + ($scale * $inf[0]) + 2, $fontheight, $col_text, $font_loc, $inf[2]);
  448. }
  449. }
  450. /* output the image */
  451. $mode = strtolower($mode);
  452. if ($mode == 'jpg' || $mode == 'jpeg') {
  453. header("Content-Type: image/jpeg; name=\"barcode.jpg\"");
  454. imagejpeg($im);
  455. } elseif ($mode == 'gif') {
  456. header("Content-Type: image/gif; name=\"barcode.gif\"");
  457. imagegif($im);
  458. } elseif (!empty($filebarcode)) {
  459. // To wxrite into afile onto disk
  460. imagepng($im, $filebarcode);
  461. } else {
  462. header("Content-Type: image/png; name=\"barcode.png\"");
  463. imagepng($im);
  464. }
  465. }
  466. /**
  467. * Check if EAN13 code is valid
  468. *
  469. * @param string $ean Code
  470. * @return bool
  471. */
  472. function isAValidEAN13($ean)
  473. {
  474. $sumEvenIndexes = 0;
  475. $sumOddIndexes = 0;
  476. $eanAsArray = array_map('intval', str_split($ean));
  477. if (!(count($eanAsArray) === 13)) {
  478. return false;
  479. };
  480. $num = (count($eanAsArray) - 1);
  481. for ($i = 0; $i < $num; $i++) {
  482. if ($i % 2 === 0) {
  483. $sumOddIndexes += $eanAsArray[$i];
  484. } else {
  485. $sumEvenIndexes += $eanAsArray[$i];
  486. }
  487. }
  488. $rest = ($sumOddIndexes + (3 * $sumEvenIndexes)) % 10;
  489. if ($rest !== 0) {
  490. $rest = 10 - $rest;
  491. }
  492. return $rest === $eanAsArray[12];
  493. }