标签归档:barcode

Zend Framework 2.x 之 Zend\Barcode

Zend\Barcode是条形码的实现。这个工具实际很常用。注意,没有包含二维码。

Introduction to Zend\Barcode
Overview

Zend\Barcode\Barcode provides a generic way to generate barcodes. The Zend\Barcode component is divided into two subcomponents: barcode objects and renderers. Objects allow you to create barcodes independently of the renderer. Renderer allow you to draw barcodes based on the support required.(分两个对象,条形码 和 渲染器)

Barcode creation using Zend\Barcode\Barcode class

Using Zend\Barcode\Barcode::factory

Zend\Barcode\Barcode uses a factory method to create an instance of a renderer that extends Zend\Barcode\Renderer\AbstractRenderer. The factory method accepts five arguments.(工厂方法创建一个渲染器,有5个参数)

*The name of the barcode format (e.g., “code39”) or a Traversable object (required) 条码格式
*The name of the renderer (e.g., “image”) (required) 渲染器(就是制定要什么渲染器)
*Options to pass to the barcode object (an array or a Traversable object) (optional) 传递给条码对象的参数,可选
*Options to pass to the renderer object (an array or a Traversable object) (optional) 传递给渲染器的参数,可选
*Boolean to indicate whether or not to automatically render errors. If an exception occurs, the provided barcode object will be replaced with an Error representation (optional default TRUE) 出错时替代品

Getting a Renderer with Zend\Barcode\Barcode::factory()
Zend\Barcode\Barcode::factory() instantiates barcode classes and renderers and ties them together(把条码对象和渲染器对象搞在一起). In this first example, we will use the Code39 barcode type together with the Image renderer.

use Zend\Barcode\Barcode;

// Only the text to draw is required
$barcodeOptions = array('text' => 'ZEND-FRAMEWORK');

// No required options
$rendererOptions = array();
$renderer = Barcode::factory(
    'code39', 'image', $barcodeOptions, $rendererOptions
);

Drawing a barcode

When you draw the barcode, you retrieve the resource in which the barcode is drawn. To draw a barcode, you can call the draw() of the renderer, or simply use the proxy method provided by Zend\Barcode\Barcode.

Drawing a barcode with the renderer object

use Zend\Barcode\Barcode;

// Only the text to draw is required
$barcodeOptions = array('text' => 'ZEND-FRAMEWORK');

// No required options
$rendererOptions = array();

// Draw the barcode in a new image,
$imageResource = Barcode::factory(
    'code39', 'image', $barcodeOptions, $rendererOptions
)->draw();

Drawing a barcode with Zend\Barcode\Barcode::draw()

use Zend\Barcode\Barcode;

// Only the text to draw is required
$barcodeOptions = array('text' => 'ZEND-FRAMEWORK');

// No required options
$rendererOptions = array();

// Draw the barcode in a new image,
$imageResource = Barcode::draw(
    'code39', 'image', $barcodeOptions, $rendererOptions
);

Rendering a barcode

When you render a barcode, you draw the barcode, you send the headers and you send the resource (e.g. to a browser). To render a barcode, you can call the render() method of the renderer or simply use the proxy method provided by Zend\Barcode\Barcode.

Rendering a barcode with the renderer object

use Zend\Barcode\Barcode;

// Only the text to draw is required
$barcodeOptions = array('text' => 'ZEND-FRAMEWORK');

// No required options
$rendererOptions = array();

// Draw the barcode in a new image,
// send the headers and the image
Barcode::factory(
    'code39', 'image', $barcodeOptions, $rendererOptions
)->render();

Rendering a barcode with Zend\Barcode\Barcode::render()

use Zend\Barcode\Barcode;

// Only the text to draw is required
$barcodeOptions = array('text' => 'ZEND-FRAMEWORK');

// No required options
$rendererOptions = array();

// Draw the barcode in a new image,
// send the headers and the image
Barcode::render(
    'code39', 'image', $barcodeOptions, $rendererOptions
);

Barcode::render()比较直接,内部跟先factory后draw然后rend()应该是一致。使用上,基本就是这样了。一个条码对象,一个渲染器。还需要知道的是针对这两个对象的配置参数。

