如何使用php html显示多个条码图像?

时间:2017-07-12 02:07:07

标签: php html image barcode

我想在文本框中插入数字时显示多个条形码图像。例如,如果我写12345,它将显示第一张图像。然后,当我插入另一个数字456789时,它将在第一个图像下方显示第二个图像。但我的问题是第二张图像与第一张图片相同。有人可以帮我检查我的代码。我的php文件名是" code39.php"。

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

    Code:<br><input type="text" name="code"><br>
    <input type="submit" name="submit"><br><br>

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

    $code = isset($_GET['code']) ? $_GET['code'] :'code' ;

    $barcode = draw($code);
    echo $barcode."<br>";
    echo $barcode."<br>";

?>

我给了一半代码。这是结果: enter image description here

1 个答案:

答案 0 :(得分:0)

我使用此代码可以成功。

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


        $unit1='px';//Unit
        $bw1=2;//bar width
        $height1=50*$bw1;// px
        $fs1=8*$bw1;//Font size
        $yt1=45*$bw1;
        $dx1=3*$bw1;
        $x1=4*$bw1;
        $y1=2.5*$bw1;
        $bl1=35*$bw1;


        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)+3)*12;
            $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;
        }






        function checksum1( $string1 )
        {
            $checksum1 = 0;
            $length1   = strlen( $string1 );
            $charset1  = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%';

            for( $i=0; $i < $length1; ++$i )
            {
                $checksum1 += strpos( $charset1, $string1[$i] );
            }

            return substr( $charset1, ($checksum1 % 43), 1 );
        }
        function draw1($str1,$checksum1=false){
            global $unit1,$x1,$Code39,$height1,$bw1;
            $str1=strtoupper($str1);
            if ($checksum1) {
                $str1=$str1.checksum1($str1);
            }
            $str1='*'.$str1.'*';
            $long1=(strlen($str1)+3)*12;
            $width1=$bw1*$long1;
            $text1=str_split($str1);
            $img1='';
            $img1.= "<?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";
            $img1.= "<svg width='$width1$unit1' height='$height1$unit1' version='1.1' xmlns='http://www.w3.org/2000/svg'>\n";

            foreach($text1 as $char1){
                $img1.=drawsymbol1($char1);
            }

            $img1.='</svg>';

            return $img1;
        }

        function drawsymbol1($char1){
            global $unit1,$Code39,$x1,$y1,$dx1,$bw1,$fs1,$dx1,$yt1,$bl1;
            $x1+=$bw1;
            $img1='';
            $img1.= '<desc>'.htmlspecialchars($char1)."</desc>\n";
            $xt1=$x1+$dx1;
            $img1.= "<text x='$xt1$unit1' y='$yt1$unit1' font-family='Arial' font-size='$fs1'>$char1</text>\n";
            $val1 =str_split($Code39[$char1]);
            $len1=9;
            for ($i=0; $i<$len1; $i++){
                $num1=(int)$val1[$i];
                $w1=$bw1*$num1;
                if(!($i % 2)){
                    $img1.= "<rect x='$x1$unit1' y='$y1$unit1' width='$w1$unit1' height='$bl1$unit1' fill='black' stroke-width='0' />\n";
                }
                $x1 += $w1;
            }
            return $img1;
        }
    ?>
    <!DOCTYPE html>
    <html>
    <head>
        <title></title>
    </head>
    <body>
    <form method="GET" action="code39exam.php">

        Code 1:&nbsp;<input type="text" name="code"><br><br>


        Code 2:&nbsp;<input type="text" name="code1"><br><br>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <input type="submit" name="submit"><br><br>

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

        $code = isset($_GET['code']) ? $_GET['code'] :'code' ;

        $barcode = draw($code);
        echo $barcode."<br>";


        $code1 = isset($_GET['code1']) ? $_GET['code1'] :'code' ;

        $barcode1 = draw1($code1);
        echo $barcode1."<br>";

    ?>

enter image description here