30 $bar_color = array(0, 0, 0);
31 $bg_color = array(255, 255, 255);
32 $text_color = array(0, 0, 0);
38 if (defined(
'DOL_DEFAULT_TTF_BOLD')) {
39 $font_loc = constant(
'DOL_DEFAULT_TTF_BOLD');
43 if (isset($_SERVER[
'WINDIR']) && @file_exists($_SERVER[
'WINDIR'])) {
44 $font_loc = $_SERVER[
'WINDIR'].
'\Fonts\arialbd.ttf';
46 if (empty($font_loc)) {
47 die(
'DOL_DEFAULT_TTF_BOLD must de defined with full path to a TTF font.');
59 if (defined(
'PHP-BARCODE_PATH_COMMAND')) {
60 $genbarcode_loc = constant(
'PHP-BARCODE_PATH_COMMAND');
63 if (!empty($conf->global->GENBARCODE_LOCATION)) {
64 $genbarcode_loc = $conf->global->GENBARCODE_LOCATION;
80 function 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 $genbarcode_loc;
340 if (preg_match(
"/^ean$/i", $encoding) && strlen($code) == 13) {
341 $code = substr($code, 0, 12);
346 $encoding = preg_replace(
"/[\\\|]/",
"_", $encoding);
347 $code = preg_replace(
"/[\\\|]/",
"_", $code);
349 $command = escapeshellarg($genbarcode_loc);
351 $paramclear =
" ".escapeshellarg($code).
" ".escapeshellarg(strtoupper($encoding));
353 $fullcommandclear = $command.
" ".$paramclear.
" 2>&1";
357 $fp = popen($fullcommandclear,
"r");
359 $bars = fgets($fp, 1024);
360 $text = fgets($fp, 1024);
361 $encoding = fgets($fp, 1024);
364 dol_syslog(
"barcode.lib.php::barcode_encode_genbarcode failed to run popen ".$fullcommandclear, LOG_ERR);
369 "bars" => trim($bars),
370 "text" => trim($text),
371 "encoding" => trim($encoding),
375 if (preg_match(
'/permission denied/i', $ret[
'bars'])) {
376 $ret[
'error'] = $ret[
'bars'];
386 if (!$ret[
'encoding']) {
403 function barcode_outimage($text, $bars, $scale = 1, $mode =
"png", $total_y = 0, $space =
'')
405 global $bar_color, $bg_color, $text_color;
406 global $font_loc, $filebarcode;
417 $total_y = (int) $total_y;
419 $total_y = (int) $scale * 60;
422 $space = array(
'top'=>2 * $scale,
'bottom'=>2 * $scale,
'left'=>2 * $scale,
'right'=>2 * $scale);
429 for ($i = 0; $i < $ln; $i++) {
430 $val = strtolower($bars[$i]);
432 $xpos += $val * $scale;
436 if (preg_match(
"/[a-z]/", $val)) {
438 $val = ord($val) - ord(
'a') + 1;
440 $xpos += $val * $scale;
445 $total_x = ($xpos) + $space[
'right'] + $space[
'right'];
446 $xpos = $space[
'left'];
447 if (!function_exists(
"imagecreate")) {
448 print
"You don't have the gd2 extension enabled<br>\n";
451 $im = imagecreate($total_x, $total_y);
453 $col_bg = ImageColorAllocate($im, $bg_color[0], $bg_color[1], $bg_color[2]);
454 $col_bar = ImageColorAllocate($im, $bar_color[0], $bar_color[1], $bar_color[2]);
455 $col_text = ImageColorAllocate($im, $text_color[0], $text_color[1], $text_color[2]);
456 $height = round($total_y - ($scale * 10));
457 $height2 = round($total_y - $space[
'bottom']);
462 for ($i = 0; $i < $ln; $i++) {
463 $val = strtolower($bars[$i]);
465 $xpos += $val * $scale;
469 if (preg_match(
"/[a-z]/", $val)) {
471 $val = ord($val) - ord(
'a') + 1;
476 imagefilledrectangle($im, $xpos, $space[
'top'], $xpos + ($val * $scale) - 1, $h, $col_bar);
477 $xpos += $val * $scale;
481 $chars = explode(
" ", $text);
482 foreach ($chars as $v) {
484 $inf = explode(
":", $v);
485 $fontsize = $scale * ($inf[1] / 1.8);
486 $fontheight = $total_y - ($fontsize / 2.7) + 2;
487 imagettftext($im, $fontsize, 0, $space[
'left'] + ($scale * $inf[0]) + 2, $fontheight, $col_text, $font_loc, $inf[2]);
492 $mode = strtolower($mode);
493 if ($mode ==
'jpg' || $mode ==
'jpeg') {
494 header(
"Content-Type: image/jpeg; name=\"barcode.jpg\"");
496 } elseif ($mode ==
'gif') {
497 header(
"Content-Type: image/gif; name=\"barcode.gif\"");
499 } elseif (!empty($filebarcode)) {
501 imagepng($im, $filebarcode);
503 header(
"Content-Type: image/png; name=\"barcode.png\"");
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_gen_ean_bars($ean)
Generate EAN bars.
barcode_encode_ean($ean, $encoding="EAN-13")
Encode EAN.
barcode_encode($code, $encoding)
Encodes $code with $encoding using genbarcode OR built-in encoder if you don't have genbarcode only E...
if(!empty($conf->global->GENBARCODE_LOCATION)) barcode_print($code, $encoding="ANY", $scale=2, $mode="png")
Print barcode.
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.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.