Source for file EAN13.php
Documentation is available at EAN13.php
* LICENSE: This source file is subject to LGPL license
* that is available through the world-wide-web at the following URI:
* http://www.gnu.org/copyleft/lesser.html
* @author Eli Fox-Epstein
* @copyright Trinity Humanitarian-FOSS Project - http://www.cs.trincoll.edu/hfoss
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License (LGPL)
* Provides a class that outputs EAN-13 barcodes
* @param $numbers the 12-digit or 13-digit number to turn into a barcode.
* If 12 digits are provided, the checksum is automatically generated.
* If 13 are provided, the first digit is considered to be the checksum and must be valid.
* @param $barHeight the height of the bars in pixels
function EAN13($numbers, $barHeight =
35) {
$this->height =
$barHeight;
$this->numbers =
$numbers;
if(strlen($this->numbers) ==
12)
$this->numbers .=
$this->checksum();
if(strlen($this->numbers) !=
13)
die('Invalid numbers length');
$this->encoded =
$this->encode();
* Generates the checksum of an EAN-13 barcode
* @return the check digit
private function checksum() {
$checksum +=
$this->numbers[$i-
1] *
$multiplier;
$checksum =
10 -
$checksum %
10;
$checksum =
($checksum ==
10) ?
0 :
$checksum;
* Returns the binary encoding a digit in L, G, or R format
* @param $digit the digit to encode
* @param $type the type of encoding
private function getEncoding($digit, $type){
if($type ==
'R') return $encoding[$digit];
if($type ==
'L') return strtr($encoding[$digit],'10','01');
if($type ==
'G') return strrev($encoding[$digit]);
* Converts the numbers into a binary sequence.
* @return the barcode pattern
private function encode() {
* The L-G type pattern for the first six digits, based on the check digit
$usePattern =
$pattern[$o];
foreach(range(1,6) as $i){
$o .=
$this->getEncoding($this->numbers{$i},$usePattern{$i-
1});
foreach(range(7,12) as $i){
$o .=
$this->getEncoding($this->numbers{$i},'R');
* Outputs a PNG image of the barcode
header('Content-type: image/png');
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
$img =
imageCreate(105,$this->height +
12);
$white =
imageColorAllocate($img, 255, 255, 255);
$black =
imageColorAllocate($img, 0, 0, 0);
foreach(str_split($this->encoded) as $key =>
$digit){
if($key <=
2 ||
($key >=
45 &&
$key <=
49) ||
$key >=
92)
imageFilledRectangle($img, $key+
10, 0, $key+
10, $this->height+
$exp, $black);
putenv("GDFONTPATH={$global['approot']}/3rd/barcode/");
imagettftext($img, $fontSize, 0.0, 0, $this->height+
$fontVertOffset, $black, "VeraMono.ttf", $this->numbers{0});
imagettftext($img, $fontSize, 0.0, $firstSixOffset, $this->height+
$fontVertOffset, $black, "VeraMono.ttf", substr($this->numbers,1,6));
imagettftext($img, $fontSize, 0.0, $secondSixOffset, $this->height+
$fontVertOffset, $black, "VeraMono.ttf", substr($this->numbers,7,6));
Documentation generated on Fri, 18 Jul 2008 11:07:43 -0400 by phpDocumentor 1.4.1