찾다
백엔드 개발PHP 튜토리얼PHP 생성 code128 바코드 예제 공유
PHP 생성 code128 바코드 예제 공유Feb 06, 2018 am 10:40 AM
php바코드

이 글은 주로 PHP에서 code128 바코드를 생성하는 방법을 소개합니다. PHP 바코드 생성 클래스의 정의와 사용법을 완전한 예제 형식으로 제공하므로 필요한 친구들이 모두 참고할 수 있기를 바랍니다.

렌더링:


<?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 =&#39;B&#39;;
  private $start_codes =array(&#39;A&#39;=>self::STARTA,&#39;B&#39;=>self::STARTB,&#39;C&#39;=>self::STARTC);
  private $code =&#39;&#39;;
  private $bin_code =&#39;&#39;;
  private $text =&#39;&#39;;
  public function __construct($code=&#39;&#39;,$text=&#39;&#39;,$type=&#39;B&#39;)
  {
    if (in_array($type,array(&#39;A&#39;,&#39;B&#39;,&#39;C&#39;)))
      $this->setType($type);
    else
      $this->setType(&#39;B&#39;);
    if ($code !==&#39;&#39;)
      $this->setCode($code);
    if ($text !==&#39;&#39;)
      $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 !=&#39;&#39;)
    {
      $this->code= $code;
      if ($this->text ===&#39;&#39;)
        $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 ==&#39;A&#39;)
      {
        if ($val > 95)
          throw new Exception(&#39; illegal barcode character &#39;.$ch.&#39; for code128A in &#39;.__FILE__.&#39; on line &#39;.__LINE__);
        if ($val < 32)
          $val += 64;
        else
          $val -=32;
      }
      elseif ($this->type ==&#39;B&#39;)
      {
        if ($val < 32 || $val > 127)
          throw new Exception(&#39; illegal barcode character &#39;.$ch.&#39; for code128B in &#39;.__FILE__.&#39; on line &#39;.__LINE__);
        else
          $val -=32;
      }
      else
      {
        if (!is_numeric($ch) || (int)$ch < 0 || (int)($ch) > 99)
          throw new Exception(&#39; illegal barcode character &#39;.$ch.&#39; for code128C in &#39;.__FILE__.&#39; on line &#39;.__LINE__);
        else
        {
          if (strlen($ch) ==1)
            $ch .=&#39;0&#39;;
          $val = (int)($ch);
        }
      }
    }
    catch(Exception $ex)
    {
      errorlog(&#39;die&#39;,$ex->getMessage());
    }
    return $val;
  }
  private function parseCode()
  {
    $this->type==&#39;C&#39;?$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 =&#39;png&#39;,$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 &#39;jpg&#39;:
      case &#39;jpeg&#39;:
        Imagejpeg($this->image,$file_name);
        break;
      case &#39;png&#39;:
        Imagepng($this->image,$file_name);
        break;
      case &#39;gif&#39;:
        break;
        Imagegif($this->image,$file_name);
      default:
        Imagepng($this->image,$file_name);
        break;
    }
  }
}
$barcode = new BarCode128(&#39;88888888&#39;);
$barcode->createBarCode();
?>

강력한 바코드 생성 확장 패키지 포함:
http://www.barcodebakery.com/

관련 권장 사항:

PHP 바코드 생성 예

PHP를 사용하여 바코드 이미지를 생성하는 방법

PHP를 사용하여 바코드 이미지를 생성하는 방법에 대한 자세한 튜토리얼

위 내용은 PHP 생성 code128 바코드 예제 공유의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php怎么设置implode没有分隔符php怎么设置implode没有分隔符Apr 18, 2022 pm 05:39 PM

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

See all articles

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

뜨거운 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

SublimeText3 Linux 새 버전

SublimeText3 Linux 새 버전

SublimeText3 Linux 최신 버전

SecList

SecList

SecLists는 최고의 보안 테스터의 동반자입니다. 보안 평가 시 자주 사용되는 다양한 유형의 목록을 한 곳에 모아 놓은 것입니다. SecLists는 보안 테스터에게 필요할 수 있는 모든 목록을 편리하게 제공하여 보안 테스트를 더욱 효율적이고 생산적으로 만드는 데 도움이 됩니다. 목록 유형에는 사용자 이름, 비밀번호, URL, 퍼징 페이로드, 민감한 데이터 패턴, 웹 셸 등이 포함됩니다. 테스터는 이 저장소를 새로운 테스트 시스템으로 간단히 가져올 수 있으며 필요한 모든 유형의 목록에 액세스할 수 있습니다.

WebStorm Mac 버전

WebStorm Mac 버전

유용한 JavaScript 개발 도구

SublimeText3 영어 버전

SublimeText3 영어 버전

권장 사항: Win 버전, 코드 프롬프트 지원!