By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,890 Members | 1,039 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,890 IT Pros & Developers. It's quick & easy.

Barcode generation

P: 40
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.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. //-----------------------------------------------------------------------------
  3. // Startup code
  4. //-----------------------------------------------------------------------------
  5.  
  6.  
  7. if(isset($_GET["text"])) $text=$_GET["text"];
  8. if(isset($_GET["format"])) $format=$_GET["format"];
  9. if(isset($_GET["quality"])) $quality=$_GET["quality"];
  10. if(isset($_GET["width"])) $width=$_GET["width"];
  11. if(isset($_GET["height"])) $height=$_GET["height"];
  12. if(isset($_GET["type"])) $type=$_GET["type"];
  13. if(isset($_GET["barcode"])) $barcode=$_GET["barcode"];
  14.  
  15.  
  16.  
  17.  
  18. if (!isset ($text)) $text = 1;
  19. if (!isset ($type)) $type = 1;
  20. if (empty ($quality)) $quality = 100;
  21. if (empty ($width)) $width = 160;
  22. if (empty ($height)) $height = 80;
  23. if (!empty ($format)) $format = strtoupper ($format);
  24.         else $format="PNG";
  25.  
  26.  
  27. switch ($type)
  28. {
  29.         default:
  30.                 $type = 1;
  31.         case 1:
  32.                 Barcode39 ($barcode, $width, $height, $quality, $format, $text);
  33.                 break;          
  34. }
  35.  
  36.  
  37. //-----------------------------------------------------------------------------
  38. // Generate a Code 3 of 9 barcode
  39. //-----------------------------------------------------------------------------
  40. function Barcode39 ($barcode, $width, $height, $quality, $format, $text)
  41. {
  42.         switch ($format)
  43.         {
  44.                 default:
  45.                         $format = "JPEG";
  46.                 case "JPEG": 
  47.                         header ("Content-type: image/jpeg");
  48.                         break;
  49.                 case "PNG":
  50.                         header ("Content-type: image/png");
  51.                         break;
  52.                 case "GIF":
  53.                         header ("Content-type: image/gif");
  54.                         break;
  55.         }
  56.  
  57.  
  58.         $im = ImageCreate ($width, $height)
  59.     or die ("Cannot Initialize new GD image stream");
  60.         $White = ImageColorAllocate ($im, 255, 255, 255);
  61.         $Black = ImageColorAllocate ($im, 0, 0, 0);
  62.         //ImageColorTransparent ($im, $White);
  63.         ImageInterLace ($im, 1);
  64.  
  65.  
  66.  
  67.         $NarrowRatio = 20;
  68.         $WideRatio = 55;
  69.         $QuietRatio = 35;
  70.  
  71.  
  72.         $nChars = (strlen($barcode)+2) * ((6 * $NarrowRatio) + (3 * $WideRatio) + ($QuietRatio));
  73.         $Pixels = $width / $nChars;
  74.         $NarrowBar = (int)(20 * $Pixels);
  75.         $WideBar = (int)(55 * $Pixels);
  76.         $QuietBar = (int)(35 * $Pixels);
  77.  
  78.  
  79.         $ActualWidth = (($NarrowBar * 6) + ($WideBar*3) + $QuietBar) * (strlen ($barcode)+2);
  80.  
  81.         if (($NarrowBar == 0) || ($NarrowBar == $WideBar) || ($NarrowBar == $QuietBar) || ($WideBar == 0) || ($WideBar == $QuietBar) || ($QuietBar == 0))
  82.         {
  83.                 ImageString ($im, 1, 0, 0, "Image is too small!", $Black);
  84.                 OutputImage ($im, $format, $quality);
  85.                 exit;
  86.         }
  87.  
  88.         $CurrentBarX = (int)(($width - $ActualWidth) / 2);
  89.         $Color = $White;
  90.         $BarcodeFull = "*".strtoupper ($barcode)."*";
  91.         settype ($BarcodeFull, "string");
  92.  
  93.         $FontNum = 3;
  94.         $FontHeight = ImageFontHeight ($FontNum);
  95.         $FontWidth = ImageFontWidth ($FontNum);
  96.         if ($text != 0)
  97.         {
  98.                 $CenterLoc = (int)(($width-1) / 2) - (int)(($FontWidth * strlen($BarcodeFull)) / 2);
  99.                 ImageString ($im, $FontNum, $CenterLoc, $height-$FontHeight, "$BarcodeFull", $Black);
  100.         }
  101.         else
  102.         {
  103.             $FontHeight=-2;
  104.         }
  105.  
  106.  
  107.         for ($i=0; $i<strlen($BarcodeFull); $i++)
  108.         {
  109.                 $StripeCode = Code39 ($BarcodeFull[$i]);
  110.  
  111.  
  112.                 for ($n=0; $n < 9; $n++)
  113.                 {
  114.                         if ($Color == $White) $Color = $Black;
  115.                         else $Color = $White;
  116.  
  117.  
  118.                         switch ($StripeCode[$n])
  119.                         {
  120.                                 case '0':
  121.                                         ImageFilledRectangle ($im, $CurrentBarX, 0, $CurrentBarX+$NarrowBar, $height-1-$FontHeight-2, $Color);
  122.                                         $CurrentBarX += $NarrowBar;
  123.                                         break;
  124.  
  125.  
  126.                                 case '1':
  127.                                         ImageFilledRectangle ($im, $CurrentBarX, 0, $CurrentBarX+$WideBar, $height-1-$FontHeight-2, $Color);
  128.                                         $CurrentBarX += $WideBar;
  129.                                         break;
  130.                         }
  131.                 }
  132.  
  133.  
  134.                 $Color = $White;
  135.                 ImageFilledRectangle ($im, $CurrentBarX, 0, $CurrentBarX+$QuietBar, $height-1-$FontHeight-2, $Color);
  136.                 $CurrentBarX += $QuietBar;
  137.         }
  138.  
  139.  
  140.         OutputImage ($im, $format, $quality);
  141. }
  142.  
  143.  
  144. //-----------------------------------------------------------------------------
  145. // Output an image to the browser
  146. //-----------------------------------------------------------------------------
  147. function OutputImage ($im, $format, $quality)
  148. {
  149.         switch ($format)
  150.         {
  151.                 case "JPEG": 
  152.                         ImageJPEG ($im, "", $quality);
  153.                         break;
  154.                 case "PNG":
  155.                         ImagePNG ($im);
  156.                         break;
  157.                 case "GIF":
  158.                         ImageGIF ($im);
  159.                         break;
  160.         }
  161. }
  162.  
  163.  
  164. //-----------------------------------------------------------------------------
  165. // Returns the Code 3 of 9 value for a given ASCII character
  166. //-----------------------------------------------------------------------------
  167. function Code39 ($Asc)
  168. {
  169.         switch ($Asc)
  170.         {
  171.                 case ' ':
  172.                         return "011000100";     
  173.                 case '$':
  174.                         return "010101000";             
  175.                 case '%':
  176.                         return "000101010"; 
  177.                 case '*':
  178.                         return "010010100"; // * Start/Stop
  179.                 case '+':
  180.                         return "010001010"; 
  181.                 case '|':
  182.                         return "010000101"; 
  183.                 case '.':
  184.                         return "110000100"; 
  185.                 case '/':
  186.                         return "010100010"; 
  187.                 case '-':
  188.                         return "010000101";
  189.                 case '0':
  190.                         return "000110100"; 
  191.                 case '1':
  192.                         return "100100001"; 
  193.                 case '2':
  194.                         return "001100001"; 
  195.                 case '3':
  196.                         return "101100000"; 
  197.                 case '4':
  198.                         return "000110001"; 
  199.                 case '5':
  200.                         return "100110000"; 
  201.                 case '6':
  202.                         return "001110000"; 
  203.                 case '7':
  204.                         return "000100101"; 
  205.                 case '8':
  206.                         return "100100100"; 
  207.                 case '9':
  208.                         return "001100100"; 
  209.                 case 'A':
  210.                         return "100001001"; 
  211.                 case 'B':
  212.                         return "001001001"; 
  213.                 case 'C':
  214.                         return "101001000";
  215.                 case 'D':
  216.                         return "000011001";
  217.                 case 'E':
  218.                         return "100011000";
  219.                 case 'F':
  220.                         return "001011000";
  221.                 case 'G':
  222.                         return "000001101";
  223.                 case 'H':
  224.                         return "100001100";
  225.                 case 'I':
  226.                         return "001001100";
  227.                 case 'J':
  228.                         return "000011100";
  229.                 case 'K':
  230.                         return "100000011";
  231.                 case 'L':
  232.                         return "001000011";
  233.                 case 'M':
  234.                         return "101000010";
  235.                 case 'N':
  236.                         return "000010011";
  237.                 case 'O':
  238.                         return "100010010";
  239.                 case 'P':
  240.                         return "001010010";
  241.                 case 'Q':
  242.                         return "000000111";
  243.                 case 'R':
  244.                         return "100000110";
  245.                 case 'S':
  246.                         return "001000110";
  247.                 case 'T':
  248.                         return "000010110";
  249.                 case 'U':
  250.                         return "110000001";
  251.                 case 'V':
  252.                         return "011000001";
  253.                 case 'W':
  254.                         return "111000000";
  255.                 case 'X':
  256.                         return "010010001";
  257.                 case 'Y':
  258.                         return "110010000";
  259.                 case 'Z':
  260.                         return "011010000";
  261.                 default:
  262.                         return "011000100"; 
  263.         }
  264. }
  265.  
  266.  
  267. ?>
Oct 21 '08 #1
Share this Question
Share on Google+
7 Replies


Dormilich
Expert Mod 5K+
P: 8,639
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
Oct 21 '08 #2

P: 40
- 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..for 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?
Oct 21 '08 #3

dlite922
Expert 100+
P: 1,584
The form is used to load the required details like cost price,selling price,discounts allowed,etc..for 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
Oct 21 '08 #4

code green
Expert 100+
P: 1,726
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
Oct 21 '08 #5

P: 40
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
Oct 22 '08 #6

code green
Expert 100+
P: 1,726
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
Oct 22 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.