如何使用 PHP 生成条码并将其显示为同一页面上的图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16022496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:23:40  来源:igfitidea点击:

How to Generate Barcode using PHP and Display it as an Image on the same page

phpajaxtwitter-bootstrapbarcode

提问by sudo

I want to Generate Bar code (any type) using PHP

我想使用 PHP 生成条码(任何类型)

I am having a variable where i store a code

我有一个存储代码的变量

<?php
    $code= 'f5c9b918c5';
?>

so just i want to generate a barcode of this and echo the barcode image where i want ..... please help

所以我只想生成一个条形码并在我想要的地方回显条形码图像.....请帮忙

回答by Praveen Kumar Purushothaman

There is a library for this BarCode PHP. You just need to include a few files:

这个BarCode PHP有一个库。你只需要包含几个文件:

require_once('class/BCGFontFile.php');
require_once('class/BCGColor.php');
require_once('class/BCGDrawing.php');

You can generate many types of barcodes, namely 1D or 2D. Add the required library:

您可以生成多种类型的条形码,即一维或二维。添加所需的库:

require_once('class/BCGcode39.barcode.php');

Generate the colours:

生成颜色:

// The arguments are R, G, and B for color.
$colorFront = new BCGColor(0, 0, 0);
$colorBack = new BCGColor(255, 255, 255);

After you have added all the codes, you will get this way:

添加完所有代码后,您将得到这样的结果:

Example

例子

Since several have asked for an example here is what I was able to do to get it done

由于有几个人要求在这里举个例子,这是我能够做的事情

require_once('class/BCGFontFile.php');
require_once('class/BCGColor.php');
require_once('class/BCGDrawing.php');

require_once('class/BCGcode128.barcode.php');

header('Content-Type: image/png');

$color_white = new BCGColor(255, 255, 255);

$code = new BCGcode128();
$code->parse('HELLO');

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

$drawing->draw();
$drawing->finish(BCGDrawing::IMG_FORMAT_PNG);

If you want to actually create the image file so you can save it then change

如果您想实际创建图像文件以便您可以保存它然后更改

$drawing = new BCGDrawing('', $color_white);

to

$drawing = new BCGDrawing('image.png', $color_white);