如何使用php html在一条垂直直线上显示条形码图像?

时间:2017-07-12 08:47:04

标签: php html image echo barcode

如何使用php html在一条垂直直线上显示我的条形码图像。第一张和第二张图片没有以有条理和整洁的形式排列,有人可以查看我的代码吗?

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<form method="GET" action="Code39.php">

</form>
</body>
</html>
<?php
    ini_set('display_errors',1);
    error_reporting(E_ALL|E_STRICT);

    $mcode=1000001; 
    $count=0;
    while ($count<=20){
    $code = isset($_GET['code']) ? $_GET['code'] :$mcode ;
    $barcode = draw($code);

    echo "<div style=\"float:left;\">";
    echo $mcode."<br>";
    echo $barcode."<br>";
    echo "</div>";

    $count++;
    $mcode++;
}
?>

enter image description here

如您所见,条形码图像没有以整齐有序的形式排列。有人可以查看我的代码。

如果你想尝试运行这段代码,这里是完整的代码,php文件名是Code39.php:

<?php
    $Code39 = array(
'0'=>'111221211',
'1'=>'211211112',
'2'=>'112211112',
'3'=>'212211111',
'4'=>'111221112',
'5'=>'211221111',
'6'=>'112221111',
'7'=>'111211212',
'8'=>'211211211',
'9'=>'112211211',
'A'=>'211112112',
'B'=>'112112112',
'C'=>'212112111',
'D'=>'111122112',
'E'=>'211122111',
'F'=>'112122111',
'G'=>'111112212',
'H'=>'211112211',
'I'=>'112112211',
'J'=>'111122211',
'K'=>'211111122',
'L'=>'112111122',
'M'=>'212111121',
'N'=>'111121122',
'O'=>'211121121',
'P'=>'112121121',
'Q'=>'111111222',
'R'=>'211111221',
'S'=>'112111221',
'T'=>'111121221',
'U'=>'221111112',
'V'=>'122111112',
'W'=>'222111111',
'X'=>'121121112',
'Y'=>'221121111',
'Z'=>'122121111',
'-'=>'121111212',
'.'=>'221111211',
' '=>'122111211',
'$'=>'121212111',
'/'=>'121211121',
'+'=>'121112121',
'%'=>'111212121',
'*'=>'121121211');

    $unit='px';//Unit
    $bw=2;//bar width
    $height=50*$bw;// px
    $fs=8*$bw;//Font size
    $yt=45*$bw;
    $dx=3*$bw;
    $x=4*$bw;
    $y=2.5*$bw;
    $bl=35*$bw;
    function checksum( $string )
    {
        $checksum = 0;
        $length   = strlen( $string );
        $charset  = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%';

        for( $i=0; $i < $length; ++$i )
        {
            $checksum += strpos( $charset, $string[$i] );
        }

        return substr( $charset, ($checksum % 43), 1 );
    }
    function draw($str,$checksum=false){
        global $unit,$x,$Code39,$height,$bw;
        $str=strtoupper($str);
        if ($checksum) {
            $str=$str.checksum($str);
        }
        $str='*'.$str.'*';
        $long=(strlen($str)+100)*100;
        $width=$bw*$long;
        $text=str_split($str);
        $img='';
        $img.= "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n";
        $img.= "<svg width='$width$unit' height='$height$unit' version='1.1' xmlns='http://www.w3.org/2000/svg'>\n";


        foreach($text as $char){
            $img.=drawsymbol($char);
        }

        $img.='</svg>';

        return $img;
    }

    function drawsymbol($char){
        global $unit,$Code39,$x,$y,$dx,$bw,$fs,$dx,$yt,$bl;
        $x+=$bw;
        $img='';
        $img.= '<desc>'.htmlspecialchars($char)."</desc>\n";
        $xt=$x+$dx;
        $img.= "<text x='$xt$unit' y='$yt$unit' font-family='Arial' font-size='$fs'>$char</text>\n";
        $val =str_split($Code39[$char]);
        $len=9;
        for ($i=0; $i<$len; $i++){
            $num=(int)$val[$i];
            $w=$bw*$num;
            if(!($i % 2)){
                $img.= "<rect x='$x$unit' y='$y$unit' width='$w$unit' height='$bl$unit' fill='black' stroke-width='0' />\n";
            }
            $x += $w;
        }
        return $img;
    }

?>
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<form method="GET" action="Code39.php">

</form>
</body>
</html>
<?php
    ini_set('display_errors',1);
    error_reporting(E_ALL|E_STRICT);

    $mcode=1000001; 
    $count=0;
    while ($count<=20){
    $code = isset($_GET['code']) ? $_GET['code'] :$mcode ;
    $barcode = draw($code);

    echo "<div style=\"float:left;\">";
    echo $mcode."<br>";
    echo $barcode."<br>";
    echo "</div>";

    $count++;
    $mcode++;
}

?>

2 个答案:

