GS1Databar for PHP is a software package that allows you to create GS1
Some of its main features are:
- You have control over many relevant paramenters in order to
customize the resulting bar code image to your particular needs.
- You can specify the width and height of the bars and the margins.
- You can specify the background color, and the color of the bars.
- The resulting image can be sent directly to the browser or to a
file, in several formats (PNG, JPEG, GIF, WBMP).
- Developed using object oriented programing techniques, you can
easily modify the source code to adapt your application needs or to add
a new bar code type. (Of course you can count on us to do it, in most
cases that will be more cost-effective for you).
- A working PHP4 system or later.
- GD library. It’s usually bundled into PHP.
- Support for specific image formats such as GIF or JPEG if you
want to use them. Default is PNG. In recent PHP versions you will
likely have at least PNG and JPEG.
Create a working directory for the application. It should be OUTSIDE
the web server tree (this is, the web server should not access it). We
will call it "the application directory".
Uncompress the provided distribution file into the application
Create another working directory INSIDE the web server tree, and
configure your web server to process PHP files in it. This is, make
sure you can acces this directory using an appropriate URL from your
browser, make sure that files with .php extension are parsed through
the PHP interpreter, etc. We will call it "the web directory".
Copy the demo.php file from the application directory to the web
Edit the demo.php in the web directory and update the include_path
setting to the "inc" subdirectory of the application directory. The
sentence is at the beginning of the script. Example :
ini_set("include_path", "/home/myusername/gs1databarphp_application/inc" );
Call demo.php using the appropriate URL in your browser and verify
it works properly.
Generating your first bar code.
You can see an example of the mininum coding effort to get up and running by using the provided sample code sample.php.
We reproduce and comment it here to show how it works.
First we set the include_path so PHP will find GS1Databar program
files, as we already did for the demo.php file:
ini_set("include_path", "/home/myusername/gs1databarphp_application/inc" );
We load the class file of GS1Expanded for PHP:
require( "GS1Expanded.php" );
When you load this class, also GS1Databar and GS1Limited will be
GS1Databar for PHP uses PHP OOP capabilities in a Java-like style, so
prior of executing
any function we must create a barcode object of the proper type. We use
$bc variable for this.
$bc = new GS1Databar();
or you use "new GS1Expanded()" to created an GS1 expanded barcode or
"new G1SLimited()" to create a GS1 limited barcode.
In case you are creating a GS1Databar you can select one of these formats:
$bc->format=FORMAT_REGULAR; // or any of these FORMAT_TRUNCATED, FORMAT_STACKED,
if you create an expanded barcode you can select one of these formats:
$bc->format=FORMAT_EXPANDED_STACKED; // or FORMAT_EXPANDED (default)
We are ready to obtain our barcode graphic. However we will usually
wish to specify some parameters. All these settings are optional and if
you don’t call the setting functions the program will use its default
values. In this example we widht of the narrow bars will be 1 pixels:
To show the barcode, we call the paint() function. It receives as
parameter the character string containing the code to be shown.
$bc->paint( "010001234567890510ABCDEF" );
If you want the image to be stored into a file see the output to file
Although all settings are optional you will usually need to set at
least some of the program parameter’s in order to adjust it to your
Important: To set any parameter you should use the provided
setParameter() functions. You shouldn’t directly set any instance
variable as it may lead to unexpected results and incompatibility of
your code with future versions of the program :
$bc->setParam( 2 ); // OK
$bc->Param = 2; // DON’T DO THIS !!
You can set the following parameters :
setTopMargin( int ) Sets the vertical margin or empty space
between the image’s border and the start of the bars. The bottom margin
is se to the same size.
setLeftMargin( int ) Sets the horizontal margin or empty
space between the image’s border and the start of the bars. The right
margin is set to the same size.
setBGColor( string ) Sets the image’s background color.
It can have two formats :
- A descriptive name among the predefined ones. They are: WHITE,
RED, GREEN, BLUE, YELLOW, CYAN, ORANGE, GRAY, BLACK.
( Note: you may add your own colors editing the source code of
Graphics.inc. Just add them to the $stdColors array definition )
- The color’s RGB values in a comma-separated string.
$bc->setBGColor( “YELLOW” );
$bc->setBGColor( “255,128,255” ); // rose
setBarColor( string ) Sets the color of the bars. Same format
setFontColor( string )
Sets the color of the text below the bars. Same format that setBGColor();
setTextFont( string )
Sets the Font in which the code text below the bars intended for human
reading of the code. You can specify in two ways :
- Use any of the five PHP default fonts. Use DEFAULT-1, DEFAULT-2,
DEFAULT-3, DEFAULT-4 or DEFAULT-5 values for this.
- Use a True Type font that you have in your system. Then you should specify
the font’s file name. To find the file, the program follows the following
- If you don’t put a file extension, it will add “.TTF”
- If you don’t specify the absolute path to the file, it will first
look in the current directory for the font file.
- If not found it will look in the fonts directory. This directory is
set in the Graphics.inc file, as the TTFPATH constant. You may want to
change the default one ("./fonts").
- If the font file is not found, nothing will be done, the previously
defined or default font will be used.
- Default : DEFAULT-3
$bc->setTextFont( “DEFAULT-5” );
$bc->setTextFont( “ARIAL” );
$bc->setTextFont( “ARIAL.TTF” );
$bc->setTextFont( “/home/me/myfonts/arial.ttf” );
Note: you can get some TTF fonts at http://sourceforge.net/projects/corefonts/
Set it to null to deactivate the drawing of the human readable text.
setFontSize( integer )
Sets the text font size. Only works for True Type fonts, not for PHP default
- Default: 10
- Example: $bc->setFontSize( 14 );
setImageType( string [, integer] ):
Sets the image type in which the graphic will be generated. Second
parameter is the quality (%). It’s optional and should be used only for
There are four possible image formats in which the barcode can be
- PNG: default and recommended format
- JPEG: note that image size will be much higher. You can reduce it
reducing image quality ( second optional parameter in setImageType() ).
- GIF: note that it was removed from GD version 1.6, so it’s likely
that you won’t have it available in recent PHP versions
- WBMP: note that this is a wireless format so it won’t show on
$bc->setImageType( “GIF” );
$bc->setImageType( “JPEG”, 50 );
setFilePath( string )
Sets the file path where the image files will be created. After
calling to this function, subseqüent calls to paint() will create
a file instead of sending the image to the browser. By default the
output will go to the browser. See “Image Ouput” paragraph below
for more details.
GS1 specific parameters
setX( int ) Sets the module width in pixels.
setStackedRowWidth(int): (GS1Expanded only) number of segment pairs in each row if using the stacked format. The default value is 2 pairs (ie. 4 symbol characters). Allowed values are 1 to 10.
sets the text to be printed below the barcode, if not set, the encoded value
will be used. For expanded barcodes yuo have to set this method if you want the
text to be painted.
setFormat( int ).
one FORMAT_REGULAR, FORMAT_TRUNCATED, FORMAT_STACKED,
FORMAT_STACKED_OMNIDIRECTIONAL, FORMAT_LIMITED (this one is automatically
set when using class GS1Limited), FORMAT_EXPANDED or FORMAT_EXPANDED_STACKED
(the last two can only be used in the class GS1Expanded).
The image that the program generates can be ouput in two ways: to
the browser or to a file.
Output to browser
This is the default mode. In the last sentence of the script we call
to the paint() method so it will directly output the image to the
browser (after appropriate HTTP headers of course). Note that you will
usually need an HTML page with a form in it that will set the
appropriate parameters. In the example we provide this is done by
demo.php. In this mode you will usually generate only one image at a
Output to file
Barcode images can be stored into files. To do so you must first
call setFilePath() in order to specify a base directory in your file system where the generated files will be stored. Please end it with
a slash (“/”). If you want to use the current directory use “./” as
path (You can use regular slash as PHP converts it to the backslash in
Windows systems) . Note that this call is mandatory as it also works as
an indicator that you want the output to a file.
You have two options for the file name the generated image will have:
You can pass the file name you wish as a second optional parameter
to the paint() method or you can do nothing so a default filename will
be used ( code plug image type extension, e.g. ‘12345678.png’ )
Multiple file generation
When you send the output to a file, you can easily create many barcode images
in one program. Just call to the paint() method as many times as you wish.