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.
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.
1 2 3 4 5 6 7 8 9 10 | 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
);
|
Using Zend\Barcode\Barcode::factory() with Zend\Config\Config objects
You may pass a Zend\Config\Config object to the factory in order to create the necessary objects. The following example is functionally equivalent to the previous.
1 2 3 4 5 6 7 8 9 10 11 12 | use Zend\Config\Config;
use Zend\Barcode\Barcode;
// Using only one Zend\Config\Config object
$config = new Config(array(
'barcode' => 'code39',
'barcodeParams' => array('text' => 'ZEND-FRAMEWORK'),
'renderer' => 'image',
'rendererParams' => array('imageType' => 'gif'),
));
$renderer = Barcode::factory($config);
|
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
1 2 3 4 5 6 7 8 9 10 11 12 | 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()
1 2 3 4 5 6 7 8 9 10 11 12 | 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
);
|
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
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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();
|
This will generate this barcode:
Rendering a barcode with Zend\Barcode\Barcode::render()
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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
);
|
This will generate the same barcode as the previous example.
The source code of this file is hosted on GitHub. Everyone can update and fix errors in this document with few clicks - no downloads needed.