1

How can i display my barcode images in one vertical straight line using php html. The first and second images is not arranged in organized and tidy form, can someone check my code?

<!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

As you can see, the barcode images is not arranged in organized and tidy form. Can someone check my code.

If you want to try run this code, here is the full code, php file name is 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 Answers2

0

Not really a PHP-Question, but try this:

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++;
}
trpx
  • 168
  • 7
  • The result is still the same @trpx, the images still like the one that i post. Do you have other method? –  Jul 13 '17 at 00:41
  • Thank you for code also, your code help me too. Thank you :) –  Jul 13 '17 at 07:01
0

Thank you to my friend John_Betong for this code:

<?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