首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 綜合知識

PHP 生成條形碼(二):開源庫 BCGBarcode 實戰集成與封裝

     編輯:PHP 綜合知識

庫介紹

BCGBarcode:支持 PHP 4/5、GD 庫、多種碼制、多格式輸出的開源條碼生成庫。

環境要求

  • PHP GD 模塊:Windows php_gd2.dll、Linux gd.so
  • 解壓至 Web 目錄,訪問 https://localhost/barcode/index.php 測試

BCGBarcode 演示界面截圖

界面參數說明

參數說明
Type條碼類型(Code39/128/EAN13/QR 等)
Output輸出圖片格式
Thickness條碼高度
Resolution條碼大小/縮放
Font下方文字大小(可隱藏)
Text編碼內容

靈活集成封裝:test.php

原庫 test.php 耦合度高,改造為參數化調用:

# URL 調用示例
https://localhost/barcode/test.php?codebar=BCGcode39&text=20090729

# 模板嵌入
<img src="test.php?codebar=BCGcode39&text=20090729">

生成條碼效果圖

test.php 核心代碼

<?php
require('class/BCGFont.php');
require('class/BCGColor.php');
require('class/BCGDrawing.php');

$codebar = $_REQUEST['codebar'];  // 如 BCGcode39, BCGcode128, BCGean13 等
include('class/'.$codebar.'.barcode.php');

$font = new BCGFont('./class/font/Arial.ttf', 10);
$color_black = new BCGColor(0, 0, 0);
$color_white = new BCGColor(255, 255, 255);

$code = new $codebar();
$code->setScale(2);           // Resolution
$code->setThickness(30);      // Thickness
$code->setForegroundColor($color_black);
$code->setBackgroundColor($color_white);
$code->setFont($font);

$text = $_REQUEST['text'];
$code->parse($text);

$drawing = new BCGDrawing('', $color_white);
$drawing->setBarcode($code);
$drawing->draw();

header('Content-Type: image/png');
$drawing->finish(BCGDrawing::IMG_FORMAT_PNG);
?>

支持的碼制常量

BCGcodabar, BCGcode11, BCGcode39, BCGcode39extended, BCGcode93,
BCGcode128, BCGean8, BCGean13, BCGisbn, BCGi25, BCGs25, BCGmsi,
BCGupca, BCGupce, BCGupcext2, BCGupcext5, BCGpostnet, BCGothercode

通過動態傳遞 codebar 參數,單一入口支持所有碼制,極大提升復用性。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved