Saving Barcodes

Include Necessary Files

Our software fully supports PSR-4 which will autoload all the classes you need if you use Composer.


require __DIR__ . '/vendor/autoload.php';

If you do not use Composer, load each file that you require as such:


require_once('../packages/barcode-common/src/BCGColor.php');
// And other files.

Next, simply indicate some common classes to use in your file:


use BarcodeBakery\Common\BCGColor;
use BarcodeBakery\Common\BCGDrawing;
use BarcodeBakery\Common\BCGFontFile;
use BarcodeBakery\Common\BCGLabel;

Then, include the barcode type you want to generate. Let's try to generate a Code 128:


use BarcodeBakery\Barcode\BCGcode128;

Generating Colors

Before we start generating barcodes, we have to decide which colors we want to use to display our barcode.
Generally, we use black and white bars.


// The arguments are R, G, and B for color.
$colorFront = new BCGColor(0, 0, 0);
$colorBack = new BCGColor(255, 255, 255);

Font for Label

We will now create a font for writing a label under the barcode. If you don't wish to have a text label, ignore this step.
The first argument is the path to the TTF font file and the second is the size in points (pt) of the font.


$font = new BCGFontFile(__DIR__ . '/font/Arial.ttf', 18);

Creating the Barcode

Now, we will create the barcode. There is no parameter for the class constructor; you must call the provided methods to modify the barcode properties (see the manual). At the end of the code, you must call the function parse() in order to analyze the code you want to draw.


$code = new BCGcode128();
$code->setScale(2); // Resolution
$code->setThickness(30); // Thickness
$code->setForegroundColor($colorFront); // Color of bars
$code->setBackgroundColor($colorBack); // Color of spaces
$code->setFont($font); // Font (or 0)
$code->parse('HELLO'); // Text

Saving the Barcode to a File

We need to put the barcode on a drawing surface. We will use the BCGDrawing class.
The first argument is the filename. The last argument is the background color.
To finish, call the Finish method with the argument BCGDrawing::IMG_FORMAT_PNG or BCGDrawing::IMG_FORMAT_JPEG to have a PNG or a JPG file.


$drawing = new BCGDrawing('hello.png', $colorBack);
$drawing->setBarcode($code);
$drawing->draw();
$drawing->finish(BCGDrawing::IMG_FORMAT_PNG);