30$bar_color = array(0, 0, 0);
31$bg_color = array(255, 255, 255);
32$text_color = array(0, 0, 0);
38if (defined(
'DOL_DEFAULT_TTF_BOLD')) {
39 $font_loc = constant(
'DOL_DEFAULT_TTF_BOLD');
43if (isset($_SERVER[
'WINDIR']) && @file_exists($_SERVER[
'WINDIR'])) {
44 $font_loc = $_SERVER[
'WINDIR'].
'\Fonts\arialbd.ttf';
46if (empty($font_loc)) {
47 die(
'DOL_DEFAULT_TTF_BOLD must de defined with full path to a TTF font.');
59if (defined(
'PHP-BARCODE_PATH_COMMAND')) {
60 $genbarcode_loc = constant(
'PHP-BARCODE_PATH_COMMAND');
64 $genbarcode_loc = $conf->global->GENBARCODE_LOCATION;
80function barcode_print($code, $encoding =
"ANY", $scale = 2, $mode =
"png")
82 dol_syslog(
"barcode.lib.php::barcode_print $code $encoding $scale $mode");
85 if (!$bars || !empty($bars[
'error'])) {
87 if (empty($bars[
'error'])) {
88 $error =
'Bad Value '.$code.
' for encoding '.$encoding;
90 $error = $bars[
'error'];
92 dol_syslog(
'barcode.lib.php::barcode_print '.$error, LOG_ERR);
129 global $genbarcode_loc;
131 if ((preg_match(
"/^upc$/i", $encoding))
132 && (preg_match(
"/^[0-9]{11,12}$/", $code))
135 dol_syslog(
"barcode.lib.php::barcode_encode Use barcode_encode_upc");
137 } elseif ((preg_match(
"/^ean$/i", $encoding))
139 || (($encoding) && (preg_match(
"/^isbn$/i", $encoding))
140 && ((strlen($code) == 9 || strlen($code) == 10) ||
141 (((preg_match(
"/^978/", $code) && strlen($code) == 12) ||
142 (strlen($code) == 13)))))
144 || ((!isset($encoding) || !$encoding || (preg_match(
"/^ANY$/i", $encoding)))
145 && (preg_match(
"/^[0-9]{12,13}$/", $code)))
148 dol_syslog(
"barcode.lib.php::barcode_encode Use barcode_encode_ean");
150 } elseif (file_exists($genbarcode_loc)) {
152 dol_syslog(
"barcode.lib.php::barcode_encode Use genbarcode ".$genbarcode_loc.
" code=".$code.
" encoding=".$encoding);
155 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";
157 print
"<LI>download gnu-barcode from <A href=\"https://www.gnu.org/software/barcode/\">www.gnu.org/software/barcode/</A>\n";
158 print
"<LI>compile and install them\n";
159 print
"<LI>specify path the genbarcode in barcode module setup\n";
180 $ln = strlen($ean) - 1;
181 for ($i = $ln; $i >= 0; $i--) {
189 return (10 - ((3 * $esum + $osum) % 10)) % 10;
201 $digits = array(3211, 2221, 2122, 1411, 1132, 1231, 1114, 1312, 1213, 3112);
202 $mirror = array(
"000000",
"001011",
"001101",
"001110",
"010011",
"011001",
"011100",
"010101",
"010110",
"011010");
203 $guards = array(
"9a1a",
"1a1a1",
"a1a7");
206 for ($i = 1; $i < 13; $i++) {
207 $str = $digits[$ean[$i]];
208 if ($i < 7 && $mirror[$ean[0]][$i - 1] == 1) {
209 $line .= strrev($str);
232 if (preg_match(
"/[^0-9]/i", $ean)) {
233 return array(
"error"=>
"Invalid encoding/code. encoding=".$encoding.
" code=".$ean.
" (not a numeric)",
"text"=>
"Invalid encoding/code. encoding=".$encoding.
" code=".$ean.
" (not a numeric)");
235 $encoding = strtoupper($encoding);
236 if ($encoding ==
"ISBN") {
237 if (!preg_match(
"/^978/", $ean)) {
241 if (preg_match(
"/^97[89]/", $ean)) {
244 if (strlen($ean) < 12 || strlen($ean) > 13) {
245 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)");
248 $ean = substr($ean, 0, 12);
256 for ($a = 0; $a < 13; $a++) {
260 $text .= $pos.
":12:".$ean[$a];
272 "encoding" => $encoding,
288 if (preg_match(
"/[^0-9]/i", $upc)) {
289 return array(
"error"=>
"Invalid encoding/code. encoding=".$encoding.
" code=".$upc.
" (not a numeric)",
"text"=>
"Invalid encoding/code. encoding=".$encoding.
" code=".$upc.
" (not a numeric)");
291 $encoding = strtoupper($encoding);
292 if (strlen($upc) < 11 || strlen($upc) > 12) {
293 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)");
296 $upc = substr(
"0".$upc, 0, 12);
304 for ($a = 1; $a < 13; $a++) {
308 $text .= $pos.
":12:".$upc[$a];
313 } elseif ($a == 11) {
322 "encoding" => $encoding,
337 global $conf, $db, $genbarcode_loc;
340 if (preg_match(
"/^ean$/i", $encoding) && strlen($code) == 13) {
341 $code = substr($code, 0, 12);
349 $command = escapeshellarg($genbarcode_loc);
350 $paramclear =
" ".escapeshellarg($code).
" ".escapeshellarg(strtoupper($encoding));
352 $fullcommandclear = $command.
" ".$paramclear.
" 2>&1";
357 $outputfile = $conf->user->dir_temp.
'/genbarcode.tmp';
360 include_once DOL_DOCUMENT_ROOT.
'/core/class/utils.class.php';
361 $utils =
new Utils($db);
362 $result = $utils->executeCLI($fullcommandclear, $outputfile);
364 if (!empty($result[
'output'])) {
365 $tmparr = explode(
"\n", $result[
'output']);
368 $encoding = $tmparr[2];
370 dol_syslog(
"barcode.lib.php::barcode_encode_genbarcode failed to run ".$fullcommandclear, LOG_ERR);
376 "bars" => trim($bars),
377 "text" => trim($text),
378 "encoding" => trim($encoding),
382 if (preg_match(
'/permission denied/i', $ret[
'bars'])) {
383 $ret[
'error'] = $ret[
'bars'];
393 if (!$ret[
'encoding']) {
410function barcode_outimage($text, $bars, $scale = 1, $mode =
"png", $total_y = 0, $space =
'')
412 global $bar_color, $bg_color, $text_color;
413 global $font_loc, $filebarcode;
424 $total_y = (int) $total_y;
426 $total_y = (int) $scale * 60;
429 $space = array(
'top'=>2 * $scale,
'bottom'=>2 * $scale,
'left'=>2 * $scale,
'right'=>2 * $scale);
436 for ($i = 0; $i < $ln; $i++) {
437 $val = strtolower($bars[$i]);
439 $xpos += $val * $scale;
443 if (preg_match(
"/[a-z]/", $val)) {
445 $val = ord($val) - ord(
'a') + 1;
447 $xpos += $val * $scale;
452 $total_x = ($xpos) + $space[
'right'] + $space[
'right'];
453 $xpos = $space[
'left'];
454 if (!function_exists(
"imagecreate")) {
455 print
"You don't have the gd2 extension enabled<br>\n";
458 $im = imagecreate($total_x, $total_y);
460 $col_bg = ImageColorAllocate($im, $bg_color[0], $bg_color[1], $bg_color[2]);
461 $col_bar = ImageColorAllocate($im, $bar_color[0], $bar_color[1], $bar_color[2]);
462 $col_text = ImageColorAllocate($im, $text_color[0], $text_color[1], $text_color[2]);
463 $height = round($total_y - ($scale * 10));
464 $height2 = round($total_y - $space[
'bottom']);
469 for ($i = 0; $i < $ln; $i++) {
470 $val = strtolower($bars[$i]);
472 $xpos += $val * $scale;
476 if (preg_match(
"/[a-z]/", $val)) {
478 $val = ord($val) - ord(
'a') + 1;
483 imagefilledrectangle($im, $xpos, $space[
'top'], $xpos + ($val * $scale) - 1, $h, $col_bar);
484 $xpos += $val * $scale;
488 $chars = explode(
" ", $text);
489 foreach ($chars as $v) {
491 $inf = explode(
":", $v);
492 $fontsize = $scale * ($inf[1] / 1.8);
493 $fontheight = $total_y - ($fontsize / 2.7) + 2;
494 imagettftext($im, $fontsize, 0, $space[
'left'] + ($scale * $inf[0]) + 2, $fontheight, $col_text, $font_loc, $inf[2]);
499 $mode = strtolower($mode);
500 if ($mode ==
'jpg' || $mode ==
'jpeg') {
501 header(
"Content-Type: image/jpeg; name=\"barcode.jpg\"");
503 } elseif ($mode ==
'gif') {
504 header(
"Content-Type: image/gif; name=\"barcode.gif\"");
506 } elseif (!empty($filebarcode)) {
508 imagepng($im, $filebarcode);
510 header(
"Content-Type: image/png; name=\"barcode.png\"");
526 $eanAsArray = array_map(
'intval', str_split($ean));
528 if (!(count($eanAsArray) === 13)) {
532 $num = (count($eanAsArray) - 1);
533 for ($i = 0; $i < $num; $i++) {
535 $sumOddIndexes += $eanAsArray[$i];
537 $sumEvenIndexes += $eanAsArray[$i];
541 $rest = ($sumOddIndexes + (3 * $sumEvenIndexes)) % 10;
547 return $rest === $eanAsArray[12];
barcode_encode_genbarcode($code, $encoding)
Encode result of genbarcode command.
if(getDolGlobalString( 'GENBARCODE_LOCATION')) barcode_print($code, $encoding="ANY", $scale=2, $mode="png")
Print barcode.
barcode_gen_ean_sum($ean)
Calculate EAN sum.
barcode_encode_upc($upc, $encoding="UPC")
Encode UPC.
barcode_gen_ean_bars($ean)
Generate EAN bars.
barcode_encode_ean($ean, $encoding="EAN-13")
Encode EAN.
isAValidEAN13($ean)
Check if EAN13 code is valid.
barcode_encode($code, $encoding)
Encodes $code with $encoding using genbarcode OR built-in encoder if you don't have genbarcode only E...
barcode_outimage($text, $bars, $scale=1, $mode="png", $total_y=0, $space='')
Output image onto standard output, or onto disk if global filebarcode is defined.
Class to manage utility methods.
dol_string_nospecial($str, $newstr='_', $badcharstoreplace='', $badcharstoremove='', $keepspaces=0)
Clean a string from all punctuation characters to use it as a ref or login.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...