会员控制台 会员登陆 会员注册 最后更新 高级搜索 会员投稿 退出登陆
设为首页
收藏本站
留言给我
  首页 | 操作系统类 | 程序设计类 | 数据库类 | 应用服务类 | 网络及硬件类 | 其他技术 | GBunix论坛
操作系统类
程序设计类
数据库类
应用服务类
网络及硬件类
其他技术
Unix中文 / 程序设计类 / 其他程序 / 用php生成EAN_13标准的条形码
用php生成EAN_13标准的条形码
2004-10-31    laokan    www.chinaunix.net    点击:
用php生成EAN_13标准的条形码
  这连天看见有人法帖讨论如很用php生成条形码,所以就用了点时间学了学EAN_13标准.EAN_13标准是广泛使用的条码编码标准,主要运用在商品流通方面,比如我们看到的商品上的条码就是这种编码标准,还有图书上的编码标准也是这种编码标准.

  下面的就是我的一段代码生成的是EAN_13标准的条码,需要php+gd 环境

<?
function EAN_13($code) {
  //一个单元的宽度
  $lw = 2;
  //条码高
  $hi = 100;
  // the guide code is no coding,is used to show the left part coding type//
  // Array guide is used to record the EAN_13 is left part coding type//
  $Guide = array(1=>'AAAAAA','AABABB','AABBAB','ABAABB','ABBAAB','ABBBAA','ABABAB','ABABBA','ABBABA');
  $Lstart ='101';
  $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'));
  $Rencode = array('1110010','1100110','1101100','1000010','1011100',
                   '1001110','1010000','1000100','1001000','1110100');         
               
  $center = '01010';
 
  $ends = '101';
  if ( strlen($code) != 13 )
   { die("UPC-A Must be 13 digits."); }
$lsum =0;
$rsum =0;
  for($i=0;$i<(strlen($code)-1);$i++)
  {
    if($i % 2)
   {
    // $odd += $ncode[$x]
     $lsum +=(int)$code[$i];
    }else{
     $rsum +=(int)$code[$i];
    }
    
  }   
  $tsum = $lsum*3 + $rsum;
    if($code[12] != (10-($tsum % 10)))
   {
      die("the code is bad!");
    }
   
// echo $Guide[$code[0]];
  $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;
 
    $img = ImageCreate($lw*95+60,$hi+30);
  $fg = ImageColorAllocate($img, 0, 0, 0);
  $bg = ImageColorAllocate($img, 255, 255, 255);
  ImageFilledRectangle($img, 0, 0, $lw*95+60, $hi+30, $bg);
  $shift=10;
  for ($x=0;$x<strlen($barcode);$x++) {
    if (($x<4) || ($x>=45 && $x<50) || ($x >=92))
     {
       $sh=10;
     } else {
       $sh=0;
     }
    if ($barcode[$x] == '1')
   {
     $color = $fg;
    } else {
     $color = $bg;
   }
    ImageFilledRectangle($img, ($x*$lw)+30,5,($x+1)*$lw+29,$hi+5+$sh,$color);
  }
  /* Add the Human Readable Label */
  ImageString($img,5,20,$hi+5,$code[0],$fg);
  for ($x=0;$x<6;$x++) {
    ImageString($img,5,$lw*(8+$x*6)+30,$hi+5,$code[$x+1],$fg);
    ImageString($img,5,$lw*(53+$x*6)+30,$hi+5,$code[$x+7],$fg);
  }
// ImageString($img,4,$lw*95+17,$hi-5,$code[12],$fg);
  /* Output the Header and Content. */
  header("Content-Type: image/png");
  ImagePNG($img);
   
}

EAN_13('6901028055048');

?>

条码样例:



了解更多详情,参与讨论,请进入GBUNIX论坛

http://www.gbunix.com/bbs/index.php
责任编辑: trotter

相关文章
Fedora core 2下建立支持MPPE/MPPC的PPTP VPN服务器 - 06-03 09:44 pm - 点击: 2439
架设Linux下最简单的VPN系统 - 12-28 11:34 pm - 点击: 797
Reahat 7.2 + PoPtoP 实现VPN一例 - 08-11 08:59 pm - 点击: 796
可加密VPN服务器的简单架设 - 08-10 05:19 pm - 点击: 711
利用openvpn+linux快速建立企业VPN - 07-09 12:02 am - 点击: 712
使用SSH建立基于Linux/Unix的VPN网络 - 07-08 11:26 pm - 点击: 1012
RedHat7.2实现免费的VPN - 07-02 11:19 pm - 点击: 1036

发表评论 查看评论 加入收藏 Email给朋友 打印本文
给该文章评分
1 2 3 4 5 6 7 8 9 10
平均得分 0, 共 0 人评分
1 2 3 4 5 6 7 8 9 10
发表评论
标题:
内容:
声明:本站资讯均来自互联网,所有文章版权归原作者和出版者所有,如涉及版权问题,请来信告知!
Copyright © 2002-2004 GBunix.com. All rights reserved.