Zend\Barcode Objects

Barcode objects allow you to generate barcodes independently独立 of the rendering support(独立存在). After generation, you can retrieve the barcode as an array of drawing instructions that you can provide to a renderer.(大概是批量的意思吧)

Objects have a large number of options. Most of them are common to all objects. These options can be set in three ways:(参数传递的方法)
*As an array or a Traversable object passed to the constructor.
*As an array passed to the setOptions() method.
*Via individual setters for each configuration type.

Different ways to parameterize a barcode object

use Zend\Barcode\Object;

$options = array('text' => 'ZEND-FRAMEWORK', 'barHeight' => 40);

// Case 1: constructor
$barcode = new Object\Code39($options);

// Case 2: setOptions()
$barcode = new Object\Code39();
$barcode->setOptions($options);

// Case 3: individual setters
$barcode = new Object\Code39();
$barcode->setText('ZEND-FRAMEWORK')
        ->setBarHeight(40);

参数多,使用数组传递比较直观。

Common Options 公共配置
In the following list, the values have no units; we will use the term “unit.” For example, the default value of the “thin bar” is “1 unit”. The real units depend on the rendering support (see the renderers documentation for more information). Setters are each named by uppercasing the initial letter of the option and prefixing the name with “set” (e.g. “barHeight” becomes “setBarHeight”). All options have a corresponding getter prefixed with “get” (e.g. “getBarHeight”). Available options are:

