How to display images in one straight vertical line using php html?

<?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++;
}
	
?>


As you can see, the images is not arranged in organized and tidy form.

Hi @georgelvc94 and welcome to the forum.

I tried running your scrip but got errors mostly because of a missing.$Code39

Can you try these lines at the top of each file.

  declare(strict_types=1); // REMOVE IF NOT PHP7
  ini_set('display_errors',  "1" ); 
  error_reporting(-1); // STRICTER THAN  E_ALL|E_STRICT);

1 Like

I only upload half of the code and it is not a complete code. Hmm. so, i will upload the full code, can you please tell me what is the problem that i facing when i echo the images of my barcode and the barcode is not properly arranged. The php file name is Code39.php.

The joys of using GLOBALS :slight_smile:

The draw() function exits with the last incremented $x value and needs resetting.

The $x value would have been better passed as a parameter to draw($str, $checksum=false, $x) and the $x value would not have been incremented the next time draw() was called. .

Try this:

//=================================================================
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:red; 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;
}

It shows error, "“Parse error: syntax error, unexpected ‘$img’ (T_VARIABLE) in C:\xampp\htdocs\barcode39new\code39.php on line 99"”.

Whoops, this editor has an annoying habit of pasting a couple of times instead of just the once. I must read the manual sometime :slight_smile:

Try removing the $OLD_X from the following:


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

1 Like

This has been a challenging time, and I appreciate you so much. You have no idea how much your help has meant. You have played such an important part and your help won’t be forgotten. Thank you @John_Betong.
Here is the result:

<?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++;
}
	
?>

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.