Monday, February 9, 2015

Create a barcode with PHP PEAR Image_Barcode2

Create a barcode with PHP PEAR Image_Barcode2

Install PEAR Image Barcode 2 on FreeBSD
# pkg_info | grep -i pear
pear-1.9.4_1 PEAR framework for PHP

# pear install Image_Barcode2-0.2.1

# ls /usr/local/share/pear/Image
Barcode2
Barcode2.php

Code:

<?php
function _str2barcodeBase64($str) {
  include_once "Image/Barcode2.php";

  $str = empty($str) ? 'test' : $str;
  $type = 'code128';
  $imgtype = 'png';
  $bSendToBrowser = FALSE;
  $height = 15;
  $width = 2;

  $img = Image_Barcode2::draw($str, $type, $imgtype, $bSendToBrowser, $height, $width);

  ob_start();
    imagepng($img);
    $imgBase64 = base64_encode(ob_get_contents());
  ob_end_clean();

  imagedestroy($img);

  return '<img src="data:image/' . $imgtype . ';base64,' . $imgBase64 . '">';
}

?>

Reference:

http://pear.php.net/package/Image_Barcode2/

No comments: