PHP SVG功能Code128无法使用激光扫描仪

时间:2016-05-25 16:32:17

标签: php svg barcode

好的,所以我在互联网上找到了这个PHP application,并打算在没有CSS的HTML页面上使用它(因为我将使用谷歌云打印,它无法读取本地CSS,无法发送css也是)和SVG输出是我的解决方案。当我测试那些功能(Code128,Code39,EAN13,EAN8和UPCA)时,我注意到我的激光扫描仪唯一能够读取的是Code128和EAN8,但Code128对我来说是最重要的。

检查代码我能够注意到Code128.php中数组的最后一项有一个额外的数字(7位),通过做一些研究,link证明我是正确的(两者都是相同的顺序只是检查最后一项),但修复它是不够的。

我该如何做到这一点?

Code128.php(功能)

<?php
//+32=ASCII Total: 106.                   
$code128=array('212222', '222122', '222221', '121223', '121322', '131222', '122213', '122312', '132212', '221213', '221312', '231212', '112232', '122132', '122231', '113222', '123122', '123221', '223211', '221132', '221231', '213212', '223112',
    '312131', '311222', '321122', '321221', '312212', '322112', '322211', '212123', '212321', '232121', '111323', '131123', '131321', '112313', '132113', '132311', '211313', '231113', '231311', 
    '112133', '112331', '132131', '113123', '113321', '133121', '313121', '211331', '231131', '213113', '213311', '213131', '311123', '311321', '331121', '312113', '312311', '332111', '314111', '221411', '431111', '111224', '111422', '121124', 
    '121421', '141122', '141221', '112214', '112412', '122114', '122411', '142112', '142211', '241211', '221114', '413111', '241112', '134111', '111242', '121142', '121241', '114212', '124112', 
    '124211', '411212', '421112', '421211', '212141', '214121', '412121', '111143', '111341', '131141', '114113', '114311', '411113', '411311', '113141', '114131', '311141', '411131', '211412', '211214', '211232', '233111', '21');

//Congugure
$unit='px';//Unit
$bw=1.5;//bar width
$height=50*$bw;
$fs=8*$bw;//Font size
$yt=45*$bw;
$dx=2*$bw;
$x=5*$bw;
$y=2.5*$bw;
$bl=35*$bw;

function checksum($str){
    $cstr=str_split($str);
    $count=count($cstr);
    $sum=ord($cstr[0])-32;
    for ($i=0; $i<$count;$i++){
        $sum+=(ord($cstr[$i])-32)*$i;
    }
    $sum=$sum % 103;
    $sum+=32;
    return chr($sum);
}

function draw($text,$type='B',$check=false){
    global $unit,$x,$code128,$height,$bw,$fs,$dx,$yt;
    $type=preg_replace('/\W/','',$type);
    $type=substr($type,0,1);
    $type=strtoupper($type);
    $clong=(strlen($text)+4)*11;
    $width=$bw*$clong;
    switch($type){
        case'A':
        $start=$code128[103];
        break;
        case'B':
        $start=$code128[104];
        break;
        case'C':
        $start=$code128[105];
        break;
        default:
        $start=$code128[104];
        break;

    }
    $ctext=$start.$text;
    if ($check) {
    $text.=checksum($ctext);
    }
    $text=str_split($text);
    $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";
    //Draw Guard bar.
    $img.= "<desc>First Guard</desc>\n";
    //StartCode
    $img.=drawchar($start);

    //Begin Content
    foreach($text as $char){
        $index=ord($char)-32;
        $xt=$x+$dx;
        $img.= "<desc>$char</desc>\n";
        $img.=drawchar($code128[$index]);
        $img.= "<text x='$xt$unit' y='$yt$unit' font-family='Arial' font-size='$fs'>$char</text>\n";
    }
    //End guard bar.
    $img.=drawchar($code128[106]);
    $img.='</svg>';
    return $img;
}
function drawchar($char){
    global $unit,$x,$y,$bl,$height,$bw,$fs,$bw;
    $val=str_split($char);
    $img='';
    $j=0;
    foreach($val as $bar){
        $num=(int)$bar;
        $w=$bw*$num;
        if(!($j % 2)){
            $img.= "<rect x='$x$unit' y='$y$unit' width='$w$unit' height='$bl$unit' fill='black' stroke-width='0' />\n";
        }
        $x += $w;
        $j++;
    }
    return $img;
}
?>

请求(code128example.php)

<?php
ini_set('display_errors',1);
error_reporting(E_ALL|E_STRICT);
include 'Code128.php';
$code = isset($_GET['code']) ? $_GET['code'] :'CODE128'; 
header("Content-type: image/svg+xml");
echo draw($code);
?>

0 个答案:

没有答案
相关问题