PHP生成条形码 实践篇

之前的一篇博客《用PHP制作条形码 原理篇》 中介绍了,EAN-13条形码的编码原理,现在我们来看看用PHP如何实现自动作成条形码。

准备

我们需要一台运行了PHP的Web服务器,同时PHP需要有GD扩展,并且GD库至少需要支持JPEG格式,关于如何建立一台Web服务器并且运行PHP,已经GD库的安装,将在另一篇博客中再作介绍。

开始

假定我们有如下条码 750103131130,将其制作出一张条形码图片。

演示页面
程序下载

1. 根据上次我们说到的第一张表,来确定编码规范:
条码第一位 左资料码规范 右资料码规范
0 AAAAAA CCCCCC
1 AABABB CCCCCC
2 AABBAB CCCCCC
3 AABBBA CCCCCC
4 ABAABB CCCCCC
5 ABBAAB CCCCCC
6 ABBBAA CCCCCC
7 ABABAB CCCCCC
8 ABABBA CCCCCC
9 ABBABA CCCCCC
// 定义起始付
$start = '101';
// 定义中止符
$end = '101';
// 定义中间分隔符
$center = '01010';
// 定义左资料码
$Guide = array(0=>'AAAAAA','AABABB','AABBAB','AABBBA','ABAABB','ABBAAB','ABBBAA','ABABAB','ABABBA','ABBABA');

2. 根据上次我们说到的第二张表,来定义每一位编码值:
数字 A 编码 B 编码 C 编码
0 0001101 0100111 1110010
1 0011001 0110011 1100110
2 0010011 0011011 1101100
3 0111101 0100001 1000010
4 0100011 0011101 1011100
5 0110001 0111001 1001110
6 0101111 0000101 1010000
7 0111011 0010001 1000100
8 0110111 0001001 1001000
9 0001011 0010111 1110100
// 定义左侧码,分为“A”、“B”两种
$Lencode = array("A" => array('0001101','0011001','0010011','0111101','0100011','0110001','0101111','0111011','0110111','0001011'),
"B" => array('0100111','0110011','0011011','0100001','0011101','0111001','0000101','0010001','0001001','0010111'));
// 定义右侧码,统一为“C”编码
$Rencode = array('1110010','1100110','1101100','1000010','1011100','1001110','1010000','1000100','1001000','1110100');
3. 计算校验位
// 我们的条码
$code = 750103131130;

// 计算校验位
$lsum = 0;
$rsum = 0;
for($i=1; $i<=strlen($code); $i++) {
    if($i % 2) {
        $lsum += (int)$code[$i-1];
    }else{
        $rsum += (int)$code[$i-1];
    }
}
$tsum = $lsum + $rsum * 3;
$chkdig = 10 - ($tsum % 10);
if ($chkdig == 10) $chkdig = 0;
$code .= $chkdig;
4. 用程序来为每一位编码
// 编码起始符
$barcode = $Lstart;

// 编码左资料位
for($i=1; $i<=6; $i++) {
    $barcode .= $Lencode[$Guide[$code[0]][($i-1)]][$code[$i]];
}

// 编码中间分隔符
$barcode .= $center;

// 编码右资料位
for($i=7; $i<13; $i++) {
    $barcode .= $Rencode[$code[($i)]];
}

// 编码中止符
$barcode .= $ends;
5. 用GD将图形输出
// 定义每个条码单元的宽度和高度,单位是像素
$width = 2;
$height = 40;

// 定义起始符、中止符、中间分隔符的高度增量
$increment = 10;

// 创建方形(×95是因为整个条码共95个单元,+60和+30是给条码图片周围留空白边框)
$img = ImageCreate($width*95+60,$height+30);  // 目前这个方形是透明的

// “1”的颜色(黑)与“0”的颜色(白)
$fg = ImageColorAllocate($img, 0, 0, 0);
$bg = ImageColorAllocate($img, 255, 255, 255);

// 以“0”的颜色(白色),填充整个方形
ImageFilledRectangle($img, 0, 0, $width*95+60, $height+30, $bg);

// 循环编码后的每一个单元,输出条码图形
for ($x=0; $x=92)为中止符,($x>=45 && $x<50)为中间分隔符
    // 这3个需要将高度增加
    if (($x<4) || ($x<=45 && $x>50) || ($x>=92)) {
        $increment = 10;
    } else {
        $increment = 0;
    }
    // 当编码值为“1”时,输出黑色;当编码值为“0”时,输出白色
    if ($barcode[$x] == '1') {
        $color = $fg;
    } else {
        $color = $bg;
    }
    ImageFilledRectangle($img, ($x*$width)+30,5,($x+1)*$width+29,$height+$increment,$color);
}
6. 在条码下方加入可供人工识别的数字
ImageString($img, 5, 20, $height+5, $code[0], $fg);
for ($x=0; $x<6; $x++) {
    // 左侧识别码
    ImageString($img, 5, $width*(8+$x*6)+30, $height+5, $code[$x+1], $fg);
    // 右侧识别码
    ImageString($img, 5, $width*(53+$x*6)+30, $height+5, $code[$x+7], $fg);
}
7. 将图片显示出来
header("Content-Type: image/jpeg");
ImageJPEG($img, "", 100);
Tagged , , .Bookmark the permalink.

4 Responses to PHP生成条形码 实践篇

  1. Pingback: 用PHP制作条形码 - 条形码原理篇 | XiaoF's an Other Blog

  2. huaidong says:

    老大,你介绍的真详细呀

  3. joe chan says:

    很感激這麼詳細的解釋!但請問是否只可每次列印一個條碼出來?

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>