ch24s08.html 23.6 KB
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Format options for barcodes</title><link rel="stylesheet" type="text/css" href="manual.css"><meta name="generator" content="DocBook XSL Stylesheets V1.76.0"><link rel="home" href="index.html" title="JpGraph Manual"><link rel="up" href="ch24.html" title="Chapter 24. Linear Barcodes (One Dimensional Barcodes)"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Format options for barcodes</th></tr><tr><td width="20%" align="left"> </td><th width="60%" align="center">Chapter 24. Linear Barcodes (One Dimensional Barcodes)</th><td width="20%" align="right"> </td></tr></table><hr></div><div class="sect1" title="Format options for barcodes"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2596824"></a>Format options for barcodes</h2></div></div></div>
            
            <div class="sect2" title="Adjusting the encodation process"><div class="titlepage"><div><div><h3 class="title"><a name="id2596909"></a>Adjusting the encodation process</h3></div></div></div>
                
                <p>For the encoding there is basically only one modification available. </p>
                <p>For those symbologies that include an optional checksum it is possible to
                    enable or disable this calculation.</p>
                <p>The following symbologies may have optional checksum character(s)</p>
                <p>
                    </p><div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem">
                            <p>Industrial 2 of 5</p>
                        </li><li class="listitem">
                            <p>Interleaved 2 of 5</p>
                        </li><li class="listitem">
                            <p>Code 39</p>
                        </li><li class="listitem">
                            <p>Code 39 Extended</p>
                        </li><li class="listitem">
                            <p>Code 11</p>
                        </li></ol></div><p>
                </p>
                <p>Turning on/off checksum calculation for other symbologies will have no affect.
                    Checksum calculation is enabled with a call to <code class="code">AddChecksum()</code> on the
                    chosen backend. </p>
                <p>For example to augment the previous "CODE 39" example to include the checksum
                    the code would be changed to</p>
                <p>
                    </p><div class="hl-main"><table class="hl-table" width="100%"><tr><td class="hl-gutter" align="right" valign="top"><pre>1
2
3
4
5
6
</pre></td><td class="hl-main" valign="top"><pre><span class="hl-inlinetags">&lt;?php</span><span class="hl-code">
</span><span class="hl-var">$symbology</span><span class="hl-code"> = </span><span class="hl-identifier">BarcodeFactory</span><span class="hl-code">::</span><span class="hl-identifier">Create</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">ENCODING_CODE39</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-var">$symbology</span><span class="hl-code">-&gt;</span><span class="hl-identifier">AddChecksum</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-var">$barcode</span><span class="hl-code"> = </span><span class="hl-identifier">BackendFactory</span><span class="hl-code"> ::</span><span class="hl-identifier">Create</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">IMAGE</span><span class="hl-quotes">'</span><span class="hl-code">, </span><span class="hl-var">$symbology</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-var">$barcode</span><span class="hl-code">-&gt;</span><span class="hl-identifier">Stroke</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">ABC123</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-inlinetags">?&gt;</span></pre></td></tr></table></div><p>
                </p>
                <p>Which would give the result shown in <a class="xref" href="ch24s08.html#fig.adding-chk-c39" title='Figure 24.6. Encoding "ABC123" with CODE 39 adding checksum (checksum=4).'>Figure 24.6. Encoding "ABC123" with CODE 39 adding checksum (checksum=4).</a></p>
                <p>
                    </p><div class="figure"><a name="fig.adding-chk-c39"></a><p class="title"><b>Figure 24.6. Encoding "ABC123" with CODE 39 adding checksum (checksum=4).</b></p><div class="figure-contents">
                        
                        <div class="mediaobject"><img src="images/barcode_c39_abc123_ex3.png" alt='Encoding "ABC123" with CODE 39 adding checksum (checksum=4).'></div>
                    </div></div><p><br class="figure-break">
                </p>
            </div>
            <div class="sect2" title="Selecting output format"><div class="titlepage"><div><div><h3 class="title"><a name="id2597037"></a>Selecting output format</h3></div></div></div>
                
                <p>The output format can be adjusted by specifying/creating the appropriate
                    backend. The library supports image and postscript (and encapsulated postscript)
                    backends. </p>
                <p>The backend is created by calling the static factory method </p>
                <p>
                    </p><div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem">
                            <p><code class="code">BackendFactory::Create($aBackend,$aEncoder,$aReport=false)</code></p>
                        </li></ul></div><p>
                </p>
                <p>So to create an image backend the following code is needed</p>
                <p>
                    </p><div class="hl-main"><table class="hl-table" width="100%"><tr><td class="hl-gutter" align="right" valign="top"><pre>1
