[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Need to generate a barcode for 5 text boxes.(PHP)

Posted on 2010-01-01
9
Medium Priority
?
864 Views
Last Modified: 2012-06-27
Hello,

Its been awhile since i have posted here. I have a issue i need to display 5 barcodes on a simple web page so that they may be printed. I found some code on the net i posted it below just want to modify it so that i can display the barcodes below each other this one only displays one.

I know very little about php. I copied and pasted then tried to modify it to my needs but no luck. Would greatly appreciate the help.

I will display one field just not the second one but i really need at total of 5 fields.
Below is my my index page:

<HTML> 
 
<BODY BGCOLOR=#000000 LINK=#0090FF VLINK=#0090FF TEXT=#FFFFFF> 
 
 
<CENTER> 
<H2><FONT COLOR=yellow>Cover Sheet</FONT></H2> 

<FORM METHOD=POST ACTION="barcode.php"> 
  <p>Field One:
    <INPUT TYPE="text" SIZE="16" NAME="barcode"> 
  <INPUT TYPE="hidden" NAME="width" VALUE=320> 
  <INPUT TYPE="hidden" NAME="height" VALUE=200>
  </p>
  
      Field Two:
      <INPUT TYPE="text" SIZE="16" NAME="barcode2"> 
  <INPUT TYPE="hidden" NAME="width" VALUE=320> 
  <INPUT TYPE="hidden" NAME="height" VALUE=200>
  
  <p>
    <INPUT TYPE="submit" VALUE="Generate Barcode">
  </p>
</FORM> 
<P>
</CENTER><p> 
<center>
  <font size="2">BETA BARCODE SHEET</font>
  <font size=2><br>
<br>
</font> 
</center> 
 
</BODY> 
 
</HTML> 



Next is my action page. 


<?php
/*===========================================================================*/
/*      PHP Barcode Image Generator v1.0 [9/28/2000]
        Copyright (C)2000 by Charles J. Scheffold - cs@sid6581.net


		---
		UPDATE 5/10/2005 by C.Scheffold
		Changed FontHeight to -2 if no text is to be displayed (this eliminates
		the whitespace at the bottom of the image)
		---
		UPDATE 03/12/2005 by C.Scheffold
		Added '-' character to translation table
        ---
        UPDATE 09/21/2002 by Laurent NAVARRO - ln@altidev.com - http://www.altidev.com
        Updated to be compatible with register_globals = off and on
        ---
        UPDATE 4/6/2001 - Important Note! This script was written with the assumption
        that "register_globals = On" is defined in your PHP.INI file! It will not 
        work as-is      and as described unless this is set. My PHP came with this 
        enabled by default, but apparently many people have turned it off. Either 
        turn it on or modify the startup code to pull the CGI variables in the old 
        fashioned way (from the HTTP* arrays). If you just want to use the functions 
        and pass the variables yourself, well then go on with your bad self.
        ---
        
        This code is hereby released into the public domain.
        Use it, abuse it, just don't get caught using it for something stupid.


        The only barcode type currently supported is Code 3 of 9. Don't ask about 
        adding support for others! This is a script I wrote for my own use. I do 
        plan to add more types as time permits but currently I only require 
        Code 3 of 9 for my purposes. Just about every scanner on the market today
        can read it.


        PARAMETERS:
        -----------
        $barcode        = [required] The barcode you want to generate


        $type           = (default=0) It's 0 for Code 3 of 9 (the only one supported)
        
        $width          = (default=160) Width of image in pixels. The image MUST be wide
                                  enough to handle the length of the given value. The default
                                  value will probably be able to display about 6 digits. If you
                                  get an error message, make it wider!


        $height         = (default=80) Height of image in pixels
        
        $format         = (default=jpeg) Can be "jpeg", "png", or "gif"
        
        $quality        = (default=100) For JPEG only: ranges from 0-100


        $text           = (default=1) 0 to disable text below barcode, >=1 to enable


        NOTE: You must have GD-1.8 or higher compiled into PHP
        in order to use PNG and JPEG. GIF images only work with
        GD-1.5 and lower. (http://www.boutell.com)


        ANOTHER NOTE: If you actually intend to print the barcodes 
        and scan them with a scanner, I highly recommend choosing 
        JPEG with a quality of 100. Most browsers can't seem to print 
        a PNG without mangling it beyond recognition. 


        USAGE EXAMPLES FOR ANY PLAIN OLD HTML DOCUMENT:
        -----------------------------------------------


        <IMG SRC="barcode.php?barcode=HELLO&quality=75">


        <IMG SRC="barcode.php?barcode=123456&width=320&height=200">
                
        
*/
/*=============================================================================*/


//-----------------------------------------------------------------------------
// 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 = ImageColorAllocate ($im, 255, 255, 255);
        $Black = ImageColorAllocate ($im, 0, 0, 0);
        //ImageColorTransparent ($im, $White);
        ImageInterLace ($im, 1);



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


        $nChars = (strlen($barcode)+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 = "*".strtoupper ($barcode)."*";
        settype ($BarcodeFull, "string");
        
        $FontNum = 3;
        $FontHeight = ImageFontHeight ($FontNum);
        $FontWidth = ImageFontWidth ($FontNum);
        if ($text != 0)
        {
                $CenterLoc = (int)(($width-1) / 2) - (int)(($FontWidth * strlen($BarcodeFull)) / 2);
                ImageString ($im, $FontNum, $CenterLoc, $height-$FontHeight, "$BarcodeFull", $Black);
        }
		else
		{
			$FontHeight=-2;
		}


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


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


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


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


                $Color = $White;
                ImageFilledRectangle ($im, $CurrentBarX, 0, $CurrentBarX+$QuietBar, $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"; 
        }
}


?>

Open in new window

0
Comment
Question by:vonsean
8 Comments
 
LVL 35

Expert Comment

by:gr8gonzo
ID: 26160861
First change your form method to GET and try a test to see  if that generates a barcode
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 26161757
Wow - a page from the year 2000!

You might want to consider upgrading that - technology has changed a lot in the last decade.  For one thing, it looks like you can get the barcodes in a font now.

http://lmgtfy.com?q=barcode+fonts+free

HTH, ~Ray
0
 

Author Comment

by:vonsean
ID: 26162629
So i changed my code to the following i found some free barcode font online. I put the barcode in the same directory just now i cant seem to call it to change the text.

Do i need to install the font on the linux box? I assumed i should be able call it from the directory i have it stored in.



--------------------------INDEX.HTML Page----------------



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Demo Barcode</title>
<style type="text/css">
<!--
#apDiv1 {
	position:absolute;
	left:19px;
	top:15px;
	width:550px;
	height:393px;
	z-index:1;
}
-->
</style>
</head>

