本文目录导读:

在PHP中生成条形码有多种方法,以下是几种常用的实现方式:
使用第三方库(推荐)
使用 Picqer/php-barcode-generator
这是最流行的PHP条形码生成库,支持多种格式。
安装:
composer require picqer/php-barcode-generator
基本使用:
<?php
require 'vendor/autoload.php';
use Picqer\Barcode\BarcodeGeneratorPNG;
use Picqer\Barcode\BarcodeGeneratorHTML;
// 生成PNG图片
$generator = new BarcodeGeneratorPNG();
$barcode = $generator->getBarcode('123456789', $generator::TYPE_CODE_128);
// 输出到浏览器
header('Content-Type: image/png');
echo $barcode;
// 保存到文件
file_put_contents('barcode.png', $barcode);
// 生成HTML形式的条形码
$htmlGenerator = new BarcodeGeneratorHTML();
$html = $htmlGenerator->getBarcode('123456789', $htmlGenerator::TYPE_CODE_128);
echo $html;
?>
支持的条形码类型
// Code 128
$barcode = $generator->getBarcode('123456', $generator::TYPE_CODE_128);
// Code 39
$barcode = $generator->getBarcode('ABC123', $generator::TYPE_CODE_39);
// EAN-13
$barcode = $generator->getBarcode('5901234123457', $generator::TYPE_EAN_13);
// UPC-A
$barcode = $generator->getBarcode('123456789012', $generator::TYPE_UPC_A);
// QR Code (需要额外安装)
$barcode = $generator->getBarcode('https://example.com', $generator::TYPE_QR_CODE);
使用 GD库手动生成
如果您不想使用第三方库,可以用GD库手动绘制:
<?php
function generateBarcode($code, $width = 300, $height = 80) {
// 创建图像
$image = imagecreate($width, $height);
// 设置颜色
$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
// 填充背景
imagefill($image, 0, 0, $white);
// 简单的Code 39编码表(简化版)
$code39 = [
'0' => '0001101000',
'1' => '1001000010',
// ... 其他字符映射
];
// 将代码转换为二进制表示
$binaryString = '';
foreach (str_split($code) as $char) {
$binaryString .= $code39[$char] ?? '0000000000';
}
// 绘制条形码
$x = 10;
$barWidth = 2;
$yTop = 10;
$yBottom = $height - 20;
foreach (str_split($binaryString) as $bit) {
if ($bit === '1') {
imagefilledrectangle($image, $x, $yTop, $x + $barWidth, $yBottom, $black);
}
$x += $barWidth;
}
// 添加文字
$textY = $height - 15;
imagestring($image, 3, ($width - strlen($code) * 8) / 2, $textY, $code, $black);
// 输出图像
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
}
// 使用示例
generateBarcode('123456789');
?>
在线API服务
使用在线条形码生成API:
<?php
function getBarcodeFromAPI($code) {
// 使用Barcode API服务
$url = "https://barcodeapi.org/api/128/{$code}";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$image = curl_exec($ch);
curl_close($ch);
// 保存或输出
header('Content-Type: image/png');
echo $image;
}
// 使用示例
getBarcodeFromAPI('123456789');
?>
完整示例:生成并保存到服务器
<?php
require 'vendor/autoload.php';
use Picqer\Barcode\BarcodeGeneratorPNG;
use Picqer\Barcode\BarcodeGeneratorJPG;
use Picqer\Barcode\BarcodeGeneratorSVG;
class BarcodeGenerator {
private $generator;
public function __construct() {
$this->generator = new BarcodeGeneratorPNG();
}
/**
* 生成条形码
* @param string $code 条形码内容
* @param string $type 条形码类型
* @param array $options 选项
* @return string 文件路径
*/
public function generate($code, $type = 'TYPE_CODE_128', $options = []) {
$width = $options['width'] ?? 2;
$height = $options['height'] ?? 30;
$color = $options['color'] ?? [0, 0, 0];
$barcode = $this->generator->getBarcode(
$code,
$this->generator::{$type},
$width,
$height,
$color
);
// 生成文件名
$filename = 'barcode_' . time() . '_' . uniqid() . '.png';
$path = __DIR__ . '/uploads/' . $filename;
// 确保目录存在
if (!file_exists(dirname($path))) {
mkdir(dirname($path), 0777, true);
}
// 保存文件
file_put_contents($path, $barcode);
return $filename;
}
}
// 使用示例
$barcodeGen = new BarcodeGenerator();
$filename = $barcodeGen->generate('1234567890', 'TYPE_CODE_128');
echo "条形码已生成: <img src='/uploads/$filename'>";
?>
在HTML中显示条形码
<!DOCTYPE html>
<html>
<head>条形码生成</title>
</head>
<body>
<form method="POST">
<input type="text" name="code" placeholder="输入条形码内容" required>
<select name="type">
<option value="TYPE_CODE_128">Code 128</option>
<option value="TYPE_CODE_39">Code 39</option>
<option value="TYPE_EAN_13">EAN-13</option>
<option value="TYPE_UPC_A">UPC-A</option>
</select>
<button type="submit">生成条形码</button>
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
require 'vendor/autoload.php';
use Picqer\Barcode\BarcodeGeneratorPNG;
use Picqer\Barcode\BarcodeGeneratorHTML;
$code = $_POST['code'];
$type = $_POST['type'];
$generator = new BarcodeGeneratorHTML();
try {
$barcode = $generator->getBarcode($code, $generator::{$type});
echo "<div class='barcode-container'>";
echo "<h3>条形码:</h3>";
echo $barcode;
echo "<p>内容: {$code}</p>";
echo "</div>";
} catch (Exception $e) {
echo "错误: " . $e->getMessage();
}
}
?>
</body>
</html>
提示
- 选择正确的条形码类型:根据应用场景选择(零售业用EAN-13,物流用Code 128等)
- 确保字符兼容性:某些条形码类型只支持数字或特定字符
- 检查宽度和高度:确保条形码可被扫描设备读取
- 测试扫描:生成后建议用手机或扫描设备测试
推荐使用 picqer/php-barcode-generator 库,因为它功能强大、易于使用,并且持续更新维护。