32$bar_color = array(0, 0, 0);
33$bg_color = array(255, 255, 255);
34$text_color = array(0, 0, 0);
40if (defined(
'DOL_DEFAULT_TTF_BOLD')) {
41 $font_loc = constant(
'DOL_DEFAULT_TTF_BOLD');
45if (isset($_SERVER[
'WINDIR']) && @file_exists($_SERVER[
'WINDIR'])) {
46 $font_loc = $_SERVER[
'WINDIR'].
'\Fonts\arialbd.ttf';
48if (empty($font_loc)) {
49 die(
'DOL_DEFAULT_TTF_BOLD must de defined with full path to a TTF font.');
61if (defined(
'PHP-BARCODE_PATH_COMMAND')) {
62 $genbarcode_loc = constant(
'PHP-BARCODE_PATH_COMMAND');
83function barcode_print($code, $encoding =
"ANY", $scale = 2, $mode =
"png", $filebarcode =
'')
85 dol_syslog(
"barcode.lib.php::barcode_print $code $encoding $scale $mode");
88 if (!$bars || !empty($bars[
'error'])) {
90 if (empty($bars[
'error'])) {
91 $error =
'Bad Value '.$code.
' for encoding '.$encoding;
93 $error = $bars[
'error'];
95 dol_syslog(
'barcode.lib.php::barcode_print '.$error, LOG_ERR);
105 barcode_outimage($bars[
'text'], $bars[
'bars'], $scale, $mode, 0, [], $filebarcode);
134 global $genbarcode_loc;
136 if ((preg_match(
"/^upc$/i", $encoding))
137 && (preg_match(
"/^[0-9]{11,12}$/", $code))
140 dol_syslog(
"barcode.lib.php::barcode_encode Use barcode_encode_upc");
142 } elseif ((preg_match(
"/^ean$/i", $encoding))
144 || (($encoding) && (preg_match(
"/^isbn$/i", $encoding))
145 && ((strlen($code) == 9 || strlen($code) == 10) ||
146 (((preg_match(
"/^978/", $code) && strlen($code) == 12) ||
147 (strlen($code) == 13)))))
149 || ((!isset($encoding) || !$encoding || (preg_match(
"/^ANY$/i", $encoding)))
150 && (preg_match(
"/^[0-9]{12,13}$/", $code)))
153 dol_syslog(
"barcode.lib.php::barcode_encode Use barcode_encode_ean");
155 } elseif (file_exists($genbarcode_loc)) {
157 dol_syslog(
"barcode.lib.php::barcode_encode Use genbarcode ".$genbarcode_loc.
" code=".$code.
" encoding=".$encoding);
160 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";
162 print
"<LI>download gnu-barcode from <A href=\"https://www.gnu.org/software/barcode/\">www.gnu.org/software/barcode/</A>\n";
163 print
"<LI>compile and install them\n";
164 print
"<LI>specify path the genbarcode in barcode module setup\n";
185 $ln = strlen($ean) - 1;
186 for ($i = $ln; $i >= 0; $i--) {
194 return (10 - ((3 * $esum + $osum) % 10)) % 10;
206 $digits = array(
'3211',
'2221',
'2122',
'1411',
'1132',
'1231',
'1114',
'1312',
'1213',
'3112');
207 $mirror = array(
"000000",
"001011",
"001101",
"001110",
"010011",
"011001",
"011100",
"010101",
"010110",
"011010");
208 $guards = array(
"9a1a",
"1a1a1",
"a1a7");
211 for ($i = 1; $i < 13; $i++) {
212 $str = $digits[(int) $ean[$i]];
213 if ($i < 7 && $mirror[(
int) $ean[0]][$i - 1] == 1) {
214 $line .= strrev($str);
237 if (preg_match(
"/[^0-9]/i", $ean)) {
238 return array(
"error" =>
"Invalid encoding/code. encoding=".$encoding.
" code=".$ean.
" (not a numeric)",
"text" =>
"Invalid encoding/code. encoding=".$encoding.
" code=".$ean.
" (not a numeric)");
240 $encoding = strtoupper($encoding);
241 if ($encoding ==
"ISBN") {
242 if (!preg_match(
"/^978/", $ean)) {
246 if (preg_match(
"/^97[89]/", $ean)) {
249 if (strlen($ean) < 12 || strlen($ean) > 13) {
250 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)");
253 $ean = substr($ean, 0, 12);
261 for ($a = 0; $a < 13; $a++) {
265 $text .= $pos.
":12:".$ean[$a];
277 "encoding" => $encoding,
293 if (preg_match(
"/[^0-9]/i", $upc)) {
294 return array(
"error" =>
"Invalid encoding/code. encoding=".$encoding.
" code=".$upc.
" (not a numeric)",
"text" =>
"Invalid encoding/code. encoding=".$encoding.
" code=".$upc.
" (not a numeric)");
296 $encoding = strtoupper($encoding);
297 if (strlen($upc) < 11 || strlen($upc) > 12) {
298 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)");
301 $upc = substr(
"0".$upc, 0, 12);
309 for ($a = 1; $a < 13; $a++) {
313 $text .= $pos.
":12:".$upc[$a];
318 } elseif ($a == 11) {
327 "encoding" => $encoding,
342 global $conf, $db, $genbarcode_loc;
345 if (preg_match(
"/^ean$/i", $encoding) && strlen($code) == 13) {
346 $code = substr($code, 0, 12);
354 $command = escapeshellarg($genbarcode_loc);
355 $paramclear =
" ".escapeshellarg($code).
" ".escapeshellarg(strtoupper($encoding));
357 $fullcommandclear = $command.
" ".$paramclear.
" 2>&1";
362 $outputfile = $conf->user->dir_temp.
'/genbarcode.tmp';
365 include_once DOL_DOCUMENT_ROOT.
'/core/class/utils.class.php';
366 $utils =
new Utils($db);
367 $result = $utils->executeCLI($fullcommandclear, $outputfile);
369 if (!empty($result[
'output'])) {
370 $tmparr = explode(
"\n", $result[
'output']);
373 $encoding = $tmparr[2];
375 dol_syslog(
"barcode.lib.php::barcode_encode_genbarcode failed to run ".$fullcommandclear, LOG_ERR);
381 "bars" => trim($bars),
382 "text" => trim($text),
383 "encoding" => trim($encoding),
387 if (preg_match(
'/permission denied/i', $ret[
'bars'])) {
388 $ret[
'error'] = $ret[
'bars'];
398 if (!$ret[
'encoding']) {
416function barcode_outimage($text, $bars, $scale = 1, $mode =
"png", $total_y = 0, $space = [], $filebarcode =
'')
418 global $bar_color, $bg_color, $text_color, $font_loc;
426 $total_y = (int) $total_y;
428 $total_y = (int) $scale * 60;
430 if (!is_array($space) || empty($space)) {
431 $space = array(
'top' => 2 * $scale,
'bottom' => 2 * $scale,
'left' => 2 * $scale,
'right' => 2 * $scale);
438 for ($i = 0; $i < $ln; $i++) {
439 $val = strtolower($bars[$i]);
441 $xpos += (int) $val * $scale;
445 if (preg_match(
"/[a-z]/", $val)) {
447 $val = ord($val) - ord(
'a') + 1;
449 $xpos += $val * $scale;
454 $total_x = ($xpos) + $space[
'right'] + $space[
'right'];
455 $xpos = $space[
'left'];
456 if (!function_exists(
"imagecreate")) {
457 print
"You don't have the gd2 extension enabled<br>\n";
460 $im = imagecreate($total_x, $total_y);
462 $col_bg = imagecolorallocate($im, $bg_color[0], $bg_color[1], $bg_color[2]);
463 $col_bar = imagecolorallocate($im, $bar_color[0], $bar_color[1], $bar_color[2]);
464 $col_text = imagecolorallocate($im, $text_color[0], $text_color[1], $text_color[2]);
465 $height = (int) round($total_y - ($scale * 10));
466 $height2 = (int) round($total_y - $space[
'bottom']);
471 for ($i = 0; $i < $ln; $i++) {
472 $val = strtolower($bars[$i]);
474 $xpos += (float) $val * $scale;
478 if (preg_match(
"/[a-z]/", $val)) {
480 $val = ord($val) - ord(
'a') + 1;
485 imagefilledrectangle($im, $xpos, $space[
'top'], $xpos + (
int) ((
float) $val * $scale) - 1, $h, $col_bar);
486 $xpos += $val * $scale;
490 $chars = explode(
" ", $text);
491 foreach ($chars as $v) {
493 $inf = explode(
":", $v);
494 $fontsize = $scale * ((float) $inf[1] / 1.8);
495 $fontheight = (int) round($total_y - ($fontsize / 2.7) + 2);
496 imagettftext($im, $fontsize, 0, $space[
'left'] + (
int) ($scale * (
float) $inf[0]) + 2, $fontheight, $col_text, $font_loc, $inf[2]);
501 $mode = strtolower($mode);
502 if (!empty($filebarcode) && (empty($mode) || $mode ==
'png')) {
504 imagepng($im, $filebarcode);
505 } elseif ($mode ==
'jpg' || $mode ==
'jpeg') {
508 } elseif ($mode ==
'gif') {
511 } elseif ($mode ==
'png') {
530 $eanAsArray = array_map(
'intval', str_split($ean));
532 if (!(count($eanAsArray) === 13)) {
536 $num = (count($eanAsArray) - 1);
537 for ($i = 0; $i < $num; $i++) {
539 $sumOddIndexes += $eanAsArray[$i];
541 $sumEvenIndexes += $eanAsArray[$i];
545 $rest = ($sumOddIndexes + (3 * $sumEvenIndexes)) % 10;
551 return $rest === $eanAsArray[12];
if(getDolGlobalString( 'GENBARCODE_LOCATION')) barcode_print($code, $encoding="ANY", $scale=2, $mode="png", $filebarcode='')
Print barcode.
barcode_encode_genbarcode($code, $encoding)
Encode result of genbarcode command.
barcode_gen_ean_sum($ean)
Calculate EAN sum.
barcode_encode_upc($upc, $encoding="UPC")
Encode UPC.
barcode_outimage($text, $bars, $scale=1, $mode="png", $total_y=0, $space=[], $filebarcode='')
Output image onto standard output, or onto disk if $filebarcode is defined.
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...
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 a 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...
if(!defined( 'NOREQUIREMENU')) if(!empty(GETPOST('seteventmessages', 'alpha'))) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.