欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-11 21:40:55
本文实例讲述了php实现生成code128条形码的方法。分享给大家供大家参考,具体如下: 效果图:

本文实例讲述了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();
?>

附加一个强大的条码生成扩展包:

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

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

在线条形码(一维码)生成/实时预览工具:

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

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