Source code | Barcode : PHP class to create barcode
Thursday April 24 2014
Google

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.

  include('Barcode.php');

These classes are included :

Barcode PHP Class utilisation

To use this class, tow statics functions are available, one for GD ressource and second one for FPDF

  Barcode::gd($res, $color, $x, $y, $angle, $type, $datas, $width = null, $height = null);
  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)

$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

array(
        'width' => w,
        'height'=> h,
        'p1' => array('x' => x, 'y' => y),
        'p2' => array('x' => x, 'y' => y),
        'p3' => array('x' => x, 'y' => y),
        'p4' => array('x' => x, 'y' => y)
      );

Example :

  $im     = imagecreatetruecolor(300, 300);
  $black  = ImageColorAllocate($im,0x00,0x00,0x00);
  $white  = ImageColorAllocate($im,0xff,0xff,0xff);
  imagefilledrectangle($im, 0, 0, 300, 300, $white);
  $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

  $box = imagettfbbox($fontSize, 0, $font, $data['hri']);
  $len = $box[2] - $box[0];
  Barcode::rotate(-$len / 2, ($data['height'] / 2) + $fontSize + $marge, $angle, $xt, $yt);
  imagettftext($im, $fontSize, $angle, $x + $xt, $y + $yt, $blue, $font, $data['hri']);