Overview
J4L Aztec Code for PHP is a software package that allows you to create Aztec Code
symbols as images that you can use in a web.
Aztec Code is a 2D matrix symbology made up of square modules on a square grid, with a square bulls eye pattern at their center. Aztec Code symbols can encode large amounts of data with user defined error correction level.
The smallest format can encode 13 numeric, 12 alphabetic characters or 6 bytes of data, while the largest format can encode 3832 numeric, 3067 alphabetic characters or 1914 bytes of data.
J4L Aztec Code for PHP implements:
- All Aztec code formats (compact and full range).
- Normal and binary encoding.
- Structured append. This allows you to encode large messages by means of a sequence of symbols.
- Extended Channel Interpretation.
- Reader initializations bit.
- Aztec Runes.
Some of its features are:
- You have control over many relevant parameters in order to customize the
symbol to your particular needs :
- Although the Aztec Code specification doesn't require quite zone, you can define one.
- You can specify the background color, and the color of the modules.
- You can rotate the image 90,180 or 270 degrees.
- 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 programming techniques, you can easily modify
the source code to adapt it to your specific application needs if you ever
need to (of course you can also count on us to do it).
Available formats are:
Rows / columns (number of modules/squares) |
Capacity (digits) |
Capacity (text) |
Capacity (binary data) |
15X15 COMPACT |
13 |
12 |
6 |
19X19 |
18 |
15 |
8 |
19X19 COMPACT |
40 |
33 |
19 |
23X23 |
49 |
40 |
24 |
23X23 COMPACT |
70 |
57 |
33 |
27X27 |
84 |
68 |
40 |
27X27 COMPACT |
110 |
89 |
53 |
31X31 |
128 |
104 |
62 |
37X37 |
178 |
144 |
87 |
41X41 |
232 |
187 |
114 |
45X45 |
294 |
236 |
145 |
49X49 |
362 |
291 |
179 |
53X53 |
433 |
348 |
214 |
57X57 |
516 |
414 |
256 |
61X61 |
601 |
482 |
298 |
67X67 |
691 |
554 |
343 |
71X71 |
793 |
636 |
394 |
75X75 |
896 |
896 |
446 |
79X79 |
1008 |
808 |
502 |
83X83 |
1123 |
900 |
559 |
87X87 |
1246 |
998 |
621 |
91X91 |
1378 |
1104 |
687 |
95X95 |
1511 |
1210 |
753 |
101X101 |
1653 |
1324 |
824 |
105X105 |
1801 |
1442 |
898 |
109X109 |
1956 |
1566 |
976 |
113X113 |
2216 |
1694 |
1056 |
117X117 |
2281 |
1826 |
1138 |
121X121 |
2452 |
1963 |
1224 |
125X125 |
2632 |
2107 |
1314 |
131X131 |
2818 |
2256 |
1407 |
135X135 |
3007 |
2407 |
1501 |
139X139 |
3205 |
2565 |
1600 |
143X143 |
3409 |
2728 |
1702 |
147X147 |
3616 |
2894 |
1806 |
151X151 |
3832 |
3067 |
1914 |
Compact formats can be used to encode short messages in a more efficient manner than full range formats. Note that reader/decoder can autodiscriminate between both formats.
There are also a set of 256 special formats called "Aztec runes" which can be used for encoding values 0 to 255 for special applications.
Requirements
- A working PHP4 or PHP5 system
- 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.
Installation
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 directory.
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 access
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 directory.
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/J4L_AztecCodephp_application/inc" );
Call demo.php using the appropriate URL in your browser and verify it works
properly.
Basic Operation
Generating your first Aztec Code symbol.
You can see an example of the minimum 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 J4L Aztec Code program files, as
we already did for the demo.php file:
ini_set("include_path", "/home/myusername/J4L_AztecCodephp_application/inc" );
We charge the class file of J4L Aztec Code for PHP:
require("AztecCode_class.inc");
J4L Aztec Code 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 the
$bc- variable for this.
$bc = new Aztec Code();
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 set the Aztec Code size to 31x31.
$bc->setSymbolSize( '31x31' );
To show the symbol, we call the paint() function. It receives as parameter
the character string containing the data to be encoded.
$bc->paint( "string to be encoded" );
If you want the image to be stored into a file or you want to create several images
at a time, see the multisample.php script and the output to file section below.
User functions overview
All functions useful for normal operation of the software are listed below,
grouped by section. Each section explains the use of the related functions,
click on the section title to go to a section.
Generic Image Parameters:
setQuiteZone()
setTopMargin()
setLeftMargin()
setBGColor()
setBarColor()
setRotation()
setImageType()
Aztec Code parameters
setModuleSize()
setSymbolSize()
setAutoConfigurate() setEncoding()
setType()
setProcessTilde() setStructuredAppendCounter()
setStructuredAppendIndex()
setFileId()
setRune()
setReaderInitialization()
|
|
Creating the symbol image
setFilePath() paint()
Printing
setResolution()
setBrowserDPI()
getImgTagWidth() getImgTagHeight() getImgTag()
Error Management
setErrorShowMode()
setErrorLogMode()
getErrorCode() getErrorMsg()
|
Generic Image Parameters
setQuiteZone( int ) Sets the margin in pixels of the four sides around
the graphic. You can also use setTopMargin() & setLeftMargin() instead.
The Aztec Code specification already requires a quite zone of a defined size.
This mandatory quite zone is always included. By using setQuiteZone() you only
increase the mandatory quite zone with additional space.
setTopMargin( int ) Sets the vertical margin or empty space between
the image’s border and the start of the bars. The bottom margin is set
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.
Examples:
$bc->setBGColor( “YELLOW” ); $bc->setBGColor( “255,128,255” ); // rose
setBarColor( string ) Sets the color of the hexagonal modules &
finder pattern. Same format than setBGColor()
setRotation( integer ) Sets the rotation of the symbol. Acceptable values
are :
- 0 normal
- 90 ( vertical, rotated counterclockwise)
- 180 ( inverted )
- 270 ( vertical, rotated clockwise)
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 JPEG.
There are four possible image formats in which the barcode can be generated:
- 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 some GD versions so maybe it's not available
in your system.
- WBMP: note that this is a wireless format so it won’t show on most
browsers
Examples:
$bc->setImageType( “GIF” ); $bc->setImageType( “JPEG”, 50 );
Aztec Code parameters
Module size
setModuleSize( int ) Sets the number of pixels which make a module (square) in the barcode (default is 4).
setSymbolSize( string ): Sets the preferred format. Acceptable values :
15x15c, 19x19, 19x19c, 23x23, 23x23c, 27x27, 27x27c, 31x31, 37x37, 41x41, 45x45, 49x49, 53x53, 57x57, 61x61, 67x67, 71x71, 75x75, 79x79, 83x83, 87x87, 91x91, 95x95, 101x101, 109x109, 113x113, 117x117, 121x121, 125x125, 131x131, 135x135, 139x139, 143x143, 147x147, 151x151
The final 'c' denotes a compact symbol. See the table at the beginning of this document.
setAutoConfigurate( boolean ): set to true to allow automatic selection of a larger size if the data to encode does not fit in the size defined by setSymbolSize().
setEncoding( string ): "BINARY" or "NORMAL" (default).
- NORMAL: can encode any character but it is not very efficient encoding binary values (values above 128).
- BINARY: use this mode only if your data contains many bytes/characters above 128.
setType( string ) : "COMPACT" / "FULL" / "ANY". Call this define which formats can be used: only compact formats, only full range formats, or any of both.
setProcessTilde( boolean ): Enables / disables tilde processing used for encoding special characters. By default is enabled.
When enabled works as follows:
- ~~: will be replaced with ~
- ~dxxx: will be replaced by the character whose ascii code is xxx. For example ~d065 will be replaced with A.
- ~F: will be replaced with the FNC1 flag (allowed as first codeword only).
- ~Exxxxxx: will be replaced with the Extended Interpretation Channel flag xxxxxx. For example to activate Extended Interpretation Channel 1, use ~E000001.
setStructuredAppendCounter( int ): When the symbol is part of an Structured Append set, this is the total number of symbols in the set. Valid values : 2 to 26. If this is not set, or set to 1, the symbol is assumed to be standalone (no structured append used).
setStructuredAppendIndex( int ): When the symbol is part of an Structured Append set, this is the number of this symbol in the set. Valid values : 2 to 26.
setFileId( string ): File Id for Structured Append. Optional.
setRune(): Set a value between 0 and 255 to create a Aztec code rune (default is -1, disabled). Then call paint() without arguments.
setReaderInitialization( boolean ): set to true to activate the reader initialization bit.
setErrorCorrectionLevel( int ): Percentage of errors which can be recovered. Valid values: 5 to 95. Default is 23 ( 23%).
Creating the symbol image
Related functions
setFilePath( string ) Sets the file path where the image files will
be created. After calling to this function, subsequent calls to paint() will
create a file instead of sending the image to the browser.
Mixed paint( string [, string] ) Creates and eventually outputs the
symbol image.
Parameters :
- data string to be encoded.
- file name to be used (optional)
Returns :
- NULL if some error condition is found that prevents the image to be created.
See Error Management.
- The file name in which the image has been stored (if output is sent to a file).
- An empty string if the image was sent to the browser.
Output to browser
By default, if you don't call to setFilePath(), the paint() function 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 time.
Output to file
Activation
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 (in Windows systems
you can use regular slash as PHP converts it to the backslash ). Note that this
call is mandatory as it also works as an indicator that you want the output
to a file.
File names
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, if you omit it the program will create one.
Multiple file generation
When you send the output to a file, you can easily create many barcode images
in one program call. Just call to the paint() method as many times as you wish.
Error Management
When trying to create the Aztec Code symbol (paint() function) a number of issues
may arise. They have been classified in three categories, similar to those of
PHP : errors, warnings and notices. Errors are serious problems that prevent
the image from being created, warnings indicate non fatal problems -the image
can be created anyway- but they should corrected as they may lead to problems
when decoding. Finally notices are used to inform of minor issues which are
usually irrelevant.
Notes:
- Unless otherwise specified by the context, we will refer generically as
"errors" for the three conditions: errors, warnings or notices.
- Errors are triggered mainly by paint(). In general, functions that set parameters
will ignore wrong parameters and will use defaults instead.
There are three ways errors/warnings/notices can be managed :
- they can be processed by your PHP script
- they can be shown to the user
- they can be logged
The three options are independent and either of them can be used or not used/disabled.
Processing errors in the calling script
string getErrorCode()
string getErrorMsg()
These functions return an error code and description of the last error occurred.
The first character of the error code indicates the type of error: "E"
for errors, "W" for warnings and "N" for notices.
If no error has been detected, both functions will return NULL.
Example :
$bc-> paint( $data );
$status = $bc->getErrorCode();
if ( $status === NULL OR $status[0] == 'N' )
ok_process();
else
if ( $status == 'W_TRUNCATED' )
ask_user_to_reduce_data_size();
else
other_error_found();
show the errors to the user
setErrorShowMode( string types, string mode [, string predefined_text [,
string image_file ] ] )
sets how errors will (or won't) be shown to the user.
Parameters :
- types: Defines which types of errors will be affected by the setting. Must
be a string containing 1 to 3 characters, of the following allowed : EWN.
- mode: Defines which show mode to use. Three allowed values : "IMG","TXT"
and "NO".
- predefined_text: for IMG and TXT modes, sets a fixed text that will be used
instead of the error description. Can be omitted or set to NULL so in both
cases the specific error description will be used.
- image_file: for the IMG mode, it should be the relative path to an image
file
There are three modes for showing errors :
- 'IMG' (image): default and recommended mode: shows the error inside the
resulting image.
- 'TXT' (text): shows the error as plain text. Note that, unless you output
the image to a file, the image won't show as it will be replaced by the error
text. In such case note that, if you are calling the script from an img tag
you will get a broken image or some error message from the browser.
- 'NO': error showing disabled. This may be fine for notices and warnings
but note that for errors you should process the error from your script, because
otherwise your browser will show a broken image or generic error.
Defaults are : show image for errors and warnings and ignore notices. This
is equivalent to :
$bc->setErrorShowMode( 'EW', 'IMG' );
$bc->setErrorShowMode( 'N', 'NO' );
Examples :
show all error types as text :
$bc->setErrorShowMode( 'EWN', 'TXT' );
disable showing warnings :
$bc->setErrorShowMode( 'W', 'NO' );
show a generic message using an ad hoc image :
$bc->setErrorShowMode( 'EW', 'IMG, 'SOME ERROR WAS FOUND', 'imgs/error.png' );
error logging
setErrorLogMode( string types, string mode [, string $file ] )
Parameters :
- types: Defines which types of errors will be affected by the setting. Must
be a string containing 1 to 3 characters, of the following allowed : EWN.
- mode: Defines which log mode to use. Three allowed values : "PHP","FILE"
and "NO".
- file : log file for mode 'FILE'
Explanation of modes :
- 'PHP': uses PHP error logging (calls trigger_error() ). This is the default
for all error types.
- Errors are logged as E_USER_WARNING, while warnings and notices are
logged as E_USER_NOTICE. This is done this way in order to avoid E_USER_ERROR
as it would abort the script execution.
- Note that if you have PHP error logging off, no logging will be done.
- If you have display_errors on, it will be disabled to avoid broken images.
Use the error showing options if you want to see the error messages in
the browser.
- 'FILE': logs errors to a file, which path is provided as a second parameter.
- 'NO': disables logging.
Example: log all errors to a file :
$bc->setLogMode( 'EWN', 'FILE', 'logs/J4L_AztecCode_errors.txt' );
Error codes currently used
E_NO_DATA |
No data to encode was supplied |
E_SIZE_LIMIT |
Reached the size of the preferred format, but still there is data remaining to be encoded. Either select a bigger format or enable auto-configure. |
E_INVALID_RUNE |
Rune value should be between 0 and 255 |
W_INVALID_PARAMETER |
Some of the parameters set has a non-allowed value. |
|