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->Open();
$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!
echo($buffer);

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

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.