MiKaelKael
Less code = less bugs

Home page > Blog > Zend_Barcode - part 1 : using

Zend_Barcode - part 1 : using

Sunday 20 December 2009, by mikaelkael

All the versions of this article: [English] [français]

Zend Framework 1.10 will integrate Zend_Barcode initially called Zend_Image_Barcode in the proposals.

The main difference between this first proposition and Zend_Barcode, it’s the separation between Objects and Renderers. This allowed to write an image generator (Zend_Barcode_Renderer_Image) and a PDF generator (Zend_Barcode_Renderer_Pdf).

This component internally uses a new version of Zend_Validate_Barcode specially rewritten for 1.10 by Thomas (http://www.thomasweidner.com/flatpr...).

The official documentation will of course integrate the examples showing how to use the component but here is some use cases.

1. The image generator file ’barcode.php’

You can put at the root of your Web server:

  1. <?php
  2. set_include_path('../library');
  3. require_once 'Zend/Barcode.php';
  4. Zend_Barcode::render($_GET['barcodeType'], 'image', $_GET, $_GET);

2. A call in your view and... a first error

  1. <img src="barcode.php?barcodeType=code39" />

This will of course give you an image of error:

Because no text is provided.

3. Your first barcode

You obtain it by simply adding the ’text’ parameter:

  1. <img src="barcode.php?barcodeType=code39&text=ZEND-FRAMEWORK" />

You have your first barcode in 5 lines of code:

4. A better font

If you use the image renderer (based on GD extension) and if you don’t specify a TTF font, the built-in fonts will be used. You can set your own font in the options of the barcode or by global definition:

  1. <?php
  2. set_include_path('../library');
  3. require_once 'Zend/Barcode.php';
  4. Zend_Barcode::setBarcodeFont('../data/fonts/tahoma.ttf');
  5. Zend_Barcode::render($_GET['barcodeType'], 'image', $_GET, $_GET);

You will obtain:

2 Forum messages

  • Zend_Barcode - part 1 : using Le 1 April à 16:59 , by Joaquin L. Robles

    hi there, this won’t work in the MVC model...
    I have this code

    public function codigoAction()

    $this->_helper->layout->disableLayout();
    $this->_helper->viewRenderer->setNoRender();
    Zend_Barcode::render($_GET[’barcodeType’], ’image’, $_GET, $_GET);

    but would return a browser error, the image couldn’t be displayed....

    Reply to this message

Reply to this article

SPIP | Sign In | Site Map | Follow-up of the site's activity RSS 2.0
Habillage visuel (c) Andreas Viklund sous Licence free for any purpose