Introduction | Zend_Barcode Programmer's Reference Guide | Zend_Barcode Objects

# Barcode creation using Zend_Barcode class

# Using Zend_Barcode::factory

Zend_Barcode uses a factory method to create an instance of a renderer that extends Zend_Barcode_Renderer_RendererAbstract. The factory method accepts five arguments.

1. The name of the barcode format (e.g., "code39") (required)

2. The name of the renderer (e.g., "image") (required)

3. Options to pass to the barcode object (an array or Zend_Config object) (optional)

4. Options to pass to the renderer object (an array or Zend_Config object) (optional)

5. 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`)

Example #1 Getting a Renderer with Zend_Barcode::factory()

Zend_Barcode::factory() instantiates barcode objects and renderers and ties them together. In this first example, we will use the Code39 barcode type together with the Image renderer.

1. // Only the text to draw is required
2. $barcodeOptions = array('text' => 'ZEND-FRAMEWORK');
4. // No required options
5. $rendererOptions = array();
6. $renderer = Zend_Barcode::factory(
7. 'code39', 'image', $barcodeOptions, $rendererOptions
8. );

Example #2 Using Zend_Barcode::factory() with Zend_Config objects

You may pass a Zend_Config object to the factory in order to create the necessary objects. The following example is functionally equivalent to the previous.

1. // Using only one Zend_Config object
2. $config = new Zend_Config(array(
3. 'barcode' => 'code39',
4. 'barcodeParams' => array('text' => 'ZEND-FRAMEWORK'),
5. 'renderer' => 'image',
6. 'rendererParams' => array('imageType' => 'gif'),
7. ));
9. $renderer = Zend_Barcode::factory($config);

# 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.

Example #3 Drawing a barcode with the renderer object

1. // Only the text to draw is required
2. $barcodeOptions = array('text' => 'ZEND-FRAMEWORK');
4. // No required options
5. $rendererOptions = array();
7. // Draw the barcode in a new image,
8. $imageResource = Zend_Barcode::factory(
9. 'code39', 'image', $barcodeOptions, $rendererOptions
10. )->draw();

Example #4 Drawing a barcode with Zend_Barcode::draw()

1. // Only the text to draw is required
2. $barcodeOptions = array('text' => 'ZEND-FRAMEWORK');
4. // No required options
5. $rendererOptions = array();
7. // Draw the barcode in a new image,
8. $imageResource = Zend_Barcode::draw(
9. 'code39', 'image', $barcodeOptions, $rendererOptions
10. );

# Renderering 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.

Example #5 Renderering a barcode with the renderer object

1. // Only the text to draw is required
2. $barcodeOptions = array('text' => 'ZEND-FRAMEWORK');
4. // No required options
5. $rendererOptions = array();
7. // Draw the barcode in a new image,
8. // send the headers and the image
9. Zend_Barcode::factory(
10. 'code39', 'image', $barcodeOptions, $rendererOptions
11. )->render();

This will generate this barcode:

Example #6 Renderering a barcode with Zend_Barcode::render()

1. // Only the text to draw is required
2. $barcodeOptions = array('text' => 'ZEND-FRAMEWORK');
4. // No required options
5. $rendererOptions = array();
7. // Draw the barcode in a new image,
8. // send the headers and the image
9. Zend_Barcode::render(
10. 'code39', 'image', $barcodeOptions, $rendererOptions
11. );

This will generate the same barcode as the previous example.

Introduction | Zend_Barcode Programmer's Reference Guide | Zend_Barcode Objects