答案 0 :(得分:0)

不是真正的PHP问题,但试试这个:

while ($count<=20){
    $code = isset($_GET['code']) ? $_GET['code'] :$mcode ;
    $barcode = draw($code);

    echo "<div style=\"float:left;\">";
    echo $mcode."<br>";
    echo $barcode."<br>";
    echo "</div>";

    $count++;
    $mcode++;
}

答案 1 :(得分:0)

感谢我的朋友John_Betong提供此代码:

<?php
    $Code39 = array(
'0'=>'111221211',
'1'=>'211211112',
'2'=>'112211112',
'3'=>'212211111',
'4'=>'111221112',
'5'=>'211221111',
'6'=>'112221111',
'7'=>'111211212',
'8'=>'211211211',
'9'=>'112211211',
'A'=>'211112112',
'B'=>'112112112',
'C'=>'212112111',
'D'=>'111122112',
'E'=>'211122111',
'F'=>'112122111',
'G'=>'111112212',
'H'=>'211112211',
'I'=>'112112211',
'J'=>'111122211',
'K'=>'211111122',
'L'=>'112111122',
'M'=>'212111121',
'N'=>'111121122',
'O'=>'211121121',
'P'=>'112121121',
'Q'=>'111111222',
'R'=>'211111221',
'S'=>'112111221',
'T'=>'111121221',
'U'=>'221111112',
'V'=>'122111112',
'W'=>'222111111',
'X'=>'121121112',
'Y'=>'221121111',
'Z'=>'122121111',
'-'=>'121111212',
'.'=>'221111211',
' '=>'122111211',
'$'=>'121212111',
'/'=>'121211121',
'+'=>'121112121',
'%'=>'111212121',
'*'=>'121121211');

    $unit='px';//Unit
    $bw=2;//bar width
    $height=50*$bw;// px
    $fs=8*$bw;//Font size
    $yt=45*$bw;
    $dx=3*$bw;
    $x=4*$bw;
    $y=2.5*$bw;
    $bl=35*$bw;
    function checksum( $string )
    {
        $checksum = 0;
        $length   = strlen( $string );
        $charset  = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%';

        for( $i=0; $i < $length; ++$i )
        {
            $checksum += strpos( $charset, $string[$i] );
        }

        return substr( $charset, ($checksum % 43), 1 );
    }
    function draw($str, $checksum=false)
{
  global $unit, $x, $Code39, $height, $bw;
  $OLD_X = $x;

  $str = strtoupper( (string) $str);
  if ($checksum)
  {
    $str = $str .checksum($str);
  }
  $str   = '*' .$str .'*';
  $long  = (strlen($str)+100)*100;
  $width = $bw*$long;
  $text  = str_split($str);
  $img   = '';
  if(0):
    $img  .= "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n";
  endif;

  $img  .= '<div style="float:left; padding:1em; background-color:white; text-align:left;">' ."\n\n\t";
  $width = '600';
  // $img  .= "<svg width='$width$unit' height='$height$unit' version='1.1' xmlns='http://www.w3.org/2000/svg' >"; // \n
  // viewpath='0 0 $width $height'
  $img  .= "<svg width='$width$unit' height='$height$unit' >"; // \n

  foreach($text as $char)
  {
    $img .= drawsymbol($char);
  }//$OLD_X
  $img .= '</svg>';
  $img .= "\n\n</div><p><br><br><br></p>\n\n";

// ESSENTIAL TO RESET GLOBAL $x value   
   $x = $OLD_X;      

  return $img;
}

    function drawsymbol($char){
        global $unit,$Code39,$x,$y,$dx,$bw,$fs,$dx,$yt,$bl;
        $x+=$bw;
        $img='';
        $img.= '<desc>'.htmlspecialchars($char)."</desc>\n";
        $xt=$x+$dx;
        $img.= "<text x='$xt$unit' y='$yt$unit' font-family='Arial' font-size='$fs'>$char</text>\n";
        $val =str_split($Code39[$char]);
        $len=9;
        for ($i=0; $i<$len; $i++){
            $num=(int)$val[$i];
            $w=$bw*$num;
            if(!($i % 2)){
                $img.= "<rect x='$x$unit' y='$y$unit' width='$w$unit' height='$bl$unit' fill='black' stroke-width='0' />\n";
            }
            $x += $w;
        }
        return $img;
    }

?>
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<form method="GET" action="Code39.php">

</form>
</body>
</html>
<?php
    ini_set('display_errors',1);
    error_reporting(E_ALL|E_STRICT);

    $mcode=1000001; 
    $count=0;
    while ($count<=20){
    $code = isset($_GET['code']) ? $_GET['code'] :$mcode ;
    $barcode = draw($code);

    echo "<div style=\"float:left;\">";
    echo $mcode."<br>";
    echo $barcode;
    //echo "</div>";

    $count++;
    $mcode++;
}

?>

enter image description here