24.8. Format options for barcodes
Prev | Chapter 24. Linear Barcodes (One Dimensional Barcodes) | Next

## 24.8. Format options for barcodes

### 24.8.1. Adjusting the encodation process

For the encoding there is basically only one modification available.

For those symbologies that include an optional checksum it is possible to enable or disable this calculation.

The following symbologies may have optional checksum character(s)

1. Industrial 2 of 5

2. Interleaved 2 of 5

3. Code 39

4. Code 39 Extended

5. Code 11

Turning on/off checksum calculation for other symbologies will have no affect. Checksum calculation is enabled with a call to `AddChecksum()` on the chosen backend.

For example to augment the previous "CODE 39" example to include the checksum the code would be changed to

    1
    2
    3
    4
    5
    6
    <?php
    $symbology = BarcodeFactory::Create (ENCODING_CODE39 );
    $symbology->AddChecksum();
    $barcode = BackendFactory ::Create('IMAGE', $symbology);
    $barcode->Stroke('ABC123');
    ?>

Which would give the result shown in Figure 24.6

Figure 24.6. Encoding "ABC123" with CODE 39 adding checksum (checksum=4).

### 24.8.2. Selecting output format

The output format can be adjusted by specifying/creating the appropriate backend. The library supports image and postscript (and encapsulated postscript) backends.

The backend is created by calling the static factory method

- `BackendFactory::Create($aBackend,$aEncoder,$aReport=false)`

So to create an image backend the following code is needed

    1
    $barcode = BackendFactory::Create (BACKEND_IMAGE ,$symbology);

Where "`$symbology`" is the chosen symbology as created by the `BarcodeFactory::Create()` factory method. Please note that both factory functions are called as static methods.

The output format is specified by using one of the following symbolic defines

1. `BACKEND_IMAGE`, Creates a standard JPEG or PNG (default) image

2. `BACKEND_PS`, Creates a standard postscript file as output. It is possible to modify this output to become EPS (Encapsulated postscript) by calling the `SetEPS()` method on the backend as the following code snippet shows

       1
       2
       $barcode = BackendFactory::Create (BACKEND_PS,$symbology);
       $barcode->SetEPS()

   Please note that for the postscript backend the postscript code is returned as a string from the `Stroke()` method.

To send the created stream (either image or postscript) back to the browser or to a file the `Backend::Stroke()` method shall be used. The parameter to the Stroke() method shall be the string to be encoded.

Assume we want to create an image that is sent back to the browser. We would then use the following code

    1
    2
    3
    4
    5
    <?php
    $symbology = BarcodeFactory::Create (ENCODING_CODE128 );
    $barcode = BackendFactory ::Create(BACKEND_IMAGE, $symbology);
    $barcode->Stroke('ABC123');
    ?>

It is also possible to write the barcode directly to a file by specifying a second argument to the `Stroke()` method above. So if we instead wanted the barcode to be stored in the file "`/tmp/barcode.png`" we could write

    1
    2
    3
    $symbology = BarcodeFactory::Create (ENCODING_CODE128 );
    $barcode = BackendFactory ::Create(BACKEND_IMAGE, $symbology);
    $barcode->Stroke('ABC123','/tmp/barcode.png');

### Note

There is no automatic added extension to the file name.

Again, please note that for the Postscript background the `Backend::Stroke()` method normally returns the postscript file as a string if everything went well.

### 24.8.3. Writing barcodes to a file

This is done by adding a second argument, the file name, to the `Backend::Stroke()` 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.

### 24.8.4. Hiding the human readable text

- `Backend::HideText($aHide=true)`

The human readable text is the string that can optionally be displayed at the bottom of the bar. By default this is enabled.

### 24.8.5. Adjusting the module width

- `Backend::SetModuleWidth($aWidth)`

There are however some subtle facts regarding the module width and backend that needs to be explained.

1. For image type backends the module width specifies the number of pixels used for a module.

2. For Postscript (and Encapsulated PS) backends the module width specifies the width in points (i.e. 1/72 inch).

This also means that for image type backends only integer values makes sense.

### Caution

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.

The following code shows how to both change the module width to 2 pixels and hide the human readable text

    1
    2
    3
    4
    5
    6
    7
    <?php
    $symbology = BarcodeFactory::Create (ENCODING_CODE39 );
    $barcode = BackendFactory ::Create('IMAGE', $symbology);
    $barcode ->SetModuleWidth (2);
    $barcode ->HideText();
    $barcode ->Stroke('ABC123');
    ?>

which would give the result shown in Figure 24.7 below

Figure 24.7. Encoding "ABC123" with CODE 39, hiding the text.

### 24.8.6. Setting vertical or horizontal layout

- `Backend::SetVertical($aVertical=true)`

Will rotate the barcode 90 degrees to create a vertical view of the barcode.

### 24.8.7. Adjusting height of bar code

- `Backend::SetHeight($aHeight)`

The height of the bar codes is specified with the `Backend::SetHeight()` method. For images the height is interpreted as pixels and for postscript files it is interpreted as points (1 pt = 1/72 inch)

### 24.8.8. Scaling of bar codes

- `Backend::SetScale($aScaleFactor)`

The scale factor is real number and specifies a scale factor for the overall barcode image.

### 24.8.9. Add frame around bar code

- `Backend::ShowFrame($aFlag=true)`

This method will enable a frame around the edges of the barcode image

### 24.8.10. Examples of adjusting the output

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)

    1
    2
    3
    4
    5
    6
    7
    8
    <?php
    $symbology = BarcodeFactory::Create (ENCODING_CODE128 );
    $barcode = BackendFactory ::Create(BACKEND_PS, $symbology);
    $barcode->SetVertical(true);
    $barcode->Scale(2);
    $barcode->SetModuleWidth(1.2);
    $barcode ->Stroke('ABC123') ) {
    ?>

Prev | Up | Next
24.7. Generating barcodes on the command line | Home | 24.9. Short description of supported symbologies