Common Options
Option Data Type Default Value Description
barcodeNamespace String Zend\Barcode\Object Namespace of the barcode; for example, if you need to extend the embedding objects
barHeight Integer 50 Height of the bars
barThickWidth Integer 3 Width of the thick bar
barThinWidth Integer 1 Width of the thin bar
factor Integer, Float, String or Boolean 1 Factor by which to multiply bar widths and font sizes (barHeight, barThinWidth, barThickWidth and fontSize)
foreColor Integer 0x000000 (black) Color of the bar and the text. Could be provided as an integer or as a HTML value (e.g. “#333333”)
backgroundColor Integer or String 0xFFFFFF (white) Color of the background. Could be provided as an integer or as a HTML value (e.g. “#333333”)
orientation Integer, Float, String or Boolean 0 Orientation of the barcode
font String or Integer NULL Font path to a TTF font or a number between 1 and 5 if using image generation with GD (internal fonts)
fontSize Float 10 Size of the font (not applicable with numeric fonts)
withBorder Boolean FALSE Draw a border around the barcode and the quiet zones
withQuietZones Boolean TRUE Leave a quiet zone before and after the barcode
drawText Boolean TRUE Set if the text is displayed below the barcode
stretchText Boolean FALSE Specify if the text is stretched all along the barcode
withChecksum Boolean FALSE Indicate whether or not the checksum is automatically added to the barcode
withChecksumInText Boolean FALSE Indicate whether or not the checksum is displayed in the textual representation
text String NULL The text to represent as a barcode

Particular case of static setBarcodeFont() 条码的字体(比较特殊)
You can set a common font for all your objects by using the static method Zend\Barcode\Barcode::setBarcodeFont(). This value can be always be overridden for individual objects by using the setFont() method.
(调用这个方法,可以给所有条码对象设置相同字体)

use Zend\Barcode\Barcode;

// In your bootstrap:
Barcode::setBarcodeFont('my_font.ttf');

// Later in your code:
Barcode::render(
    'code39',
    'pdf',
    array('text' => 'ZEND-FRAMEWORK')
); // will use 'my_font.ttf'

// or:
Barcode::render(
    'code39',
    'image',
    array(
        'text' => 'ZEND-FRAMEWORK',
        'font' => 3
    )
); // will use the 3rd GD internal font

Common Additional Getters

<table border="1" id="zend-barcode-objects-common-getters-table" class="docutils"><caption>Common Getters</caption>
<colgroup><col width="21%"><col width="6%"><col width="73%"></colgroup><thead valign="bottom"><tr class="row-odd"><th class="head">Getter</th>
<th class="head">Data Type</th>
<th class="head">Description</th>
</tr></thead><tbody valign="top"><tr class="row-even"><td>getType()</td>
<td>String</td>
<td>Return the name of the barcode class without the namespace (e.g. Zend\Barcode\Object\Code39 returns simply “code39”)</td>
</tr><tr class="row-odd"><td>getRawText()</td>
<td>String</td>
<td>Return the original text provided to the object</td>
</tr><tr class="row-even"><td>getTextToDisplay()</td>
<td>String</td>
<td>Return the text to display, including, if activated, the checksum value</td>
</tr><tr class="row-odd"><td>getQuietZone()</td>
<td>Integer</td>
<td>Return the size of the space needed before and after the barcode without any drawing</td>
</tr><tr class="row-even"><td>getInstructions()</td>
<td>Array</td>
<td>Return drawing instructions as an array.</td>
</tr><tr class="row-odd"><td>getHeight($recalculate = false)</td>
<td>Integer</td>
<td>Return the height of the barcode calculated after possible rotation</td>
</tr><tr class="row-even"><td>getWidth($recalculate = false)</td>
<td>Integer</td>
<td>Return the width of the barcode calculated after possible rotation</td>
</tr><tr class="row-odd"><td>getOffsetTop($recalculate = false)</td>
<td>Integer</td>
<td>Return the position of the top of the barcode calculated after possible rotation</td>
</tr><tr class="row-even"><td>getOffsetLeft($recalculate = false)</td>
<td>Integer</td>
<td>Return the position of the left of the barcode calculated after possible rotation</td>
</tr></tbody></table>
不同类型的条形码对象,有自己特定参数。

Zend\Barcode Renderers

Renderers have some common options. These options can be set in three ways:
*As an array or a Traversable object passed to the constructor.
*As an array passed to the setOptions() method.
*As discrete values passed to individual setters.

Different ways to parameterize a renderer object

use Zend\Barcode\Renderer;

$options = array('topOffset' => 10);

// Case 1
$renderer = new Renderer\Pdf($options);

// Case 2
$renderer = new Renderer\Pdf();
$renderer->setOptions($options);

// Case 3
$renderer = new Renderer\Pdf();
$renderer->setTopOffset(10);

Common Options

In the following list, the values have no unit; we will use the term “unit.” For example, the default value of the “thin bar” is “1 unit.” The real units depend on the rendering support. The individual setters are obtained by uppercasing the initial letter of the option and prefixing the name with “set” (e.g. “barHeight” => “setBarHeight”). All options have a correspondent getter prefixed with “get” (e.g. “getBarHeight”). Available options are:

Common Options
Option Data Type Default Value Description
rendererNamespace String Zend\Barcode\Renderer Namespace of the renderer; for example, if you need to extend the renderers
horizontalPosition String “left” Can be “left”, “center” or “right”. Can be useful with PDF or if the setWidth() method is used with an image renderer.
verticalPosition String “top” Can be “top”, “middle” or “bottom”. Can be useful with PDF or if the setHeight() method is used with an image renderer.
leftOffset Integer 0 Top position of the barcode inside the renderer. If used, this value will override the “horizontalPosition” option.
topOffset Integer 0 Top position of the barcode inside the renderer. If used, this value will override the “verticalPosition” option.
automaticRenderError Boolean FALSE Whether or not to automatically render errors. If an exception occurs, the provided barcode object will be replaced with an Error representation. Note that some errors (or exceptions) can not be rendered.
moduleSize Float 1 Size of a rendering module in the support.
barcode Zend\Barcode\Object NULL The barcode object to render.

An additional getter exists: getType(). It returns the name of the renderer class without the namespace (e.g. Zend\Barcode\Renderer\Image returns “image”).

Zend\Barcode\Renderer\Image

The Image renderer will draw the instruction list of the barcode object in an image resource. The component requires the GD extension. The default width of a module is 1 pixel.

Available options are:

Zend\Barcode\Renderer\Image Options
Option Data Type Default Value Description
height Integer 0 Allow you to specify the height of the result image. If “0”, the height will be calculated by the barcode object.
width Integer 0 Allow you to specify the width of the result image. If “0”, the width will be calculated by the barcode object.
imageType String “png” Specify the image format. Can be “png”, “jpeg”, “jpg” or “gif”.

一般都会渲染为一个图片输出。