barcode.lib.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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 (!empty($conf->global->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 $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 = preg_replace("/[\\\|]/", "_", $encoding);
  314. $code = preg_replace("/[\\\|]/", "_", $code);
  315. $command = escapeshellarg($genbarcode_loc);
  316. //$paramclear=" \"".str_replace("\"", "\\\"",$code)."\" \"".str_replace("\"", "\\\"",strtoupper($encoding))."\"";
  317. $paramclear = " ".escapeshellarg($code)." ".escapeshellarg(strtoupper($encoding));
  318. $fullcommandclear = $command." ".$paramclear." 2>&1";
  319. //print $fullcommandclear."<br>\n";exit;
  320. dol_syslog("Run command ".$fullcommandclear);
  321. $fp = popen($fullcommandclear, "r");
  322. if ($fp) {
  323. $bars = fgets($fp, 1024);
  324. $text = fgets($fp, 1024);
  325. $encoding = fgets($fp, 1024);
  326. pclose($fp);
  327. } else {
  328. dol_syslog("barcode.lib.php::barcode_encode_genbarcode failed to run popen ".$fullcommandclear, LOG_ERR);
  329. return false;
  330. }
  331. //var_dump($bars);
  332. $ret = array(
  333. "bars" => trim($bars),
  334. "text" => trim($text),
  335. "encoding" => trim($encoding),
  336. "error" => ""
  337. );
  338. //var_dump($ret);
  339. if (preg_match('/permission denied/i', $ret['bars'])) {
  340. $ret['error'] = $ret['bars'];
  341. $ret['bars'] = '';
  342. return $ret;
  343. }
  344. if (!$ret['bars']) {
  345. return false;
  346. }
  347. if (!$ret['text']) {
  348. return false;
  349. }
  350. if (!$ret['encoding']) {
  351. return false;
  352. }
  353. return $ret;
  354. }
  355. /**
  356. * Output image onto standard output, or onto disk if global filebarcode is defined
  357. *
  358. * @param string $text the text-line (<position>:<font-size>:<character> ...)
  359. * @param string $bars where to place the bars (<space-width><bar-width><space-width><bar-width>...)
  360. * @param int $scale scale factor ( 1 < scale < unlimited (scale 50 will produce 5400x300 pixels when using EAN-13!!!))
  361. * @param string $mode png,gif,jpg (default='png')
  362. * @param int $total_y the total height of the image ( default: scale * 60 )
  363. * @param array $space default: $space[top] = 2 * $scale; $space[bottom]= 2 * $scale; $space[left] = 2 * $scale; $space[right] = 2 * $scale;
  364. * @return string|null
  365. */
  366. function barcode_outimage($text, $bars, $scale = 1, $mode = "png", $total_y = 0, $space = '')
  367. {
  368. global $bar_color, $bg_color, $text_color;
  369. global $font_loc, $filebarcode;
  370. //print "$text, $bars, $scale, $mode, $total_y, $space, $font_loc, $filebarcode<br>";
  371. //var_dump($text);
  372. //var_dump($bars);
  373. //var_dump($font_loc);
  374. /* set defaults */
  375. if ($scale < 1) {
  376. $scale = 2;
  377. }
  378. $total_y = (int) $total_y;
  379. if ($total_y < 1) {
  380. $total_y = (int) $scale * 60;
  381. }
  382. if (!$space) {
  383. $space = array('top'=>2 * $scale, 'bottom'=>2 * $scale, 'left'=>2 * $scale, 'right'=>2 * $scale);
  384. }
  385. /* count total width */
  386. $xpos = 0;
  387. $width = true;
  388. $ln = strlen($bars);
  389. for ($i = 0; $i < $ln; $i++) {
  390. $val = strtolower($bars[$i]);
  391. if ($width) {
  392. $xpos += $val * $scale;
  393. $width = false;
  394. continue;
  395. }
  396. if (preg_match("/[a-z]/", $val)) {
  397. /* tall bar */
  398. $val = ord($val) - ord('a') + 1;
  399. }
  400. $xpos += $val * $scale;
  401. $width = true;
  402. }
  403. /* allocate the image */
  404. $total_x = ($xpos) + $space['right'] + $space['right'];
  405. $xpos = $space['left'];
  406. if (!function_exists("imagecreate")) {
  407. print "You don't have the gd2 extension enabled<br>\n";
  408. return "";
  409. }
  410. $im = imagecreate($total_x, $total_y);
  411. /* create two images */
  412. $col_bg = ImageColorAllocate($im, $bg_color[0], $bg_color[1], $bg_color[2]);
  413. $col_bar = ImageColorAllocate($im, $bar_color[0], $bar_color[1], $bar_color[2]);
  414. $col_text = ImageColorAllocate($im, $text_color[0], $text_color[1], $text_color[2]);
  415. $height = round($total_y - ($scale * 10));
  416. $height2 = round($total_y - $space['bottom']);
  417. /* paint the bars */
  418. $width = true;
  419. $ln = strlen($bars);
  420. for ($i = 0; $i < $ln; $i++) {
  421. $val = strtolower($bars[$i]);
  422. if ($width) {
  423. $xpos += $val * $scale;
  424. $width = false;
  425. continue;
  426. }
  427. if (preg_match("/[a-z]/", $val)) {
  428. /* tall bar */
  429. $val = ord($val) - ord('a') + 1;
  430. $h = $height2;
  431. } else {
  432. $h = $height;
  433. }
  434. imagefilledrectangle($im, $xpos, $space['top'], $xpos + ($val * $scale) - 1, $h, $col_bar);
  435. $xpos += $val * $scale;
  436. $width = true;
  437. }
  438. $chars = explode(" ", $text);
  439. foreach ($chars as $v) {
  440. if (trim($v)) {
  441. $inf = explode(":", $v);
  442. $fontsize = $scale * ($inf[1] / 1.8);
  443. $fontheight = $total_y - ($fontsize / 2.7) + 2;
  444. imagettftext($im, $fontsize, 0, $space['left'] + ($scale * $inf[0]) + 2, $fontheight, $col_text, $font_loc, $inf[2]);
  445. }
  446. }
  447. /* output the image */
  448. $mode = strtolower($mode);
  449. if ($mode == 'jpg' || $mode == 'jpeg') {
  450. header("Content-Type: image/jpeg; name=\"barcode.jpg\"");
  451. imagejpeg($im);
  452. } elseif ($mode == 'gif') {
  453. header("Content-Type: image/gif; name=\"barcode.gif\"");
  454. imagegif($im);
  455. } elseif (!empty($filebarcode)) {
  456. // To wxrite into afile onto disk
  457. imagepng($im, $filebarcode);
  458. } else {
  459. header("Content-Type: image/png; name=\"barcode.png\"");
  460. imagepng($im);
  461. }
  462. }