php实现生成code128条形码的方法详解

本文实例讲述了php实现生成code128条形码的方法。分享给大家供大家参考,具体如下:

效果图:

<?php
class BarCode128 {
  const STARTA = 103;
  const STARTB = 104;
  const STARTC = 105;
  const STOP = 106;
  private $unit_width = 1; //单位宽度 缺省1个象素
  private $is_set_height = false;
  private $width = -1;
  private $heith = 35;
  private $quiet_zone = 6;
  private $font_height = 15;
  private $font_type = 4;
  private $color =0x000000;
  private $bgcolor =0xFFFFFF;
  private $image = null;
  private $codes = array("212222","222122","222221","121223","121322","131222","122213","122312","132212","221213","221312","231212","112232","122132","122231","113222","123122","123221","223211","221132","221231","213212","223112","312131","311222","321122","321221","312212","322112","322211","212123","212321","232121","111323","131123","131321","112313","132113","132311","211313","231113","231311","112133","112331","132131","113123","113321","133121","313121","211331","231131","213113","213311","213131","311123","311321","331121","312113","312311","332111","314111","221411","431111","111224","111422","121124","121421","141122","141221","112214","112412","122114","122411","142112","142211","241211","221114","413111","241112","134111","111242","121142","121241","114212","124112","124211","411212","421112","421211","212141","214121","412121","111143","111341","131141","114113","114311","411113","411311","113141","114131","311141","411131","211412","211214","211412","2331112");
  private $valid_code = -1;
  private $type ='B';
  private $start_codes =array('A'=>self::STARTA,'B'=>self::STARTB,'C'=>self::STARTC);
  private $code ='';
  private $bin_code ='';
  private $text ='';
  public function __construct($code='',$text='',$type='B')
  {
    if (in_array($type,array('A','B','C')))
      $this->setType($type);
    else
      $this->setType('B');
    if ($code !=='')
      $this->setCode($code);
    if ($text !=='')
      $this->setText($text);
  }
  public function setUnitWidth($unit_width)
  {
    $this->unit_width = $unit_width;
    $this->quiet_zone = $this->unit_width*6;
    $this->font_height = $this->unit_width*15;
    if (!$this->is_set_height)
    {
      $this->heith = $this->unit_width*35;
    }
  }
  public function setFontType($font_type)
  {
    $this->font_type = $font_type;
  }
  public function setBgcolor($bgcoloe)
  {
    $this->bgcolor = $bgcoloe;
  }
  public function setColor($color)
  {
    $this->color = $color;
  }
  public function setCode($code)
  {
    if ($code !='')
    {
      $this->code= $code;
      if ($this->text ==='')
        $this->text = $code;
    }
  }
  public function setText($text)
  {
    $this->text = $text;
  }
  public function setType($type)
  {
    $this->type = $type;
  }
  public function setHeight($height)
  {
    $this->height = $height;
    $this->is_set_height = true;
  }
  private function getValueFromChar($ch)
  {
    $val = ord($ch);
    try
    {
      if ($this->type =='A')
      {
        if ($val > 95)
          throw new Exception(' illegal barcode character '.$ch.' for code128A in '.__FILE__.' on line '.__LINE__);
        if ($val < 32)
          $val += 64;
        else
          $val -=32;
      }
      elseif ($this->type =='B')
      {
        if ($val < 32 || $val > 127)
          throw new Exception(' illegal barcode character '.$ch.' for code128B in '.__FILE__.' on line '.__LINE__);
        else
          $val -=32;
      }
      else
      {
        if (!is_numeric($ch) || (int)$ch < 0 || (int)($ch) > 99)
          throw new Exception(' illegal barcode character '.$ch.' for code128C in '.__FILE__.' on line '.__LINE__);
        else
        {
          if (strlen($ch) ==1)
            $ch .='0';
          $val = (int)($ch);
        }
      }
    }
    catch(Exception $ex)
    {
      errorlog('die',$ex->getMessage());
    }
    return $val;
  }
  private function parseCode()
  {
    $this->type=='C'?$step=2:$step=1;
    $val_sum = $this->start_codes[$this->type];
    $this->width = 35;
    $this->bin_code = $this->codes[$val_sum];
    for($i =0;$i<strlen($this->code);$i+=$step)
    {
      $this->width +=11;
      $ch = substr($this->code,$i,$step);
      $val = $this->getValueFromChar($ch);
      $val_sum += $val;
      $this->bin_code .= $this->codes[$val];
    }
    $this->width *=$this->unit_width;
    $val_sum = $val_sum%103;
    $this->valid_code = $val_sum;
    $this->bin_code .= $this->codes[$this->valid_code];
    $this->bin_code .= $this->codes[self::STOP];
  }
  public function getValidCode()
  {
    if ($this->valid_code == -1)
      $this->parseCode();
    return $this->valid_code;
  }
  public function getWidth()
  {
    if ($this->width ==-1)
      $this->parseCode();
    return $this->width;
  }
  public function getHeight()
  {
    if ($this->width ==-1)
      $this->parseCode();
    return $this->height;
  }
  public function createBarCode($image_type ='png',$file_name=null)
  {
    $this->parseCode();
    $this->image = ImageCreate($this->width+2*$this->quiet_zone,$this->heith + $this->font_height);
    $this->bgcolor = imagecolorallocate($this->image,$this->bgcolor >> 16,($this->bgcolor >> 8)&0x00FF,$this->bgcolor & 0xFF);
    $this->color = imagecolorallocate($this->image,$this->color >> 16,($this->color >> 8)&0x00FF,$this->color & 0xFF);
    ImageFilledRectangle($this->image, 0, 0, $this->width + 2*$this->quiet_zone,$this->heith + $this->font_height, $this->bgcolor);
    $sx = $this->quiet_zone;
    $sy = $this->font_height -1;
    $fw = 10; //編號為2或3的字體的寬度為10,為4或5的字體寬度為11
    if ($this->font_type >3)
    {
      $sy++;
      $fw=11;
    }
    $ex = 0;
    $ey = $this->heith + $this->font_height - 2;
    for($i=0;$i<strlen($this->bin_code);$i++)
    {
      $ex = $sx + $this->unit_width*(int) $this->bin_code{$i} -1;
      if ($i%2==0)
        ImageFilledRectangle($this->image, $sx, $sy, $ex,$ey, $this->color);
      $sx =$ex + 1;
    }
    $t_num = strlen($this->text);
    $t_x = $this->width/$t_num;
    $t_sx = ($t_x -$fw)/2;    //目的为了使文字居中平均分布
    for($i=0;$i<$t_num;$i++)
    {
      imagechar($this->image,$this->font_type,6*$this->unit_width +$t_sx +$i*$t_x,0,$this->text{$i},$this->color);
    }
    if (!$file_name)
    {
      header("Content-Type: image/".$image_type);
    }
    switch ($image_type)
    {
      case 'jpg':
      case 'jpeg':
        Imagejpeg($this->image,$file_name);
        break;
      case 'png':
        Imagepng($this->image,$file_name);
        break;
      case 'gif':
        break;
        Imagegif($this->image,$file_name);
      default:
        Imagepng($this->image,$file_name);
        break;
    }
  }
}
$barcode = new BarCode128('88888888');
$barcode->createBarCode();
?>

