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')) $font_loc = constant(
'DOL_DEFAULT_TTF_BOLD');
41 if (isset(
$_SERVER[
'WINDIR']) && @file_exists(
$_SERVER[
'WINDIR'])) $font_loc =
$_SERVER[
'WINDIR'].
'\Fonts\arialbd.ttf';
42 if (empty($font_loc)) die(
'DOL_DEFAULT_TTF_BOLD must de defined with full path to a TTF font.');
53 if (defined(
'PHP-BARCODE_PATH_COMMAND')) $genbarcode_loc = constant(
'PHP-BARCODE_PATH_COMMAND');
54 else $genbarcode_loc = $conf->global->GENBARCODE_LOCATION;
68 function barcode_print($code, $encoding =
"ANY", $scale = 2, $mode =
"png")
70 dol_syslog(
"barcode.lib.php::barcode_print $code $encoding $scale $mode");
73 if (!$bars || !empty($bars[
'error']))
76 if (empty($bars[
'error'])) $error =
'Bad Value '.$code.
' for encoding '.$encoding;
77 else $error = $bars[
'error'];
78 dol_syslog(
'barcode.lib.php::barcode_print '.$error, LOG_ERR);
81 if (!$mode) $mode =
"png";
113 global $genbarcode_loc;
116 (preg_match(
"/^ean$/i", $encoding))
118 || (($encoding) && (preg_match(
"/^isbn$/i", $encoding))
119 && ((strlen($code) == 9 || strlen($code) == 10) ||
120 (((preg_match(
"/^978/", $code) && strlen($code) == 12) ||
121 (strlen($code) == 13)))))
123 || ((!isset($encoding) || !$encoding || (preg_match(
"/^ANY$/i", $encoding)))
124 && (preg_match(
"/^[0-9]{12,13}$/", $code)))
128 dol_syslog(
"barcode.lib.php::barcode_encode Use barcode_encode_ean");
130 } elseif (file_exists($genbarcode_loc))
133 dol_syslog(
"barcode.lib.php::barcode_encode Use genbarcode ".$genbarcode_loc.
" code=".$code.
" encoding=".$encoding);
136 print "barcode_encode needs an external programm for encodings other then EAN/ISBN (code=".$code.
", encoding=".$encoding.
")<BR>\n";
138 print "<LI>download gnu-barcode from <A href=\"https://www.gnu.org/software/barcode/\">www.gnu.org/software/barcode/</A>\n";
139 print "<LI>compile and install them\n";
140 print "<LI>download genbarcode from <A href=\"http://www.ashberg.de/bar/\">www.ashberg.de/bar/</A>\n";
141 print "<LI>compile and install them\n";
142 print "<LI>specify path the genbarcode in barcode module setup\n";
160 $even =
true; $esum = 0; $osum = 0;
161 $ln = strlen($ean) - 1;
162 for ($i = $ln; $i >= 0; $i--)
164 if ($even) $esum += $ean[$i];
else $osum += $ean[$i];
167 return (10 - ((3 * $esum + $osum) % 10)) % 10;
179 $digits = array(3211, 2221, 2122, 1411, 1132, 1231, 1114, 1312, 1213, 3112);
180 $mirror = array(
"000000",
"001011",
"001101",
"001110",
"010011",
"011001",
"011100",
"010101",
"010110",
"011010");
181 $guards = array(
"9a1a",
"1a1a1",
"a1a");
184 if (preg_match(
"/[^0-9]/i", $ean))
186 return array(
"error"=>
"Invalid encoding/code. encoding=".$encoding.
" code=".$ean.
" (not a numeric)",
"text"=>
"Invalid encoding/code. encoding=".$encoding.
" code=".$ean.
" (not a numeric)");
188 $encoding = strtoupper($encoding);
189 if ($encoding ==
"ISBN")
191 if (!preg_match(
"/^978/", $ean)) $ean =
"978".$ean;
193 if (preg_match(
"/^978/", $ean)) $encoding =
"ISBN";
194 if (strlen($ean) < 12 || strlen($ean) > 13)
196 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)");
199 $ean = substr($ean, 0, 12);
203 for ($i = 1; $i < 13; $i++)
205 $str = $digits[$ean[$i]];
206 if ($i < 7 && $mirror[$ean[0]][$i - 1] == 1) $line .= strrev($str);
else $line .= $str;
207 if ($i == 6) $line .= $guards[1];
214 for ($a = 0; $a < 13; $a++)
216 if ($a > 0) $text .=
" ";
217 $text .=
"$pos:12:{$ean[$a]}";
218 if ($a == 0) $pos += 12;
219 elseif ($a == 6) $pos += 12;
225 "encoding" => $encoding,
240 global $genbarcode_loc;
243 if (preg_match(
"/^ean$/i", $encoding) && strlen($code) == 13) $code = substr($code, 0, 12);
244 if (!$encoding) $encoding =
"ANY";
245 $encoding = preg_replace(
"/[\\\|]/",
"_", $encoding);
246 $code = preg_replace(
"/[\\\|]/",
"_", $code);
248 $command = escapeshellarg($genbarcode_loc);
250 $paramclear =
" ".escapeshellarg($code).
" ".escapeshellarg(strtoupper($encoding));
252 $fullcommandclear = $command.
" ".$paramclear.
" 2>&1";
256 $fp = popen($fullcommandclear,
"r");
259 $bars = fgets($fp, 1024);
260 $text = fgets($fp, 1024);
261 $encoding = fgets($fp, 1024);
264 dol_syslog(
"barcode.lib.php::barcode_encode_genbarcode failed to run popen ".$fullcommandclear, LOG_ERR);
269 "bars" => trim($bars),
270 "text" => trim($text),
271 "encoding" => trim($encoding),
275 if (preg_match(
'/permission denied/i', $ret[
'bars']))
277 $ret[
'error'] = $ret[
'bars']; $ret[
'bars'] =
'';
280 if (!$ret[
'bars'])
return false;
281 if (!$ret[
'text'])
return false;
282 if (!$ret[
'encoding'])
return false;
297 function barcode_outimage($text, $bars, $scale = 1, $mode =
"png", $total_y = 0, $space =
'')
299 global $bar_color, $bg_color, $text_color;
300 global $font_loc, $filebarcode;
308 if ($scale < 1) $scale = 2;
309 $total_y = (int) $total_y;
310 if ($total_y < 1) $total_y = (int) $scale * 60;
312 $space = array(
'top'=>2 * $scale,
'bottom'=>2 * $scale,
'left'=>2 * $scale,
'right'=>2 * $scale);
318 for ($i = 0; $i < $ln; $i++)
320 $val = strtolower($bars[$i]);
323 $xpos += $val * $scale;
327 if (preg_match(
"/[a-z]/", $val))
330 $val = ord($val) - ord(
'a') + 1;
332 $xpos += $val * $scale;
337 $total_x = ($xpos) + $space[
'right'] + $space[
'right'];
338 $xpos = $space[
'left'];
339 if (!function_exists(
"imagecreate"))
341 print "You don't have the gd2 extension enabled<br>\n";
344 $im = imagecreate($total_x, $total_y);
346 $col_bg = ImageColorAllocate($im, $bg_color[0], $bg_color[1], $bg_color[2]);
347 $col_bar = ImageColorAllocate($im, $bar_color[0], $bar_color[1], $bar_color[2]);
348 $col_text = ImageColorAllocate($im, $text_color[0], $text_color[1], $text_color[2]);
349 $height = round($total_y - ($scale * 10));
350 $height2 = round($total_y - $space[
'bottom']);
355 for ($i = 0; $i < $ln; $i++)
357 $val = strtolower($bars[$i]);
360 $xpos += $val * $scale;
364 if (preg_match(
"/[a-z]/", $val))
367 $val = ord($val) - ord(
'a') + 1;
370 imagefilledrectangle($im, $xpos, $space[
'top'], $xpos + ($val * $scale) - 1, $h, $col_bar);
371 $xpos += $val * $scale;
375 $chars = explode(
" ", $text);
376 foreach ($chars as $v) {
378 $inf = explode(
":", $v);
379 $fontsize = $scale * ($inf[1] / 1.8);
380 $fontheight = $total_y - ($fontsize / 2.7) + 2;
381 imagettftext($im, $fontsize, 0, $space[
'left'] + ($scale * $inf[0]) + 2, $fontheight, $col_text, $font_loc, $inf[2]);
386 $mode = strtolower($mode);
387 if ($mode ==
'jpg' || $mode ==
'jpeg') {
388 header(
"Content-Type: image/jpeg; name=\"barcode.jpg\"");
390 } elseif ($mode ==
'gif') {
391 header(
"Content-Type: image/gif; name=\"barcode.gif\"");
393 } elseif (!empty($filebarcode)) {
395 imagepng($im, $filebarcode);
397 header(
"Content-Type: image/png; name=\"barcode.png\"");
barcode_encode($code, $encoding)
Encodes $code with $encoding using genbarcode OR built-in encoder if you don't have genbarcode only E...
barcode_gen_ean_sum($ean)
Calculate EAN sum.
barcode_print($code, $encoding="ANY", $scale=2, $mode="png")
Print barcode.
barcode_encode_ean($ean, $encoding="EAN-13")
Encode EAN.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
print $_SERVER["PHP_SELF"]
Edit parameters.
print
Draft customers invoices.
barcode_encode_genbarcode($code, $encoding)
Encode result of genbarcode command.
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. ...