From 04dcc86f1ae79edba171ff4fe763cb829596604d Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Mon, 28 Nov 2011 23:07:19 +0100 Subject: [PATCH] Fix: barcode generation using genbarcode is ok. --- htdocs/core/lib/phpbarcode.php | 443 +++++++++--------- .../modules/barcode/phpbarcode.modules.php | 5 +- 2 files changed, 225 insertions(+), 223 deletions(-) diff --git a/htdocs/core/lib/phpbarcode.php b/htdocs/core/lib/phpbarcode.php index 3f4a99dca1c..d1238d36bf0 100644 --- a/htdocs/core/lib/phpbarcode.php +++ b/htdocs/core/lib/phpbarcode.php @@ -16,12 +16,9 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see . - * */ -/* CONFIGURATION */ - /* ******************************************************************** */ /* COLORS */ /* ******************************************************************** */ @@ -48,13 +45,112 @@ if (empty($font_loc)) die('DOL_DEFAULT_TTF_BOLD must de defined with full path t * genbarcode is needed to render encodings other than EAN-12/EAN-13/ISBN */ -// DOL_CHANGE LDR if (defined('PHP-BARCODE_PATH_COMMAND')) $genbarcode_loc=constant('PHP-BARCODE_PATH_COMMAND'); else $genbarcode_loc = $conf->global->GENBARCODE_LOCATION; //dol_syslog("genbarcode_loc=".$genbarcode_loc." - env_windows=".$_SERVER['WINDIR']); -/* CONFIGURATION ENDS HERE */ + + +/** + * barcode_print(code [, encoding [, scale [, mode ]]] ); + * + * encodes and prints a barcode + * + * return: + * array[encoding] : the encoding which has been used + * array[bars] : the bars + * array[text] : text-positioning info + */ +function barcode_print($code, $encoding="ANY", $scale = 2 ,$mode = "png") +{ + // DOLCHANGE LDR Add log + dol_syslog("phpbarcode.php::barcode_print $code $encoding $scale $mode"); + + $bars=barcode_encode($code,$encoding); + + if (! $bars) + { + // DOLCHANGE LDR Return error message instead of array + $error='Bad Value '.$code.' for encoding '.$encoding; + dol_syslog('phpbarcode.php::barcode_print '.$error, LOG_ERR); + return $error; + } + if (! $mode) $mode="png"; + //if (preg_match("/^(text|txt|plain)$/i",$mode)) print barcode_outtext($bars['text'],$bars['bars']); + //elseif (preg_match("/^(html|htm)$/i",$mode)) print barcode_outhtml($bars['text'],$bars['bars'], $scale,0, 0); + //else + barcode_outimage($bars['text'], $bars['bars'], $scale, $mode); + return $bars; +} + +/** + * barcode_encode(code, encoding) + * encodes $code with $encoding using genbarcode OR built-in encoder + * if you don't have genbarcode only EAN-13/ISBN is possible + * + * You can use the following encodings (when you have genbarcode): + * ANY choose best-fit (default) + * EAN 8 or 13 EAN-Code + * UPC 12-digit EAN + * ISBN isbn numbers (still EAN-13) + * 39 code 39 + * 128 code 128 (a,b,c: autoselection) + * 128C code 128 (compact form for digits) + * 128B code 128, full printable ascii + * I25 interleaved 2 of 5 (only digits) + * 128RAW Raw code 128 (by Leonid A. Broukhis) + * CBR Codabar (by Leonid A. Broukhis) + * MSI MSI (by Leonid A. Broukhis) + * PLS Plessey (by Leonid A. Broukhis) + * + * return: + * array[encoding] : the encoding which has been used + * array[bars] : the bars + * array[text] : text-positioning info + */ +function barcode_encode($code,$encoding) +{ + global $genbarcode_loc; + + if ( + ((preg_match("/^ean$/i", $encoding) + && ( strlen($code)==12 || strlen($code)==13))) + + || (($encoding) && (preg_match("/^isbn$/i", $encoding)) + && (( strlen($code)==9 || strlen($code)==10) || + (((preg_match("/^978/", $code) && strlen($code)==12) || + (strlen($code)==13))))) + + || (( !isset($encoding) || !$encoding || (preg_match("/^ANY$/i", $encoding) )) + && (preg_match("/^[0-9]{12,13}$/", $code))) + ) + { + /* use built-in EAN-Encoder */ + dol_syslog("phpbarcode.php::barcode_encode Use barcode_encode_ean"); + $bars=barcode_encode_ean($code, $encoding); + } + else if (file_exists($genbarcode_loc)) + { + /* use genbarcode */ + dol_syslog("phpbarcode.php::barcode_encode Use genbarcode ".$genbarcode_loc." code=".$code." encoding=".$encoding); + $bars=barcode_encode_genbarcode($code, $encoding); + } + else + { + print "barcode_encode needs an external programm for encodings other then EAN/ISBN
\n"; + print "\n"; + print "
\n"; + return false; + } + return $bars; +} /** @@ -77,7 +173,6 @@ else $genbarcode_loc = $conf->global->GENBARCODE_LOCATION; * * You should have received a copy of the GNU General Public License * along with this program. If not, see . - * */ function barcode_gen_ean_sum($ean) { @@ -152,133 +247,6 @@ function barcode_encode_ean($ean, $encoding = "EAN-13") ); } - -/** - * barcode_outimage(text, bars [, scale [, mode [, total_y [, space ]]]] ) - * - * Outputs an image using libgd - * - * text : the text-line (:: ...) - * bars : where to place the bars (...) - * scale : scale factor ( 1 < scale < unlimited (scale 50 will produce - * 5400x300 pixels when - * using EAN-13!!!)) - * mode : png,gif,jpg, depending on libgd ! (default='png') - * total_y: the total height of the image ( default: scale * 60 ) - * space : space - * default: - * $space[top] = 2 * $scale; - * $space[bottom]= 2 * $scale; - * $space[left] = 2 * $scale; - * $space[right] = 2 * $scale; - */ -function barcode_outimage($text, $bars, $scale = 1, $mode = "png", $total_y = 0, $space = '') -{ - global $bar_color, $bg_color, $text_color; - global $font_loc; - - //var_dump($text); - //var_dump($bars); - //var_dump($font_loc); - - /* set defaults */ - if ($scale<1) $scale=2; - $total_y=(int)($total_y); - if ($total_y<1) $total_y=(int)$scale * 60; - if (!$space) - $space=array('top'=>2*$scale,'bottom'=>2*$scale,'left'=>2*$scale,'right'=>2*$scale); - - /* count total width */ - $xpos=0; - $width=true; - for ($i=0;$i\n"; - return ""; - } - $im=imagecreate($total_x, $total_y); - /* create two images */ - $col_bg=ImageColorAllocate($im,$bg_color[0],$bg_color[1],$bg_color[2]); - $col_bar=ImageColorAllocate($im,$bar_color[0],$bar_color[1],$bar_color[2]); - $col_text=ImageColorAllocate($im,$text_color[0],$text_color[1],$text_color[2]); - $height=round($total_y-($scale*10)); - $height2=round($total_y-$space['bottom']); - - /* paint the bars */ - $width=true; - for ($i=0;$i&1"; - //print "'$cmd'
\n"; - $fp=popen($cmd, "r"); + $command=escapeshellarg($genbarcode_loc); + $paramclear=" \"".str_replace("\"", "\\\"",$code)."\" \"".str_replace("\"", "\\\"",strtoupper($encoding))."\""; + + $fullcommandclear=$command." ".$paramclear." 2>&1"; + //print $fullcommandclear."
\n"; + + dol_syslog("Run command ".$fullcommandclear); + $fp=popen($fullcommandclear, "r"); if ($fp) { $bars=fgets($fp, 1024); @@ -311,7 +283,7 @@ function barcode_encode_genbarcode($code,$encoding) } else { - dol_syslog("phpbarcode::barcode_encode_genbarcode failed to run popen ".$cmd, LOG_ERR); + dol_syslog("phpbarcode::barcode_encode_genbarcode failed to run popen ".$fullcommandclear, LOG_ERR); return false; } //var_dump($bars); @@ -328,102 +300,129 @@ function barcode_encode_genbarcode($code,$encoding) } /** - * barcode_encode(code, encoding) - * encodes $code with $encoding using genbarcode OR built-in encoder - * if you don't have genbarcode only EAN-13/ISBN is possible + * Output image onto standard output, or onto disk if global filebarcode is defined * - * You can use the following encodings (when you have genbarcode): - * ANY choose best-fit (default) - * EAN 8 or 13 EAN-Code - * UPC 12-digit EAN - * ISBN isbn numbers (still EAN-13) - * 39 code 39 - * 128 code 128 (a,b,c: autoselection) - * 128C code 128 (compact form for digits) - * 128B code 128, full printable ascii - * I25 interleaved 2 of 5 (only digits) - * 128RAW Raw code 128 (by Leonid A. Broukhis) - * CBR Codabar (by Leonid A. Broukhis) - * MSI MSI (by Leonid A. Broukhis) - * PLS Plessey (by Leonid A. Broukhis) - * - * return: - * array[encoding] : the encoding which has been used - * array[bars] : the bars - * array[text] : text-positioning info + * @param string $text the text-line (:: ...) + * @param string $bars where to place the bars (...) + * @param int $scale scale factor ( 1 < scale < unlimited (scale 50 will produce + * 5400x300 pixels when + * using EAN-13!!!)) + * @param string $mode png,gif,jpg (default='png') + * @param int $total_y the total height of the image ( default: scale * 60 ) + * @param array $space default: $space[top] = 2 * $scale; $space[bottom]= 2 * $scale; $space[left] = 2 * $scale; $space[right] = 2 * $scale; + * @return void */ -function barcode_encode($code,$encoding) +function barcode_outimage($text, $bars, $scale = 1, $mode = "png", $total_y = 0, $space = '') { - global $genbarcode_loc; + global $bar_color, $bg_color, $text_color; + global $font_loc, $filebarcode; - if ( - ((preg_match("/^ean$/i", $encoding) - && ( strlen($code)==12 || strlen($code)==13))) + //print "$text, $bars, $scale, $mode, $total_y, $space, $font_loc, $filebarcode
"; + //var_dump($text); + //var_dump($bars); + //var_dump($font_loc); - || (($encoding) && (preg_match("/^isbn$/i", $encoding)) - && (( strlen($code)==9 || strlen($code)==10) || - (((preg_match("/^978/", $code) && strlen($code)==12) || - (strlen($code)==13))))) + /* set defaults */ + if ($scale<1) $scale=2; + $total_y=(int)($total_y); + if ($total_y<1) $total_y=(int)$scale * 60; + if (!$space) + $space=array('top'=>2*$scale,'bottom'=>2*$scale,'left'=>2*$scale,'right'=>2*$scale); - || (( !isset($encoding) || !$encoding || (preg_match("/^ANY$/i", $encoding) )) - && (preg_match("/^[0-9]{12,13}$/", $code))) - ) + /* count total width */ + $xpos=0; + $width=true; + for ($i=0;$i\n"; + return ""; + } + $im=imagecreate($total_x, $total_y); + /* create two images */ + $col_bg=ImageColorAllocate($im,$bg_color[0],$bg_color[1],$bg_color[2]); + $col_bar=ImageColorAllocate($im,$bar_color[0],$bar_color[1],$bar_color[2]); + $col_text=ImageColorAllocate($im,$text_color[0],$text_color[1],$text_color[2]); + $height=round($total_y-($scale*10)); + $height2=round($total_y-$space['bottom']); + + /* paint the bars */ + $width=true; + for ($i=0;$i\n"; - print "
    \n"; - print "
  • download gnu-barcode from www.gnu.org/software/barcode/\n"; - print "
  • compile and install them\n"; - print "
  • download genbarcode from www.ashberg.de/bar/\n"; - print "
  • compile and install them\n"; - print "
  • specify path the genbarcode in barcode module setup\n"; - print "
\n"; - print "
\n"; - return false; + header("Content-Type: image/png; name=\"barcode.png\""); + imagepng($im); } - return $bars; } -/** - * barcode_print(code [, encoding [, scale [, mode ]]] ); - * - * encodes and prints a barcode - * - * return: - * array[encoding] : the encoding which has been used - * array[bars] : the bars - * array[text] : text-positioning info - */ -function barcode_print($code, $encoding="ANY", $scale = 2 ,$mode = "png") -{ - // DOLCHANGE LDR Add log - dol_syslog("phpbarcode.php::barcode_print $code $encoding $scale $mode"); - - $bars=barcode_encode($code,$encoding); - if (!$bars) - { - // DOLCHANGE LDR Return error message instead of array - $error='Bad Value '.$code.' for encoding '.$encoding; - dol_syslog('phpbarcode.php::barcode_print '.$error, LOG_ERR); - return $error; - } - if (!$mode) $mode="png"; - //if (preg_match("/^(text|txt|plain)$/i",$mode)) print barcode_outtext($bars['text'],$bars['bars']); - //elseif (preg_match("/^(html|htm)$/i",$mode)) print barcode_outhtml($bars['text'],$bars['bars'], $scale,0, 0); - //else - barcode_outimage($bars['text'],$bars['bars'],$scale, $mode); - return $bars; -} ?> diff --git a/htdocs/core/modules/barcode/phpbarcode.modules.php b/htdocs/core/modules/barcode/phpbarcode.modules.php index 768f34948a2..ce9034c9ec5 100644 --- a/htdocs/core/modules/barcode/phpbarcode.modules.php +++ b/htdocs/core/modules/barcode/phpbarcode.modules.php @@ -23,7 +23,9 @@ * \brief Fichier contenant la classe du modele de generation code barre phpbarcode */ -require_once(DOL_DOCUMENT_ROOT ."/core/modules/barcode/modules_barcode.php"); +require_once(DOL_DOCUMENT_ROOT."/core/modules/barcode/modules_barcode.php"); +require_once(DOL_DOCUMENT_ROOT."/core/lib/phpbarcode.php"); // This is to include def like $genbarcode_loc and $font_loc + /** \class modPhpbarcode * \brief Classe du modele de numerotation de generation code barre phpbarcode @@ -79,6 +81,7 @@ class modPhpbarcode extends ModeleBarCode function encodingIsSupported($encoding) { global $genbarcode_loc; + //print 'genbarcode_loc='.$genbarcode_loc.' encoding='.$encoding;exit; $supported=0; if ($encoding == 'EAN13') $supported=1;