php 生成條形碼(支持任意php框架)

一:插件安裝php

在php中咱們可使用php-barcode-generator插件來生成條形碼,php-barcode-generator插件github地址:https://github.com/picqer/php...html

composer require picqer/php-barcode-generator

二:php-barcode-generator插件簡單使用git

1:例:github

$generator = new \Picqer\Barcode\BarcodeGeneratorHTML();
echo $generator->getBarcode('123456', $generator::TYPE_CODE_128);

根據如上就能夠實現條形碼的生成:composer

image.png

2:根據條形碼生成類型,咱們可使用以下四種方式生成條形碼ui

$generatorSVG = new \Picqer\Barcode\BarcodeGeneratorSVG();        #建立SVG類型條形碼
$generatorPNG = new \Picqer\Barcode\BarcodeGeneratorPNG();       #建立PNG類型條形碼
$generatorJPG = new \Picqer\Barcode\BarcodeGeneratorJPG();          #建立JPG類型條形碼
$generatorHTML = new \Picqer\Barcode\BarcodeGeneratorHTML();  #建立HTML類型條形碼

若是要生成PNG或JPG圖像,則還須要在系統上安裝GD庫或Imagick拓展,Imagick拓展安裝方式可參考: PHP的Imagick拓展安裝編碼

3:getBarcode方法參數說明:spa

getBarcode($code, $type, $widthFactor = 2, $totalHeight = 30, $color = 'black')

code:條形碼數據插件

type:條形碼的類型,使用在類中定義的常量,具體參數可參考githubcode

widthFactor:條形碼的寬度

totalHeight:條形碼高度

color:條形碼的顏色

4:若是你想要將條形碼嵌入到html中

將生成的條形碼進行base64編碼嵌入到img標籤中

$generator = new \Picqer\Barcode\BarcodeGeneratorPNG();
$barcode = $generator->getBarcode('123456', $generator::TYPE_CODE_128);
$barcode = base64_encode($barcode);
echo  ' <img src="data:image/png;base64,'. $barcode .'"/>';

這樣就能夠將條形碼嵌入到html中了

相關文章
相關標籤/搜索