EAN 13 + PHP

Associate
Joined
10 May 2007
Posts
537
Location
Kent
Hey guys,

This is pretty complicated to explain, so hopefully I don't butcher it..and good ol' google only seems to make images not a 'text' format of what I need, so here goes;
Got a project I'm working on, and this is really giving me some hassle. Essentially, I have account ids that I wish to convert into EAN13 barcode, usually I do this with a VB macro, but I wish to convert the VB Code into PHP code and I can't get the same output whatsoever.

I have the EAN-13 barcode font, so it's just a case of making a "text" version of the barcode. eg the VB macro makes it something like "!AAAAE|384290]" that can then be pasted on the EAN font and it will create the barcode which is also scannable (hugely important).

Code:
Public Function EAN13(BarTextIn As String) As String

' Initialize input and output strings
BarTextOut = ""
BarTextIn = RTrim(LTrim(BarTextIn))

' Throw away non-numeric data
TempString = ""
For II = 1 To Len(BarTextIn)
If IsNumeric(Mid(BarTextIn, II, 1)) Then
TempString = TempString & Mid(BarTextIn, II, 1)
End If
Next II

' Better be 12 digits long, or error it
If Len(TempString) < 12 Then TempString = "000000000000"
If Len(TempString) > 12 Then TempString = Mid(TempString, 1, 12)

' Now calculate checksum and character map left and right sides
Sum = 0
WorkL = ""
WorkR = ""
For II = 1 To 12
If (II Mod 2) = 0 Then
Sum = Sum + (3 * Mid(TempString, II, 1))
Else
Sum = Sum + Mid(TempString, II, 1)
End If

If (II > 2) And (II < 8) Then
WorkL = WorkL & getAB(Mid(TempString, 1, 1), Mid(TempString, II, 1), II - 2)
ElseIf II > 7 Then
WorkR = WorkR & Mid(TempString, II, 1)
End If
Next II

' Build actual checksum character
CheckSumValue = 10 - (Sum Mod 10)
If CheckSumValue = 10 Then CheckSumValue = 0
CheckSum = Chr(48 + CheckSumValue)

' Build working bar code string
BarCodeOut = Chr(Asc(Mid(TempString, 1, 1)) - 15) & Chr(Asc(Mid(TempString, 2, 1)) + 17) & WorkL & "|" & WorkR & CheckSum & "]"

EAN13 = BarCodeOut

End Function

That's the VB script. Which essentially gets the barcode, adds leading zeros, calculates checksum needs to and prints it in a format that you can paste into a text field with the EAN font and it will be scannable.

Code:
function ean13($output, $places){
//first change output to a string so that we can access individual numbers
$output =(string)$output;
// 1. Add the outputs of the output in the even-numbered positions: 2, 4, 6, etc.
$even_sum = $output{1} + $output{3} + $output{5} + $output2{7} + $output{9} + $output{11};
// 2. Multiply this result by 3.
$even_sum_three = $even_sum * 3;
// 3. Add the outputs of the output in the odd-numbered positions: 1, 3, 5, etc.
$odd_sum = $output{0} + $output{2} + $output{4} + $output{6} + $output{8} + $output{10};
// 4. Sum the results of steps 2 and 3.
$total_sum = $even_sum_three + $odd_sum;
// 5. The check character is the smallest number which, when added to the result in step 4,  produces a multiple of 10.
$next_ten = (ceil($total_sum/10))*10;
$check_digit = $next_ten - $total_sum;

if(is_numeric($output)){
        for($x = 1; $x <= $places; $x++){
            $ceiling = pow(10, $x);
            if($output < $ceiling){
                $zeros = $places - $x;
                for($y = 1; $y <= $zeros; $y++){
                    $leading .= "0";
                }
            $x = $places + 1;
            }
        }
        $output = $leading . $output;
    }
    else{
        $output = $output;
    }
	
return $output . $check_digit; 
}

That's the PHP Script I have so far, which grabs the Account ID, creates leading zeros, but the code I was trying to get to make it scannable isn't working.
 
Associate
OP
Joined
10 May 2007
Posts
537
Location
Kent
The VB scripts output is: !AAAAE|20434598]
PHP Script is just check sum so it prints as: 0000012043598 and it's not scannable
 
Associate
OP
Joined
10 May 2007
Posts
537
Location
Kent
I know it just did checksum..that's what I was trying to explain. I couldn't find a way to replicate the workings of the latter part of the VB script to make it a scannable barcode
 
Associate
OP
Joined
10 May 2007
Posts
537
Location
Kent
RobH: Been trying that, but all it seems to print as (using their font) is
29eolev.jpg
 
Associate
OP
Joined
10 May 2007
Posts
537
Location
Kent
Still no luck on this :( Tried about 3-4 different EAN13 fonts, same thing.
Will try some more over the weekend, but I'm running out of ideas.
 
Back
Top Bottom