Fix: barcode generation using genbarcode is ok.

This commit is contained in:
Laurent Destailleur 2011-11-28 23:07:19 +01:00
parent 65cdda9978
commit 04dcc86f1a
2 changed files with 225 additions and 223 deletions

View File

@ -16,12 +16,9 @@
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/* 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<BR>\n";
print "<UL>\n";
print "<LI>download gnu-barcode from <A href=\"http://www.gnu.org/software/barcode/\">www.gnu.org/software/barcode/</A>\n";
print "<LI>compile and install them\n";
print "<LI>download genbarcode from <A href=\"http://www.ashberg.de/bar/\">www.ashberg.de/bar/</A>\n";
print "<LI>compile and install them\n";
print "<LI>specify path the genbarcode in barcode module setup\n";
print "</UL>\n";
print "<BR>\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 <http://www.gnu.org/licenses/>.
*
*/
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 (<position>:<font-size>:<character> ...)
* bars : where to place the bars (<space-width><bar-width><space-width><bar-width>...)
* 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<strlen($bars);$i++){
$val=strtolower($bars[$i]);
if ($width){
$xpos+=$val*$scale;
$width=false;
continue;
}
if (preg_match("/[a-z]/", $val)){
/* tall bar */
$val=ord($val)-ord('a')+1;
}
$xpos+=$val*$scale;
$width=true;
}
/* allocate the image */
$total_x=( $xpos )+$space['right']+$space['right'];
$xpos=$space['left'];
if (!function_exists("imagecreate"))
{
print "You don't have the gd2 extension enabled<BR>\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<strlen($bars);$i++){
$val=strtolower($bars[$i]);
if ($width){
$xpos+=$val*$scale;
$width=false;
continue;
}
if (preg_match("/[a-z]/", $val)){
/* tall bar */
$val=ord($val)-ord('a')+1;
$h=$height2;
} else $h=$height;
imagefilledrectangle($im, $xpos, $space['top'], $xpos+($val*$scale)-1, $h, $col_bar);
$xpos+=$val*$scale;
$width=true;
}
/* write out the text */
global $_SERVER;
$chars=explode(" ", $text);
reset($chars);
while (list($n, $v)=each($chars)){
if (trim($v)){
$inf=explode(":", $v);
$fontsize=$scale*($inf[1]/1.8);
$fontheight=$total_y-($fontsize/2.7)+2;
@imagettftext($im, $fontsize, 0, $space['left']+($scale*$inf[0])+2,
$fontheight, $col_text, $font_loc, $inf[2]);
}
}
// DOLCHANGE LDR
global $filebarcode;
/* output the image */
$mode=strtolower($mode);
if ($mode=='jpg' || $mode=='jpeg'){
header("Content-Type: image/jpeg; name=\"barcode.jpg\"");
imagejpeg($im);
} else if ($mode=='gif'){
header("Content-Type: image/gif; name=\"barcode.gif\"");
imagegif($im);
// Begin DOLCHANGE LDR
} else if (! empty($filebarcode))
{
imagepng($im,$filebarcode);
// End DOLCHANGE LDR
} else {
header("Content-Type: image/png; name=\"barcode.png\"");
imagepng($im);
}
}
/**
* barcode_encode_genbarcode(code, encoding)
* encodes $code with $encoding using genbarcode
@ -291,17 +259,21 @@ function barcode_outimage($text, $bars, $scale = 1, $mode = "png", $total_y = 0,
function barcode_encode_genbarcode($code,$encoding)
{
global $genbarcode_loc;
/* delete EAN-13 checksum */
// Clean parameters
if (preg_match("/^ean$/i", $encoding) && strlen($code)==13) $code=substr($code,0,12);
if (!$encoding) $encoding="ANY";
$encoding=preg_replace("/[\\\|]/", "_", $encoding);
$code=preg_replace("/[\\\|]/", "_", $code);
$cmd=$genbarcode_loc." \""
.str_replace("\"", "\\\"",$code)."\" \""
.str_replace("\"", "\\\"",strtoupper($encoding))."\" 2>&1";
//print "'$cmd'<BR>\n";
$fp=popen($cmd, "r");
$command=escapeshellarg($genbarcode_loc);
$paramclear=" \"".str_replace("\"", "\\\"",$code)."\" \"".str_replace("\"", "\\\"",strtoupper($encoding))."\"";
$fullcommandclear=$command." ".$paramclear." 2>&1";
//print $fullcommandclear."<br>\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 (<position>:<font-size>:<character> ...)
* @param string $bars where to place the bars (<space-width><bar-width><space-width><bar-width>...)
* @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<br>";
//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<strlen($bars);$i++)
{
/* use built-in EAN-Encoder */
dol_syslog("phpbarcode.php::barcode_encode Use barcode_encode_ean");
$bars=barcode_encode_ean($code, $encoding);
$val=strtolower($bars[$i]);
if ($width)
{
$xpos+=$val*$scale;
$width=false;
continue;
}
if (preg_match("/[a-z]/", $val))
{
/* tall bar */
$val=ord($val)-ord('a')+1;
}
$xpos+=$val*$scale;
$width=true;
}
else if (file_exists($genbarcode_loc))
/* allocate the image */
$total_x=( $xpos )+$space['right']+$space['right'];
$xpos=$space['left'];
if (! function_exists("imagecreate"))
{
/* use genbarcode */
dol_syslog("phpbarcode.php::barcode_encode Use genbarcode ".$genbarcode_loc." code=".$code." encoding=".$encoding);
$bars=barcode_encode_genbarcode($code, $encoding);
print "You don't have the gd2 extension enabled<br>\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<strlen($bars);$i++)
{
$val=strtolower($bars[$i]);
if ($width)
{
$xpos+=$val*$scale;
$width=false;
continue;
}
if (preg_match("/[a-z]/", $val))
{
/* tall bar */
$val=ord($val)-ord('a')+1;
$h=$height2;
} else $h=$height;
imagefilledrectangle($im, $xpos, $space['top'], $xpos+($val*$scale)-1, $h, $col_bar);
$xpos+=$val*$scale;
$width=true;
}
$chars=explode(" ", $text);
reset($chars);
while (list($n, $v)=each($chars))
{
if (trim($v))
{
$inf=explode(":", $v);
$fontsize=$scale*($inf[1]/1.8);
$fontheight=$total_y-($fontsize/2.7)+2;
imagettftext($im, $fontsize, 0, $space['left']+($scale*$inf[0])+2, $fontheight, $col_text, $font_loc, $inf[2]);
}
}
/* output the image */
$mode=strtolower($mode);
if ($mode=='jpg' || $mode=='jpeg')
{
header("Content-Type: image/jpeg; name=\"barcode.jpg\"");
imagejpeg($im);
}
else if ($mode=='gif')
{
header("Content-Type: image/gif; name=\"barcode.gif\"");
imagegif($im);
}
else if (! empty($filebarcode)) // To wxrite into afile onto disk
{
imagepng($im,$filebarcode);
}
else
{
print "barcode_encode needs an external programm for encodings other then EAN/ISBN<BR>\n";
print "<UL>\n";
print "<LI>download gnu-barcode from <A href=\"http://www.gnu.org/software/barcode/\">www.gnu.org/software/barcode/</A>\n";
print "<LI>compile and install them\n";
print "<LI>download genbarcode from <A href=\"http://www.ashberg.de/bar/\">www.ashberg.de/bar/</A>\n";
print "<LI>compile and install them\n";
print "<LI>specify path the genbarcode in barcode module setup\n";
print "</UL>\n";
print "<BR>\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;
}
?>

View File

@ -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;