</pre></td><td class="hl-main" valign="top"><pre><span class="hl-code">$barcode = BackendFactory::Create (BACKEND_IMAGE ,$symbology);</span></pre></td></tr></table></div><p>
                </p>
                <p>Where "<code class="code">$symbology</code>" is the chosen symbology as created by the
                        <code class="code">BarcodeFactory::Create()</code> factory method. Please note that both
                    factory functions are called as static methods.</p>
                <p>The output format is specified by using one of the following symbolic
                    defines</p>
                <p>
                    </p><div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem">
                            <p><code class="code">BACKEND_IMAGE</code>, Creates a standard JPEG or PNG
                                (default) image</p>
                        </li><li class="listitem">
                            <p><code class="code">BACKEND_PS</code>, Creates a standard postscript file as
                                output. It is possible to modify this output to become EPS
                                (Encapsulated postscript) by calling the <code class="code">SetEPS()</code>
                                method on the backend as the following code snippet shows</p>
                            <p>
                                </p><div class="hl-main"><table class="hl-table" width="100%"><tr><td class="hl-gutter" align="right" valign="top"><pre>1
2
</pre></td><td class="hl-main" valign="top"><pre><span class="hl-code">$barcode = BackendFactory::Create (BACKEND_PS,$symbology);
$barcode-&gt;SetEPS()</span></pre></td></tr></table></div><p>
                            </p>
                            <p>Please note that for the postscript backend the postscript code is
                                returned as a string from the <code class="code">Stroke()</code> method. </p>
                        </li></ol></div><p>
                </p>
                <p>To send the created stream (either image or postscript) back to the browser or
                    to a file the <code class="code">Backend::Stroke()</code> method shall be used. The parameter
                    to the Stroke() method shall be the string to be encoded.</p>
                <p>Assume we want to create an image that is sent back to the browser. We would
                    then use the following code</p>
                <p>
                    </p><div class="hl-main"><table class="hl-table" width="100%"><tr><td class="hl-gutter" align="right" valign="top"><pre>1
