Tag: PHP Barcode

Morovia KB 10148 talks about how to use Barcode ActiveX to generate barcode images in PHP 5. Barcode ActiveX 3 has a long history in Morovia, as it was conceived in year 2003 and our first ActiveX control product.

In this article, barcode is generated first in a front end script and the image is saved to a disk file. In the backend script the image is loaded and sent to the browser. Sometimes an in-memory solution is preferred, as programers will not have to deal with file permission, clean up and possible privacy issues.

The classic ASP example, which is included in the installer package, is an in-memory solution. It utilizes ADODB.Stream object. It exports barcode images to ADODB.Stream object, then calls Response.BinaryWrite to write all the bytes out. While it looks simple, the ADODB.Stream object’s Read method does not give back a sequence of bytes as defined in PHP. It is actually a COM SafeArray packaged in VARIANT type.

try {
$objBarcode = new COM("Morovia.BarcodeActiveX");
$objBarcode->RasterImageResolution = 96; //Set the resolution to 96 dpi (screen)
$objBarcode->NarrowBarWidth = 15; //Default NarrowBarWidth 15 mils
$objBarcode->BorderStyle = 0; //No border
$objBarcode->ShowComment = false; //No comment
$objBarcode->ShowHRText = false; //No human readable text
// By default the Barcode ActiveX creates margins around the symbol. The margins are around 100 mils
// You can increase or decrease the margins by setting 4 SymbolMargin properties like
$objBarcode->SymbolMarginTop = 100;

// Retrieve input parameters through the URL query string
$objBarcode->Rotation = $_GET["rotation"];
$objBarcode->ShowHRText = (strlen($_GET["showhrtext"])==0 || $_GET['showhrtext']==0) ? 0 : 1;
$objBarcode->Symbology = $_GET["symbology"];
$objBarcode->NarrowBarWidth = $_GET['narrowbarwidth'];

$objBarcode->BarHeight = $_GET['barheight'];
$objBarcode->Message = $_GET['message'];

$objBarcode->Font->Name = “MRV OCRB I”;
$objBarcode->Font->Bold = false;
$objBarcode->ShowComment = false;

// The Stream object is available in MDAC 2.5 and above versions. You can download the most
// recent MDAC at http://www.microsoft.com/data/mdac/
$objStream = new COM(”ADODB.Stream”);
$objStream->Type = adTypeBinary;

// Export the Image to Stream object.
// After transfer completes, the Position must set to 0 before Calling Response.BinaryWrite
// otherwise an “unknown type” is reported.
$objBarcode->ExportImage($objStream, imgTypePNG);

// BinaryWrite the image data to the client browser, in PHP use echo
$objStream->Position = 0;

// until we figured out how to convert a byte array in COM to PHP,
// we have to save it to a file first, then read it back to PHP.
header(’Content-Type: image/png’);
$buffer = $objStream->Read();

// Note: this does not work!

$objStream = null;
$objBarcode = null;
} catch(Exception $ex) {
header('Content-Type: text/plain');

It turns out that the solution is pretty simple - although PHP does not provide direct way to convert a COM safe array to string, it provides a way to iterate the content. Thus, we can write:

foreach ($buffer as $byte) echo chr($byte);

In the place of echo($buffer) statement. It worked.

Creating Barcode Strings Using PHP

If you are creating barcode using barcode fonts, you should already know that you can not just type your number and format with a font. Extra characters such as start/stop characters, and often times checksum characters are required to be present in the barcode.  Some formats such as Code 39 and Code25, are easy. But formats like Code 128 are quite difficult. To easy the difficulty creating barcode strings, Morovia provides source code, Windows DLLs and COM classes that you can integrate in your applications.

For an overview on these utilities, visit http://www.morovia.com/font/support/font-tool.asp.

Many people asked the question - do you have an example with PHP? It turns out that it is not difficult to do it in PHP at all. The following code creates a Code128 barcode string (which becomes a Code128 barcode after being formatted with Morovia Code128 font):

$encoder = new COM("cruflMorovia.Barcode");

$result = $encoder->Code128Ex(”PN-NUMBER-13334432″);

echo $result;

Note that the Prog ID of the encoder COM object is cruflMorovia.Barcode. You can write code for other barcode types. For example, for EAN-128 barcodes, just replace Code128Ex with EAN128Ex.