Displaying Barcodes

Repeat the previous step

You have learned how to save the barcode to a file. If you prefer to display it on the screen, you simply need to change the last part of your script.

Output the Stream

We use the same BCGDrawing class, but this time we omit the first argument.

We will now change the header to direct the browser to output an image. If the image is a JPG, you must modify it.


$drawing = new BCGDrawing($colorBack);
$drawing->setBarcode($code);
$drawing->draw();

header('Content-Type: image/png');
$drawing->finish(BCGDrawing::IMG_FORMAT_PNG);

Remember if you output the image to a browser, you must not have output anything else to the stream beforehand.
One common mistake is to try to display the barcode stream directly in an HTML page.