This article mainly introduces the PHP implementation of the Code128 barcode generation method, combined with a complete example of the PHP barcode generation class definition and use method, the need for friends can refer to the next
This paper introduces the method of PHP implementation to generate CODE128 barcode. Share to everyone for your reference, as follows:
:
<?phpclass BarCode128 {Const STARTA = 103; Const STARTB = 104; Const STARTC = 105; Const STOP = 106; Private $unit _width = 1; Unit width default 1 pixels 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->sett Ype ($type); else $this->settype (' B '); if ($code!== ") $this->setcode ($code); if ($text!== ") $this->settext ($text); The 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; The Public Function Setbgcolor ($bgcoloe) {$this->bgcolor = $bgcoloe; The Public Function SetColor ($color) {$this->color = $color; The Public Function Setcode ($code) {if ($code! = ") {$this->code= $code; if ($this->text = = =) $this->text = $code; }} Public Function SetText ($text) {$this->text = $text; The Public Function SetType ($type) {$this->type = $type; The 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 >) throw new Exception (' Illegal barcode cha Racter '. $ch. ' For code128a in '. __file__ ' on LIne '. __line__); if ($val < +) $val + = 64; else $val-=32; } elseif ($this->type = = ' B ') {if ($val < | | $val > 127) throw new Exception (' Illeg Al barcode character '. $ch. ' For code128b in '. __file__. "On line". __line__); else $val-=32; } else {if (!is_numeric ($ch) | | (int) $ch < 0 | | (int) ($ch) >) throw new Exception (' illegal barcode character '. $ch. ' For code128c '. __file__. ' On '. __l INE__); 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 >>, ($this->bgcolor >> 8) &0X00FF, $this->bgcolor & 0xFF); $this->color = imagecolorallocate ($this->image, $this->color >>, ($this->color >> 8) & 0x00FF, $this->color & 0xFF); Imagefilledrectangle ($this->image, 0, 0, $this->width + $this->quiet_zone, $this->heith + $this->font _height, $this->bgcolor); $SX = $this->quiet_zone; $sy = $this->font_height-1; $FW = 10; The width of the 2 or 3 is 10, and the width of the 4 or 5 of the characters is one 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; The purpose is to distribute the text centered evenly for ($i =0; $i < $t _num; $i + +) {Imagechar ($this->image, $this->font_type,6* $this->unit_wi DTH + $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 ();? >
Attach a powerful barcode generation extension Package:
http://www.barcodebakery.com/