1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
<?xml version="1.0" encoding="UTF-8"?>
<!-- Reviewed: no -->
<sect1 id="zend.barcode.creation">
<title>Barcode creation using Zend_Barcode class</title>
<sect2 id="zend.barcode.creation.configuration">
<title>Using Zend_Barcode::factory</title>
<para>
<classname>Zend_Barcode</classname> uses a factory method to create an instance of a
renderer that extends <classname>Zend_Barcode_Renderer_RendererAbstract</classname>. The
factory method accepts five arguments.
</para>
<orderedlist>
<listitem>
<para>The name of the barcode format (e.g., "code39") (required)</para>
</listitem>
<listitem>
<para>The name of the renderer (e.g., "image") (required)</para>
</listitem>
<listitem>
<para>
Options to pass to the barcode object (an array or
<classname>Zend_Config</classname> object) (optional)
</para>
</listitem>
<listitem>
<para>
Options to pass to the renderer object (an array or
<classname>Zend_Config</classname> object) (optional)
</para>
</listitem>
<listitem>
<para>
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 <constant>TRUE</constant>)
</para>
</listitem>
</orderedlist>
<example id="zend.barcode.creation.configuration.example-1">
<title>Getting a Renderer with Zend_Barcode::factory()</title>
<para>
<methodname>Zend_Barcode::factory()</methodname> instantiates barcode objects and
renderers and ties them together. In this first example, we will use the
<emphasis>Code39</emphasis> barcode type together with the
<emphasis>Image</emphasis> renderer.
</para>
<programlisting language="php"><![CDATA[
// Only the text to draw is required
$barcodeOptions = array('text' => 'ZEND-FRAMEWORK');
// No required options
$rendererOptions = array();
$renderer = Zend_Barcode::factory(
'code39', 'image', $barcodeOptions, $rendererOptions
);
]]></programlisting>
</example>
<example id="zend.barcode.creation.configuration.example-2">
<title>Using Zend_Barcode::factory() with Zend_Config objects</title>
<para>
You may pass a <classname>Zend_Config</classname> object to the factory in order to
create the necessary objects. The following example is functionally equivalent to
the previous.
</para>
<programlisting language="php"><![CDATA[
// Using only one Zend_Config object
$config = new Zend_Config(array(
'barcode' => 'code39',
'barcodeParams' => array('text' => 'ZEND-FRAMEWORK'),
'renderer' => 'image',
'rendererParams' => array('imageType' => 'gif'),
));
$renderer = Zend_Barcode::factory($config);
]]></programlisting>
</example>
</sect2>
<sect2 id="zend.barcode.creation.drawing">
<title>Drawing a barcode</title>
<para>
When you <emphasis>draw</emphasis> the barcode, you retrieve the resource in which the
barcode is drawn. To draw a barcode, you can call the <methodname>draw()</methodname> of
the renderer, or simply use the proxy method provided by
<classname>Zend_Barcode</classname>.
</para>
<example id="zend.barcode.creation.drawing.example-1">
<title>Drawing a barcode with the renderer object</title>
<programlisting language="php"><![CDATA[
// 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 = Zend_Barcode::factory(
'code39', 'image', $barcodeOptions, $rendererOptions
)->draw();
]]></programlisting>
</example>
<example id="zend.barcode.creation.drawing.example-2">
<title>Drawing a barcode with Zend_Barcode::draw()</title>
<programlisting language="php"><![CDATA[
// 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 = Zend_Barcode::draw(
'code39', 'image', $barcodeOptions, $rendererOptions
);
]]></programlisting>
</example>
</sect2>
<sect2 id="zend.barcode.creation.renderering">
<title>Renderering a barcode</title>
<para>
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
<methodname>render()</methodname> method of the renderer or simply use the proxy method
provided by <classname>Zend_Barcode</classname>.
</para>
<example id="zend.barcode.creation.renderering.example-1">
<title>Renderering a barcode with the renderer object</title>
<programlisting language="php"><![CDATA[
// 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
Zend_Barcode::factory(
'code39', 'image', $barcodeOptions, $rendererOptions
)->render();
]]></programlisting>
<para>
This will generate this barcode:
</para>
<para>
<inlinegraphic width="275" align="center" valign="middle"
fileref="figures/zend.barcode.introduction.example-1.png" format="PNG"/>
</para>
</example>
<example id="zend.barcode.creation.renderering.example-2">
<title>Renderering a barcode with Zend_Barcode::render()</title>
<programlisting language="php"><![CDATA[
// 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
Zend_Barcode::render(
'code39', 'image', $barcodeOptions, $rendererOptions
);
]]></programlisting>
<para>
This will generate the same barcode as the previous example.
</para>
</example>
</sect2>
</sect1>
|