2
3
4
5
</pre></td><td class="hl-main" valign="top"><pre><span class="hl-inlinetags">&lt;?php</span><span class="hl-code">
</span><span class="hl-var">$symbology</span><span class="hl-code"> = </span><span class="hl-identifier">BarcodeFactory</span><span class="hl-code">::</span><span class="hl-identifier">Create</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">ENCODING_CODE128</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-var">$barcode</span><span class="hl-code"> = </span><span class="hl-identifier">BackendFactory</span><span class="hl-code"> ::</span><span class="hl-identifier">Create</span><span class="hl-brackets">(</span><span class="hl-identifier">BACKEND_IMAGE</span><span class="hl-code">, </span><span class="hl-var">$symbology</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-var">$barcode</span><span class="hl-code">-&gt;</span><span class="hl-identifier">Stroke</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">ABC123</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-inlinetags">?&gt;</span></pre></td></tr></table></div><p>
                </p>
                <p>It is also possible to write the barcode directly to a file by specifying a
                    second argument to the <code class="code">Stroke()</code> method above. So if we instead
                    wanted the barcode to be stored in the file
                        "<code class="filename">/tmp/barcode.png</code>" we could write </p>
                <p>
                    </p><div class="hl-main"><table class="hl-table" width="100%"><tr><td class="hl-gutter" align="right" valign="top"><pre>1
2
3
</pre></td><td class="hl-main" valign="top"><pre><span class="hl-code">$symbology = BarcodeFactory::Create (ENCODING_CODE128 );
$barcode = BackendFactory ::Create(BACKEND_IMAGE, $symbology);
$barcode-&gt;Stroke('ABC123','/tmp/barcode.png');</span></pre></td></tr></table></div><p>
                </p>
                <p>
                    </p><div class="note" title="Note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3>
                        <p>There is no automatic added extension to the file name.</p>
                    </div><p>
                </p>
                <p>Again, please note that for the Postscript background the
                        <code class="code">Backend::Stroke()</code> method normally returns the postscript file
                    as a string if everything went well.</p>
            </div>
            <div class="sect2" title="Writing barcodes to a file"><div class="titlepage"><div><div><h3 class="title"><a name="id2597044"></a>Writing barcodes to a file</h3></div></div></div>
                
                <p>This is done by adding a second argument, the file name, to the
                        <code class="code">Backend::Stroke()</code> method. This works for all backends. The file
                    name should be an absolute path name. Since it is the PHP process that writes
                    the file the permissions must allow the PHP process to write to the directory if
                    PHP is called from a browser. If the command line version of PHP is used this
                    does of course not apply.</p>
            </div>
            <div class="sect2" title="Hiding the human readable text"><div class="titlepage"><div><div><h3 class="title"><a name="id2597234"></a>Hiding the human readable text</h3></div></div></div>
                
                <p>
                    </p><div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem">
                            <p><code class="code">Backend::HideText($aHide=true)</code></p>
                        </li></ul></div><p>
                </p>
                <p>The human readable text is the string that can optionally be displayed at the
                    bottom of the bar. By default this is enabled. </p>
            </div>
            <div class="sect2" title="Adjusting the module width"><div class="titlepage"><div><div><h3 class="title"><a name="id2597265"></a>Adjusting the module width</h3></div></div></div>
                
                <p>
                    </p><div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem">
                            <p><code class="code">Backend::SetModuleWidth($aWidth)</code></p>
                        </li></ul></div><p>
                </p>
                <p>There are however some subtle facts regarding the module width and backend
                    that needs to be explained. </p>
                <p>
                    </p><div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem">
                            <p>For image type backends the module width specifies the number of
                                pixels used for a module.</p>
                        </li><li class="listitem">
                            <p>For Postscript (and Encapsulated PS) backends the module width
                                specifies the width in points (i.e. 1/72 inch). </p>
                        </li></ol></div><p>
                </p>
                <p>This also means that for image type backends only integer values makes
                    sense.</p>
                <p>
                    </p><div class="caution" title="Caution" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Caution</h3>
                        <p>Depending on the quality of the printer (and paper) very small module
                            width might not be readable with all bar code readers. For images it is
                            therefore recommended to use "2" pixels as the minimum module width and
                            for postscript output the minimum recommended width is "0.8" pt.</p>
                    </div><p>
                </p>
                <p>The following code shows how to both change the module width to 2 pixels and
                    hide the human readable text </p>
                <p>
                    </p><div class="hl-main"><table class="hl-table" width="100%"><tr><td class="hl-gutter" align="right" valign="top"><pre>1
2
3
4
5
6
7
</pre></td><td class="hl-main" valign="top"><pre><span class="hl-inlinetags">&lt;?php</span><span class="hl-code">
</span><span class="hl-var">$symbology</span><span class="hl-code"> = </span><span class="hl-identifier">BarcodeFactory</span><span class="hl-code">::</span><span class="hl-identifier">Create</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">ENCODING_CODE39</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-var">$barcode</span><span class="hl-code"> = </span><span class="hl-identifier">BackendFactory</span><span class="hl-code"> ::</span><span class="hl-identifier">Create</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">IMAGE</span><span class="hl-quotes">'</span><span class="hl-code">, </span><span class="hl-var">$symbology</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-var">$barcode</span><span class="hl-code"> -&gt;</span><span class="hl-identifier">SetModuleWidth</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-number">2</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-var">$barcode</span><span class="hl-code"> -&gt;</span><span class="hl-identifier">HideText</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-var">$barcode</span><span class="hl-code"> -&gt;</span><span class="hl-identifier">Stroke</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">ABC123</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-inlinetags">?&gt;</span></pre></td></tr></table></div><p>
                </p>
                <p>which would give the result shown in <a class="xref" href="ch24s08.html#fig.barcode-c39-ex2" title='Figure 24.7. Encoding "ABC123" with CODE 39, hiding the text.'>Figure 24.7. Encoding "ABC123" with CODE 39, hiding the text.</a>
                    below</p>
                <p>
                    </p><div class="figure"><a name="fig.barcode-c39-ex2"></a><p class="title"><b>Figure 24.7. Encoding "ABC123" with CODE 39, hiding the text.</b></p><div class="figure-contents">
                        
                        <div class="mediaobject"><img src="images/barcode_c39_abc123_ex2.png" alt='Encoding "ABC123" with CODE 39, hiding the text.'></div>
                    </div></div><p><br class="figure-break">
                </p>
            </div>
            <div class="sect2" title="Setting vertical or horizontal layout"><div class="titlepage"><div><div><h3 class="title"><a name="id2597395"></a> Setting vertical or horizontal layout</h3></div></div></div>
                
                <p>
                    </p><div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem">
                            <p><code class="code">Backend::SetVertical($aVertical=true)</code></p>
                        </li></ul></div><p>
                </p>
                <p>Will rotate the barcode 90 degrees to create a vertical view of the
                    barcode.</p>
            </div>
            <div class="sect2" title="Adjusting height of bar code"><div class="titlepage"><div><div><h3 class="title"><a name="id2597418"></a>Adjusting height of bar code</h3></div></div></div>
                
                <p>
                    </p><div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem">
                            <p>
                                <code class="code">Backend::SetHeight($aHeight)</code></p>
                        </li></ul></div><p>
                </p>
                <p>The height of the bar codes is specified with the
                        <code class="code">Backend::SetHeight()</code> method. For images the height is
                    interpreted as pixels and for postscript files it is interpreted as points (1 pt
                    = 1/72 inch)</p>
            </div>
            <div class="sect2" title="Scaling of bar codes"><div class="titlepage"><div><div><h3 class="title"><a name="id2597385"></a>Scaling of bar codes</h3></div></div></div>
                
                <p>
                    </p><div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem">
                            <p><code class="code">Backend::SetScale($aScaleFactor)</code></p>
                        </li></ul></div><p>
                </p>
                <p>The scale factor is real number and specifies a scale factor for the overall
                    barcode image. </p>
            </div>
            <div class="sect2" title="Add frame around bar code"><div class="titlepage"><div><div><h3 class="title"><a name="id2597465"></a>Add frame around bar code</h3></div></div></div>
                
                <p>
                    </p><div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem">
                            <p><code class="code">Backend::ShowFrame($aFlag=true)</code></p>
                        </li></ul></div><p>
                </p>
                <p>This method will enable a frame around the edges of the barcode image</p>
            </div>
            <div class="sect2" title="Examples of adjusting the output"><div class="titlepage"><div><div><h3 class="title"><a name="id2597509"></a>Examples of adjusting the output</h3></div></div></div>
                
                <p>The following example outputs a postscript file representing the bar code with
                    a module width of 1.2 pt, using a vertical layout and scaling the image 2 times.
                    For this example we are using CODE 39 with a checksum (which is automatically
                    generated) </p>
                <p>
                    </p><div class="hl-main"><table class="hl-table" width="100%"><tr><td class="hl-gutter" align="right" valign="top"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="hl-main" valign="top"><pre><span class="hl-inlinetags">&lt;?php</span><span class="hl-code">
</span><span class="hl-var">$symbology</span><span class="hl-code"> = </span><span class="hl-identifier">BarcodeFactory</span><span class="hl-code">::</span><span class="hl-identifier">Create</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">ENCODING_CODE128</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-var">$barcode</span><span class="hl-code"> = </span><span class="hl-identifier">BackendFactory</span><span class="hl-code"> ::</span><span class="hl-identifier">Create</span><span class="hl-brackets">(</span><span class="hl-identifier">BACKEND_PS</span><span class="hl-code">, </span><span class="hl-var">$symbology</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-var">$barcode</span><span class="hl-code">-&gt;</span><span class="hl-identifier">SetVertical</span><span class="hl-brackets">(</span><span class="hl-reserved">true</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-var">$barcode</span><span class="hl-code">-&gt;</span><span class="hl-identifier">Scale</span><span class="hl-brackets">(</span><span class="hl-number">2</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-var">$barcode</span><span class="hl-code">-&gt;</span><span class="hl-identifier">SetModuleWidth</span><span class="hl-brackets">(</span><span class="hl-number">1</span><span class="hl-number">.2</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-var">$barcode</span><span class="hl-code"> -&gt;</span><span class="hl-identifier">Stroke</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">ABC123</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code"> ) </span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-inlinetags">?&gt;</span></pre></td></tr></table></div><p>
                </p>
            </div>
        </div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"> </td><td width="20%" align="center"><a accesskey="u" href="ch24.html">Up</a></td><td width="40%" align="right"> </td></tr><tr><td width="40%" align="left" valign="top"> </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> </td></tr></table></div></body></html>