最近因为业务需求,需要后端来生成条形码,所以写个简单的php生成条形码demo
引入composer包
composer require codeitnowin/barcode
git地址:https://github.com/codeitnowin/barcode-generator
要求
You must have php >= 5.3.2 installed and GD extension enabled.
代码
目录结构:
就直接在test.php
文件写代码做测试,代码如下:
<?php
include 'vendor/autoload.php';
class barcodeService
{
private $filePath, $foregroundColor,$backgroundColor;
private $text = '', $labelName = '', $scale = 2, $tn = 25, $fontSize = 10;
public function options($data)
{
foreach ($data as $key => $val) {
$this->{
$key} = $val;
}
return $this;
}
public function generate()
{
try {
$barcode = new CodeItNow\BarcodeBundle\Utils\BarcodeGenerator(