您可以使用phpBarcode庫來生成條形碼,并設置尺寸和顏色。以下是一個示例代碼:
// Include the library
require_once('class/BCGFontFile.php');
require_once('class/BCGColor.php');
require_once('class/BCGDrawing.php');
// Including all required barcode classes
require_once('class/BCGcodabar.barcode.php');
// The arguments are R, G, B for color.
$colorFront = new BCGColor(0, 0, 0);
$colorBack = new BCGColor(255, 255, 255);
// Barcode Part
$code = new BCGcodabar();
$code->setScale(2); // Resolution
$code->setThickness(30); // Thickness
$code->setForegroundColor($colorFront); // Color of bars
$code->setBackgroundColor($colorBack); // Color of spaces
$code->setFont(5); // Font (or 0)
$code->parse('1234');
// Drawing Part
$drawing = new BCGDrawing('', $colorBack);
$drawing->setBarcode($code);
$drawing->draw();
// Header that says it is an image (remove it if you save the barcode to a file)
header('Content-Type: image/png');
// Draw (or save) the image into PNG format.
$drawing->finish(BCGDrawing::IMG_FORMAT_PNG);
在上面的代碼中,您可以通過設置setScale
方法來設置條形碼的大小,通過設置setForegroundColor
和setBackgroundColor
來設置條形碼的前景色和背景色。您還可以通過設置setThickness
方法來設置條形碼的粗細。