Need help in generating barcode

Hi,

Good day!

I found a sample code in generating barcode.

here’s the link:

http://www.brandonchecketts.com/archives/quick-php-script-to-generate-a-barcode

and this is the code:


<?php

$number = isset($_GET['number']) ? $_GET['number'] : '';

$barcode_font = dirname(__FILE__).'/fonts/FREE3OF9.TTF';
$plain_font   = dirname(__FILE__).'/fonts/plain.pfb';

$width = 200;
$height = 80;

$img = imagecreate($width, $height);

// First call to imagecolorallocate is the background color
$white = imagecolorallocate($img, 255, 255, 255);
$black = imagecolorallocate($img, 0, 0, 0);

// Reference for the imagettftext() function
// imagettftext($img, $fontsize, $angle, $xpos, $ypos, $color, $fontfile, $text);
imagettftext($img, 36, 0, 10, 50, $black, $barcode_font, $number);

imagettftext($img, 14, 0, 40, 70, $black, $plain_font, $number);

header('Content-type: image/png');

imagepng($img);
imagedestroy($img);

?>


fisrt i got an error at because I don’t have plain.pfb, i don’t know where i can download that font. and when I tried to change it to pala.tff and I run the code the error was gone bt the output is blank.

I need to create a barcode. It’s my first time to create a barcode using php.

Any help is highly appreciated.

Thank you so much.

Hi…

I saw this thread I tried this code:


<?php

$bc = new barCode();
$bc->build('LO130319001');

class barCode
{
    public $bcHeight, $bcThinWidth, $bcThickWidth, $bcFontSize, $mode;

    function __construct($mode='gif', $height=25, $thin=1, $thick=2, $fSize=1)
    {
        $this->bcHeight = $height;
        $this->bcThinWidth = $thin;
        $this->bcThickWidth = $this->bcThinWidth * $thick;
        $this->fontSize = $fSize;
        $this->mode = $mode;
        $this->outMode = array('gif'=>'gif', 'png'=>'png', 'jpeg'=>'jpeg', 'wbmp'=>'vnd.wap.wbmp');
        $this->codeMap = array(
            '0'=>'000110100',    '1'=>'100100001',    '2'=>'001100001',    '3'=>'101100000',
            '4'=>'000110001',    '5'=>'100110000',    '6'=>'001110000',    '7'=>'000100101',
            '8'=>'100100100',    '9'=>'001100100',    'A'=>'100001001',    'B'=>'001001001',
            'C'=>'101001000',    'D'=>'000011001',    'E'=>'100011000',    'F'=>'001011000',
            'G'=>'000001101',    'H'=>'100001100',    'I'=>'001001100',    'J'=>'000011100',
            'K'=>'100000011',    'L'=>'001000011',    'M'=>'101000010',    'N'=>'000010011',
            'O'=>'100010010',    'P'=>'001010010',    'Q'=>'000000111',    'R'=>'100000110',
            'S'=>'001000110',    'T'=>'000010110',    'U'=>'110000001',    'V'=>'011000001',
            'W'=>'111000000',    'X'=>'010010001',    'Y'=>'110010000',    'Z'=>'011010000',
            ' '=>'011000100',    '$'=>'010101000',    '%'=>'000101010',    '*'=>'010010100',
            '+'=>'010001010',    '-'=>'010000101',    '.'=>'110000100',    '/'=>'010100010'
            );
    }

    public function build($text='', $showText=false, $fileName=null)
    {
        if (trim($text) <= ' ')
            throw new exception('barCode::build - must be passed text to operate');
        if (!$fileType = $this->outMode[$this->mode])
            throw new exception("barCode::build - unrecognized output format ({$this->mode})");
        if (!function_exists("image{$this->mode}"))
            throw new exception("barCode::build - unsupported output format ({$this->mode} - check phpinfo)");

        $text  =  strtoupper($text);
        $dispText = "* $text *";
        $text = "*$text*"; // adds start and stop chars
        $textLen  =  strlen($text);
        $barcodeWidth  =  $textLen * (7 * $this->bcThinWidth + 3 * $this->bcThickWidth) - $this->bcThinWidth;
        $im = imagecreate($barcodeWidth, $this->bcHeight);
        $black = imagecolorallocate($im, 0, 0, 0);
        $white = imagecolorallocate($im, 255, 255, 255);
        imagefill($im, 0, 0, $white);

        $xpos = 0;
        for ($idx=0; $idx<$textLen; $idx++)
        {
            if (!$char = $text[$idx]) $char = '-';
            for ($ptr=0; $ptr<=8; $ptr++)
            {
                $elementWidth = ($this->codeMap[$char][$ptr]) ? $this->bcThickWidth : $this->bcThinWidth;
                if (($ptr + 1) % 2)
                    imagefilledrectangle($im, $xpos, 0, $xpos + $elementWidth-1, $this->bcHeight, $black);
                $xpos += $elementWidth;
            }
            $xpos += $this->bcThinWidth;
        }

        if ($showText)
        {
            $pxWid = imagefontwidth($this->fontSize) * strlen($dispText) + 10;
            $pxHt = imagefontheight($this->fontSize) + 2;
            $bigCenter = $barcodeWidth / 2;
            $textCenter = $pxWid / 2;
            imagefilledrectangle($im, $bigCenter - $textCenter, $this->bcHeight - $pxHt, $bigCenter + $textCenter, $this->bcHeight, $white);
            imagestring($im, $this->fontSize, ($bigCenter - $textCenter) + 5, ($this->bcHeight - $pxHt) + 1, $dispText, $black);
        }

        if (!$fileName) header("Content-type:  image/{$fileType}");
        switch($this->mode)
        {
            case 'gif': imagegif($im, $fileName);
            case 'png': imagepng($im, $fileName);
            case 'jpeg': imagejpeg($im, $fileName);
            case 'wbmp': imagewbmp($im, $fileName);
        }

        imagedestroy($im);
    }
}

