Location: PHPKode > scripts > Barcode Print > barcode-print/MyBarcode.php
<?
/* ******************************************************************** */
/*                          GENBARCODE                                  */
/* ******************************************************************** */
/* location of 'genbarcode'
 * leave blank if you don't have them :(
 * genbarcode is needed to render encodings other than EAN-12/EAN-13/ISBN
 */
 class MyBarcode{
	var $genbarcode_loc="c:\winnt\genbarcode.exe";
	//var $genbarcode_loc="/usr/local/bin/genbarcode";


	function barcode_print($printer,$code, $showtext=true,$encoding="ANY", $scale = 2 ,$height=30,$x=0,$y=0,$space=0 ){
    	$bars=$this->barcode_encode($code,$encoding);
	    if (!$bars) return;
		if($showtext)
			$this->barcode_out($printer,$bars['text'],$bars['bars'],$scale, $height,$x,$y,$space);
		else
			$this->barcode_out($printer,'',$bars['bars'],$scale, $height,$x,$y,$space);
	    return $bars;
	}


/* barcode_encode(code, encoding)
 *   encodes $code with $encoding using genbarcode OR built-in encoder
 *   if you don't have genbarcode only EAN-13/ISBN is possible
 *
 * You can use the following encodings (when you have genbarcode):
 *   ANY    choose best-fit (default)
 *   EAN    8 or 13 EAN-Code
 *   UPC    12-digit EAN 
 *   ISBN   isbn numbers (still EAN-13) 
 *   39     code 39 
 *   128    code 128 (a,b,c: autoselection) 
 *   128C   code 128 (compact form for digits)
 *   128B   code 128, full printable ascii 
 *   I25    interleaved 2 of 5 (only digits) 
 *   128RAW Raw code 128 (by Leonid A. Broukhis)
 *   CBR    Codabar (by Leonid A. Broukhis) 
 *   MSI    MSI (by Leonid A. Broukhis) 
 *   PLS    Plessey (by Leonid A. Broukhis)
 * 
 *   return:
 *    array[encoding] : the encoding which has been used
 *    array[bars]     : the bars
 *    array[text]     : text-positioning info
 */


/* barcode_print(code [, encoding [, scale [, mode ]]] );
 *
 *  encodes and prints a barcode
 *
 *   return:
 *    array[encoding] : the encoding which has been used
 *    array[bars]     : the bars
 *    array[text]     : text-positioning info
 *    array[text]     : text-positioning info
 */
	function barcode_encode($code,$encoding){
    	if (
			((eregi("^ean$", $encoding)
			 && ( strlen($code)==12 || strlen($code)==13)))
		 
			|| (($encoding) && (eregi("^isbn$", $encoding))
			 && (( strlen($code)==9 || strlen($code)==10) ||
			 (((ereg("^978", $code) && strlen($code)==12) ||
			  (strlen($code)==13)))))

			|| (( !isset($encoding) || !$encoding || (eregi("^ANY$", $encoding) ))
			 && (ereg("^[0-9]{12,13}$", $code)))
	      
			){
		/* use built-in EAN-Encoder */
		$bars=$this->barcode_encode_ean($code, $encoding);
	    } else if (file_exists($this->genbarcode_loc)){
		/* use genbarcode */
		$bars=$this->barcode_encode_genbarcode($code, $encoding);
    	} else {
		print "php-barcode needs an external programm for encodings other then EAN/ISBN<BR>\n";
		print "<UL>\n";
		print "<LI>download gnu-barcode from <A href=\"http://www.gnu.org/software/barcode/\">www.gnu.org/software/barcode/</A>\n";
		print "<LI>compile and install them\n";
		print "<LI>download genbarcode from <A href=\"http://www.ashberg.de/bar/\">www.ashberg.de/bar/</A>\n";
		print "<LI>compile and install them\n";
		print "<LI>specify path the genbarcode in php-barcode.php\n";
		print "</UL>\n";
		print "<BR>\n";
		print "<A HREF=\"http://www.ashberg.de/bar/\">Folke Ashberg's OpenSource PHP-Barcode</A><BR>\n";
		return false;
    	}
	    return $bars;
	}

/* barcode_encode_genbarcode(code, encoding)
 *   encodes $code with $encoding using genbarcode
 *
 *   return:
 *    array[encoding] : the encoding which has been used
 *    array[bars]     : the bars
 *    array[text]     : text-positioning info
 */
	function barcode_encode_genbarcode($code,$encoding){
	    /* delete EAN-13 checksum */
    	if (eregi("^ean$", $encoding) && strlen($code)==13) $code=substr($code,0,12);
	    if (!$encoding) $encoding="ANY";
    	$encoding=ereg_replace("[|\\]", "_", $encoding);
	    $code=ereg_replace("[|\\]", "_", $code);
    	$cmd=$this->genbarcode_loc." \""
		.str_replace("\"", "\\\"",$code)."\" \""
		.str_replace("\"", "\\\"",strtoupper($encoding))."\"";
	    //print "'$cmd'<BR>\n";
    	$fp=popen($cmd, "r");
	    if ($fp){
		$bars=fgets($fp, 1024);
		$text=fgets($fp, 1024);
		$encoding=fgets($fp, 1024);
		pclose($fp);
    	} else return false;
	    $ret=array(
			"encoding" => trim($encoding),
			"bars" => trim($bars),
			"text" => trim($text)
		      );
    	if (!$ret['encoding']) return false;
	    if (!$ret['bars']) return false;
    	if (!$ret['text']) return false;
	    return $ret;
	}	
	function barcode_out($printer,$text,$bars,$scale,$height,$x,$y,$space){
    	/* set defaults */
	    if ($scale<1) $scale=1;
    	$height=(int)($height);
	    if ($height<1) $height=(int)$scale * 30;
    	if (!$space)
	      $space=array('top'=>2*$scale,'bottom'=>2*$scale,'left'=>2*$scale,'right'=>2*$scale);
    	
	    /* paint the bars */
    	$width=true;
		$brush=printer_create_brush (PRINTER_BRUSH_SOLID,"000000");
		printer_select_brush ($printer,$brush);
	    for ($i=0;$i<strlen($bars);$i++){
			$val=strtolower($bars[$i]);
			if ($width){
			    $xpos+=$val*$scale;
		    	$width=false;
		    	continue;
			}
			if (ereg("[a-z]", $val)){
		    /* tall bar */
			    $val=ord($val)-ord('a')+1;
			}
		
			printer_draw_rectangle($printer, $x+$xpos, $y+$space['top'], $x+$xpos+($val*$scale)-1, $y+$space['top']+$height);

			$xpos+=$val*$scale;
			$width=true;
    	}
		printer_delete_brush ($brush);
    	/* write out the text */
	    $fontheight=$height/2;
		$font = printer_create_font("Arial", $fontheight, $fontheight, 400, false, false, false, 0);
		printer_select_font($printer, $font);

    	$chars=explode(" ", $text);
	    reset($chars);
    	while (list($n, $v)=each($chars)){
			if (trim($v)){
		    	$inf=explode(":", $v);
				printer_draw_text($printer,  $inf[2], $x+$space['left']+($scale*$inf[0])+2, $y+$fontheight+50);
			}
    	}
		printer_delete_font($font);
	}


	function barcode_gen_ean_sum($ean){
	  $even=true; $esum=0; $osum=0;
	  for ($i=strlen($ean)-1;$i>=0;$i--){
		if ($even) $esum+=$ean[$i];	else $osum+=$ean[$i];
		$even=!$even;
  		}
  		return (10-((3*$esum+$osum)%10))%10;
	}

/* barcode_encode_ean(code [, encoding])
 *   encodes $ean with EAN-13 using builtin functions
 *
 *   return:
 *    array[encoding] : the encoding which has been used (EAN-13)
 *    array[bars]     : the bars
 *    array[text]     : text-positioning info
 */
	function barcode_encode_ean($ean, $encoding = "EAN-13"){
    	$digits=array(3211,2221,2122,1411,1132,1231,1114,1312,1213,3112);
	    $mirror=array("000000","001011","001101","001110","010011","011001","011100","010101","010110","011010");
    	$guards=array("9a1a","1a1a1","a1a");

	    $ean=trim($ean);
    	if (eregi("[^0-9]",$ean)){
		return array("text"=>"Invalid EAN-Code");
    	}
	    $encoding=strtoupper($encoding);
    	if ($encoding=="ISBN"){
		if (!ereg("^978", $ean)) $ean="978".$ean;
    	}
	    if (ereg("^978", $ean)) $encoding="ISBN";
    	if (strlen($ean)<12 || strlen($ean)>13){
		return array("text"=>"Invalid $encoding Code (must have 12/13 numbers)");
    	}

	    $ean=substr($ean,0,12);
    	$eansum=$this->barcode_gen_ean_sum($ean);
	    $ean.=$eansum;
    	$line=$guards[0];
	    for ($i=1;$i<13;$i++){
		$str=$digits[$ean[$i]];
		if ($i<7 && $mirror[$ean[0]][$i-1]==1) $line.=strrev($str); else $line.=$str;
		if ($i==6) $line.=$guards[1];
	    }
    	$line.=$guards[2];

    /* create text */
	    $pos=0;
    	$text="";
	    for ($a=0;$a<13;$a++){
		if ($a>0) $text.=" ";
		$text.="$pos:12:{$ean[$a]}";
		if ($a==0) $pos+=12;
		else if ($a==6) $pos+=12;
		else $pos+=7;
	    }

    	return array(
			"encoding" => $encoding,
			"bars" => $line,
			"text" => $text
			);
	}
}
?>
Return current item: Barcode Print