php生成条形码的图片的实例详解


Posted in PHP onSeptember 13, 2017

php生成条形码的图片的实例详解

因为用户的需要  写了一个条形码;用php生成一个条形码的图片  这个大家应该比我要好很多的吧,在自己项目的根目录下建立一个测试文件(直接把下面的代码放进去运行一下看看,我也是抄袭别人的),在实际的项目中你可以将下面的代码封装到一个公共类文件下的一个函数,然后调用。

class testinfo{
  function UPCAbarcode($code) {
    $trans_code = $code;
    $lw = 2.2; $hi = 40;
    $Lencode = array('0001101','0011001','0010011','0111101','0100011',
        '0110001','0101111','0111011','0110111','0001011');
    $Rencode = array('1110010','1100110','1101100','1000010','1011100',
        '1001110','1010000','1000100','1001000','1110100');
    $ends = '101'; $center = '01010';
    /* Compute the EAN-13 Checksum digit */
    $ncode = '0'.$code;
    $even = 0; $odd = 0;
    for ($x=0;$x<12;$x++) {
      if ($x % 2) {
        $odd += $ncode[$x];
      } else { $even += $ncode[$x];
      }
    }
    $code.=(10 - (($odd * 3 + $even) % 10)) % 10;
    /* Create the bar encoding using a binary string */
    $bars=$ends;
    $bars.=$Lencode[$code[0]];
    
    for($x=1;$x<6;$x++) {
      $bars.=$Lencode[$code[$x]];
    }
    $bars.=$center;
    for($x=6;$x<12;$x++) {
      $bars.=$Rencode[$code[$x]];
    }
    $bars.=$ends;
    /* Generate the Barcode Image */
    $img = ImageCreate($lw*75+30,$hi-3);  // 95
    $fg = ImageColorAllocate($img, 0, 0, 0);
    $bg = ImageColorAllocate($img, 255, 255, 255);
    ImageFilledRectangle($img, 0, 0, $lw*75+30, $hi+30, $bg);
    $shift=10;
    for ($x=0;$x<strlen($bars);$x++) {
      if (($x<0) || ($x>=45 && $x<46) || ($x >=85)) {
        $sh=10;
      } else { $sh=0;
      }
      if ($bars[$x] == '1') {
        $color = $fg;
      } else { $color = $bg;
      }
      ImageFilledRectangle($img, ($x*$lw)+15,5,($x+1)*$lw+14,$hi+5+$sh,$color);
    }
    /* Add the Human Readable Label */
    ImageString($img,4,5,$hi-5,$code[0],$fg);
    for ($x=0;$x<5;$x++) {
      ImageString($img,5,$lw*(13+$x*6)+15,$hi+5,$code[$x+1],$fg);
      ImageString($img,5,$lw*(53+$x*6)+15,$hi+5,$code[$x+6],$fg);
    }
    ImageString($img,4,$lw*95-7,$hi,$code[11],$fg);
    /* Output the Header and Content. */
    header("Content-Type: image/png");
    ImagePNG($img);
  }
//}
echo UPCAbarcode('201212070099');

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

PHP 相关文章推荐
php mssql 日期出现中文字符的解决方法
Mar 10 PHP
PHP n个不重复的随机数生成代码
Jun 23 PHP
fleaphp crud操作之find函数的使用方法
Apr 23 PHP
关于查看MSSQL 数据库 用户每个表 占用的空间大小
Jun 21 PHP
php实现的发送带附件邮件类实例
Sep 22 PHP
php中mt_rand()随机数函数用法
Nov 24 PHP
php reset() 函数指针指向数组中的第一个元素并输出实例代码
Nov 21 PHP
php使用函数pathinfo()、parse_url()和basename()解析URL
Nov 25 PHP
PHP 并发场景的几种解决方案
Jun 14 PHP
PHP判断函数是否被定义的方法
Jun 21 PHP
使用swoole 定时器变更超时未支付订单状态的解决方案
Jul 24 PHP
phpQuery采集网页实现代码实例
Apr 02 PHP
php之可变函数的实例详解
Sep 13 #PHP
PHP实现一个多功能购物网站的案例
Sep 13 #PHP
php之可变变量的实例详解
Sep 12 #PHP
PHP清除缓存的几种方法总结
Sep 12 #PHP
PHP 传输会话curl函数的实例详解
Sep 12 #PHP
基于ThinkPHP5.0实现图片上传插件
Sep 25 #PHP
Laravel中encrypt和decrypt的实现方法
Sep 24 #PHP
You might like
php封装一个异常的处理类
2017/06/08 PHP
使用PHP连接数据库_实现用户数据的增删改查的整体操作示例
2017/09/01 PHP
Laravel Eloquent ORM 实现查询表中指定的字段
2019/10/17 PHP
更正确的asp冒泡排序
2007/05/24 Javascript
很多人都是用下面的js刷新站IP和PV
2008/09/05 Javascript
jQuery实现返回顶部效果的方法
2015/05/29 Javascript
jquery验证邮箱格式是否正确实例讲解
2015/11/16 Javascript
基于JS实现的笛卡尔乘积之商品发布
2016/05/13 Javascript
实例解析jQuery中proxy()函数的用法
2016/05/24 Javascript
Three.js学习之文字形状及自定义形状
2016/08/01 Javascript
用NodeJS实现批量查询地理位置的经纬度接口
2016/08/16 NodeJs
JS正则表达式判断有效数实例代码
2017/03/13 Javascript
echarts学习笔记之图表自适应问题详解
2017/11/22 Javascript
Phaser.js实现简单的跑酷游戏附源码下载
2018/10/26 Javascript
js实现抽奖的两种方法
2020/03/19 Javascript
微信小程序实现列表滚动头部吸顶的示例代码
2020/07/12 Javascript
Python中装饰器兼容加括号和不加括号的写法详解
2017/07/05 Python
Random 在 Python 中的使用方法
2018/08/09 Python
django 外键model的互相读取方法
2018/12/15 Python
python中import与from方法总结(推荐)
2019/03/21 Python
详解Python中的各种转义符\n\r\t
2019/07/10 Python
python global关键字的用法详解
2019/09/05 Python
Python 实现递归法解决迷宫问题的示例代码
2020/01/12 Python
k-means 聚类算法与Python实现代码
2020/06/01 Python
matplotlib.pyplot.plot()参数使用详解
2020/07/28 Python
如何从csv文件构建Tensorflow的数据集
2020/09/21 Python
Python 使用SFTP和FTP实现对服务器的文件下载功能
2020/12/17 Python
德国机场停车位比较和预订网站:Ich-parke-billiger
2018/01/08 全球购物
美国牛仔品牌:True Religion
2018/11/16 全球购物
伦敦香水公司:The London Perfume Company
2019/11/13 全球购物
超市采购员岗位职责
2014/02/01 职场文书
学校消防演习方案
2014/02/19 职场文书
中学优秀班主任事迹材料
2014/05/01 职场文书
机关干部正风肃纪心得体会
2016/01/15 职场文书
小程序实现侧滑删除功能
2022/06/25 Javascript
Win10本地连接不见了怎么恢复? win10系统电脑本地连接不见了解决方法
2023/01/09 数码科技