Wednesday, June 12, 2013

PHP Barcode Generator

PHP CLASS : BARCODE

Version : 2.0.3
Licence : GPL / Cecill
Date : 2013-01-06
Auteur: DEMONTE Jean-Baptiste, HOUREZ Jonathan

Download the class PHP Barcode 2.0.3

Barcode PHP Class installation

Like all class, juste include the source file.
  1. include('Barcode.php');  
These classes are included :
  • Barcode
  • BarcodeI25
  • BarcodeEAN
  • BarcodeMSI
  • Barcode11
  • Barcode39
  • Barcode93
  • Barcode128
  • BarcodeCodabar
  • BarcodeDatamatrix

Barcode PHP Class utilisation

To use this class, tow statics functions are available, one for GD ressource and second one for FPDF
  1. Barcode::gd($res$color$x$y$angle$type$datas$width = null, $height = null);  
  2. Barcode::fpdf($res$color$x$y$angle$type$datas$width = null, $height = null);  

$res

ressource gd or fpdf

Unit depends on ressource : pixel for GD and defined in the constructor for fpdf

$color

int, string

Descriptor of the color (int) for GD and hexadecimal string for fpdf (ie: "FF0000")

$x, $y

numeric

coordinate of barcode center

$angle

numeric

Rotation angle in degree

$datas

string

Value barcode (dependent on the type of barcode)
If barcode type include it, the presence of the checksum is not mandatory, it ise automatically recalculated
array
type : ean8, ean13, code11, code39, code128, codabar
memberType
codestring
type : std25, int25, code93
memberType
codestring
crcboolean
type : msi
memberType
codestring
crcboolean
arraycrc1 : string("mod10", "mod11")
crc2 : string("mod10", "mod11")
type : datamatrix
memberType
codestring
rectboolean (default : false)

type (string)

  • codabar
  • code11 (code 11)
  • code39 (code 39)
  • code93 (code 93)
  • code128 (code 128)
  • ean8 (ean 8)
  • ean13 (ean 13)
  • std25 (standard 2 of 5 - industrial 2 of 5)
  • int25 (interleaved 2 of 5)
  • msi
  • datamatrix (ASCII + extended)

$width, $height

numeric

width and height of an element, for 2D barcode (datamatrix), $height is not used

<< Returned value >>

Both methods return an array containing the dimensions of the barcode and the coordinates of its ends
  1. array(  
  2.         'width' => w,  
  3.         'height'=> h,  
  4.         'p1' => array('x' => x, 'y' => y),  
  5.         'p2' => array('x' => x, 'y' => y),  
  6.         'p3' => array('x' => x, 'y' => y),  
  7.         'p4' => array('x' => x, 'y' => y)  
  8.       );  
Example :
  1. $im     = imagecreatetruecolor(300, 300);  
  2. $black  = ImageColorAllocate($im,0x00,0x00,0x00);  
  3. $white  = ImageColorAllocate($im,0xff,0xff,0xff);  
  4. imagefilledrectangle($im, 0, 0, 300, 300, $white);  
  5. $data = Barcode::gd($im$black, 150, 150, 0, "code128""12345678", 2, 50);  
"rotate" function allow to calculate coordinate of rotated point, usefull to center the "hri" (human readable interpretation)
Example
  1. $box = imagettfbbox($fontSize, 0, $font$data['hri']);  
  2. $len = $box[2] - $box[0];  
  3. Barcode::rotate(-$len / 2, ($data['height'] / 2) + $fontSize + $marge$angle$xt$yt);  
  4. imagettftext($im$fontSize$angle$x + $xt$y + $yt$blue$font$data['hri']);  

No comments:

Post a Comment