Barcode generation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • divyac
    New Member
    • Aug 2008
    • 40

    Barcode generation

    I am doing an inventory control project and i want to create barcodes for the products in addition to the product details in a form.The form values should be submitted to the database to retrieve for future use.How to generate a barcode,how to load it in the database and retrieve it back.can i generate the barcode and submit the form with a single "submit" button?pls guide me in this issue.The following code is used to generate a barcode.i just dont know how to implement it in my form.
    [code=PHP]<?php
    //-----------------------------------------------------------------------------
    // Startup code
    //-----------------------------------------------------------------------------


    if(isset($_GET["text"])) $text=$_GET["text"];
    if(isset($_GET["format"])) $format=$_GET["format"];
    if(isset($_GET["quality"])) $quality=$_GET["quality"];
    if(isset($_GET["width"])) $width=$_GET["width"];
    if(isset($_GET["height"])) $height=$_GET["height"];
    if(isset($_GET["type"])) $type=$_GET["type"];
    if(isset($_GET["barcode"])) $barcode=$_GET["barcode"];




    if (!isset ($text)) $text = 1;
    if (!isset ($type)) $type = 1;
    if (empty ($quality)) $quality = 100;
    if (empty ($width)) $width = 160;
    if (empty ($height)) $height = 80;
    if (!empty ($format)) $format = strtoupper ($format);
    else $format="PNG";


    switch ($type)
    {
    default:
    $type = 1;
    case 1:
    Barcode39 ($barcode, $width, $height, $quality, $format, $text);
    break;
    }


    //-----------------------------------------------------------------------------
    // Generate a Code 3 of 9 barcode
    //-----------------------------------------------------------------------------
    function Barcode39 ($barcode, $width, $height, $quality, $format, $text)
    {
    switch ($format)
    {
    default:
    $format = "JPEG";
    case "JPEG":
    header ("Content-type: image/jpeg");
    break;
    case "PNG":
    header ("Content-type: image/png");
    break;
    case "GIF":
    header ("Content-type: image/gif");
    break;
    }


    $im = ImageCreate ($width, $height)
    or die ("Cannot Initialize new GD image stream");
    $White = ImageColorAlloc ate ($im, 255, 255, 255);
    $Black = ImageColorAlloc ate ($im, 0, 0, 0);
    //ImageColorTrans parent ($im, $White);
    ImageInterLace ($im, 1);



    $NarrowRatio = 20;
    $WideRatio = 55;
    $QuietRatio = 35;


    $nChars = (strlen($barcod e)+2) * ((6 * $NarrowRatio) + (3 * $WideRatio) + ($QuietRatio));
    $Pixels = $width / $nChars;
    $NarrowBar = (int)(20 * $Pixels);
    $WideBar = (int)(55 * $Pixels);
    $QuietBar = (int)(35 * $Pixels);


    $ActualWidth = (($NarrowBar * 6) + ($WideBar*3) + $QuietBar) * (strlen ($barcode)+2);

    if (($NarrowBar == 0) || ($NarrowBar == $WideBar) || ($NarrowBar == $QuietBar) || ($WideBar == 0) || ($WideBar == $QuietBar) || ($QuietBar == 0))
    {
    ImageString ($im, 1, 0, 0, "Image is too small!", $Black);
    OutputImage ($im, $format, $quality);
    exit;
    }

    $CurrentBarX = (int)(($width - $ActualWidth) / 2);
    $Color = $White;
    $BarcodeFull = "*".strtoup per ($barcode)."*";
    settype ($BarcodeFull, "string");

    $FontNum = 3;
    $FontHeight = ImageFontHeight ($FontNum);
    $FontWidth = ImageFontWidth ($FontNum);
    if ($text != 0)
    {
    $CenterLoc = (int)(($width-1) / 2) - (int)(($FontWid th * strlen($Barcode Full)) / 2);
    ImageString ($im, $FontNum, $CenterLoc, $height-$FontHeight, "$BarcodeFu ll", $Black);
    }
    else
    {
    $FontHeight=-2;
    }


    for ($i=0; $i<strlen($Barc odeFull); $i++)
    {
    $StripeCode = Code39 ($BarcodeFull[$i]);


    for ($n=0; $n < 9; $n++)
    {
    if ($Color == $White) $Color = $Black;
    else $Color = $White;


    switch ($StripeCode[$n])
    {
    case '0':
    ImageFilledRect angle ($im, $CurrentBarX, 0, $CurrentBarX+$N arrowBar, $height-1-$FontHeight-2, $Color);
    $CurrentBarX += $NarrowBar;
    break;


    case '1':
    ImageFilledRect angle ($im, $CurrentBarX, 0, $CurrentBarX+$W ideBar, $height-1-$FontHeight-2, $Color);
    $CurrentBarX += $WideBar;
    break;
    }
    }


    $Color = $White;
    ImageFilledRect angle ($im, $CurrentBarX, 0, $CurrentBarX+$Q uietBar, $height-1-$FontHeight-2, $Color);
    $CurrentBarX += $QuietBar;
    }


    OutputImage ($im, $format, $quality);
    }


    //-----------------------------------------------------------------------------
    // Output an image to the browser
    //-----------------------------------------------------------------------------
    function OutputImage ($im, $format, $quality)
    {
    switch ($format)
    {
    case "JPEG":
    ImageJPEG ($im, "", $quality);
    break;
    case "PNG":
    ImagePNG ($im);
    break;
    case "GIF":
    ImageGIF ($im);
    break;
    }
    }


    //-----------------------------------------------------------------------------
    // Returns the Code 3 of 9 value for a given ASCII character
    //-----------------------------------------------------------------------------
    function Code39 ($Asc)
    {
    switch ($Asc)
    {
    case ' ':
    return "011000100" ;
    case '$':
    return "010101000" ;
    case '%':
    return "000101010" ;
    case '*':
    return "010010100" ; // * Start/Stop
    case '+':
    return "010001010" ;
    case '|':
    return "010000101" ;
    case '.':
    return "110000100" ;
    case '/':
    return "010100010" ;
    case '-':
    return "010000101" ;
    case '0':
    return "000110100" ;
    case '1':
    return "100100001" ;
    case '2':
    return "001100001" ;
    case '3':
    return "101100000" ;
    case '4':
    return "000110001" ;
    case '5':
    return "100110000" ;
    case '6':
    return "001110000" ;
    case '7':
    return "000100101" ;
    case '8':
    return "100100100" ;
    case '9':
    return "001100100" ;
    case 'A':
    return "100001001" ;
    case 'B':
    return "001001001" ;
    case 'C':
    return "101001000" ;
    case 'D':
    return "000011001" ;
    case 'E':
    return "100011000" ;
    case 'F':
    return "001011000" ;
    case 'G':
    return "000001101" ;
    case 'H':
    return "100001100" ;
    case 'I':
    return "001001100" ;
    case 'J':
    return "000011100" ;
    case 'K':
    return "100000011" ;
    case 'L':
    return "001000011" ;
    case 'M':
    return "101000010" ;
    case 'N':
    return "000010011" ;
    case 'O':
    return "100010010" ;
    case 'P':
    return "001010010" ;
    case 'Q':
    return "000000111" ;
    case 'R':
    return "100000110" ;
    case 'S':
    return "001000110" ;
    case 'T':
    return "000010110" ;
    case 'U':
    return "110000001" ;
    case 'V':
    return "011000001" ;
    case 'W':
    return "111000000" ;
    case 'X':
    return "010010001" ;
    case 'Y':
    return "110010000" ;
    case 'Z':
    return "011010000" ;
    default:
    return "011000100" ;
    }
    }


    ?>[/code]
    Last edited by acoder; Feb 4 '13, 10:18 PM.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    Originally posted by divyac
    How to generate a barcode,how to load it in the database and retrieve it back.can i generate the barcode and submit the form with a single "submit" button?pls guide me in this issue.The following code is used to generate a barcode.i just dont know how to implement it in my form.
    - As I see it the script will do that
    - the barcode is a picture (thus binary content)
    - dito (but I think it's a good idea to store the actual value too)
    - first submit, then generate (and do all the stuff you want with the barcode)
    - what is the form expected to do?

    regards

    Comment

    • divyac
      New Member
      • Aug 2008
      • 40

      #3
      Originally posted by Dormilich
      - As I see it the script will do that
      - the barcode is a picture (thus binary content)
      - dito (but I think it's a good idea to store the actual value too)
      - first submit, then generate (and do all the stuff you want with the barcode)
      - what is the form expected to do?

      regards
      The form is used to load the required details like cost price,selling price,discounts allowed,etc..fo r the new product which should be retrieved back from the database once when the id of that product is given including the barcode.what value should i give to generate a barcode?

      Comment

      • dlite922
        Recognized Expert Top Contributor
        • Dec 2007
        • 1586

        #4
        Originally posted by divyac
        The form is used to load the required details like cost price,selling price,discounts allowed,etc..fo r the new product which should be retrieved back from the database once when the id of that product is given including the barcode.what value should i give to generate a barcode?
        That's easy: a value that uniquely identifies that item, such as a product number or product ID.


        This is a great idea. It really stretches the limit on what you could do with a PHP application. After so many years, people still don't fully understand the possibilities and capabilites of PHP.




        Dan

        Comment

        • code green
          Recognized Expert Top Contributor
          • Mar 2007
          • 1726

          #5
          I might be missing something but what use is a barcode on a web page?
          And why store it as a JPEG etc?

          Barcodes are read by barcode scanners.
          Is somebody going to to be waving a scanner in front of a monitor?

          I am asking because I don't want divyac producing something impractical (unless this is just for fun).

          I have written php scripts that generate barcodes onto packing lists, stock control proformas etc.

          And as barcodes are usually!?! on paper the output was done on PDF (FPDF class).
          In this case a barcode is simply a font, so you don't need a picture of anything,
          simply a font file

          Comment

          • divyac
            New Member
            • Aug 2008
            • 40

            #6
            Originally posted by code green
            I might be missing something but what use is a barcode on a web page?
            And why store it as a JPEG etc?

            Barcodes are read by barcode scanners.
            Is somebody going to to be waving a scanner in front of a monitor?

            I am asking because I don't want divyac producing something impractical (unless this is just for fun).

            I have written php scripts that generate barcodes onto packing lists, stock control proformas etc.

            And as barcodes are usually!?! on paper the output was done on PDF (FPDF class).
            In this case a barcode is simply a font, so you don't need a picture of anything,
            simply a font file


            My client asked this work in a very simple way and in a very short period.so i haven't tried out PDF generation.I think he would take the print of the barcode from the web page itself.

            Also,once I generated a PDF document but when i tried to open that document it showed some error and i couldn't open it.Can you just guide me through in this issue??

            Thanks
            Divya

            Comment

            • code green
              Recognized Expert Top Contributor
              • Mar 2007
              • 1726

              #7
              I have only written PDF docs using an open source class FPDF FPDF I also know of ezPDF.
              Creating PDF is slow and fiddly, but so is any programming requiring a printout.
              People think it is simple only because they have been spoilt with microsoft office
              Printing from a web page has hidden problems (size, browser variations etc)
              whereas PDF is precise.

              Take a look at the FPDF site and see what you think

              Comment

              • Browning
                New Member
                • Feb 2013
                • 2

                #8
                FREE 2d barcode creating component.

                Comment

                Working...