php生成EAN_13标准条形码实例
下面的就是生成EAN_13标准的条码的PHP方法,需要php+gd环境
functionEAN_13($code){
//一个单元的宽度
$lw=2;
//条码高
$hi=100;
//theguidecodeisnocoding,isusedtoshowtheleftpartcodingtype//
//ArrayguideisusedtorecordtheEAN_13isleftpartcodingtype//
$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-AMustbe13digits.");}
$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("thecodeisbad!");
}
//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);
}
/*AddtheHumanReadableLabel*/
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);
/*OutputtheHeaderandContent.*/
header("Content-Type:image/png");
ImagePNG($img);
}
EAN_13("6901028055048");
?>
相关文章
- MySQL多实例安装部署
- MYSQL数据库读写分离实例
- ansible安装配置及实例
- Oracle连接字符串的简单使用实例(oracle中连接字符串)
- 使用PHP实现Redis分布式缓存(php实例化redis)
- Linux串口编程实例(linux串口例程)
- 数据库解决PHP连接远程MySQL数据库的问题(php连接远程mysql)
- PHP读取Linux文件的方法(php读取linux文件)
- PHP驱动MSSQL数据库操作实例研究(php mssql操作类)
- Linux下快速搭建PHP开发环境(linux php集成环境)
- 混合开发助力企业信息化C语言PHP和MySQL联动提升系统性能(c php mysql)
- windows服务器下IIS6/7下PHP无法加载php_curl.dll等动态链接库
- php根据身份证号码计算年龄的实例代码
- jQueryValidate验证,校验规则写在控件中的具体实例
- 在Mysql上创建数据表实例代码
- jquery的ajax异步请求接收返回json数据实例
- Java中使用数组实现栈数据结构实例
- C#入门之checked和unchecked的区别实例解析
- PHP基于数组实现的分页函数实例