php生成EAN_13标准条形码实例


Posted in PHP onNovember 13, 2013

下面的就是生成EAN_13标准的条码的PHP方法,需要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'); 
?> 
PHP 相关文章推荐
利用static实现表格的颜色隔行显示的代码
Sep 02 PHP
php下使用以下代码连接并测试
Apr 09 PHP
一篇有意思的技术文章php介绍篇
Oct 26 PHP
从php核心代码分析require和include的区别
Jan 02 PHP
php中防止SQL注入的最佳解决方法
Apr 25 PHP
codeigniter自带数据库类使用方法说明
Mar 25 PHP
php下foreach提示Warning:Invalid argument supplied for foreach()的解决方法
Nov 11 PHP
php中随机函数mt_rand()与rand()性能对比分析
Dec 01 PHP
PHP Warning: Module 'modulename' already loaded in问题解决办法
Mar 16 PHP
php采集中国代理服务器网的方法
Jun 16 PHP
ThinkPHP 模板substr的截取字符串函数详解
Jan 09 PHP
PHP单例模式应用示例【多次连接数据库只实例化一次】
Dec 18 PHP
使用php计算排列组合的方法
Nov 13 #PHP
测试php函数的方法
Nov 13 #PHP
PHP中判断变量为空的几种方法小结
Nov 12 #PHP
php不允许用户提交空表单(php空值判断)
Nov 12 #PHP
php5.3 不支持 session_register() 此函数已启用的解决方法
Nov 12 #PHP
session在php5.3中的变化 session_is_registered() is deprecated in
Nov 12 #PHP
PHP使用正则表达式清除超链接文本
Nov 12 #PHP
You might like
snoopy 强大的PHP采集类使用实例代码
2010/12/09 PHP
如何批量替换相对地址为绝对地址(利用bat批处理实现)
2013/05/27 PHP
phpword插件导出word文件时中文乱码问题处理方案
2014/08/19 PHP
以实例全面讲解PHP中多进程编程的相关函数的使用
2015/08/18 PHP
PHP实现唤起微信支付功能
2019/02/18 PHP
PHP基于openssl实现非对称加密代码实例
2020/06/19 PHP
php实现对短信验证码发送次数的限制实例讲解
2021/03/04 PHP
JQuery 学习笔记 element属性控制
2009/07/23 Javascript
网络之美 JavaScript中Get和Set访问器的实现代码
2010/09/19 Javascript
javascript 实现 秒杀,团购 倒计时展示的记录 分享
2013/07/12 Javascript
js正则匹配出所有图片及图片地址src的方法
2015/06/08 Javascript
分享有关jQuery中animate、slide、fade等动画的连续触发、滞后反复执行的bug
2016/01/10 Javascript
js和jQuery设置Opacity半透明 兼容IE6
2016/05/24 Javascript
bootstrap精简教程_动力节点Java学院整理
2017/07/14 Javascript
Vue实现按钮旋转和移动位置的实例代码
2018/08/09 Javascript
JavaScript中的ES6 Proxy的具体使用
2019/06/16 Javascript
微信小程序图片左右摆动效果详解
2019/07/13 Javascript
Elementui表格组件+sortablejs实现行拖拽排序的示例代码
2019/08/28 Javascript
vue-路由精讲 二级路由和三级路由的作用
2020/08/06 Javascript
JavaScript动态生成表格的示例
2020/11/02 Javascript
[01:02:06]LGD vs Mineski Supermajor 胜者组 BO3 第二场 6.5
2018/06/06 DOTA
django接入新浪微博OAuth的方法
2015/06/29 Python
Python 实现链表实例代码
2017/04/07 Python
读取本地json文件,解析json(实例讲解)
2017/12/06 Python
如何使用VSCode愉快的写Python于调试配置步骤
2018/04/06 Python
python中安装django模块的方法
2020/03/12 Python
Python对excel的基本操作方法
2021/02/18 Python
Tomcat中怎么使用log4j输出所有的log
2016/07/07 面试题
strlen的几种不同实现方法
2013/05/31 面试题
既然说Ruby中一切都是对象,那么Ruby中类也是对象吗
2013/01/26 面试题
电力公司个人求职信范文
2014/02/04 职场文书
工厂会计员职责
2014/02/06 职场文书
计算机应用应届生求职信
2014/07/12 职场文书
领导干部作风建设工作总结
2014/10/23 职场文书
2015年社区民政工作总结
2015/04/21 职场文书
一个成功的互联网创业项目,必须满足这些要求
2019/08/23 职场文书