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
 
Soldato
Joined
9 May 2005
Posts
4,528
Location
Nottingham
The script that Dj_Jestar linked to should do what you want with a small modification. You just need to intercept the script before it generates the image. All the PHP scripts that output an image with text will be created from a string to begin with.
 
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.
 
Associate
Joined
3 Jun 2013
Posts
1
Can I post an EAN 13 barcode creation question here? If not, sorry for this. I want to create some EAN 13 barcodes in VB.NET. I am new to this field. I googled and found a sample code to create EAN 13 in VB.NET. So I copied, but it did not work. I do not know the reason. Here is the sample code:
Imports OnBarcode.Barcode


Dim barcode As Linear = New Linear

'EAN 13 Barcode Basic Settings
barcode.Type = BarcodeType.EAN13

'EAN 13 Valid data char set:
'0, 1, 2, 3, 4, 5, 6, 7, 8, 9 (Digits)

'EAN 13 Valid data length: 12 digits only, excluding the last checksum digit
barcode.Data = "012345678901"

'for EAN13 with supplement data (2 or 5 digits)
'barcode.SupData = "12";
'supplement bar height vs bar height ratio
'barcode.SupHeight = 0.8f;
'space between barcode and supplement barcode (in pixel)
'barcode.SupSpace = 15;

'Barcode Size Related Settings
barcode.UOM = UnitOfMeasure.PIXEL
barcode.X = 1
barcode.Y = 80
barcode.LeftMargin = 0
barcode.RightMargin = 0
barcode.TopMargin = 0
barcode.BottomMargin = 0
barcode.Resolution = 96
barcode.Rotate = Rotate.Rotate0

'Barcode Text Settings
barcode.ShowText = True
barcode.TextFont = New Drawing.Font("Arial", 9.0F, Drawing.FontStyle.Regular)
barcode.TextMargin = 6

'Image format setting
barcode.Format = System.Drawing.Imaging.ImageFormat.Gif()

barcode.drawBarcode("c://ean-13.gif")
I have read the guide of creating barcode in VB.NET. I still can not figure it out. Can you guys give me some suggestions about EAN 13 creation? Thanks.
 
Permabanned
Joined
7 Apr 2015
Posts
2
Can I post an EAN 13 barcode creation question here? If not, sorry for this. I want to create some EAN 13 barcodes in VB.NET. I am new to this field. I googled and found a sample code to create EAN 13 in VB.NET. So I copied, but it did not work. I do not know the reason. Here is the sample code:
Imports OnBarcode.Barcode


Dim barcode As Linear = New Linear

'EAN 13 Barcode Basic Settings
barcode.Type = BarcodeType.EAN13

'EAN 13 Valid data char set:
'0, 1, 2, 3, 4, 5, 6, 7, 8, 9 (Digits)

'EAN 13 Valid data length: 12 digits only, excluding the last checksum digit
barcode.Data = "012345678901"

'for EAN13 with supplement data (2 or 5 digits)
'barcode.SupData = "12";
'supplement bar height vs bar height ratio
'barcode.SupHeight = 0.8f;
'space between barcode and supplement barcode (in pixel)
'barcode.SupSpace = 15;

'Barcode Size Related Settings
barcode.UOM = UnitOfMeasure.PIXEL
barcode.X = 1
barcode.Y = 80
barcode.LeftMargin = 0
barcode.RightMargin = 0
barcode.TopMargin = 0
barcode.BottomMargin = 0
barcode.Resolution = 96
barcode.Rotate = Rotate.Rotate0

'Barcode Text Settings
barcode.ShowText = True
barcode.TextFont = New Drawing.Font("Arial", 9.0F, Drawing.FontStyle.Regular)
barcode.TextMargin = 6

'Image format setting
barcode.Format = System.Drawing.Imaging.ImageFormat.Gif()

barcode.drawBarcode("c://ean-13.gif")
I have read the guide of creating barcode in VB.NET. I still can not figure it out. Can you guys give me some suggestions about EAN 13 creation? Thanks.

Check this:
http://www.codeproject.com/Articles/29409/EAN-Barcode-Control
There are many code resource on CodeProject.
 
Back
Top Bottom