附加一个强大的条码生成扩展包:
http://www.barcodebakery.com/

PS:这里再为大家推荐一款相似的条形码生成工具供大家参考使用:

在线条形码(一维码)生成/实时预览工具:
http://tools.jb51.net/aideddesign/tiaoxingma

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP图形与图片操作技巧汇总》、《PHP数组(Array)操作技巧大全》、《PHP数据结构与算法教程》、《php程序设计算法总结》、《PHP数学运算技巧总结》、《php字符串(string)用法总结》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

(0)

相关推荐

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

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

  • PHP生成条形码大揭秘

    1.什么是条形码? 百度百科定义:条形码(barcode)是将宽度不等的多个黑条和空白,按照一定的编码规则排列,用以表达一组信息的图形标识符.常见的条形码是由反射率相差很大的黑条(简称条)和白条(简称空)排成平行线的图案.在日常生活中,条形码可以标出物品的生产国.制造厂家.商品名称.生产日期.图书分类号.邮件地点起止.类别.日期等许多信息.条形码编码格式具体请参考 打印出来的优惠券,商家需要用验证器读取条形码,来获得其有效性. 2.如何生成条形码? 首先找到强大的开源资料,在barcode官网下

  • php实现在线生成条形码示例分享(条形码生成器)

    复制代码 代码如下: <?phpdefine('IN_CB',true);include('header.php'); $keys = array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','-','.',' ','$','/','+','%'); $n

  • php生成EAN_13标准条形码实例

    下面的就是生成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

  • php实现生成code128条形码的方法详解

    本文实例讲述了php实现生成code128条形码的方法.分享给大家供大家参考,具体如下: 效果图: <?php class BarCode128 { const STARTA = 103; const STARTB = 104; const STARTC = 105; const STOP = 106; private $unit_width = 1; //单位宽度 缺省1个象素 private $is_set_height = false; private $width = -1; priva

  • Python批量生成字幕图片的方法详解

    目录 说明 前提 放码 说明 视频剪辑时需要为视频添加字幕,添加字幕方法之一:根据字幕文本文件批量生成透明底只有字幕内容的图片文件,如下图,然后将这些图片文件添加到视频剪辑软件轨道中. 于是用pillow这Python图片工具库执行本次批量生成工作. 前提 pip intall pillow 放码 from PIL import Image, ImageDraw, ImageFont import os imageWidth, imageHeight = 1920, 1080 fontsFold

  • PHP实现生成推广海报的方法详解

    本文实例讲述了PHP实现生成推广海报的方法.分享给大家供大家参考,具体如下: 经常有这样的需求,就是需要在生成推广海报,包含指定的二维码,分享出去别人扫码之后就可以确定用户推荐关系. 仔细分析一下,推广海报必要的要素就是海报背景图和二维码,这两者都容易生成,但要两者结合到一起组合成为一张图二维还要可以保存到本地便于分享出去,这就是难点了,在H5中可以借助canvas画出来完成类似于截图的功能,但放到小程序里边很多局限性.那么我们直接在后台生成海报,前台直接调用. 前期准备: 1.海报背景图,背景

  • ASP.NET生成图形验证码的方法详解

    本文实例讲述了ASP.NET生成图形验证码的方法.分享给大家供大家参考,具体如下: 通常生成一个图形验证码主要 有3个步骤: (1)随机产生一个长度为N的随机字符串,N的值可由开发可由开发人员自行设置.该字符串可以包含数字.字母等. (2)将随机生成的字符串创建成图片,并显示. (3)保存验证码. 新建一个页面为default.aspx,  放置一个TextBox控件和一个Image控件,TextBox控件用于输入生成的字符串,Image控件用于显示字符串,它的图片就为生成的图形验证码image

  • Python使用qrcode二维码库生成二维码方法详解

    安装qrcode库 pip install qrcode 声明 import qrcode 使用qrcode QRCode 方法 qrcode.QRCode( version=1, error_correction=qrcode.ERROR_CORRECT_L, box_size=10, border=4, image_factory=None, mask_pattern=None ) 参数解释: version:控制二维码的大小,取值范围从1到40.取最小值1时,二维码大小为21*21.取值为

  • C#生成code128条形码的方法

    本文实例讲述了物流条形码的C#实现方法,分享一下供大家参考.具体实现方法如下: 主要功能代码如下: using System; using System.Collections.Generic; using System.Data; using System.Drawing; namespace Code { class BarCode { public class Code128 { private DataTable m_Code128 = new DataTable(); private

  • PHP基于phpqrcode类生成二维码的方法详解

    本文实例讲述了PHP基于phpqrcode类生成二维码的方法.分享给大家供大家参考,具体如下: 使用PHP语言生成二维码,还是挺有难度的,当然调用生成二维码图片的接口(比如:联图网http://www.liantu.com/的接口)除外,如果自己写代码生成,真的无从下手.然而,我们可以使用phpqrcode这个现成的类文件,PHP二维码生成类库,利用它可以轻松生成二维码. 前期准备: 1.phpqrcode类文件下载,下载地址:https://sourceforge.net/projects/p

  • Java实现添加条形码到PDF表格的方法详解

    目录 程序环境 代码示例 条码的应用已深入生活和工作的方方面面.在处理条码时,常需要和各种文档格式相结合.当需要在文档中插入.编辑或者删除条码时,可借助于一些专业的类库工具来实现.本文,以操作PDF文件为例,介绍如何在编辑表格时,向单元格中添加条形码. 程序环境 本次功能测试中,使用 Free Spire.PDF for Java. 实现功能的大致思路:生成条形码,将条形码保存为图片,然后在PDF中的表格单元格中插入条码图片. Spire.PDF for Java 中的Spire.Pdf.Bar

  • python3转换code128条形码的方法

    这年头如果用 python3 做条形码的,肯定(推荐)用 pystrich . 这货官方文档貌似都没写到支持 Code128 ,但是居然有这个类( Code128Encoder ).... 一些喷墨打印机,如果质量差一点的话,喷出来的条码,会沾到一起,不好识别. 而用 pystrich 的话,会发觉宽度无法调节. 于是想到了用 条形码字体 来自己控制大小,找是找到字库了,但是你会发觉,你生成的东西,无法被扫描识别, 那是因为,这东西得转换后,才能打印啊... 经过千辛万苦,终于找到一篇文章说到转

  • 对python实现模板生成脚本的方法详解

    最近项目需要,针对主项目提取一个小的基础版本,供于在新建项目时使用,所以就有这个python模板生成脚本,其作用如下: 1.通过配置文件来控制模板中的数据.格式化的过滤条件 2.执行后会把目录下所有的文件都会执行一篇 #!/usr/bin/python #encoding: utf-8 import json import codecs import os def get_files(root_path): for dir in os.walk(root_path): if dir[2]: fo

随机推荐