?>


as you can see $bc->build(‘LO130319001’);

but when I tried to scan the barcode the output become LO13-319—1 the 0 become –

before this is my code:


$sql = "SELECT r.lot_number, m.mat_code, m.description FROM wms_receiving AS r LEFT JOIN bom_material AS m ON r.uid_mat = m.uid_mat WHERE r.rm_id = " . $_SESSION["rm_id"];
            $res = mysql_query($sql);

            $row = mysql_fetch_assoc($res);

            $labelprint = "<table>";
            $labelprint .= "<tr><td><strong> Lot # : </strong></td><td> $row[lot_number] </td></tr>";
         //   $labelprint .= "<tr><td><strong> Stock Item : </strong></td><td> $row[mat_code] </td></tr><tr><td><strong> Stock Desc. : </strong></td><td> $row[description] </td></tr>";
         $description_to_print =htmlspecialchars($row[description], ENT_QUOTES);
         $labelprint .= "<tr><td><strong> Stock Item : </strong></td><td> $row[mat_code] </td></tr><tr><td><strong> Stock Desc. : </strong></td><td> $description_to_print</td></tr>";

            $labelprint .= "</table>";
            unset($_POST);

Now I need to put the Lot number into barcode. How can I used the above code in generating barcode.

Thank you so much

Hi,

I resolved my first problem which is 0 become -. I just exchange the value of 0 and -.

Now my problem is how can I put the lot_number value from my database to barcode value which handled by $text. I mean no need to declare to : $bc->build(‘LO130319001’); manually.

I just get the lotnumber value to:

$_SESSION[“rm_id”] = mysql_insert_id();

        $sql = "SELECT r.lot_number, m.mat_code, m.description FROM wms_receiving AS r LEFT JOIN bom_material AS m ON r.uid_mat = m.uid_mat WHERE r.rm_id = " . $_SESSION["rm_id"];
        $res = mysql_query($sql);

        $row = mysql_fetch_assoc($res);

$lot_number = $row[‘lot_number’];

Thank you so much

From the Imagemagick examples website:

Some fonts are very specialized. For example you can get a font file from IDAutomation called “IDAutomationHC39M.ttf” that can be used to generate barcodes. For example…

convert -font IDAutomationHC39M -pointsize 16 label:'*314-76*' \\
          -bordercolor white -border 5x5  label_barcode.gif

well, I think he want some free sample code to generate barcode. Here is some source code, but for VB.NET class project, anyway, hope it will offer some help.

[B]http://www.devasp.net/net/articles/display/742.html[/B]

Thank you

Defines the boundaries of the barcode and also Clears the area and Calculates the scale Draw the BarCode. And Finally Draw the code which is entered by user.

Public Sub PrintEANBarCode(ByVal strEANCode As String, ByVal objPicBox As PictureBox, _

                                Optional ByVal sngX1 As Single = (-1), _

                                Optional ByVal sngY1 As Single = (-1), _

                                Optional ByVal sngX2 As Single = (-1), _

                                Optional ByVal sngY2 As Single = (-1), _

                                Optional ByVal FontForText As Font = Nothing)

        Dim K As Single

        Dim sngPosX As Single

        Dim sngPosY As Single

        Dim sngScaleX As Single

        Dim strEANBin As String

        Dim strFormat As New StringFormat

        strEANBin = EAN2Bin(strEANCode)

        If (FontForText Is Nothing) Then

            FontForText = New Font("Courier New", 10)

        End If

        If sngX1 = (-1) Then sngX1 = 0

        If sngY1 = (-1) Then sngY1 = 0

        If sngX2 = (-1) Then sngX2 = objPicBox.Width

        If sngY2 = (-1) Then sngY2 = objPicBox.Height

                sngPosX = sngX1

        sngPosY = sngY2 - CSng(1.5 * FontForText.Height)

                objPicBox.CreateGraphics.FillRectangle(New System.Drawing.SolidBrush(objPicBox.BackColor.Blue), sngX1, sngY1, sngX2 - sngX1, sngY2 - sngY1)

        For K = 1 To Len(strEANBin)

            If Mid(strEANBin, K, 1) = "1" Then

                objPicBox.CreateGraphics.FillRectangle(New System.Drawing.SolidBrush(objPicBox.ForeColor.Red), sngPosX, sngY1, sngScaleX, sngPosY)

            End If

            sngPosX = sngX1 + (K * sngScaleX)

        Next K

               strFormat.Alignment = StringAlignment.Center

        strFormat.FormatFlags = StringFormatFlags.NoWrap

        objPicBox.CreateGraphics.DrawString(strEANCode, FontForText, New System.Drawing.SolidBrush(objPicBox.ForeColor.Orange), CSng((sngX2 - sngX1) / 2), CSng(sngY2 - FontForText.Height), strFormat)

    End Sub

This is a the sample. <snip/>
Hope you success.

Best regards,
Arron