<body>
<div id="apDiv1">
  <form action="barcode.php" method="post" name="form" target="_blank" id="form">
    <p align="right">
      <label>Account Name:
        <input type="text" name="account" id="account" />
      </label>
    </p>
    <p align="right">
      <label>Sales Rep:
        <input type="text" name="salesrep" id="salesrep" />
      </label>
    </p>
    <p align="right">
      <label>Revenue:
        <input type="text" name="revenue" id="revenue" />
      </label>
    </p>
    <p align="right">
      <label>Install Date:
        <input type="text" name="install" id="install" />
      </label>
    </p>
    <label>
      <div align="right">Select One
        <select name="select" size="1" id="select">
          <option value="demo">Demo</option>
          <option value="sale">Sale</option>
        </select>
      </div>
    </label>
    <p>
      <label>      </label>
    </p>
    <label>
      <div align="center">
        <input type="submit" name="submit" id="submit" value="Submit" />
      </div>
    </label>
  </form>
</div>
</body>
</html>



-----------------BARCODE.PHP Page---------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Print Barcode</title>
</head>

<?php

// Output
 echo("<p align='center'><font face='Arial' size='3' color=''>$account</font></p>");
 echo("<p align='center'><font face='Arial' size='3' color=''>$salesrep</font></p>");
 echo("<p align='center'><font face='Arial' size='3' color=''>$revenue</font></p>");
 echo("<p align='center'><font face='Arial' size='3' color=''>$install</font></p>");
 echo("<p align='center'><font face='Arial' size='3' color=''>$select</font></p>");
 
?>




<body>
</body>
</html>

Open in new window

0
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 26167151
I am not understanding something here.  You are not seeing barcodes, yet you have an explicit declaration of font face='Arial' in the action script?  Arial would not be the name of a barcode font.

Usually there is an installation process with a font.  IIRC "arial" is one that comes already installed on nearly every computer.
0
 

Author Comment

by:vonsean
ID: 26168566
I have the font on the server in the root directory of the site. Just when i try to replace arial with "CODE3OF9" font. but it will not display the font. Not sure maybe i need to install the font not sure how to do that on a RHEL 5.4.

0
 

Author Comment

by:vonsean
ID: 26177057
I figured out how to install the font but now when i try and specify it font face it still does not show up as a barcode font.

I wrote one in VB a while back had great success with it but i did have to install the font on the workstations.
0
 

Author Comment

by:vonsean
ID: 26203124
Well i changed my code to the following to make it work and i used "PEAR" with image_barcode this seem to work very easy. just need to know how to create 5 more barcode images from the text boxes. when i copy and paste it but just change the value like below it only displays one barcode.

<?php
require("Image/Barcode.php");

$bc = new Image_Barcode;

$bc->draw("$account", "code128", "png");
$bc->draw("$salesrep", "code128", "png");
$bc->draw("$revenue", "code128", "png");
$bc->draw("$install", "code128", "png");
$bc->draw("$select", "code128", "png");
?>





---index.html---
HTML> 
 
<BODY BGCOLOR=#000000 LINK=#0090FF VLINK=#0090FF TEXT=#FFFFFF> 
 
 
<CENTER> 
<H2><FONT COLOR=yellow>Cover Sheet</FONT></H2> 

<FORM METHOD=POST ACTION="barcode.php"> 
  <p>Field One:
    <INPUT TYPE="text" SIZE="16" NAME="barcode"> 
  <INPUT TYPE="hidden" NAME="width" VALUE=320> 
  <INPUT TYPE="hidden" NAME="height" VALUE=200>
  </p>
  
      Field Two:
      <INPUT TYPE="text" SIZE="16" NAME="barcode2"> 
  <INPUT TYPE="hidden" NAME="width" VALUE=320> 
  <INPUT TYPE="hidden" NAME="height" VALUE=200>
  
  <p>
    <INPUT TYPE="submit" VALUE="Generate Barcode">
  </p>
</FORM> 
<P>
</CENTER><p> 
<center>
  <font size="2">BETA BARCODE SHEET</font>
  <font size=2><br>
<br>
</font> 
</center> 
 
</BODY> 
 
</HTML>

-----barcode.php-----

<?php
require("Image/Barcode.php");

$bc = new Image_Barcode;

$bc->draw("$account", "code128", "png");
?>

Open in new window

0
 

Accepted Solution

by:
ee_auto earned 0 total points
ID: 26569988
Question PAQ'd, 500 points refunded, and stored in the solution database.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month19 days, 3 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question