Generating Barcodes with PHP

Note: if you are going to use the barcode class I mention check out the comments from Zech below. He found a bug

barcode example
see below for the translation of the barcode

PHP Classes is always my first stop when looking for PHP code, no matter how advanced (or easy to be honest).

I have recently been working on a shopping cart system and implementing barcode generation, as well as allowing a barcode scanner be used as an input device to speed up aspects of order management and data entry.

There are a number of barcode generation classes at PHP Classes. Harish Chauhan’s Barcode Class has been very useful. His class supports a number of barcode standards and allows generation at various sizes. There is a simple form provided to allow generation of barcodes and demonstrate the features of the class.

I will update this when I have finished all the functionality.

The barcode scanner arrives in the morning!

Oh – and the barcode above says “www.createwebsite.info” in the code128 format.

UPDATE: here it is with the font uploaded for the text!

a barcode example with the text embedded

5 Responses to “Generating Barcodes with PHP”

  1. Zech Campbell says:

    I have been trying to use this same script and noticed that the 128 bar codes are not working properly. Did you have this problem and if so, were you able to fix it? Thanks

  2. Zech Campbell says:

    It was generating the wrong 128 and my barcode scanner could not scan it but I found the solution on http://www.phpclasses.org/discuss/package/2441/thread/2/

    Here is the fix:

    Original Code:

    $sum=0;
    $mfcStr=”";
    if($useKeys==’C')
    {
    for($i=0;$i<strlen($barnumber);$i+=2)
    {
    $val=substr($barnumber,$i,2);
    if(is_int($val))
    $sum+=($i+1)*(int)($val);
    elseif($barnumber==chr(129))
    $sum+=($i+1)*100;
    elseif($barnumber==chr(130))
    $sum+=($i+1)*101;
    $mfcStr.=$encTable[$val];
    }
    }

    Fixed Code:

    $sum=0;
    $weight = 0;
    $mfcStr=”";
    if($useKeys==’C')
    {
    for($i=0;$i<strlen($barnumber);$i+=2)
    {
    $weight++;
    $val=substr($barnumber,$i,2);
    if(is_numeric($val))
    $sum+=$weight*(int)($val);
    elseif($barnumber==chr(129))
    $sum+=$weight*100;
    elseif($barnumber==chr(130))
    $sum+=$weight*101;
    $mfcStr.=$encTable[(int) $val];
    }
    }

    If you read his post he gives some information to why you have to change that stuff.

  3. create a website says:

    Thanks Zech. I am sure that will help others out.

  4. Lyle says:

    Even though I grabbed the code, I didn’t read the comments here! I should have. I found the same problem in the code, and rewrote it myself. Might have saved me some time counting the width of bars trying to figure out what was going wrong!

  5. create a website says:

    @Lyle – I updated the post so it says up front to check out the comments for a bug fix. Hopefully that helps other people using this!

Leave a Reply