BCGBarcode:支持 PHP 4/5、GD 庫、多種碼制、多格式輸出的開源條碼生成庫。
php_gd2.dll、Linux gd.sohttps://localhost/barcode/index.php 測試
| 參數 | 說明 |
|---|---|
| Type | 條碼類型(Code39/128/EAN13/QR 等) |
| Output | 輸出圖片格式 |
| Thickness | 條碼高度 |
| Resolution | 條碼大小/縮放 |
| Font | 下方文字大小(可隱藏) |
| Text | 編碼內容 |
原庫 test.php 耦合度高,改造為參數化調用:
# URL 調用示例
https://localhost/barcode/test.php?codebar=BCGcode39&text=20090729
# 模板嵌入
<img src="test.php?codebar=BCGcode39&text=20090729">

<?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 參數,單一入口支持所有碼制,極大提升復用性。