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 相关文章推荐
一个odbc连mssql分页的类
Oct 09 PHP
PHP读MYSQL中文乱码的解决方法
Dec 17 PHP
亲密接触PHP之PHP语法学习笔记1
Dec 17 PHP
php 随机数的产生、页面跳转、件读写、文件重命名、switch语句
Aug 07 PHP
PHP XML操作类DOMDocument
Dec 16 PHP
理解PHP中的stdClass类
Apr 18 PHP
ThinkPHP连接数据库及主从数据库的设置教程
Aug 22 PHP
PHP使用mysql_fetch_row查询获得数据行列表的方法
Mar 18 PHP
[原创]php正则删除html代码中class样式属性的方法
May 24 PHP
phpcms配置列表页以及获得文章发布时间
Jul 04 PHP
PHP操作MySQL中BLOB字段的方法示例【存储文本与图片】
Sep 15 PHP
PHP区块查询实现方法分析
May 12 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
8个出色的WordPress SEO插件收集
2011/02/26 PHP
基于PHP字符串的比较函数strcmp()与strcasecmp()的使用详解
2013/05/15 PHP
PHP使用PHPMailer发送邮件的简单使用方法
2013/11/12 PHP
phpstorm配置Xdebug进行调试PHP教程
2014/12/01 PHP
php传值赋值和传地址赋值用法实例分析
2015/06/20 PHP
php生成mysql的数据字典
2016/07/07 PHP
php处理抢购类功能的高并发请求
2018/02/08 PHP
JavaScript计算字符串中每个字符出现次数的小例子
2013/07/02 Javascript
JS关键字球状旋转效果的实例代码
2013/11/29 Javascript
将form表单中的元素转换成对象的方法适用表单提交
2014/05/02 Javascript
原生js实现的贪吃蛇网页版游戏完整实例
2015/05/18 Javascript
基于jQuery代码实现圆形菜单展开收缩效果
2017/02/13 Javascript
nodejs和C语言插入mysql数据库乱码问题的解决方法
2017/04/14 NodeJs
微信小程序--onShareAppMessage分享参数用处(页面分享)
2017/04/18 Javascript
javascript中如何判断类型汇总
2019/05/14 Javascript
vue的keep-alive用法技巧
2019/08/15 Javascript
JQuery样式与属性设置方法分析
2019/12/07 jQuery
javascript设计模式 ? 代理模式原理与用法实例分析
2020/04/16 Javascript
Python标准库之多进程(multiprocessing包)介绍
2014/11/25 Python
Python3计算三角形的面积代码
2017/12/18 Python
python贪婪匹配以及多行匹配的实例讲解
2018/04/19 Python
Python3实现的字典、列表和json对象互转功能示例
2018/05/22 Python
python 读写文件,按行修改文件的方法
2018/07/12 Python
python 2.7.13 安装配置方法图文教程
2018/09/18 Python
在python里面运用多继承方法详解
2019/07/01 Python
Python Django Cookie 简单用法解析
2019/08/13 Python
python或C++读取指定文件夹下的所有图片
2019/08/31 Python
HTML5 DeviceOrientation实现手机网站摇一摇功能代码实例
2015/04/24 HTML / CSS
生产车间班组长岗位职责
2014/01/06 职场文书
人力资源管理专业应届生求职信
2014/04/24 职场文书
体育口号大全
2014/06/18 职场文书
冲出亚马逊观后感
2015/06/03 职场文书
标枪加油稿
2015/07/22 职场文书
解决numpy数组互换两行及赋值的问题
2021/04/17 Python
为什么node.js不适合大型项目
2021/04/28 Javascript
SQL Server实现分页方法介绍
2022/03/16 SQL Server