欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php實(shí)現(xiàn)生成code128條形碼的方法詳解

 更新時(shí)間:2017年07月19日 11:38:57   作者:aguang  
這篇文章主要介紹了php實(shí)現(xiàn)生成code128條形碼的方法,結(jié)合完整實(shí)例形式給出了php條形碼生成類(lèi)的定義與使用方法,需要的朋友可以參考下

本文實(shí)例講述了php實(shí)現(xiàn)生成code128條形碼的方法。分享給大家供大家參考,具體如下:

效果圖:

<?php
class BarCode128 {
  const STARTA = 103;
  const STARTB = 104;
  const STARTC = 105;
  const STOP = 106;
  private $unit_width = 1; //單位寬度 缺省1個(gè)象素
  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; //編號(hào)為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();
?>

附加一個(gè)強(qiáng)大的條碼生成擴(kuò)展包:
http://www.barcodebakery.com/

PS:這里再為大家推薦一款相似的條形碼生成工具供大家參考使用:

在線(xiàn)條形碼(一維碼)生成/實(shí)時(shí)預(yù)覽工具:
http://tools.jb51.net/aideddesign/tiaoxingma

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《PHP圖形與圖片操作技巧匯總》、《PHP數(shù)組(Array)操作技巧大全》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計(jì)算法總結(jié)》、《PHP數(shù)學(xué)運(yùn)算技巧總結(jié)》、《php字符串(string)用法總結(jié)》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總

希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • php寫(xiě)入數(shù)據(jù)到CSV文件的方法

    php寫(xiě)入數(shù)據(jù)到CSV文件的方法

    這篇文章主要介紹了php寫(xiě)入數(shù)據(jù)到CSV文件的方法,涉及php操作csv文件的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-03-03
  • php全排列遞歸算法代碼

    php全排列遞歸算法代碼

    php全排列遞歸算法代碼,需要的朋友可以參考下
    2012-10-10
  • PHP程序員學(xué)習(xí)使用Swoole的理由

    PHP程序員學(xué)習(xí)使用Swoole的理由

    這篇文章給大家詳細(xì)分析了為什么PHP程序員需要學(xué)習(xí)使用Swoole,并通過(guò)實(shí)例做了分析,有興趣的朋友參考下。
    2018-06-06
  • 淺談Laravel中使用Slack進(jìn)行異常通知

    淺談Laravel中使用Slack進(jìn)行異常通知

    異常處理是軟件開(kāi)發(fā)過(guò)程中無(wú)法逃避的問(wèn)題。對(duì)于一套設(shè)計(jì)良好代碼高效的程序,出現(xiàn)異常的可能性會(huì)比較低,但這并不意味著不會(huì)出現(xiàn)異常,有些異常甚至?xí)饑?yán)重的后果,所以如何及時(shí)的發(fā)現(xiàn)程序中的異常并處理它便顯得十分重要了。
    2021-05-05
  • PHP使用 Imagick 擴(kuò)展實(shí)現(xiàn)圖片合成,圓角處理功能示例

    PHP使用 Imagick 擴(kuò)展實(shí)現(xiàn)圖片合成,圓角處理功能示例

    這篇文章主要介紹了PHP使用 Imagick 擴(kuò)展實(shí)現(xiàn)圖片合成,圓角處理功能,結(jié)合具體實(shí)例形式分析了PHP使用 Imagick 擴(kuò)展的圖形處理、生成相關(guān)操作技巧,需要的朋友可以參考下
    2019-09-09
  • 深入研究PHP中的preg_replace和代碼執(zhí)行

    深入研究PHP中的preg_replace和代碼執(zhí)行

    這篇文章主要給大家介紹了關(guān)于PHP中preg_replace和代碼執(zhí)行的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-08-08
  • PHP連接MySQL進(jìn)行增、刪、改、查操作

    PHP連接MySQL進(jìn)行增、刪、改、查操作

    本篇文章主要介紹了PHP連接MySQL進(jìn)行增、刪、改、查操作的方法,具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧
    2017-02-02
  • PHP根據(jù)key刪除數(shù)組中指定的元素

    PHP根據(jù)key刪除數(shù)組中指定的元素

    今天小編就為大家分享一篇關(guān)于PHP根據(jù)key刪除數(shù)組中指定的元素,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-02-02
  • php 查找數(shù)組元素提高效率的方法詳解

    php 查找數(shù)組元素提高效率的方法詳解

    本文主要介紹了php查找數(shù)組元素提高效率的方法,具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧
    2017-05-05
  • php發(fā)送http請(qǐng)求的常用方法分析

    php發(fā)送http請(qǐng)求的常用方法分析

    這篇文章主要介紹了php發(fā)送http請(qǐng)求的常用方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了php使用curl及socket發(fā)送post請(qǐng)求的操作技巧,需要的朋友可以參考下
    2016-11-11

最新評(píng)論